From dfa428a809d9a98d7f93093b99af33c616e3b51c Mon Sep 17 00:00:00 2001 From: zhangstar333 <2561612514@qq.com> Date: Tue, 21 May 2024 23:10:53 +0800 Subject: [PATCH] push down count --- .../apache/doris/nereids/rules/RuleType.java | 1 + .../implementation/AggregateStrategies.java | 49 +++++++++++++++++++ .../explain/test_pushdown_explain.groovy | 8 +++ 3 files changed, 58 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java index b1101b7592b60cc..f63ae1bca6e0961 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java @@ -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), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java index e1095df7bab0b97..3b1659d4edbf7e5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java @@ -215,6 +215,16 @@ public List 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 project = ctx.root; + LogicalOlapScan olapScan = project.child(); + return pushDownCountWithoutSlotRef(project, olapScan, ctx.cascadesContext); + }) + ), RuleType.STORAGE_LAYER_AGGREGATE_WITH_PROJECT.build( logicalAggregate( logicalProject( @@ -306,6 +316,45 @@ && couldConvertToMulti(agg)) ); } + /* + * select 66 from baseall_dup; could use pushAggOp=COUNT to not scan real data. + */ + private LogicalProject pushDownCountWithoutSlotRef( + LogicalProject project, + LogicalOlapScan logicalScan, + CascadesContext cascadesContext) { + final LogicalProject canNotPush = project; + if (!enablePushDownNoGroupAgg()) { + return canNotPush; + } + if (logicalScan != null) { + KeysType keysType = logicalScan.getTable().getKeysType(); + if (keysType != KeysType.DUP_KEYS) { + return canNotPush; + } + } + List 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(); diff --git a/regression-test/suites/nereids_p0/explain/test_pushdown_explain.groovy b/regression-test/suites/nereids_p0/explain/test_pushdown_explain.groovy index f7360963e559a72..1e691f2be9ff527 100644 --- a/regression-test/suites/nereids_p0/explain/test_pushdown_explain.groovy +++ b/regression-test/suites/nereids_p0/explain/test_pushdown_explain.groovy @@ -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 """