From 3dee88794941e10c83674c954cc4baea5d2c3942 Mon Sep 17 00:00:00 2001 From: "thinh.le" Date: Tue, 17 Dec 2024 14:19:20 +0700 Subject: [PATCH] feat: add support for engine --- ch/query_test.go | 30 +++++++++++++++++++++++++++++- ch/query_view_create.go | 28 ++++++++++++++++++++++++---- ch/testdata/snapshots/TestQuery-20 | 1 + ch/testdata/snapshots/TestQuery-21 | 1 + 4 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 ch/testdata/snapshots/TestQuery-20 create mode 100644 ch/testdata/snapshots/TestQuery-21 diff --git a/ch/query_test.go b/ch/query_test.go index 136d3ab..26c7812 100644 --- a/ch/query_test.go +++ b/ch/query_test.go @@ -134,6 +134,35 @@ func TestQuery(t *testing.T) { OnCluster("my-cluster"). IfNotExists() }, + func(db *ch.DB) chschema.QueryAppender { + return db.NewCreateView(). + Materialized(). + Engine("MergeTree"). + IfNotExists(). + View("view_name"). + Column("col1"). + ColumnExpr("col1 AS alias"). + TableExpr("src_table AS alias"). + Where("foo = bar"). + Group("group1"). + GroupExpr("group2, group3"). + OrderExpr("order2, order3") + }, + func(db *ch.DB) chschema.QueryAppender { + return db.NewCreateView(). + Materialized(). + Engine("MergeTree"). + To("dest_table"). // expect override engine + IfNotExists(). + View("view_name"). + Column("col1"). + ColumnExpr("col1 AS alias"). + TableExpr("src_table AS alias"). + Where("foo = bar"). + Group("group1"). + GroupExpr("group2, group3"). + OrderExpr("order2, order3") + }, } db := chDB() @@ -145,7 +174,6 @@ func TestQuery(t *testing.T) { for i, fn := range queries { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { q := fn(db) - query, err := q.AppendQuery(db.Formatter(), nil) if err != nil { snapshot.SnapshotT(t, err.Error()) diff --git a/ch/query_view_create.go b/ch/query_view_create.go index 8a672bc..5a04f6e 100644 --- a/ch/query_view_create.go +++ b/ch/query_view_create.go @@ -14,6 +14,7 @@ type CreateViewQuery struct { materialized bool ifNotExists bool view chschema.QueryWithArgs + engine chschema.QueryWithArgs onCluster chschema.QueryWithArgs to chschema.QueryWithArgs where whereQuery @@ -62,6 +63,7 @@ func (q *CreateViewQuery) OnClusterExpr(query string, args ...any) *CreateViewQu return q } +// will disable engine option func (q *CreateViewQuery) To(to string) *CreateViewQuery { q.to = chschema.UnsafeName(to) return q @@ -84,6 +86,11 @@ func (q *CreateViewQuery) TableExpr(query string, args ...any) *CreateViewQuery return q } +func (q *CreateViewQuery) Engine(query string, args ...any) *CreateViewQuery { + q.engine = chschema.SafeQuery(query, args) + return q +} + func (q *CreateViewQuery) ModelTableExpr(query string, args ...any) *CreateViewQuery { q.modelTableName = chschema.SafeQuery(query, args) return q @@ -205,11 +212,24 @@ func (q *CreateViewQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []b } } - b = append(b, " TO "...) - b, err = q.to.AppendQuery(fmter, b) - if err != nil { - return nil, err + if !q.to.IsEmpty() { + b = append(b, " TO "...) + b, err = q.to.AppendQuery(fmter, b) + if err != nil { + return nil, err + } } + + // clickhouse do not support both to and engine + if !q.engine.IsEmpty() && q.to.IsEmpty() { + b = append(b, " Engine = "...) + + b, err = q.engine.AppendQuery(fmter, b) + if err != nil { + return nil, err + } + } + b = append(b, " AS "...) b = append(b, "SELECT "...) diff --git a/ch/testdata/snapshots/TestQuery-20 b/ch/testdata/snapshots/TestQuery-20 new file mode 100644 index 0000000..11e8db8 --- /dev/null +++ b/ch/testdata/snapshots/TestQuery-20 @@ -0,0 +1 @@ +CREATE MATERIALIZED VIEW IF NOT EXISTS "view_name" Engine = MergeTree AS SELECT "col1", col1 AS alias FROM src_table AS alias WHERE (foo = bar) GROUP BY "group1", group2, group3 ORDER BY order2, order3 diff --git a/ch/testdata/snapshots/TestQuery-21 b/ch/testdata/snapshots/TestQuery-21 new file mode 100644 index 0000000..2d8c1d8 --- /dev/null +++ b/ch/testdata/snapshots/TestQuery-21 @@ -0,0 +1 @@ +CREATE MATERIALIZED VIEW IF NOT EXISTS "view_name" TO "dest_table" AS SELECT "col1", col1 AS alias FROM src_table AS alias WHERE (foo = bar) GROUP BY "group1", group2, group3 ORDER BY order2, order3