-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests to this PR showed 2 issues in all pipeline aggregations: * we should stop log warnings when numeric values are null * so far all pipeline aggregation's "parent" was at the same level of nesting. But it doesn't have to be the case, I'll need to fix this, it shouldn't be very hard. I comment out the test for this so far. But especially as those are issues in all types of pipeline aggr, I'd fix those in separate PR. Some screens: <img width="1723" alt="Screenshot 2024-05-20 at 12 07 50" src="https://github.com/QuesmaOrg/quesma/assets/5407146/caeaf9b0-7d70-4a44-9ef2-753482a917cb"> <img width="1728" alt="Screenshot 2024-05-20 at 12 08 29" src="https://github.com/QuesmaOrg/quesma/assets/5407146/f8af46d4-8a38-480c-a1bf-8edec3a89b27">
- Loading branch information
Showing
8 changed files
with
1,140 additions
and
36 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
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,117 @@ | ||
package pipeline_aggregations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"mitmproxy/quesma/logger" | ||
"mitmproxy/quesma/model" | ||
"mitmproxy/quesma/queryprocessor" | ||
"mitmproxy/quesma/util" | ||
) | ||
|
||
type SumBucket struct { | ||
ctx context.Context | ||
Parent string | ||
} | ||
|
||
func NewSumBucket(ctx context.Context, bucketsPath string) SumBucket { | ||
return SumBucket{ctx: ctx, Parent: parseBucketsPathIntoParentAggregationName(ctx, bucketsPath)} | ||
} | ||
|
||
func (query SumBucket) IsBucketAggregation() bool { | ||
return false | ||
} | ||
|
||
func (query SumBucket) TranslateSqlResponseToJson(rows []model.QueryResultRow, level int) []model.JsonMap { | ||
if len(rows) == 0 { | ||
logger.WarnWithCtx(query.ctx).Msg("no rows returned for average bucket aggregation") | ||
return []model.JsonMap{nil} | ||
} | ||
if len(rows) > 1 { | ||
logger.WarnWithCtx(query.ctx).Msg("more than one row returned for average bucket aggregation") | ||
} | ||
if returnMap, ok := rows[0].LastColValue().(model.JsonMap); ok { | ||
return []model.JsonMap{returnMap} | ||
} | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to JsonMap: %v, type: %T", rows[0].LastColValue(), rows[0].LastColValue()) | ||
return []model.JsonMap{nil} | ||
} | ||
|
||
func (query SumBucket) CalculateResultWhenMissing(qwa *model.Query, parentRows []model.QueryResultRow) []model.QueryResultRow { | ||
resultRows := make([]model.QueryResultRow, 0) | ||
if len(parentRows) == 0 { | ||
return resultRows // maybe null? | ||
} | ||
qp := queryprocessor.NewQueryProcessor(query.ctx) | ||
parentFieldsCnt := len(parentRows[0].Cols) - 2 // -2, because row is [parent_cols..., current_key, current_value] | ||
// in calculateSingleAvgBucket we calculate avg all current_keys with the same parent_cols | ||
// so we need to split into buckets based on parent_cols | ||
if parentFieldsCnt < 0 { | ||
logger.WarnWithCtx(query.ctx).Msgf("parentFieldsCnt is less than 0: %d", parentFieldsCnt) | ||
} | ||
for _, parentRowsOneBucket := range qp.SplitResultSetIntoBuckets(parentRows, parentFieldsCnt) { | ||
resultRows = append(resultRows, query.calculateSingleSumBucket(parentRowsOneBucket)) | ||
} | ||
return resultRows | ||
} | ||
|
||
// we're sure len(parentRows) > 0 | ||
func (query SumBucket) calculateSingleSumBucket(parentRows []model.QueryResultRow) model.QueryResultRow { | ||
var resultValue any | ||
|
||
firstNonNilIndex := -1 | ||
for i, row := range parentRows { | ||
if row.LastColValue() != nil { | ||
firstNonNilIndex = i | ||
break | ||
} | ||
} | ||
if firstNonNilIndex == -1 { | ||
resultRow := parentRows[0].Copy() | ||
resultRow.Cols[len(resultRow.Cols)-1].Value = model.JsonMap{ | ||
"value": resultValue, | ||
} | ||
return resultRow | ||
} | ||
|
||
if firstRowValueFloat, firstRowValueIsFloat := util.ExtractFloat64Maybe(parentRows[firstNonNilIndex].LastColValue()); firstRowValueIsFloat { | ||
sum := firstRowValueFloat | ||
for _, row := range parentRows[firstNonNilIndex+1:] { | ||
value, ok := util.ExtractFloat64Maybe(row.LastColValue()) | ||
if ok { | ||
sum += value | ||
} else { | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to float: %v, type: %T. Skipping", row.LastColValue(), row.LastColValue()) | ||
} | ||
} | ||
resultValue = sum | ||
} else if firstRowValueInt, firstRowValueIsInt := util.ExtractInt64Maybe(parentRows[firstNonNilIndex].LastColValue()); firstRowValueIsInt { | ||
sum := firstRowValueInt | ||
for _, row := range parentRows[firstNonNilIndex+1:] { | ||
value, ok := util.ExtractInt64Maybe(row.LastColValue()) | ||
if ok { | ||
sum += value | ||
} else { | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to int: %v, type: %T. Skipping", row.LastColValue(), row.LastColValue()) | ||
} | ||
} | ||
resultValue = sum | ||
} else { | ||
logger.WarnWithCtx(query.ctx).Msgf("could not convert value to float or int: %v, type: %T. Returning nil.", | ||
parentRows[firstNonNilIndex].LastColValue(), parentRows[firstNonNilIndex].LastColValue()) | ||
} | ||
|
||
resultRow := parentRows[0].Copy() | ||
resultRow.Cols[len(resultRow.Cols)-1].Value = model.JsonMap{ | ||
"value": resultValue, | ||
} | ||
return resultRow | ||
} | ||
|
||
func (query SumBucket) PostprocessResults(rowsFromDB []model.QueryResultRow) []model.QueryResultRow { | ||
return rowsFromDB | ||
} | ||
|
||
func (query SumBucket) String() string { | ||
return fmt.Sprintf("sum_bucket(%s)", query.Parent) | ||
} |
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.