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