[Analytics/DataFusion] Sort on an index-sorted Parquet shard returns wrong null order: declared file ordering ignores `index.sort.missing`
Describe the bug
For a composite index with a Parquet primary and index.sort.field, the DataFusion backend tells DataFusion that each Parquet file is already sorted by the index sort (build_file_sort_order in session_context.rs, and build_projected_lex_ordering in indexed_table/table_provider.rs). The declared null placement is hardcoded: ASC means NULLS FIRST and DESC means NULLS LAST. index.sort.missing is never passed to the native side, because DataFusionPlugin only forwards index.sort.field and index.sort.order.
The Parquet writer does honour index.sort.missing, and its default is _last in both directions. So on a default ASC index sort, the file is written with nulls last but declared as nulls first.
When a shard's data sits in a single Parquet file (a small index, or after a force merge), DataFusion trusts the declared ordering and drops the SortExec. The query then returns rows in file order, with nulls in the wrong place. With several files per shard the SortExec/TopK is kept and results are correct.
The comment on build_file_sort_order says a wrong nulls claim "at worst causes DataFusion's per-file chain validator to reject the ordering ... never wrong results". That only holds when there is more than one file.
To reproduce
PUT sort_repro
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"index.pluggable.dataformat.enabled": true,
"index.pluggable.dataformat": "composite",
"index.composite.primary_data_format": "parquet",
"index.composite.secondary_data_formats": ["lucene"],
"index.sort.field": "x",
"index.sort.order": "asc"
},
"mappings": { "properties": { "id": { "type": "long" }, "x": { "type": "long" } } }
}
POST sort_repro/_bulk?refresh=true
{"index":{}}
{"id":1,"x":1}
{"index":{}}
{"id":2,"x":50}
{"index":{}}
{"id":3,"x":-5}
{"index":{}}
{"id":4}
POST _plugins/_ppl
{"query": "source=sort_repro | sort x | fields id"}| Index sort | Query | Expected | Actual |
|---|---|---|---|
x asc (default missing _last) |
sort x |
4, 3, 1, 2 | 3, 1, 2, 4 |
x desc, missing: _first |
sort - x |
2, 1, 3, 4 | 4, 2, 1, 3 |
The expected values match the same query on a Lucene index, and on the same composite index without index.sort.field. With profile, the shard plan contains only DataSourceExec: ... projection=[id], limit=..., with no SortExec.
Expected behavior
A query's ORDER BY / sort result must not depend on the index sort. Either:
- pass
index.sort.missingthrough tobuild_file_sort_order/build_projected_lex_orderingand declare the null placement the writer actually used, or - don't declare an ordering for a field when its null placement isn't known.
DataFusion will then keep the sort whenever the query's null order differs from the file's.
Related
#22905 extends the same two builders to LIST (multi-value) sort keys, declaring array_min for ASC and array_max for DESC. That change also needs to honour index.sort.mode: with index.sort.order: asc and index.sort.mode: max, the file is written in MAX order but declared as array_min, so the same single-file shape returns wrong row order and not just wrong null placement.
Environment
main-equivalent build with #22900 and #22905 applied; the analytics-backend-datafusion stack; queries through PPL.
Source: opensearch-project/OpenSearch