Skip to content

Commit

Permalink
ES|QL: fix stats by constant expresson with alias (elastic#117551)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila authored Nov 26, 2024
1 parent f57c43c commit b22d185
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/117551.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 117551
summary: Fix stats by constant expresson with alias
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,18 @@ m:integer | y+1:integer
11 | 12
;

statsByConstantExpressionWithAliasAndSort
required_capability: fix_stats_by_foldable_expression_2
FROM employees
| EVAL y = "a"
| STATS count = COUNT() BY x = y
| SORT x
;

count:long | x:keyword
100 | a
;

filterIsAlwaysTrue
required_capability: per_agg_filtering
FROM employees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,12 @@ public enum Cap {
/**
* support for aggregations on semantic_text
*/
SEMANTIC_TEXT_AGGREGATIONS(EsqlCorePlugin.SEMANTIC_TEXT_FEATURE_FLAG);
SEMANTIC_TEXT_AGGREGATIONS(EsqlCorePlugin.SEMANTIC_TEXT_FEATURE_FLAG),

/**
* Fix for https://github.com/elastic/elasticsearch/issues/114714, again
*/
FIX_STATS_BY_FOLDABLE_EXPRESSION_2,;

private final boolean enabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ static Set<String> fieldNames(LogicalPlan parsed, Set<String> enrichPolicyMatchF
// remove any already discovered UnresolvedAttributes that are in fact aliases defined later down in the tree
// for example "from test | eval x = salary | stats max = max(x) by gender"
// remove the UnresolvedAttribute "x", since that is an Alias defined in "eval"
AttributeSet planRefs = Expressions.references(p.expressions());
AttributeSet planRefs = p.references();
p.forEachExpressionDown(Alias.class, alias -> {
// do not remove the UnresolvedAttribute that has the same name as its alias, ie "rename id = id"
// or the UnresolvedAttributes that are used in Functions that have aliases "STATS id = MAX(id)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,114 @@ public void testDocsStats() {
| SORT languages""", Set.of("emp_no", "emp_no.*", "languages", "languages.*"));
}

public void testEvalStats() {

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY y""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY y
| SORT y""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y
| SORT x""", Set.of("_index"));

assertFieldNames("""
FROM employees
| STATS count = COUNT(*) BY first_name
| SORT first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y
| SORT x, first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL first_name = "a"
| STATS count = COUNT(*) BY first_name
| SORT first_name""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY first_name = to_upper(y)
| SORT first_name""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = to_upper(first_name), z = "z"
| STATS count = COUNT(*) BY first_name = to_lower(y), z
| SORT first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y, z = first_name
| SORT x, z""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y, first_name
| SORT x, first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(first_name) BY x = y
| SORT x
| DROP first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y
| MV_EXPAND x""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY first_name, y
| MV_EXPAND first_name""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| MV_EXPAND first_name
| EVAL y = "a"
| STATS count = COUNT(*) BY first_name, y
| SORT y""", Set.of("first_name", "first_name.*"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| MV_EXPAND y
| STATS count = COUNT(*) BY x = y
| SORT x""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY x = y
| STATS count = COUNT(count) by x
| SORT x""", Set.of("_index"));

assertFieldNames("""
FROM employees
| EVAL y = "a"
| STATS count = COUNT(*) BY first_name, y
| STATS count = COUNT(count) by x = y
| SORT x""", Set.of("first_name", "first_name.*"));
}

public void testSortWithLimitOne_DropHeight() {
assertFieldNames("from employees | sort languages | limit 1 | drop height*", ALL_FIELDS);
}
Expand Down

0 comments on commit b22d185

Please sign in to comment.