Skip to content

Commit

Permalink
push down count
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangstar333 committed May 21, 2024
1 parent 1120f96 commit dfa428a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,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,45 @@ && 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;
}
}
List<Expression> projectExpr = project.getProjects()
.stream()
.map(p -> p instanceof Alias ? p.child(0) : p)
.collect(ImmutableList.toImmutableList());
boolean noSlotRef = projectExpr.stream().allMatch(expr -> {
if (expr instanceof SlotReference) {
return false;
}
return true;
});
if (!noSlotRef) {
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 @@ -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 dfa428a

Please sign in to comment.