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

Commit 74cdd62

Browse files
committed
Cleanup
1 parent 54bdae5 commit 74cdd62

File tree

3 files changed

+37
-59
lines changed

3 files changed

+37
-59
lines changed

quesma/clickhouse/util.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ package clickhouse
55
import (
66
"bytes"
77
"fmt"
8-
"github.com/QuesmaOrg/quesma/quesma/model"
98
"github.com/goccy/go-json"
109
"strings"
11-
"time"
1210
)
1311

1412
// Code doesn't need to be pretty, 99.9% it's just for our purposes
@@ -110,50 +108,3 @@ func PrettyJson(jsonStr string) string {
110108
}
111109
return prettyJSON.String()
112110
}
113-
114-
// TimestampGroupBy returns string to be used in the select part of Clickhouse query, when grouping by timestamp interval.
115-
// e.g.
116-
// - timestampGroupBy("@timestamp", DateTime64, 30 seconds) --> toInt64(toUnixTimestamp64Milli(`@timestamp`)/30000)
117-
// - timestampGroupBy("@timestamp", DateTime, 30 seconds) --> toInt64(toUnixTimestamp(`@timestamp`)/30)
118-
func TimestampGroupBy(fullTimestampExpr model.Expr, timestampField model.ColumnRef, groupByInterval time.Duration) model.Expr {
119-
toUnixTsFunc := model.NewInfixExpr(
120-
model.NewFunction(model.ToUnixTimestampMs, fullTimestampExpr),
121-
" / ", // TODO nasty hack to make our string-based tests pass. Operator should not contain spaces obviously
122-
model.NewMillisecondsLiteral(timestampField, groupByInterval.Milliseconds()),
123-
)
124-
return model.NewFunction("toInt64", toUnixTsFunc)
125-
}
126-
127-
func TimestampGroupByWithTimezone(fullTimestampExpr model.Expr, timestampField model.ColumnRef, groupByInterval time.Duration, timezone string) model.Expr {
128-
129-
// If no timezone, or timezone is default (UTC), we just return TimestampGroupBy(...)
130-
if timezone == "" {
131-
return TimestampGroupBy(fullTimestampExpr, timestampField, groupByInterval)
132-
}
133-
134-
var offset model.Expr = model.NewInfixExpr(
135-
model.NewFunction(
136-
"timeZoneOffset",
137-
model.NewFunction(
138-
"toTimezone",
139-
fullTimestampExpr, model.NewLiteralSingleQuoteString(timezone),
140-
),
141-
),
142-
"*",
143-
model.NewMillisecondsLiteral(timestampField, 1000),
144-
)
145-
146-
unixTsWithOffset := model.NewInfixExpr(
147-
model.NewFunction(model.ToUnixTimestampMs, fullTimestampExpr),
148-
"+",
149-
offset,
150-
)
151-
152-
groupByExpr := model.NewInfixExpr(
153-
model.NewParenExpr(unixTsWithOffset),
154-
" / ", // TODO nasty hack to make our string-based tests pass. Operator should not contain spaces obviously
155-
model.NewMillisecondsLiteral(timestampField, groupByInterval.Milliseconds()),
156-
)
157-
158-
return model.NewFunction("toInt64", groupByExpr)
159-
}

quesma/model/bucket_aggregations/date_histogram.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,44 @@ func (query *DateHistogram) generateSQLForFixedInterval() model.Expr {
153153
interval, err := util.ParseInterval(query.interval)
154154
if err != nil {
155155
logger.ErrorWithCtx(query.ctx).Msg(err.Error())
156+
return model.InvalidExpr
156157
}
157-
return clickhouse.TimestampGroupByWithTimezone(query.field, query.timestampColumn, interval, query.timezone)
158+
159+
var groupBy model.InfixExpr
160+
// If no timezone, or timezone is default (UTC)
161+
if query.timezone == "" {
162+
groupBy = model.NewInfixExpr(
163+
model.NewFunction(model.ToUnixTimestampMs, query.field),
164+
" / ", // TODO nasty hack to make our string-based tests pass. Operator should not contain spaces obviously
165+
model.NewMillisecondsLiteral(query.timestampColumn, interval.Milliseconds()),
166+
)
167+
} else {
168+
offset := model.NewInfixExpr(
169+
model.NewFunction(
170+
"timeZoneOffset",
171+
model.NewFunction(
172+
"toTimezone",
173+
query.field, model.NewLiteralSingleQuoteString(query.timezone),
174+
),
175+
),
176+
"*",
177+
model.NewMillisecondsLiteral(query.timestampColumn, 1000),
178+
)
179+
180+
unixTsWithOffset := model.NewInfixExpr(
181+
model.NewFunction(model.ToUnixTimestampMs, query.field),
182+
"+",
183+
offset,
184+
)
185+
186+
groupBy = model.NewInfixExpr(
187+
model.NewParenExpr(unixTsWithOffset),
188+
" / ", // TODO nasty hack to make our string-based tests pass. Operator should not contain spaces obviously
189+
model.NewMillisecondsLiteral(query.timestampColumn, interval.Milliseconds()),
190+
)
191+
}
192+
193+
return model.NewFunction("toInt64", groupBy)
158194
}
159195

160196
func (query *DateHistogram) generateSQLForCalendarInterval() model.Expr {

quesma/testdata/aggregation_requests.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@
33
package testdata
44

55
import (
6-
"github.com/QuesmaOrg/quesma/quesma/clickhouse"
76
"github.com/QuesmaOrg/quesma/quesma/model"
87
"math"
9-
"time"
108
)
119

12-
var timestampGroupByClause = model.AsString(clickhouse.TimestampGroupBy(
13-
model.NewColumnRef("@timestamp"), model.NewColumnRef("@timestamp"), 30*time.Second))
14-
15-
func groupBySQL(fieldName string, typ clickhouse.DateTimeType, groupByInterval time.Duration) string {
16-
return model.AsString(clickhouse.TimestampGroupBy(model.NewColumnRef(fieldName), model.NewColumnRef(fieldName), groupByInterval))
17-
}
18-
1910
const fullTextFieldName = `"` + model.FullTextFieldNamePlaceHolder + `"`
2011

2112
// TODO change some tests to size > 0, and track_total_hits different values

0 commit comments

Comments
 (0)