Skip to content

Commit

Permalink
Fix suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
trzysiek committed Oct 3, 2024
1 parent 454d0ef commit 6e51ea0
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions quesma/model/bucket_aggregations/date_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
DateHistogramFixedInterval DateHistogramIntervalType = true
DateHistogramCalendarInterval DateHistogramIntervalType = false
defaultDateTimeType = clickhouse.DateTime64
maxEmptyBucketsAdded = 1000
)

type DateHistogram struct {
Expand Down Expand Up @@ -219,10 +220,8 @@ func (query *DateHistogram) NewRowsTransformer() model.QueryRowsTransformer {
if err == nil {
differenceBetweenTwoNextKeys = duration.Milliseconds()
} else {
// Let's do 1000 years here (arbitrary huge number), log an error, and continue normally.
// The transformer just won't add any rows.
logger.ErrorWithCtx(query.ctx).Err(err)
differenceBetweenTwoNextKeys = (time.Hour * 24 * 365 * 1000).Milliseconds()
differenceBetweenTwoNextKeys = 0
}
}
return &DateHistogramRowsTransformer{minDocCount: query.minDocCount, differenceBetweenTwoNextKeys: differenceBetweenTwoNextKeys}
Expand All @@ -232,23 +231,27 @@ func (query *DateHistogram) NewRowsTransformer() model.QueryRowsTransformer {

type DateHistogramRowsTransformer struct {
minDocCount int
differenceBetweenTwoNextKeys int64
differenceBetweenTwoNextKeys int64 // if 0, we don't add keys
addedEmptyRows int
}

// if minDocCount == 0, and we have buckets e.g. [key, value1], [key+10, value2], we need to insert [key+1, 0], [key+2, 0]...
// CAUTION: a different kind of postprocessing is needed for minDocCount > 1, but I haven't seen any query with that yet, so not implementing it now.
func (qt *DateHistogramRowsTransformer) Transform(ctx context.Context, rowsFromDB []model.QueryResultRow) []model.QueryResultRow {

if qt.minDocCount != 0 || len(rowsFromDB) < 2 {
if qt.minDocCount != 0 || qt.differenceBetweenTwoNextKeys == 0 || len(rowsFromDB) < 2 {
// we only add empty rows, when
// a) minDocCount == 0
// b) we have > 1 rows, with < 2 rows we can't add anything in between
// b) we have valid differenceBetweenTwoNextKeys (>0)
// c) we have > 1 rows, with < 2 rows we can't add anything in between
return rowsFromDB
}
if qt.minDocCount < 0 {
logger.WarnWithCtx(ctx).Msgf("unexpected negative minDocCount: %d. Skipping postprocess", qt.minDocCount)
return rowsFromDB
}

emptyRowsAdded := 0
postprocessedRows := make([]model.QueryResultRow, 0, len(rowsFromDB))
postprocessedRows = append(postprocessedRows, rowsFromDB[0])
for i := 1; i < len(rowsFromDB); i++ {
Expand All @@ -261,11 +264,12 @@ func (qt *DateHistogramRowsTransformer) Transform(ctx context.Context, rowsFromD
}
lastKey := qt.getKey(rowsFromDB[i-1])
currentKey := qt.getKey(rowsFromDB[i])
for midKey := lastKey + qt.differenceBetweenTwoNextKeys; midKey < currentKey; midKey += qt.differenceBetweenTwoNextKeys {
for midKey := lastKey + qt.differenceBetweenTwoNextKeys; midKey < currentKey && emptyRowsAdded < maxEmptyBucketsAdded; midKey += qt.differenceBetweenTwoNextKeys {
midRow := rowsFromDB[i-1].Copy()
midRow.Cols[len(midRow.Cols)-2].Value = midKey
midRow.Cols[len(midRow.Cols)-1].Value = 0
postprocessedRows = append(postprocessedRows, midRow)
emptyRowsAdded++
}
postprocessedRows = append(postprocessedRows, rowsFromDB[i])
}
Expand Down

0 comments on commit 6e51ea0

Please sign in to comment.