xiangfu0 commented on code in PR #19866:
URL: https://github.com/apache/pinot/pull/19866#discussion_r4361574979
##########
pinot-core/src/main/java/org/apache/pinot/core/plan/SelectionPlanNode.java:
##########
@@ -150,13 +150,28 @@ private BaseProjectOperator<?>
getSortedByProject(List<ExpressionContext> expres
/// but ORDER BY sorted_col1 DESC, sorted_col2 ASC and ORDER BY sorted_col1
ASC, sorted_col2 DESC will return 1 while
/// ORDER BY not_sorted, sorted_col1 will return 0 because the first column
is not sorted.
///
- /// It doesn't make sense to add literal expressions in an order by
expression, but if they are included, they are
- /// considered sorted and its ASC/DESC is ignored.
+ /// Literal and function expressions cannot establish a sorted prefix
because they are not backed by any data source
+ /// column, so ORDER BY <literal>, ... or ORDER BY <function>, ... returns 0
even if followed by sorted identifiers.
+ /// Once a sorted identifier has established the prefix, trailing literals
are effectively constant and do not disrupt
+ /// ordering, so they extend the prefix (e.g. ORDER BY sorted_col, 'x'
returns 2).
///
/// @return the max number that guarantees that from the first expression to
the returned number, the index is already
/// sorted.
private int getSortedColumnsPrefix(List<OrderByExpressionContext>
orderByExpressions, boolean isNullHandlingEnabled) {
- boolean asc = orderByExpressions.get(0).isAsc();
+ // LITERAL and FUNCTION expressions cannot establish a sorted prefix: they
are not backed by a data source column,
+ // so the segment's natural sort order cannot be relied on for them. Only
IDENTIFIER expressions can start the
+ // prefix. Once established, trailing LITERAL expressions extend the
prefix because their constant value does not
+ // disrupt ordering, but trailing FUNCTION expressions still terminate it
(we don't know whether the function is
+ // monotonic over the sorted column).
+ OrderByExpressionContext firstExpr = orderByExpressions.get(0);
+ switch (firstExpr.getExpression().getType()) {
+ case LITERAL:
+ case FUNCTION:
+ return 0;
+ default:
+ // fall through
+ }
Review Comment:
Confirmed this is correct for the mixed cases. Because the partial-order
operators derive ASC/DESC from the first IDENTIFIER (not order-by[0]), a
leading literal/function would otherwise mismatch the prefix the plan node
assumes starts at index 0. Returning 0 here cleanly sidesteps that. A test
asserting these resolve to `SelectionOrderByOperator` would be a great
companion.
##########
pinot-core/src/main/java/org/apache/pinot/core/plan/SelectionPlanNode.java:
##########
@@ -150,13 +150,28 @@ private BaseProjectOperator<?>
getSortedByProject(List<ExpressionContext> expres
/// but ORDER BY sorted_col1 DESC, sorted_col2 ASC and ORDER BY sorted_col1
ASC, sorted_col2 DESC will return 1 while
/// ORDER BY not_sorted, sorted_col1 will return 0 because the first column
is not sorted.
///
- /// It doesn't make sense to add literal expressions in an order by
expression, but if they are included, they are
- /// considered sorted and its ASC/DESC is ignored.
+ /// Literal and function expressions cannot establish a sorted prefix
because they are not backed by any data source
+ /// column, so ORDER BY <literal>, ... or ORDER BY <function>, ... returns 0
even if followed by sorted identifiers.
+ /// Once a sorted identifier has established the prefix, trailing literals
are effectively constant and do not disrupt
+ /// ordering, so they extend the prefix (e.g. ORDER BY sorted_col, 'x'
returns 2).
///
/// @return the max number that guarantees that from the first expression to
the returned number, the index is already
/// sorted.
private int getSortedColumnsPrefix(List<OrderByExpressionContext>
orderByExpressions, boolean isNullHandlingEnabled) {
- boolean asc = orderByExpressions.get(0).isAsc();
+ // LITERAL and FUNCTION expressions cannot establish a sorted prefix: they
are not backed by a data source column,
+ // so the segment's natural sort order cannot be relied on for them. Only
IDENTIFIER expressions can start the
+ // prefix. Once established, trailing LITERAL expressions extend the
prefix because their constant value does not
+ // disrupt ordering, but trailing FUNCTION expressions still terminate it
(we don't know whether the function is
+ // monotonic over the sorted column).
+ OrderByExpressionContext firstExpr = orderByExpressions.get(0);
Review Comment:
This guard is the right place for the fix and reads clearly. Heads up
though: the PR description says the solution is "change `isSorted()` to return
false for LITERAL" — but the actual change keeps `isSorted` returning true and
adds this leading-term check instead. Worth updating the description so
reviewers and the git history match what shipped.
--
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]