This is an automated email from the ASF dual-hosted git repository.
libenchao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 454899a451 [CALCITE-6031] Add the planner rule that pushes Filter past
Sample
454899a451 is described below
commit 454899a451d90726affdddfa75b1de24904d3072
Author: shenlang <[email protected]>
AuthorDate: Wed Oct 4 13:17:03 2023 +0800
[CALCITE-6031] Add the planner rule that pushes Filter past Sample
Close apache/calcite#3453
---
.../java/org/apache/calcite/plan/RelOptRules.java | 3 +-
.../org/apache/calcite/rel/rules/CoreRules.java | 4 ++
.../rel/rules/FilterSampleTransposeRule.java | 69 ++++++++++++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.java | 31 ++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 64 ++++++++++++++++++++
5 files changed, 170 insertions(+), 1 deletion(-)
diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptRules.java
b/core/src/main/java/org/apache/calcite/plan/RelOptRules.java
index 91b023bca7..46f39fc326 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptRules.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptRules.java
@@ -88,7 +88,8 @@ public class RelOptRules {
CoreRules.SORT_UNION_TRANSPOSE,
CoreRules.EXCHANGE_REMOVE_CONSTANT_KEYS,
CoreRules.SORT_EXCHANGE_REMOVE_CONSTANT_KEYS,
- CoreRules.SAMPLE_TO_FILTER);
+ CoreRules.SAMPLE_TO_FILTER,
+ CoreRules.FILTER_SAMPLE_TRANSPOSE);
static final List<RelOptRule> ABSTRACT_RULES =
ImmutableList.of(CoreRules.AGGREGATE_ANY_PULL_UP_CONSTANTS,
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
index c02b1ab12f..395a04c3b3 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
@@ -279,6 +279,10 @@ public class CoreRules {
public static final FilterProjectTransposeRule FILTER_PROJECT_TRANSPOSE =
FilterProjectTransposeRule.Config.DEFAULT.toRule();
+ /** Rule that pushes a {@link Filter} past a {@link Sample}. */
+ public static final FilterSampleTransposeRule FILTER_SAMPLE_TRANSPOSE =
+ FilterSampleTransposeRule.Config.DEFAULT.toRule();
+
/** Rule that pushes a {@link LogicalFilter}
* past a {@link LogicalTableFunctionScan}. */
public static final FilterTableFunctionTransposeRule
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/FilterSampleTransposeRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/FilterSampleTransposeRule.java
new file mode 100644
index 0000000000..40572100c4
--- /dev/null
+++
b/core/src/main/java/org/apache/calcite/rel/rules/FilterSampleTransposeRule.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Sample;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+/**
+ * Planner rule that pushes
+ * a {@link org.apache.calcite.rel.core.Filter}
+ * past a {@link org.apache.calcite.rel.core.Sample}.
+ *
+ * @see CoreRules#FILTER_SAMPLE_TRANSPOSE
+ */
[email protected]
+public class FilterSampleTransposeRule
+ extends RelRule<FilterSampleTransposeRule.Config>
+ implements TransformationRule {
+
+ protected FilterSampleTransposeRule(Config config) {
+ super(config);
+ }
+
+ @Override public void onMatch(final RelOptRuleCall call) {
+ final Filter filter = call.rel(0);
+ final Sample sample = call.rel(1);
+ final RelBuilder relBuilder = call.builder();
+ // Push filter condition past the Sample.
+ relBuilder.push(sample.getInput());
+ relBuilder.filter(filter.getCondition());
+ // Build the sample on top of the filter.
+ relBuilder.sample(sample.getSamplingParameters().isBernoulli(),
+ sample.getSamplingParameters().sampleRate,
+ sample.getSamplingParameters().getRepeatableSeed());
+ call.transformTo(relBuilder.build());
+ }
+
+ /** Rule configuration. */
+ @Value.Immutable
+ public interface Config extends RelRule.Config {
+ Config DEFAULT = ImmutableFilterSampleTransposeRule.Config.of()
+ .withOperandSupplier(b0 ->
+ b0.operand(Filter.class).oneInput(b1 ->
+ b1.operand(Sample.class).anyInputs()));
+
+ @Override default FilterSampleTransposeRule toRule() {
+ return new FilterSampleTransposeRule(this);
+ }
+ }
+}
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index 3df32bafa5..25ee34b4c8 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -479,6 +479,37 @@ class RelOptRulesTest extends RelOptTestBase {
}
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6031">[CALCITE-6031]
+ * Add the planner rule that pushes the Filter past a Sample</a>. */
+ @Test void testFilterSampleTransposeWithBernoulli() {
+ final String sql = "select deptno from emp tablesample bernoulli(50) where
deptno > 10";
+ sql(sql)
+ .withRule(CoreRules.FILTER_SAMPLE_TRANSPOSE)
+ .check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6031">[CALCITE-6031]
+ * Add the planner rule that pushes the Filter past a Sample</a>. */
+ @Test void testFilterSampleTransposeWithSystem() {
+ final String sql = "select deptno from emp tablesample system(50) where
deptno > 10";
+ sql(sql)
+ .withRule(CoreRules.FILTER_SAMPLE_TRANSPOSE)
+ .check();
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6031">[CALCITE-6031]
+ * Add the planner rule that pushes the Filter past a Sample</a>. */
+ @Test void testFilterSampleTransposeWithSystemAndSeed() {
+ final String sql = "select deptno from emp\n"
+ + "tablesample system(50) repeatable(10) where deptno > 10";
+ sql(sql)
+ .withRule(CoreRules.FILTER_SAMPLE_TRANSPOSE)
+ .check();
+ }
+
@Test void testReduceOrCaseWhen() {
HepProgramBuilder builder = new HepProgramBuilder();
builder.addRuleClass(ReduceExpressionsRule.class);
diff --git
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index 2e6d55427b..a3656f9374 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -4514,6 +4514,70 @@ LogicalFilter(condition=[IS NOT DISTINCT FROM($7, 20)])
<![CDATA[
LogicalFilter(condition=[=($7, 20)])
LogicalTableScan(table=[[scott, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testFilterSampleTransposeWithBernoulli">
+ <Resource name="sql">
+ <![CDATA[select deptno from emp tablesample bernoulli(50) where deptno >
10]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[>($7, 10)])
+ Sample(mode=[bernoulli], rate=[0.5], repeatableSeed=[-])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ Sample(mode=[bernoulli], rate=[0.5], repeatableSeed=[0])
+ LogicalFilter(condition=[>($7, 10)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testFilterSampleTransposeWithSystem">
+ <Resource name="sql">
+ <![CDATA[select deptno from emp tablesample system(50) where deptno >
10]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[>($7, 10)])
+ Sample(mode=[system], rate=[0.5], repeatableSeed=[-])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ Sample(mode=[system], rate=[0.5], repeatableSeed=[0])
+ LogicalFilter(condition=[>($7, 10)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase name="testFilterSampleTransposeWithSystemAndSeed">
+ <Resource name="sql">
+ <![CDATA[select deptno from emp
+tablesample system(50) repeatable(10) where deptno > 10]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ LogicalFilter(condition=[>($7, 10)])
+ Sample(mode=[system], rate=[0.5], repeatableSeed=[10])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(DEPTNO=[$7])
+ Sample(mode=[system], rate=[0.5], repeatableSeed=[10])
+ LogicalFilter(condition=[>($7, 10)])
+ LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>