Skip to content

Commit

Permalink
[improve](nereids) support pushdown count when scan project without a…
Browse files Browse the repository at this point in the history
…ny slot ref (apache#35162)

select 66 from baseall_dup;
could use pushAggOp=COUNT, so no need to scan real data
  • Loading branch information
zhangstar333 authored May 27, 2024
1 parent 717722e commit b66f9ff
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ public enum RuleType {
STORAGE_LAYER_AGGREGATE_WITH_PROJECT(RuleTypeClass.IMPLEMENTATION),
STORAGE_LAYER_AGGREGATE_WITHOUT_PROJECT_FOR_FILE_SCAN(RuleTypeClass.IMPLEMENTATION),
STORAGE_LAYER_AGGREGATE_WITH_PROJECT_FOR_FILE_SCAN(RuleTypeClass.IMPLEMENTATION),
STORAGE_LAYER_WITH_PROJECT_NO_SLOT_REF(RuleTypeClass.IMPLEMENTATION),
STORAGE_LAYER_AGGREGATE_MINMAX_ON_UNIQUE(RuleTypeClass.IMPLEMENTATION),
STORAGE_LAYER_AGGREGATE_MINMAX_ON_UNIQUE_WITHOUT_PROJECT(RuleTypeClass.IMPLEMENTATION),
COUNT_ON_INDEX(RuleTypeClass.IMPLEMENTATION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ public List<Rule> buildRules() {
.when(agg -> agg.isNormalized() && enablePushDownNoGroupAgg())
.thenApply(ctx -> storageLayerAggregate(ctx.root, null, ctx.root.child(), ctx.cascadesContext))
),
RuleType.STORAGE_LAYER_WITH_PROJECT_NO_SLOT_REF.build(
logicalProject(
logicalOlapScan()
)
.thenApply(ctx -> {
LogicalProject<LogicalOlapScan> project = ctx.root;
LogicalOlapScan olapScan = project.child();
return pushDownCountWithoutSlotRef(project, olapScan, ctx.cascadesContext);
})
),
RuleType.STORAGE_LAYER_AGGREGATE_WITH_PROJECT.build(
logicalAggregate(
logicalProject(
Expand Down Expand Up @@ -306,6 +316,37 @@ && couldConvertToMulti(agg))
);
}

/*
* select 66 from baseall_dup; could use pushAggOp=COUNT to not scan real data.
*/
private LogicalProject<? extends Plan> pushDownCountWithoutSlotRef(
LogicalProject<? extends Plan> project,
LogicalOlapScan logicalScan,
CascadesContext cascadesContext) {
final LogicalProject<? extends Plan> canNotPush = project;
if (!enablePushDownNoGroupAgg()) {
return canNotPush;
}
if (logicalScan != null) {
KeysType keysType = logicalScan.getTable().getKeysType();
if (keysType != KeysType.DUP_KEYS) {
return canNotPush;
}
}
for (Expression e : project.getProjects()) {
if (e.anyMatch(SlotReference.class::isInstance)) {
return canNotPush;
}
}
PhysicalOlapScan physicalOlapScan
= (PhysicalOlapScan) new LogicalOlapScanToPhysicalOlapScan()
.build()
.transform(logicalScan, cascadesContext)
.get(0);
return project.withChildren(ImmutableList.of(new PhysicalStorageLayerAggregate(
physicalOlapScan, PushDownAggOp.COUNT)));
}

private boolean enablePushDownMinMaxOnUnique() {
ConnectContext connectContext = ConnectContext.get();
return connectContext != null && connectContext.getSessionVariable().isEnablePushDownMinMaxOnUnique();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ PhysicalResultSink
--PhysicalLimit[GLOBAL]
----PhysicalLimit[LOCAL]
------PhysicalProject
--------PhysicalOlapScan[t1]
--------PhysicalStorageLayerAggregate[t1]

-- !basic_2 --
PhysicalResultSink
--PhysicalLimit[GLOBAL]
----PhysicalLimit[LOCAL]
------PhysicalProject
--------PhysicalOlapScan[t2]
--------PhysicalStorageLayerAggregate[t2]

-- !basic_3 --
PhysicalResultSink
--PhysicalLimit[GLOBAL]
----PhysicalLimit[LOCAL]
------PhysicalProject
--------PhysicalOlapScan[t3]
--------PhysicalStorageLayerAggregate[t3]

-- !basic_4 --
PhysicalResultSink
--PhysicalLimit[GLOBAL]
----PhysicalLimit[LOCAL]
------PhysicalProject
--------PhysicalOlapScan[t4]
--------PhysicalStorageLayerAggregate[t4]

-- !basic_1 --
1 2 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,5 @@ PhysicalResultSink
------PhysicalOlapScan[t1]
----PhysicalLimit[GLOBAL]
------PhysicalLimit[LOCAL]
--------PhysicalOlapScan[t2]
--------PhysicalStorageLayerAggregate[t2]

Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ suite("test_pushdown_explain") {
sql("select count(cast(lo_orderkey as bigint)) from test_lineorder;")
contains "pushAggOp=COUNT"
}
explain {
sql("select 66 from test_lineorder;")
contains "pushAggOp=COUNT"
}
explain {
sql("select lo_orderkey from test_lineorder;")
contains "pushAggOp=NONE"
}

sql "DROP TABLE IF EXISTS table_unique0"
sql """
Expand Down

0 comments on commit b66f9ff

Please sign in to comment.