Closed
Labels
bugSomething isn't working
Description
What is the bug?
For this query: source=opensearch-sql_test_index_account| sort state | eval age2 = age + 2 | dedup age2 | fields state, age2, the output is ought to be sorted by state. However, the order is not preserved in the result:
| State | Age2 |
|---|---|
| AK | 32 |
| AK | 33 |
| AL | 34 |
| AK | 35 |
| AL | 36 |
| AK | 37 |
| AK | 38 |
| AK | 39 |
| AK | 40 |
| AK | 41 |
| AK | 42 |
| AK | 22 |
| AK | 23 |
| CA | 24 |
| AL | 25 |
| AK | 26 |
| AK | 27 |
| AK | 28 |
| AL | 29 |
| AK | 30 |
| AR | 31 |
The problem can be seen from its physical plan:
{
"calcite": {
"logical": "LogicalSort(sort0=[$0], dir0=[ASC-nulls-first])\n LogicalProject(state=[$7], age2=[$17])\n LogicalFilter(condition=[<=($18, 1)])\n LogicalProject(account_number=[$0], firstname=[$1], address=[$2], balance=[$3], gender=[$4], city=[$5], employer=[$6], state=[$7], age=[$8], email=[$9], lastname=[$10], _id=[$11], _index=[$12], _score=[$13], _maxscore=[$14], _sort=[$15], _routing=[$16], age2=[$17], _row_number_=[ROW_NUMBER() OVER (PARTITION BY $17 ORDER BY $17)])\n LogicalFilter(condition=[IS NOT NULL($17)])\n LogicalProject(account_number=[$0], firstname=[$1], address=[$2], balance=[$3], gender=[$4], city=[$5], employer=[$6], state=[$7], age=[$8], email=[$9], lastname=[$10], _id=[$11], _index=[$12], _score=[$13], _maxscore=[$14], _sort=[$15], _routing=[$16], age2=[+($8, 2)])\n LogicalSort(sort0=[$7], dir0=[ASC-nulls-first])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n",
"physical": "EnumerableCalc(expr#0..2=[{inputs}], expr#3=[1], expr#4=[<=($t2, $t3)], proj#0..1=[{exprs}], $condition=[$t4])\n EnumerableWindow(window#0=[window(partition {1} order by [1] rows between UNBOUNDED PRECEDING and CURRENT ROW aggs [ROW_NUMBER()])])\n EnumerableCalc(expr#0..16=[{inputs}], expr#17=[2], expr#18=[+($t8, $t17)], expr#19=[IS NOT NULL($t8)], state=[$t7], age2=[$t18], $condition=[$t19])\n EnumerableSort(sort0=[$7], dir0=[ASC-nulls-first])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n"
}
}
The order is distorted after dedup, but should be restored afterwards.
I guess it has something to do with incorrect optimization.
How can one reproduce the bug?
curl --location --globoff '{{opensearch}}/_plugins/_ppl/_explain' \
--data '{
"query" : "source=opensearch-sql_test_index_account| sort state | eval age2 = age + 2 | dedup age2 | fields state, age2"
}
'What is the expected behavior?
Return the result with the state order being preserved.
What is your host/environment?
- OS: MacOS
- Version: 3.1.0 (e8e081a)
- Plugins: opensearch-sql
Do you have any screenshots?
Do you have any additional context?
In v2, this is not a problem since its DedupeOperator is an order-preserving operator.
Reacted by florentcheneau
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Type
Projects
- StatusShow more project fieldsDone
Activity
yuancu commentedon Jul 28, 2025
I'm looking into this.
[-][BUG] Order not preserved when deduping columns generated with eval (in v3)[/-][+][BUG] Order not preserved when deduping columns generated (in v3)[/+]Fix dedup pushdown: renamed fields nullified and column order unstable
[BugFix] Fix sort order not preserved through dedup in Calcite engine (…
test(dedup): extend opensearch-project#3922 fixture to actually repro…
[BugFix] Fix sort order not preserved through dedup in Calcite engine (…
[BugFix] Fix sort order not preserved through dedup in Calcite engine (…
Merge upstream/main into feature/mustang-ppl-integration
[BugFix] Fix sort order not preserved through dedup in Calcite engine (…