Skip to content

Commit

Permalink
Deterministic use of DuckDB "last" function (#5291)
Browse files Browse the repository at this point in the history
  • Loading branch information
esevastyanov authored Jul 16, 2024
1 parent fac7997 commit 1d6bfca
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion runtime/queries/column_timeseries.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,14 @@ func getCoalesceStatementsMeasures(measures []*runtimev1.ColumnTimeSeriesRequest
func getCoalesceStatementsMeasuresLast(dialect drivers.Dialect, measures []*runtimev1.ColumnTimeSeriesRequest_BasicMeasure) string {
var result string
for i, measure := range measures {
result += fmt.Sprintf(` `+lastValue(dialect)+`(%[1]s) as %[1]s`, safeName(measure.SqlName))
switch dialect {
case drivers.DialectDuckDB:
// "last" function of DuckDB returns non-deterministic results by default so requires an ORDER BY clause
// https://duckdb.org/docs/sql/functions/aggregates.html#order-by-clause-in-aggregate-functions
result += fmt.Sprintf(` `+lastValue(dialect)+`(%[1]s ORDER BY %[1]s NULLS FIRST) as %[1]s`, safeName(measure.SqlName))
default:
result += fmt.Sprintf(` `+lastValue(dialect)+`(%[1]s) as %[1]s`, safeName(measure.SqlName))
}
if i < len(measures)-1 {
result += ", "
}
Expand Down

0 comments on commit 1d6bfca

Please sign in to comment.