Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,12 @@ private PartitionSortNode translatePartitionSortNode(PhysicalPartitionTopN<? ext

private SortNode translateSortNode(AbstractPhysicalSort<? extends Plan> sort, PlanNode childNode,
PlanTranslatorContext context) {
TupleDescriptor sortTuple = generateTupleDesc(sort.child().getOutput(), null, context);
Set<ExprId> deferredMaterializedExprIds = sort
.getMutableState(PhysicalOlapScan.DEFERRED_MATERIALIZED_SLOTS)
.map(s -> (Set<ExprId>) s)
.orElse(Collections.emptySet());
TupleDescriptor sortTuple = generateTupleDesc(sort.child().getOutput(),
null, deferredMaterializedExprIds, context);
List<Expr> orderingExprs = Lists.newArrayList();
List<Boolean> ascOrders = Lists.newArrayList();
List<Boolean> nullsFirstParams = Lists.newArrayList();
Expand All @@ -1900,12 +1905,10 @@ private SortNode translateSortNode(AbstractPhysicalSort<? extends Plan> sort, Pl
});
SortInfo sortInfo = new SortInfo(orderingExprs, ascOrders, nullsFirstParams, sortTuple);
SortNode sortNode = new SortNode(context.nextPlanNodeId(), childNode, sortInfo, sort instanceof PhysicalTopN);
if (sort.getMutableState(PhysicalTopN.TWO_PHASE_READ_OPT).isPresent()) {
if (sort.getMutableState(PhysicalOlapScan.DEFERRED_MATERIALIZED_SLOTS).isPresent()) {
sortNode.setUseTwoPhaseReadOpt(true);
sortNode.getSortInfo().setUseTwoPhaseRead();
injectRowIdColumnSlot(sortNode.getSortInfo().getSortTupleDescriptor());
SlotDescriptor childRowIdDesc = sortTuple.getSlots().get(sortTuple.getSlots().size() - 1);
sortNode.getResolvedTupleExprs().add(new SlotRef(childRowIdDesc));
}
if (sort.getStats() != null) {
sortNode.setCardinality((long) sort.getStats().getRowCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public PhysicalTopN visitPhysicalTopN(PhysicalTopN<? extends Plan> mergeTopN, Ca
.map(Slot.class::cast)
.map(NamedExpression::getExprId)
.forEach(deferredMaterializedExprIds::remove);
localTopN.setMutableState(PhysicalTopN.TWO_PHASE_READ_OPT, true);
mergeTopN.setMutableState(PhysicalTopN.TWO_PHASE_READ_OPT, true);
olapScan.setMutableState(PhysicalOlapScan.DEFERRED_MATERIALIZED_SLOTS, deferredMaterializedExprIds);
localTopN.setMutableState(PhysicalOlapScan.DEFERRED_MATERIALIZED_SLOTS, deferredMaterializedExprIds);
mergeTopN.setMutableState(PhysicalOlapScan.DEFERRED_MATERIALIZED_SLOTS, deferredMaterializedExprIds);

return mergeTopN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
public class PhysicalTopN<CHILD_TYPE extends Plan> extends AbstractPhysicalSort<CHILD_TYPE> implements TopN {

public static final String TOPN_RUNTIME_FILTER = "topn_runtime_filter";
public static final String TWO_PHASE_READ_OPT = "two_phase_read_opt";

private final long limit;
private final long offset;
Expand Down
6 changes: 6 additions & 0 deletions regression-test/data/nereids_p0/sort/sort.out
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ true
2023-03-21T07:00 area1 p0 aaaaa ddddd2 100.000 100.000 100.000 100.000 2023-03-21T17:00
2023-03-21T06:00 area1 p0 aaaaa ddddd1 100.000 100.000 100.000 100.000 2023-03-21T17:00

-- !sql --
1 1024
2 1024
3 0
4 \N

18 changes: 18 additions & 0 deletions regression-test/suites/nereids_p0/sort/sort.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,22 @@ suite("sort") {
"""

qt_sql_orderby_non_overlap_desc """ select * from sort_non_overlap order by time_period desc limit 4; """

sql """ DROP TABLE if exists `sort_default_value`; """
sql """ CREATE TABLE `sort_default_value` (
`k1` int NOT NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`)
DISTRIBUTED BY HASH(`k1`) BUCKETS 1
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"disable_auto_compaction" = "true"
);
"""
sql "insert into sort_default_value values (1)"
sql "insert into sort_default_value values (2)"
sql """ alter table sort_default_value add column k4 INT default "1024" """
sql "insert into sort_default_value values (3, 0)"
sql "insert into sort_default_value values (4, null)"
qt_sql "select * from sort_default_value order by k1 limit 10"
}