Skip to content

Commit

Permalink
feat: add CreateTableQuery.Order
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Mar 8, 2022
1 parent 9f0d6ab commit 50192cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion ch/query_table_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type CreateTableQuery struct {
engine chschema.QueryWithArgs
ttl chschema.QueryWithArgs
partition chschema.QueryWithArgs
order chschema.QueryWithArgs
}

var _ Query = (*CreateTableQuery)(nil)
Expand Down Expand Up @@ -78,6 +79,11 @@ func (q *CreateTableQuery) Partition(query string, args ...any) *CreateTableQuer
return q
}

func (q *CreateTableQuery) Order(query string, args ...any) *CreateTableQuery {
q.order = chschema.SafeQuery(query, args)
return q
}

func (q *CreateTableQuery) Setting(query string, args ...any) *CreateTableQuery {
q.settings = append(q.settings, chschema.SafeQuery(query, args))
return q
Expand Down Expand Up @@ -159,7 +165,14 @@ func (q *CreateTableQuery) AppendQuery(fmter chschema.Formatter, b []byte) (_ []
}

b = append(b, " ORDER BY "...)
if len(q.table.PKs) > 0 {
if !q.order.IsZero() {
b = append(b, '(')
b, err = q.order.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
b = append(b, ')')
} else if len(q.table.PKs) > 0 {
b = append(b, '(')
for i, pk := range q.table.PKs {
if i > 0 {
Expand Down
1 change: 1 addition & 0 deletions ch/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestQuery(t *testing.T) {
return db.NewCreateTable().Model((*Model)(nil)).
TTL("time + INTERVAL 30 DAY DELETE").
Partition("toDate(time)").
Order("id").
Setting("ttl_only_drop_parts = 1")
},
}
Expand Down
2 changes: 1 addition & 1 deletion ch/testdata/snapshots/TestQuery-10
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE TABLE "spans" (id UInt64, text LowCardinality(String), time DateTime) Engine = MergeTree() PARTITION BY toDate(time) ORDER BY (time) TTL time + INTERVAL 30 DAY DELETE SETTINGS ttl_only_drop_parts = 1
CREATE TABLE "spans" (id UInt64, text LowCardinality(String), time DateTime) Engine = MergeTree() PARTITION BY toDate(time) ORDER BY (id) TTL time + INTERVAL 30 DAY DELETE SETTINGS ttl_only_drop_parts = 1

0 comments on commit 50192cd

Please sign in to comment.