Asthenia0412 opened a new pull request, #19866:
URL: https://github.com/apache/pinot/pull/19866
## Background
Issue #19701: Single-stage selection queries that use positional ORDER BY
(e.g. `ORDER BY 2`) or order by constant-folded functions (e.g. `ORDER BY
now()` where Calcite folds `now()` to a literal timestamp) fail with:
```
java.lang.IllegalArgumentException: The query is not order by identifiers
at
org.apache.pinot.core.operator.query.SelectionPartiallyOrderedByDescOperation.<init>
at org.apache.pinot.core.plan.SelectionPlanNode.run
```
## Problem
In `SelectionPlanNode.isSorted()`, the `LITERAL` case unconditionally
returned `true`. This caused queries whose first order-by expression is a
literal to enter the partial-order optimized path
(`SelectionPartiallyOrderedByLinearOperator` /
`SelectionPartiallyOrderedByDescOperation`).
Both of these operators require the first order-by expression to be an
`IDENTIFIER` backed by a real data source column (so they can rely on the
segment's natural sort order). When only `LITERAL` (or `FUNCTION`) expressions
are present, their precondition check throws the exception above.
Two reproduction cases from the issue:
- `SELECT col, now() AS t FROM my_table ORDER BY 2 DESC LIMIT 1;` — `ORDER
BY 2` may remain unresolved as a `LITERAL`, or Calcite may fold `now()` into a
constant `LITERAL`.
- `SELECT col, now() AS t FROM my_table ORDER BY now() DESC LIMIT 1;` —
`now()` gets folded into a constant literal.
## Solution
Change `isSorted()` to return `false` for `LITERAL` expressions. Literals
are not backed by any data source column and therefore cannot participate in
the sorted-columns-prefix optimization. This makes such queries fall through to
the generic `SelectionOrderByOperator`, which handles them correctly.
The `FUNCTION` case already returned `false`, which is correct for the same
reason.
## Testing
- `mvn -pl pinot-core -am compile -DskipTests` succeeds.
- Behavior change is conservative: the query now takes a generic sorting
path instead of the optimized partial-order path. Results are identical; only
the execution strategy differs.
- All existing queries that legitimately use `IDENTIFIER` order-by continue
to take the optimized path.
Closes #19701
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]