Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit aed1d92

Browse files
committed
Cleanup 2
1 parent a10ebd9 commit aed1d92

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

quesma/queryparser/pancake_sql_query_generation.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,12 @@ func (p *pancakeSqlQueryGenerator) generateSelectCommand(aggregation *pancakeMod
310310
if layer.nextBucketAggregation != nil {
311311
if combinator, isCombinator := layer.nextBucketAggregation.queryType.(bucket_aggregations.CombinatorAggregationInterface); isCombinator {
312312
var isFilter bool
313-
//pp.Println(combinator)
314313
switch combinator.(type) {
315314
case *bucket_aggregations.FilterAgg, bucket_aggregations.Filters:
316315
isFilter = true
317316
}
318-
fmt.Println("isFilter: ", isFilter, len(aggregation.layers))
319-
//pp.Println(aggregation.layers[0])
320-
if isFilter && i == 0 && len(aggregation.layers) > 1 && len(layer.currentMetricAggregations) == 0 && len(layer.currentPipelineAggregations) == 0 {
321-
// If filter is in the first layer, we can just add it to the where clause
322-
switch combinatorTyped := combinator.(type) {
323-
case bucket_aggregations.FilterAgg:
324-
//aggregation.whereClause = model.And([]model.Expr{aggregation.whereClause, combinatorTyped.WhereClause})
325-
case bucket_aggregations.Filters:
326-
// TODO accept second
327-
fmt.Println("Adding ", combinatorTyped.Filters[0].Sql.WhereClause)
328-
aggregation.whereClause = model.And([]model.Expr{aggregation.whereClause, combinatorTyped.Filters[0].Sql.WhereClause}) // TODO check [0]
329-
}
330-
} else {
317+
filterAlreadyInWhereClause := i == 0 && len(aggregation.layers) > 1 && len(layer.currentMetricAggregations) == 0 && len(layer.currentPipelineAggregations) == 0
318+
if !isFilter || !filterAlreadyInWhereClause {
331319
addIfCombinators = append(addIfCombinators, addIfCombinator{len(selectColumns), combinator})
332320
}
333321
}
@@ -353,7 +341,6 @@ func (p *pancakeSqlQueryGenerator) generateSelectCommand(aggregation *pancakeMod
353341
// this change selects by adding -If suffix, e.g. count(*) -> countIf(response_time < 1000)
354342
// they may also add more columns with different prefix and where clauses
355343
var combinatorWhere []model.Expr
356-
fmt.Println("len(addIfCombinators): ", len(addIfCombinators))
357344
for i := len(addIfCombinators) - 1; i >= 0; i-- { // reverse order is important
358345
combinator := addIfCombinators[i]
359346
selectsBefore := selectColumns[:combinator.selectNr]

quesma/queryparser/pancake_sql_query_generation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestPancakeQueryGeneration(t *testing.T) {
165165
if len(expectedMinusActual) != 0 {
166166
pp.Println("EXPECTED diff", expectedMinusActual)
167167
}
168-
pp.Println("ACTUAL", pancakeJson)
168+
//pp.Println("ACTUAL", pancakeJson)
169169
//pp.Println("EXPECTED", expectedAggregationsPart)
170170
assert.True(t, util.AlmostEmpty(actualMinusExpected, acceptableDifference))
171171
assert.True(t, util.AlmostEmpty(expectedMinusActual, acceptableDifference))

quesma/queryparser/pancake_transformer.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,26 @@ func (a *pancakeTransformer) aggregationTreeToPancakes(topLevel pancakeAggregati
403403
whereClause: topLevel.whereClause,
404404
sampleLimit: sampleLimit,
405405
}
406+
pancakeResults = append(pancakeResults, &newPancake)
407+
408+
// TODO: if both top_hits/top_metrics, and filters, it probably won't work...
409+
// Care: order of these two functions is unfortunately important.
410+
// Should be fixed after this TODO
411+
newFiltersPancakes := a.createFiltersPancakes(&newPancake)
406412
additionalTopHitPancakes, err := a.createTopHitAndTopMetricsPancakes(&newPancake)
407413
if err != nil {
408414
return nil, err
409415
}
410-
pancakeResults = append(pancakeResults, &newPancake)
416+
411417
pancakeResults = append(pancakeResults, additionalTopHitPancakes...)
412-
pancakeResults = append(pancakeResults, a.createFiltersPancakes(&newPancake)...)
418+
pancakeResults = append(pancakeResults, newFiltersPancakes...)
413419
}
414420

415421
return
416422
}
417423

424+
// createFiltersPancakes only does something, if first layer aggregation is Filters.
425+
// It creates new pancakes for each filter in that aggregation, and updates `pancake` to have only first filter.
418426
func (a *pancakeTransformer) createFiltersPancakes(pancake *pancakeModel) (newPancakes []*pancakeModel) {
419427
if len(pancake.layers) == 0 || pancake.layers[0].nextBucketAggregation == nil {
420428
return
@@ -423,9 +431,9 @@ func (a *pancakeTransformer) createFiltersPancakes(pancake *pancakeModel) (newPa
423431
firstLayer := pancake.layers[0]
424432
filters, isFilters := firstLayer.nextBucketAggregation.queryType.(bucket_aggregations.Filters)
425433
canSimplyAddFilterToWhereClause := len(firstLayer.currentMetricAggregations) == 0 && len(firstLayer.currentPipelineAggregations) == 0
426-
isItNeeded := len(pancake.layers) > 1
434+
areNewPancakesReallyNeeded := len(pancake.layers) > 1 // if there is only one layer, it's better to get it done with combinators.
427435

428-
if !isFilters || !canSimplyAddFilterToWhereClause || !isItNeeded {
436+
if !isFilters || !canSimplyAddFilterToWhereClause || !areNewPancakesReallyNeeded || len(filters.Filters) == 0 {
429437
return
430438
}
431439

@@ -441,6 +449,7 @@ func (a *pancakeTransformer) createFiltersPancakes(pancake *pancakeModel) (newPa
441449

442450
// Then update original to have 1 filter as well
443451
pancake.layers[0].nextBucketAggregation.queryType = filters.NewFiltersSingleFilter(0)
452+
pancake.whereClause = model.And([]model.Expr{pancake.whereClause, filters.Filters[0].Sql.WhereClause})
444453

445454
return
446455
}

quesma/testdata/aggregation_requests.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,9 +3487,9 @@ var AggregationTests = []AggregationTestCase{
34873487
sumOrNull("taxful_total_price") AS "metric__time_offset_split__0__1_col_0",
34883488
sumOrNull("taxful_total_price") AS "metric__time_offset_split__0__2_col_0"
34893489
FROM __quesma_table_name
3490-
WHERE ((("order_date">=fromUnixTimestamp64Milli(1708639056376) AND
3491-
"order_date"<=fromUnixTimestamp64Milli(1709243856376)) OR
3492-
("order_date">=fromUnixTimestamp64Milli(1708034256376) AND "order_date"<=
3490+
WHERE ((("order_date">=fromUnixTimestamp64Milli(1708639056376) AND "order_date"
3491+
<=fromUnixTimestamp64Milli(1709243856376)) OR ("order_date">=
3492+
fromUnixTimestamp64Milli(1708034256376) AND "order_date"<=
34933493
fromUnixTimestamp64Milli(1708639056376))) AND ("order_date">=
34943494
fromUnixTimestamp64Milli(1708639056376) AND "order_date"<=
34953495
fromUnixTimestamp64Milli(1709243856376)))
@@ -3504,14 +3504,12 @@ var AggregationTests = []AggregationTestCase{
35043504
sumOrNull("taxful_total_price") AS "metric__time_offset_split__0__1_col_0",
35053505
sumOrNull("taxful_total_price") AS "metric__time_offset_split__0__2_col_0"
35063506
FROM __quesma_table_name
3507-
WHERE (((("order_date">=fromUnixTimestamp64Milli(1708639056376) AND "order_date"
3508-
<=fromUnixTimestamp64Milli(1709243856376)) OR ("order_date">=
3509-
fromUnixTimestamp64Milli(1708034256376) AND "order_date"<=
3510-
fromUnixTimestamp64Milli(1708639056376))) AND ("order_date">=
3511-
fromUnixTimestamp64Milli(1708034256376) AND "order_date"<=
3512-
fromUnixTimestamp64Milli(1708639056376))) AND ("order_date">=
3513-
fromUnixTimestamp64Milli(1708034256376) AND "order_date"<=
3514-
fromUnixTimestamp64Milli(1708639056376)))
3507+
WHERE ((("order_date">=fromUnixTimestamp64Milli(1708639056376) AND
3508+
"order_date"<=fromUnixTimestamp64Milli(1709243856376)) OR
3509+
("order_date">=fromUnixTimestamp64Milli(1708034256376) AND
3510+
"order_date"<=fromUnixTimestamp64Milli(1708639056376))) AND
3511+
("order_date">=fromUnixTimestamp64Milli(1708034256376) AND
3512+
"order_date"<=fromUnixTimestamp64Milli(1708639056376)))
35153513
GROUP BY toInt64(toUnixTimestamp64Milli("order_date") / 86400000) AS
35163514
"aggr__time_offset_split__0__key_0"
35173515
ORDER BY "aggr__time_offset_split__0__key_0" ASC`,

0 commit comments

Comments
 (0)