Skip to content

Commit f8811a1

Browse files
ES|QL: fix stats by constant expresson with alias (#117551) (#117613)
1 parent c29de8a commit f8811a1

File tree

5 files changed

+132
-2
lines changed

5 files changed

+132
-2
lines changed

docs/changelog/117551.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 117551
2+
summary: Fix stats by constant expresson with alias
3+
area: ES|QL
4+
type: bug
5+
issues: []

x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats.csv-spec

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,18 @@ m:integer | y+1:integer
27582758
11 | 12
27592759
;
27602760

2761+
statsByConstantExpressionWithAliasAndSort
2762+
required_capability: fix_stats_by_foldable_expression_2
2763+
FROM employees
2764+
| EVAL y = "a"
2765+
| STATS count = COUNT() BY x = y
2766+
| SORT x
2767+
;
2768+
2769+
count:long | x:keyword
2770+
100 | a
2771+
;
2772+
27612773
filterIsAlwaysTrue
27622774
required_capability: per_agg_filtering
27632775
FROM employees

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,12 @@ public enum Cap {
504504
/**
505505
* LOOKUP JOIN
506506
*/
507-
JOIN_LOOKUP(Build.current().isSnapshot());
507+
JOIN_LOOKUP(Build.current().isSnapshot()),
508+
509+
/**
510+
* Fix for https://github.com/elastic/elasticsearch/issues/114714, again
511+
*/
512+
FIX_STATS_BY_FOLDABLE_EXPRESSION_2,;
508513

509514
private final boolean enabled;
510515

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ static Set<String> fieldNames(LogicalPlan parsed, Set<String> enrichPolicyMatchF
463463
// remove any already discovered UnresolvedAttributes that are in fact aliases defined later down in the tree
464464
// for example "from test | eval x = salary | stats max = max(x) by gender"
465465
// remove the UnresolvedAttribute "x", since that is an Alias defined in "eval"
466-
AttributeSet planRefs = Expressions.references(p.expressions());
466+
AttributeSet planRefs = p.references();
467467
p.forEachExpressionDown(Alias.class, alias -> {
468468
// do not remove the UnresolvedAttribute that has the same name as its alias, ie "rename id = id"
469469
// or the UnresolvedAttributes that are used in Functions that have aliases "STATS id = MAX(id)"

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/session/IndexResolverFieldNamesTests.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,114 @@ public void testDocsStats() {
353353
| SORT languages""", Set.of("emp_no", "emp_no.*", "languages", "languages.*"));
354354
}
355355

356+
public void testEvalStats() {
357+
358+
assertFieldNames("""
359+
FROM employees
360+
| EVAL y = "a"
361+
| STATS count = COUNT(*) BY y""", Set.of("_index"));
362+
363+
assertFieldNames("""
364+
FROM employees
365+
| EVAL y = "a"
366+
| STATS count = COUNT(*) BY y
367+
| SORT y""", Set.of("_index"));
368+
369+
assertFieldNames("""
370+
FROM employees
371+
| EVAL y = "a"
372+
| STATS count = COUNT(*) BY x = y
373+
| SORT x""", Set.of("_index"));
374+
375+
assertFieldNames("""
376+
FROM employees
377+
| STATS count = COUNT(*) BY first_name
378+
| SORT first_name""", Set.of("first_name", "first_name.*"));
379+
380+
assertFieldNames("""
381+
FROM employees
382+
| EVAL y = "a"
383+
| STATS count = COUNT(*) BY x = y
384+
| SORT x, first_name""", Set.of("first_name", "first_name.*"));
385+
386+
assertFieldNames("""
387+
FROM employees
388+
| EVAL first_name = "a"
389+
| STATS count = COUNT(*) BY first_name
390+
| SORT first_name""", Set.of("_index"));
391+
392+
assertFieldNames("""
393+
FROM employees
394+
| EVAL y = "a"
395+
| STATS count = COUNT(*) BY first_name = to_upper(y)
396+
| SORT first_name""", Set.of("_index"));
397+
398+
assertFieldNames("""
399+
FROM employees
400+
| EVAL y = to_upper(first_name), z = "z"
401+
| STATS count = COUNT(*) BY first_name = to_lower(y), z
402+
| SORT first_name""", Set.of("first_name", "first_name.*"));
403+
404+
assertFieldNames("""
405+
FROM employees
406+
| EVAL y = "a"
407+
| STATS count = COUNT(*) BY x = y, z = first_name
408+
| SORT x, z""", Set.of("first_name", "first_name.*"));
409+
410+
assertFieldNames("""
411+
FROM employees
412+
| EVAL y = "a"
413+
| STATS count = COUNT(*) BY x = y, first_name
414+
| SORT x, first_name""", Set.of("first_name", "first_name.*"));
415+
416+
assertFieldNames("""
417+
FROM employees
418+
| EVAL y = "a"
419+
| STATS count = COUNT(first_name) BY x = y
420+
| SORT x
421+
| DROP first_name""", Set.of("first_name", "first_name.*"));
422+
423+
assertFieldNames("""
424+
FROM employees
425+
| EVAL y = "a"
426+
| STATS count = COUNT(*) BY x = y
427+
| MV_EXPAND x""", Set.of("_index"));
428+
429+
assertFieldNames("""
430+
FROM employees
431+
| EVAL y = "a"
432+
| STATS count = COUNT(*) BY first_name, y
433+
| MV_EXPAND first_name""", Set.of("first_name", "first_name.*"));
434+
435+
assertFieldNames("""
436+
FROM employees
437+
| MV_EXPAND first_name
438+
| EVAL y = "a"
439+
| STATS count = COUNT(*) BY first_name, y
440+
| SORT y""", Set.of("first_name", "first_name.*"));
441+
442+
assertFieldNames("""
443+
FROM employees
444+
| EVAL y = "a"
445+
| MV_EXPAND y
446+
| STATS count = COUNT(*) BY x = y
447+
| SORT x""", Set.of("_index"));
448+
449+
assertFieldNames("""
450+
FROM employees
451+
| EVAL y = "a"
452+
| STATS count = COUNT(*) BY x = y
453+
| STATS count = COUNT(count) by x
454+
| SORT x""", Set.of("_index"));
455+
456+
assertFieldNames("""
457+
FROM employees
458+
| EVAL y = "a"
459+
| STATS count = COUNT(*) BY first_name, y
460+
| STATS count = COUNT(count) by x = y
461+
| SORT x""", Set.of("first_name", "first_name.*"));
462+
}
463+
356464
public void testSortWithLimitOne_DropHeight() {
357465
assertFieldNames("from employees | sort languages | limit 1 | drop height*", ALL_FIELDS);
358466
}

0 commit comments

Comments
 (0)