gortiz commented on code in PR #8979: URL: https://github.com/apache/pinot/pull/8979#discussion_r973917527
########## pinot-core/src/main/java/org/apache/pinot/core/plan/SelectionPlanNode.java: ########## @@ -50,67 +52,113 @@ public Operator<IntermediateResultsBlock> run() { List<ExpressionContext> expressions = SelectionOperatorUtils.extractExpressions(_queryContext, _indexSegment); int limit = _queryContext.getLimit(); - if (limit > 0) { - List<OrderByExpressionContext> orderByExpressions = _queryContext.getOrderByExpressions(); - if (orderByExpressions == null) { - // Selection only - TransformOperator transformOperator = new TransformPlanNode(_indexSegment, _queryContext, expressions, - Math.min(limit, DocIdSetPlanNode.MAX_DOC_PER_CALL)).run(); - return new SelectionOnlyOperator(_indexSegment, _queryContext, expressions, transformOperator); - } else { - // Selection order-by - if (isAllOrderByColumnsSorted(orderByExpressions)) { - // All order-by columns are sorted, no need to sort the records - TransformOperator transformOperator = new TransformPlanNode(_indexSegment, _queryContext, expressions, - Math.min(limit + _queryContext.getOffset(), DocIdSetPlanNode.MAX_DOC_PER_CALL)).run(); - return new SelectionOrderByOperator(_indexSegment, _queryContext, expressions, transformOperator, true); - } else if (orderByExpressions.size() == expressions.size()) { - // All output expressions are ordered - TransformOperator transformOperator = - new TransformPlanNode(_indexSegment, _queryContext, expressions, DocIdSetPlanNode.MAX_DOC_PER_CALL).run(); - return new SelectionOrderByOperator(_indexSegment, _queryContext, expressions, transformOperator, false); - } else { - // Not all output expressions are ordered, only fetch the order-by expressions and docId to avoid the - // unnecessary data fetch - List<ExpressionContext> expressionsToTransform = new ArrayList<>(orderByExpressions.size()); - for (OrderByExpressionContext orderByExpression : orderByExpressions) { - expressionsToTransform.add(orderByExpression.getExpression()); - } - TransformOperator transformOperator = - new TransformPlanNode(_indexSegment, _queryContext, expressionsToTransform, - DocIdSetPlanNode.MAX_DOC_PER_CALL).run(); - return new SelectionOrderByOperator(_indexSegment, _queryContext, expressions, transformOperator, false); - } - } - } else { + if (limit == 0) { // Empty selection (LIMIT 0) TransformOperator transformOperator = new TransformPlanNode(_indexSegment, _queryContext, expressions, 0).run(); return new EmptySelectionOperator(_indexSegment, expressions, transformOperator); } + List<OrderByExpressionContext> orderByExpressions = _queryContext.getOrderByExpressions(); + + if (orderByExpressions == null) { + // Selection only + // ie: SELECT ... FROM Table WHERE ... LIMIT 10 + int actualLimit = Math.min(limit, DocIdSetPlanNode.MAX_DOC_PER_CALL); + TransformPlanNode planNode = new TransformPlanNode(_indexSegment, _queryContext, expressions, actualLimit); + TransformOperator transformOperator = planNode.run(); + + return new SelectionOnlyOperator(_indexSegment, _queryContext, expressions, transformOperator); + } + int numOrderByExpressions = orderByExpressions.size(); + // Although it is a break of abstraction, some code, specially merging, assumes that if there is an order by + // expression the operator will return a block whose selection result is a priority queue. + int sortedColumnsPrefixSize = getSortedColumnsPrefix(orderByExpressions); Review Comment: > So what we can do here is deciding whether to use this optimization at query time, and not use it when null handling is enabled and the column contains null values What I did was to change the method that checks whether an expression is sorted or not by adding the following condition: ```java // If there are null values, we cannot trust DataSourceMetadata.isSorted if (isNullHandlingEnabled) { NullValueVectorReader nullValueVector = dataSource.getNullValueVector(); if (nullValueVector != null && !nullValueVector.getNullBitmap().isEmpty()) { return false; } } ``` AFAIK that is the most precise way to check if the column has a null value. I'm open to change that to other better ways if exists. It would be great if we could do something like that directly in DataSourceMetadata, but we don't have the null vector there. -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org