Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 5e7c203

Browse files
committed
Adding experimental (and limited) support for table partitioning
gitignore cont'd # Conflicts: # .gitignore # platform/ingest/processor.go
1 parent 4a191fe commit 5e7c203

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

platform/clickhouse/clickhouse.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ const (
2626
allElasticsearchIndicesPattern = "_all"
2727
)
2828

29+
// PartitionStrategy is a custom type for partitioning strategies
30+
type PartitionStrategy string
31+
32+
// Enum values for PartitionStrategy
33+
const (
34+
Hourly PartitionStrategy = "hourly"
35+
Daily PartitionStrategy = "daily"
36+
Monthly PartitionStrategy = "monthly"
37+
Yearly PartitionStrategy = "yearly"
38+
)
39+
2940
type (
3041
LogManager struct {
3142
ctx context.Context
@@ -55,10 +66,11 @@ type (
5566
ClusterName string // Name of the cluster if created with `CREATE TABLE ... ON CLUSTER ClusterName`
5667
Engine string // "Log", "MergeTree", etc.
5768
OrderBy string // "" if none
58-
PartitionBy string // "" if none
59-
PrimaryKey string // "" if none
60-
Settings string // "" if none
61-
Ttl string // of type Interval, e.g. 3 MONTH, 1 YEAR
69+
//PartitionBy string // "" if none
70+
PartitionStrategy PartitionStrategy
71+
PrimaryKey string // "" if none
72+
Settings string // "" if none
73+
Ttl string // of type Interval, e.g. 3 MONTH, 1 YEAR
6274
// look https://clickhouse.com/docs/en/sql-reference/data-types/special-data-types/interval
6375
// "" if none
6476
// TODO make sure it's unique in schema (there's no other 'others' field)
@@ -360,9 +372,10 @@ func NewDefaultCHConfig() *ChTableConfig {
360372
TimestampDefaultsNow: true,
361373
Engine: "MergeTree",
362374
OrderBy: "(" + `"@timestamp"` + ")",
363-
PartitionBy: "",
364-
PrimaryKey: "",
365-
Ttl: "",
375+
//PartitionBy: "",
376+
PartitionStrategy: Hourly,
377+
PrimaryKey: "",
378+
Ttl: "",
366379
Attributes: []Attribute{
367380
NewDefaultInt64Attribute(),
368381
NewDefaultFloat64Attribute(),
@@ -381,9 +394,9 @@ func NewNoTimestampOnlyStringAttrCHConfig() *ChTableConfig {
381394
TimestampDefaultsNow: false,
382395
Engine: "MergeTree",
383396
OrderBy: "(" + `"@timestamp"` + ")",
384-
PartitionBy: "",
385-
PrimaryKey: "",
386-
Ttl: "",
397+
//PartitionBy: "",
398+
PrimaryKey: "",
399+
Ttl: "",
387400
Attributes: []Attribute{
388401
NewDefaultStringAttribute(),
389402
},

platform/clickhouse/schema.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/QuesmaOrg/quesma/platform/util"
99
"math"
1010
"reflect"
11+
"strconv"
1112
"strings"
1213
"time"
1314
)
@@ -342,8 +343,8 @@ func (config *ChTableConfig) CreateTablePostFieldsString() string {
342343
if config.OrderBy != "" {
343344
s += "ORDER BY " + config.OrderBy + "\n"
344345
}
345-
if config.PartitionBy != "" {
346-
s += "PARTITION BY " + config.PartitionBy + "\n"
346+
if config.PartitionStrategy != "" {
347+
s += "PARTITION BY " + getPartitioningFunc(config.PartitionStrategy) + "(" + strconv.Quote(timestampFieldName) + ")" + "\n"
347348
}
348349
if config.PrimaryKey != "" {
349350
s += "PRIMARY KEY " + config.PrimaryKey + "\n"
@@ -358,6 +359,21 @@ func (config *ChTableConfig) CreateTablePostFieldsString() string {
358359
return s
359360
}
360361

362+
func getPartitioningFunc(strategy PartitionStrategy) string {
363+
switch strategy {
364+
case Hourly:
365+
return "toStartOfHour"
366+
case Daily:
367+
return "toYYYYMMDD"
368+
case Monthly:
369+
return "toYYYYMM"
370+
case Yearly:
371+
return "toYYYY"
372+
default:
373+
return ""
374+
}
375+
}
376+
361377
func NewDefaultStringAttribute() Attribute {
362378
return Attribute{
363379
KeysArrayName: DeprecatedAttributesKeyColumn,

platform/clickhouse/table.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (t *Table) CreateTableString() string {
7373
for _, index := range t.Indexes {
7474
rows = append(rows, util.Indent(1)+index.Statement())
7575
}
76+
t.Config.PartitionStrategy = Hourly
7677
return s + strings.Join(rows, ",\n") + "\n)\n" + t.Config.CreateTablePostFieldsString()
7778
}
7879

platform/ingest/processor.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func createTableQuery(name string, columns string, config *chLib.ChTableConfig)
229229
if config.ClusterName != "" {
230230
onClusterClause = "ON CLUSTER " + strconv.Quote(config.ClusterName) + " "
231231
}
232+
config.PartitionStrategy = chLib.Hourly
232233
createTableCmd := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS "%s" %s
233234
(
234235
@@ -1008,11 +1009,11 @@ func NewIngestProcessor(cfg *config.QuesmaConfiguration, chDb quesma_api.Backend
10081009

10091010
func NewOnlySchemaFieldsCHConfig(clusterName string) *chLib.ChTableConfig {
10101011
return &chLib.ChTableConfig{
1011-
HasTimestamp: true,
1012-
TimestampDefaultsNow: true,
1013-
Engine: "MergeTree",
1014-
OrderBy: "(" + `"@timestamp"` + ")",
1015-
PartitionBy: "",
1012+
HasTimestamp: true,
1013+
TimestampDefaultsNow: true,
1014+
Engine: "MergeTree",
1015+
OrderBy: "(" + `"@timestamp"` + ")",
1016+
//PartitionBy: "",
10161017
ClusterName: clusterName,
10171018
PrimaryKey: "",
10181019
Ttl: "",
@@ -1029,9 +1030,9 @@ func NewDefaultCHConfig() *chLib.ChTableConfig {
10291030
TimestampDefaultsNow: true,
10301031
Engine: "MergeTree",
10311032
OrderBy: "(" + `"@timestamp"` + ")",
1032-
PartitionBy: "",
1033-
PrimaryKey: "",
1034-
Ttl: "",
1033+
//PartitionBy: "",
1034+
PrimaryKey: "",
1035+
Ttl: "",
10351036
Attributes: []chLib.Attribute{
10361037
chLib.NewDefaultInt64Attribute(),
10371038
chLib.NewDefaultFloat64Attribute(),

0 commit comments

Comments
 (0)