Skip to content
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

SQL: Wrong sort order for derived long fields #77301

Open
Luegg opened this issue Sep 6, 2021 · 3 comments
Open

SQL: Wrong sort order for derived long fields #77301

Luegg opened this issue Sep 6, 2021 · 3 comments
Labels
:Analytics/SQL SQL querying >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo)

Comments

@Luegg
Copy link
Contributor

Luegg commented Sep 6, 2021

Given an index long_oder with the following content:

POST long_order/_bulk

{"index": {"_id": 1}}
{"l": 9223372036854775807}
{"index": {"_id": 2}}
{"l": 9223372036854775805}
{"index": {"_id": 3}}
{"l": 9223372036854775806}
{"index": {"_id": 4}}
{"l": 9223372036854775804}

The query SELECT l - 1 + 1 FROM long_order ORDER BY 1 retrieves the records in the wrong order:

     l - 1 + 1     
-------------------
9223372036854775807
9223372036854775805
9223372036854775806
9223372036854775804

This bug is likely caused by the precision loss of converting long values to double in the script sort context:

{"size":1000,"_source":false,"fields":[{"field":"l"}],"sort":[{"_script":{"script":{"source":"InternalQlScriptUtils.nullSafeSortNumeric(InternalSqlScriptUtils.add(InternalSqlScriptUtils.sub(InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2))","lang":"painless","params":{"v0":"l","v1":1,"v2":1}},"type":"number","order":"asc"}}]}
@Luegg Luegg added >bug :Analytics/SQL SQL querying labels Sep 6, 2021
@elasticmachine elasticmachine added the Team:QL (Deprecated) Meta label for query languages team label Sep 6, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-ql (Team:QL)

@chenyuzhi459
Copy link

Hello, it is really precision loss in InternalQlScriptUtils.nullSafeSortNumeric method, I have fixed it in #77701 , you can try it out.

@wchaparro wchaparro removed the Team:QL (Deprecated) Meta label for query languages team label Jan 17, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 17, 2024
@elasticsearchmachine
Copy link
Collaborator

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

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

No branches or pull requests

5 participants