1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-08-08 22:16:32 +02:00

fix: move FINAL modifier to the right place (#52)

The documentation (http://devdoc.net/database/ClickhouseDocs_19.4.1.3-docs/query_language/select/)
says FINAL has to be placed right after the table name, before SAMPLE:
  SELECT [DISTINCT] expr_list
    [FROM [db.]table | (subquery) | table_function] [FINAL]
    [SAMPLE sample_coeff]

At the moment it's appended to the end of the query which results in
invalid queries being generated when there are WHERE, ORDER BY or any
other clauses used.
This commit is contained in:
Antony Dovgal
2022-11-23 14:03:58 +03:00
committed by GitHub
parent 3874d74090
commit 0f4c06861f
5 changed files with 25 additions and 3 deletions

View File

@ -325,6 +325,9 @@ func (q *SelectQuery) appendQuery(
return nil, err return nil, err
} }
} }
if q.final {
b = append(b, " FINAL"...)
}
if !q.sample.IsZero() { if !q.sample.IsZero() {
b = append(b, " SAMPLE "...) b = append(b, " SAMPLE "...)
b, err = q.sample.AppendQuery(fmter, b) b, err = q.sample.AppendQuery(fmter, b)
@ -396,9 +399,6 @@ func (q *SelectQuery) appendQuery(
b = append(b, " OFFSET "...) b = append(b, " OFFSET "...)
b = strconv.AppendInt(b, int64(q.offset), 10) b = strconv.AppendInt(b, int64(q.offset), 10)
} }
if q.final {
b = append(b, " FINAL"...)
}
} else if cteCount { } else if cteCount {
b = append(b, `) SELECT `...) b = append(b, `) SELECT `...)
b = append(b, "count()"...) b = append(b, "count()"...)

View File

@ -96,6 +96,25 @@ func TestQuery(t *testing.T) {
GroupExpr("group2, group3"). GroupExpr("group2, group3").
OrderExpr("order2, order3") OrderExpr("order2, order3")
}, },
func(db *ch.DB) chschema.QueryAppender {
return db.NewSelect().
Model((*Model)(nil)).
Final()
},
func(db *ch.DB) chschema.QueryAppender {
return db.NewSelect().
Model((*Model)(nil)).
Where("id = ?", 1).
Final()
},
func(db *ch.DB) chschema.QueryAppender {
return db.NewSelect().
Model((*Model)(nil)).
Where("id = ?", 1).
Final().
Group("id").
OrderExpr("id")
},
} }
db := chDB() db := chDB()

1
ch/testdata/snapshots/TestQuery-14 vendored Normal file
View File

@ -0,0 +1 @@
SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL

1
ch/testdata/snapshots/TestQuery-15 vendored Normal file
View File

@ -0,0 +1 @@
SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL WHERE (id = 1)

1
ch/testdata/snapshots/TestQuery-16 vendored Normal file
View File

@ -0,0 +1 @@
SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL WHERE (id = 1) GROUP BY "id" ORDER BY id