-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into date-histogram-eb
- Loading branch information
Showing
14 changed files
with
333 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package bucket_aggregations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"quesma/logger" | ||
"quesma/model" | ||
"time" | ||
) | ||
|
||
// TODO: only bucketsNr=1 is supported for now. Implement other cases. | ||
type AutoDateHistogram struct { | ||
ctx context.Context | ||
field model.ColumnRef // name of the field, e.g. timestamp | ||
bucketsNr int | ||
key int64 | ||
} | ||
|
||
// NewAutoDateHistogram creates a new AutoDateHistogram aggregation, during parsing. | ||
// Key is set later, during pancake transformation. | ||
func NewAutoDateHistogram(ctx context.Context, field model.ColumnRef, bucketsNr int) *AutoDateHistogram { | ||
return &AutoDateHistogram{ctx: ctx, field: field, bucketsNr: bucketsNr} | ||
} | ||
|
||
func (query *AutoDateHistogram) AggregationType() model.AggregationType { | ||
return model.BucketAggregation | ||
} | ||
|
||
func (query *AutoDateHistogram) TranslateSqlResponseToJson(rows []model.QueryResultRow) model.JsonMap { | ||
if len(rows) == 0 { | ||
logger.WarnWithCtx(query.ctx).Msgf("no rows returned for %s", query.String()) | ||
return make(model.JsonMap, 0) | ||
} | ||
if len(rows) != 1 { | ||
logger.WarnWithCtx(query.ctx).Msgf("unexpected (!= 1) number of rows returned for %s: %d.", query.String(), len(rows)) | ||
} | ||
return model.JsonMap{ | ||
"buckets": []model.JsonMap{{ | ||
"key": query.key, | ||
"key_as_string": time.UnixMilli(query.key).Format("2006-01-02T15:04:05.000-07:00"), | ||
"doc_count": rows[0].LastColValue(), | ||
}}, | ||
"interval": "100y", // seems working for bucketsNr=1 case. Will have to be changed for other cases. | ||
} | ||
} | ||
|
||
func (query *AutoDateHistogram) String() string { | ||
return fmt.Sprintf("auto_date_histogram(field: %v, bucketsNr: %d)", model.AsString(query.field), query.bucketsNr) | ||
} | ||
|
||
func (query *AutoDateHistogram) GetField() model.ColumnRef { | ||
return query.field | ||
} | ||
|
||
func (query *AutoDateHistogram) SetKey(key int64) { | ||
query.key = key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package model | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
// FindLowerBounds returns y if there is "x>=y" or "x>y" in the WHERE clause, but only as a single top-level expression. | ||
// (I mean by that a>=0 is fine, a>=0 AND expr2 [AND ...]] is also fine (AND between all), but a>=0 OR e2 is not fine. | ||
// a>=0 AND (expr2 OR expr3) is also fine, as on top level it's only an AND. | ||
// We achieve that by only descending for AND operators. | ||
// If there are multiple such expressions, we return the smallest one. | ||
// | ||
// TODO: add upper bound here too, when bucket_nr=1 in auto_date_histogram (only use case of this function), it's not needed. | ||
func FindTimestampLowerBound(field ColumnRef, whereClause Expr) (timestamp int64, found bool) { | ||
timestamp = math.MaxInt64 | ||
visitor := NewBaseVisitor() | ||
visitor.OverrideVisitInfix = func(visitor *BaseExprVisitor, e InfixExpr) interface{} { | ||
if columnRef, ok := e.Left.(ColumnRef); ok && columnRef == field && e.Op == ">=" || e.Op == ">" { | ||
if fun, ok := e.Right.(FunctionExpr); ok && fun.Name == "fromUnixTimestamp64Milli" && len(fun.Args) == 1 { | ||
if rhs, ok := fun.Args[0].(LiteralExpr); ok { | ||
if rhsInt64, ok := rhs.Value.(int64); ok { | ||
timestamp = min(timestamp, rhsInt64) | ||
found = true | ||
} | ||
} | ||
} | ||
} else if e.Op == "AND" { | ||
e.Left.Accept(visitor) | ||
e.Right.Accept(visitor) | ||
} | ||
return nil | ||
} | ||
|
||
whereClause.Accept(visitor) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.