Skip to content

ES|QL: columns not projected away despite KEEP #126026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luigidellaquila opened this issue Apr 1, 2025 · 4 comments · Fixed by #126598
Closed

ES|QL: columns not projected away despite KEEP #126026

luigidellaquila opened this issue Apr 1, 2025 · 4 comments · Fixed by #126598
Labels
:Analytics/ES|QL AKA ESQL >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)

Comments

@luigidellaquila
Copy link
Contributor

row foo = 10 
| stats field_1 = max(foo), this = count(*), field_2 = max(foo) by foo  
| rename this AS field_2 
| keep field_1, field_2  
| eval field_2 = 1, field_1 = 1
      foo      |    field_1    |    field_2    
---------------+---------------+---------------
10             |1              |1              

foo shouldn't be there.

Interestingly, if you try to use the wrong column, the query will fail:

row foo = 10 
| stats field_1 = max(foo), this = count(*), field_2 = max(foo) by foo  
| rename this AS field_2 
| keep field_1, field_2  
| eval field_2 = 1, field_1 = 1, bar = foo
{
    "error": {
        "root_cause": [
            {
                "type": "verification_exception",
                "reason": "Found 1 problem\nline 1:176: Unknown column [foo]"
            }
        ],
        "type": "verification_exception",
        "reason": "Found 1 problem\nline 1:176: Unknown column [foo]"
    },
    "status": 400
}
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Apr 1, 2025
@kanoshiou
Copy link
Contributor

kanoshiou commented Apr 3, 2025

Here is a simplified query to reproduce this issue.

row foo = 10 
| stats this = max(foo) by foo 
| keep  this 
| eval this = 2

However, I came across another query that doesn't seem to produce the correct result. I'm unsure whether it was triggered by the same cause.

row foo = 10 
| stats this = max(foo) 
| eval this = 2
               |     this      
---------------+---------------
null           |2                    

@kanoshiou
Copy link
Contributor

After conducting some research, I have concluded that the queries mentioned above are unrelated and I believe the second query deserves a separate issue #126392.

@kanoshiou
Copy link
Contributor

kanoshiou commented Apr 9, 2025

For this query

row foo = 10 
| stats this = max(foo) by foo 
| keep  this 
| eval this = 2

stats this = max(foo) by foo | keep this are initially combined to Aggregate[[foo{r}#12],[MAX(foo{r}#12,true[BOOLEAN]) AS this]], then column this is pruned due to eval this = 2, resulting in the LogicalPlan transforming into Aggregate[[foo{r}#12],[foo{r}#12]].

After the optimizer rules CombineProjections and PruneColumns are applied, column this is removed, while column foo is retained, which serves as the root cause of the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants