-
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.
- Loading branch information
Showing
6 changed files
with
265 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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" | ||
) | ||
|
||
type AutoDateHistogram struct { | ||
ctx context.Context | ||
field model.Expr // name of the field, e.g. timestamp | ||
bucketsNr int | ||
key int64 | ||
} | ||
|
||
func NewAutoDateHistogram(ctx context.Context, field model.Expr, 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 { | ||
fmt.Println(rows) | ||
if len(rows) == 0 { | ||
logger.WarnWithCtx(query.ctx).Msgf("no rows returned for %s", query.String()) | ||
return make(model.JsonMap, 0) | ||
} | ||
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", | ||
} | ||
} | ||
|
||
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.Expr { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package model | ||
|
||
// TODO: it's not 100% full/proper implementation, but works in the client case | ||
func FindTimestampLowerBound(expr Expr) (InfixExpr, bool) { | ||
candidates := make([]InfixExpr, 0) | ||
visitor := NewBaseVisitor() | ||
visitor.OverrideVisitInfix = func(visitor *BaseExprVisitor, e InfixExpr) interface{} { | ||
if e.Op == ">=" { | ||
candidates = append(candidates, e) | ||
} else if e.Op == "AND" { | ||
e.Left.Accept(visitor) | ||
e.Right.Accept(visitor) | ||
} | ||
return nil | ||
} | ||
|
||
expr.Accept(visitor) | ||
if len(candidates) == 1 { | ||
return candidates[0], true | ||
} | ||
return InfixExpr{}, false | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
package clients | ||
|
||
import ( | ||
"quesma/model" | ||
"quesma/testdata" | ||
) | ||
|
||
var CloverTests = []testdata.AggregationTestCase{ | ||
{ // [0] | ||
TestName: "simplest auto_date_histogram", | ||
QueryRequestJson: ` | ||
{ | ||
"aggs": { | ||
"timeseries": { | ||
"aggs": { | ||
"469ef7fe-5927-42d1-918b-37c738c600f0": { | ||
"bucket_script": { | ||
"buckets_path": { | ||
"count": "_count" | ||
}, | ||
"gap_policy": "skip", | ||
"script": { | ||
"lang": "expression", | ||
"source": "count * 1" | ||
} | ||
} | ||
} | ||
}, | ||
"auto_date_histogram": { | ||
"buckets": 1, | ||
"field": "timestamp" | ||
}, | ||
"meta": { | ||
"dataViewId": "d3d7af60-4c81-11e8-b3d7-01146121b73d", | ||
"indexPatternString": "kibana_sample_data_flights", | ||
"intervalString": "54000000ms", | ||
"normalized": true, | ||
"panelId": "1a1d745d-0c21-4103-a2ae-df41d4fbd366", | ||
"seriesId": "866fb08f-b9a4-43eb-a400-38ebb6c13aed", | ||
"timeField": "timestamp" | ||
} | ||
} | ||
}, | ||
"query": { | ||
"bool": { | ||
"filter": [], | ||
"must": [ | ||
{ | ||
"range": { | ||
"timestamp": { | ||
"format": "strict_date_optional_time", | ||
"gte": "2024-10-10T17:33:47.125Z", | ||
"lte": "2024-10-11T08:33:47.125Z" | ||
} | ||
} | ||
} | ||
], | ||
"must_not": [], | ||
"should": [] | ||
} | ||
}, | ||
"runtime_mappings": { | ||
"hour_of_day": { | ||
"script": { | ||
"source": "emit(doc['timestamp'].value.getHour());" | ||
}, | ||
"type": "long" | ||
} | ||
}, | ||
"size": 0, | ||
"timeout": "30000ms", | ||
"track_total_hits": true | ||
}`, | ||
ExpectedResponse: ` | ||
{ | ||
"completion_time_in_millis": 1728635627258, | ||
"expiration_time_in_millis": 1728635687254, | ||
"id": "FlhaTzBhMkpQU3lLMmlzNHhBeU9FMHcbaUp3ZGNYdDNSaGF3STVFZ2xWY3RuQTo2MzU4", | ||
"is_partial": false, | ||
"is_running": false, | ||
"response": { | ||
"_shards": { | ||
"failed": 0, | ||
"skipped": 0, | ||
"successful": 1, | ||
"total": 1 | ||
}, | ||
"aggregations": { | ||
"timeseries": { | ||
"buckets": [ | ||
{ | ||
"469ef7fe-5927-42d1-918b-37c738c600f0": { | ||
"value": 202.0 | ||
}, | ||
"doc_count": 202, | ||
"key": 1728518400000, | ||
"key_as_string": "2024-10-10T00:00:00.000Z" | ||
} | ||
], | ||
"interval": "7d", | ||
"meta": { | ||
"dataViewId": "d3d7af60-4c81-11e8-b3d7-01146121b73d", | ||
"indexPatternString": "kibana_sample_data_flights", | ||
"intervalString": "54000000ms", | ||
"normalized": true, | ||
"panelId": "1a1d745d-0c21-4103-a2ae-df41d4fbd366", | ||
"seriesId": "866fb08f-b9a4-43eb-a400-38ebb6c13aed", | ||
"timeField": "timestamp" | ||
} | ||
} | ||
}, | ||
"hits": { | ||
"hits": [], | ||
"max_score": null, | ||
"total": { | ||
"relation": "eq", | ||
"value": 202 | ||
} | ||
}, | ||
"timed_out": false, | ||
"took": 4 | ||
}, | ||
"start_time_in_millis": 1728635627254 | ||
}`, | ||
ExpectedPancakeResults: []model.QueryResultRow{ | ||
{Cols: []model.QueryResultCol{ | ||
model.NewQueryResultCol("aggr__timeseries__count", int64(202)), | ||
}}, | ||
}, | ||
ExpectedPancakeSQL: ` | ||
SELECT count(*) AS "aggr__timeseries__count" | ||
FROM __quesma_table_name | ||
WHERE ("timestamp">=parseDateTime64BestEffort('2024-10-10T17:33:47.125Z') AND | ||
"timestamp"<=parseDateTime64BestEffort('2024-10-11T08:33:47.125Z'))`, | ||
}, | ||
} |