From 0f4c06861f4d2bf5687063a3b52cbf0c3d3b5c4d Mon Sep 17 00:00:00 2001 From: Antony Dovgal Date: Wed, 23 Nov 2022 14:03:58 +0300 Subject: [PATCH] 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. --- ch/query_select.go | 6 +++--- ch/query_test.go | 19 +++++++++++++++++++ ch/testdata/snapshots/TestQuery-14 | 1 + ch/testdata/snapshots/TestQuery-15 | 1 + ch/testdata/snapshots/TestQuery-16 | 1 + 5 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 ch/testdata/snapshots/TestQuery-14 create mode 100644 ch/testdata/snapshots/TestQuery-15 create mode 100644 ch/testdata/snapshots/TestQuery-16 diff --git a/ch/query_select.go b/ch/query_select.go index 3a8251b..05125b0 100644 --- a/ch/query_select.go +++ b/ch/query_select.go @@ -325,6 +325,9 @@ func (q *SelectQuery) appendQuery( return nil, err } } + if q.final { + b = append(b, " FINAL"...) + } if !q.sample.IsZero() { b = append(b, " SAMPLE "...) b, err = q.sample.AppendQuery(fmter, b) @@ -396,9 +399,6 @@ func (q *SelectQuery) appendQuery( b = append(b, " OFFSET "...) b = strconv.AppendInt(b, int64(q.offset), 10) } - if q.final { - b = append(b, " FINAL"...) - } } else if cteCount { b = append(b, `) SELECT `...) b = append(b, "count()"...) diff --git a/ch/query_test.go b/ch/query_test.go index 993f42c..b3cbb62 100644 --- a/ch/query_test.go +++ b/ch/query_test.go @@ -96,6 +96,25 @@ func TestQuery(t *testing.T) { GroupExpr("group2, group3"). 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() diff --git a/ch/testdata/snapshots/TestQuery-14 b/ch/testdata/snapshots/TestQuery-14 new file mode 100644 index 0000000..5f72bbe --- /dev/null +++ b/ch/testdata/snapshots/TestQuery-14 @@ -0,0 +1 @@ +SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL diff --git a/ch/testdata/snapshots/TestQuery-15 b/ch/testdata/snapshots/TestQuery-15 new file mode 100644 index 0000000..c20ab3f --- /dev/null +++ b/ch/testdata/snapshots/TestQuery-15 @@ -0,0 +1 @@ +SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL WHERE (id = 1) diff --git a/ch/testdata/snapshots/TestQuery-16 b/ch/testdata/snapshots/TestQuery-16 new file mode 100644 index 0000000..72207ed --- /dev/null +++ b/ch/testdata/snapshots/TestQuery-16 @@ -0,0 +1 @@ +SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" FINAL WHERE (id = 1) GROUP BY "id" ORDER BY id