diff --git a/docs/changelog/104718.yaml b/docs/changelog/104718.yaml new file mode 100644 index 000000000000..ffe889bb28a3 --- /dev/null +++ b/docs/changelog/104718.yaml @@ -0,0 +1,6 @@ +pr: 104718 +summary: "ESQL: Fix replacement of nested expressions in aggs with multiple parameters" +area: ES|QL +type: bug +issues: + - 104706 diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec index b3743d53e197..0dd2f4f93742 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec @@ -929,6 +929,21 @@ sum(sal):l | sum(salary + 10000):l | left(first_name, 1):s | concat(gender, t 43370 | 43370 | B | F2 ; +nestedExpressionMultipleParams#[skip:-8.12.99,reason:supported in 8.13+] +FROM employees +| STATS p = percentile(emp_no + 10, 50), m = median(emp_no + 10) BY languages +| SORT languages +; + +p:double | m:double | languages:integer +10053.0 | 10053.0 | 1 +10069.0 | 10069.0 | 2 +10068.0 | 10068.0 | 3 +10060.5 | 10060.5 | 4 +10076.0 | 10076.0 | 5 +10034.5 | 10034.5 | null +; + groupByNull#[skip:-8.12.99,reason:bug fixed in 8.13+] ROW a = 1, c = null | STATS COUNT(a) BY c; diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats_count_distinct.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats_count_distinct.csv-spec index e0e8ca351cfe..8f926fd8f6ed 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats_count_distinct.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats_count_distinct.csv-spec @@ -152,3 +152,16 @@ m:long | languages:i 20 | 5 10 | null ; + + +countDistinctWithGroupPrecisionAndNestedExpression#[skip:-8.12.99,reason:supported in 8.13+] +from employees | stats m = count_distinct(height + 5, 9876) by languages | sort languages; + +m:long | languages:i +13 | 1 +16 | 2 +14 | 3 +15 | 4 +20 | 5 +10 | null +; diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java index b0b5ec37dbcc..81f712ae0408 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizer.java @@ -1141,7 +1141,9 @@ protected LogicalPlan rule(Aggregate aggregate) { return newAlias.toAttribute(); }); // replace field with attribute - result = af.replaceChildren(Collections.singletonList(attr)); + List newChildren = new ArrayList<>(af.children()); + newChildren.set(0, attr); + result = af.replaceChildren(newChildren); } return result; });