Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion ch/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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())
Expand Down
28 changes: 24 additions & 4 deletions ch/query_view_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 "...)
Expand Down
1 change: 1 addition & 0 deletions ch/testdata/snapshots/TestQuery-20
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions ch/testdata/snapshots/TestQuery-21
Original file line number Diff line number Diff line change
@@ -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