You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] IS NOT NULL with alias throws SemanticCheckException #292
We need to evaluate the tech solution. The challenge is that SQL statements is analyzed in predefined order of, FROM -> WHERE -> SELECT, alias defined in SELECT which is not visible to WHERE clause.
Hey team,
I notice that IS NOT NULL/IS NULL with alias in ORDER BY also throws exception: "This query is not explainable."
(Using Query Workbench kibana plugin in ES 7.10)
SELECT dest_state_nm AS city FROM all_flights_integ_tests ORDER BY city IS NOT NULL
Hey team, I notice that IS NOT NULL/IS NULL with alias in ORDER BY also throws exception: "This query is not explainable." (Using Query Workbench kibana plugin in ES 7.10)
SELECT dest_state_nm AS city FROM all_flights_integ_tests ORDER BY city IS NOT NULL
Could you help and check as well? Thanks
@jingyali-apr Thanks for reporting the issue! Could you share your index mapping and some test data for investigation? And it would be helpful if you can find error stacktrace in ES log.
Hey team, I notice that IS NOT NULL/IS NULL with alias in ORDER BY also throws exception: "This query is not explainable." (Using Query Workbench kibana plugin in ES 7.10)
SELECT dest_state_nm AS city FROM all_flights_integ_tests ORDER BY city IS NOT NULL
Could you help and check as well? Thanks
@jingyali-apr Thanks for reporting the issue! Could you share your index mapping and some test data for investigation? And it would be helpful if you can find error stacktrace in ES log.
POST _opendistro/_sql/
{
"query": "SELECT dest_state_nm AS state FROM all_flights_integ_tests GROUP BY state ORDER BY state limit 5"
}
{
"schema": [
{
"name": "dest_state_nm",
"alias": "state",
"type": "keyword"
}
],
"datarows": [
[
"Alabama"
],
[
"Alaska"
],
[
"California"
],
[
"Colorado"
],
[
"Connecticut"
]
],
"total": 5,
"size": 5,
"status": 200
}
Failed case with IS NOT NULL
POST _opendistro/_sql/
{
"query": "SELECT dest_state_nm AS state FROM all_flights_integ_tests GROUP BY state ORDER BY state IS NOT NULL limit 5"
}
{
"error": {
"reason": "Invalid SQL query",
"details": "can't resolve Symbol(namespace=FIELD_NAME, name=state) in type env",
"type": "SemanticCheckException"
},
"status": 400
}
Successful case without using alias
POST _opendistro/_sql/
{
"query": "SELECT dest_state_nm AS state FROM all_flights_integ_tests GROUP BY state ORDER BY dest_state_nm IS NOT NULL limit 5"
}
{
"schema": [
{
"name": "dest_state_nm",
"alias": "state",
"type": "keyword"
}
],
"datarows": [
[
"Alabama"
],
[
"Wisconsin"
],
[
"West Virginia"
],
[
"Virginia"
],
[
"Vermont"
]
],
"total": 5,
"size": 5,
"status": 200
}
@jingyali-apr Could you try to use the original field name in ORDER BY clause as below?
POST _opendistro/_sql
{
"query" : """
SELECT dest_state_nm AS state FROM all_flights_integ_tests GROUP BY state ORDER BY dest_state_nm IS NOT NULL
"""
}
@jingyali-apr Could you try to use the original field name in ORDER BY clause as below?
POST _opendistro/_sql
{
"query" : """
SELECT dest_state_nm AS state FROM all_flights_integ_tests GROUP BY state ORDER BY dest_state_nm IS NOT NULL
"""
}
Yeah, I tried this and it worked.
See my Successful case without using alias in above comment.
When we try to replace alias in ORDER BY, we only replace ordinal or alias directly. In our case, state is NOT NULL is an expression which causes alias replacement fail. Will debug to confirm and open issue/PR to fix. However, just to let you know, the fix would be only available in latest OpenSearch version (probably 2.3 next). Thanks!
Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed because when the WHERE code is executed, the column value may not yet be determined.
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
Activity
penghuo commentedon Jan 6, 2022
Thanks for reporting issue. Currently we doesn't support use alias in where clause. One workaround should be
penghuo commentedon Jan 6, 2022
@ankurgupta4891 In opendistro-for-elasticsearch/sql#1145, you mentioned, "I tried to give simple version of our use case but we need to get alias support with IS NOT NULL"
Could you share your use case? Are u using tools to auto generate query?
penghuo commentedon Mar 15, 2022
confirmed offline. Query is auto generated.
Next Step
We need to evaluate the tech solution. The challenge is that SQL statements is analyzed in predefined order of, FROM -> WHERE -> SELECT, alias defined in SELECT which is not visible to WHERE clause.
jingyali-apr commentedon Aug 31, 2022
Hey team,
I notice that
IS NOT NULL/IS NULLwith alias inORDER BYalso throws exception: "This query is not explainable."(Using Query Workbench kibana plugin in ES 7.10)
Could you help and check as well?
Thanks
dai-chen commentedon Sep 1, 2022
@jingyali-apr Thanks for reporting the issue! Could you share your index mapping and some test data for investigation? And it would be helpful if you can find error stacktrace in ES log.
jingyali-apr commentedon Sep 1, 2022
Hey @dai-chen, please check below samples.
Successful case without
IS NOT NULLFailed case with
IS NOT NULLSuccessful case without using alias
Partial test data:
Thanks
dai-chen commentedon Sep 1, 2022
@jingyali-apr Could you try to use the original field name in
ORDER BYclause as below?jingyali-apr commentedon Sep 1, 2022
Yeah, I tried this and it worked.
See my
Successful case without using aliasin above comment.dai-chen commentedon Sep 1, 2022
@jingyali-apr Thanks for confirming! I may have found where things go wrong:
sql/sql/src/main/java/org/opensearch/sql/sql/parser/AstSortBuilder.java
Line 53 in 907019c
When we try to replace alias in
ORDER BY, we only replace ordinal or alias directly. In our case,state is NOT NULLis an expression which causes alias replacement fail. Will debug to confirm and open issue/PR to fix. However, just to let you know, the fix would be only available in latest OpenSearch version (probably 2.3 next). Thanks!Yury-Fridlyand commentedon Sep 12, 2022
https://stackoverflow.com/a/942592
You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses.
Copied from MySQL documentation
jpluimers commentedon Dec 1, 2022
Note that was the [Wayback/Archive] MySQL :: MySQL 5.1 Reference Manual :: B.5.5.4 Problems with Column Aliases documentation until December 2009. After that it got rephrased (including "doesn't allow" to "disallow", "alias" to "aliases", "code" to "clause) and still is in the current form at [Wayback/Archive] MySQL :: MySQL 8.0 Reference Manual :: B.3.4.4 Problems with Column Aliases:
Fix analyzer/search semantics issues (opensearch-project#292, opensea…