From 50192cd8fb1bb6aa65f50daee5e7b11435627255 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Tue, 8 Mar 2022 14:02:44 +0200 Subject: [PATCH] feat: add CreateTableQuery.Order --- ch/query_table_create.go | 15 ++++++++++++++- ch/query_test.go | 1 + ch/testdata/snapshots/TestQuery-10 | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ch/query_table_create.go b/ch/query_table_create.go index aba8bf5..6b1b626 100644 --- a/ch/query_table_create.go +++ b/ch/query_table_create.go @@ -15,6 +15,7 @@ type CreateTableQuery struct { engine chschema.QueryWithArgs ttl chschema.QueryWithArgs partition chschema.QueryWithArgs + order chschema.QueryWithArgs } var _ Query = (*CreateTableQuery)(nil) @@ -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 @@ -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 { diff --git a/ch/query_test.go b/ch/query_test.go index f15c314..cffbfe3 100644 --- a/ch/query_test.go +++ b/ch/query_test.go @@ -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") }, } diff --git a/ch/testdata/snapshots/TestQuery-10 b/ch/testdata/snapshots/TestQuery-10 index a08270b..b3f2fd6 100644 --- a/ch/testdata/snapshots/TestQuery-10 +++ b/ch/testdata/snapshots/TestQuery-10 @@ -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