You've already forked go-clickhouse
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:
@ -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()"...)
|
||||||
|
@ -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
1
ch/testdata/snapshots/TestQuery-14
vendored
Normal 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
1
ch/testdata/snapshots/TestQuery-15
vendored
Normal 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
1
ch/testdata/snapshots/TestQuery-16
vendored
Normal 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
|
Reference in New Issue
Block a user