This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new b9f476256db Add PushFilterIntoScanRuleTest and 
PushProjectIntoScanRuleTest (#37331)
b9f476256db is described below

commit b9f476256dbaf462732a90c17ddddfaba7947a3b
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 10 18:23:39 2025 +0800

    Add PushFilterIntoScanRuleTest and PushProjectIntoScanRuleTest (#37331)
---
 .../converter/EnumerableModifyConverterRule.java   |   7 +-
 .../transformation/PushFilterIntoScanRuleTest.java | 120 +++++++++++++++++++++
 .../PushProjectIntoScanRuleTest.java               | 118 ++++++++++++++++++++
 3 files changed, 241 insertions(+), 4 deletions(-)

diff --git 
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/converter/EnumerableModifyConverterRule.java
 
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/converter/EnumerableModifyConverterRule.java
index 32df6b8ce6a..d47fe5bc9f6 100644
--- 
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/converter/EnumerableModifyConverterRule.java
+++ 
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/converter/EnumerableModifyConverterRule.java
@@ -45,10 +45,9 @@ public final class EnumerableModifyConverterRule extends 
ConverterRule {
         ModifiableTable modifiableTable = 
modify.getTable().unwrap(ModifiableTable.class);
         if (null == modifiableTable) {
             return null;
-        } else {
-            RelTraitSet traitSet = 
modify.getTraitSet().replace(EnumerableConvention.INSTANCE);
-            return new EnumerableModify(modify.getCluster(), traitSet, 
modify.getTable(), modify.getCatalogReader(), convert(modify.getInput(), 
traitSet),
-                    modify.getOperation(), modify.getUpdateColumnList(), 
modify.getSourceExpressionList(), modify.isFlattened());
         }
+        RelTraitSet traitSet = 
modify.getTraitSet().replace(EnumerableConvention.INSTANCE);
+        return new EnumerableModify(modify.getCluster(), traitSet, 
modify.getTable(), modify.getCatalogReader(), convert(modify.getInput(), 
traitSet),
+                modify.getOperation(), modify.getUpdateColumnList(), 
modify.getSourceExpressionList(), modify.isFlattened());
     }
 }
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushFilterIntoScanRuleTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushFilterIntoScanRuleTest.java
new file mode 100644
index 00000000000..4d90b27704a
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushFilterIntoScanRuleTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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.shardingsphere.sqlfederation.compiler.planner.rule.transformation;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.logical.LogicalFilter;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexSubQuery;
+import 
org.apache.shardingsphere.sqlfederation.compiler.rel.operator.logical.LogicalScan;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class PushFilterIntoScanRuleTest {
+    
+    private final PushFilterIntoScanRule rule = 
PushFilterIntoScanRule.Config.DEFAULT.toRule();
+    
+    @Mock
+    private RelOptRuleCall call;
+    
+    @Mock
+    private LogicalScan logicalScan;
+    
+    @Mock
+    private LogicalFilter logicalFilter;
+    
+    @Test
+    void assertNotMatchWhenSystemSchema() {
+        mockQualifiedName("shardingsphere", "t_order");
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertNotMatchWhenConditionIsRexSubQuery() {
+        mockQualifiedName("public", "t_order");
+        when(call.rel(0)).thenReturn(logicalFilter);
+        when(logicalFilter.getCondition()).thenReturn(mock(RexSubQuery.class));
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertNotMatchWhenConditionContainsRexSubQueryOperand() {
+        mockQualifiedName("public", "t_order");
+        when(call.rel(0)).thenReturn(logicalFilter);
+        RexCall rexCall = mock(RexCall.class);
+        
when(rexCall.getOperands()).thenReturn(Collections.singletonList(mock(RexSubQuery.class)));
+        when(logicalFilter.getCondition()).thenReturn(rexCall);
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertNotMatchWhenConditionContainsCorrelate() {
+        mockQualifiedName("public", "t_order");
+        when(call.rel(0)).thenReturn(logicalFilter);
+        RexNode operand = mock(RexNode.class);
+        when(operand.toString()).thenReturn("$cor0");
+        RexCall rexCall = mock(RexCall.class);
+        
when(rexCall.getOperands()).thenReturn(Collections.singletonList(operand));
+        when(logicalFilter.getCondition()).thenReturn(rexCall);
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertMatchAndOnMatchWhenConditionIsRexCallWithoutCorrelate() {
+        mockQualifiedName("public", "t_order");
+        when(call.rel(0)).thenReturn(logicalFilter);
+        RexNode operand = mock(RexNode.class);
+        when(operand.toString()).thenReturn("column");
+        RexCall rexCall = mock(RexCall.class);
+        
when(rexCall.getOperands()).thenReturn(Collections.singletonList(operand));
+        when(logicalFilter.getCondition()).thenReturn(rexCall);
+        assertTrue(rule.matches(call));
+        rule.onMatch(call);
+        verify(logicalScan).pushDown(logicalFilter);
+        verify(call).transformTo(logicalScan);
+    }
+    
+    @Test
+    void assertMatchWhenConditionIsNotRexCall() {
+        mockQualifiedName("public", "t_order");
+        when(call.rel(0)).thenReturn(logicalFilter);
+        when(logicalFilter.getCondition()).thenReturn(mock(RexNode.class));
+        assertTrue(rule.matches(call));
+    }
+    
+    private void mockQualifiedName(final String... names) {
+        RelOptTable relOptTable = mock(RelOptTable.class);
+        when(relOptTable.getQualifiedName()).thenReturn(Arrays.asList(names));
+        when(logicalScan.getTable()).thenReturn(relOptTable);
+        when(call.rel(1)).thenReturn(logicalScan);
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushProjectIntoScanRuleTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushProjectIntoScanRuleTest.java
new file mode 100644
index 00000000000..f6b88fa152f
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/planner/rule/transformation/PushProjectIntoScanRuleTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.shardingsphere.sqlfederation.compiler.planner.rule.transformation;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexSubQuery;
+import org.apache.calcite.sql.SqlOperator;
+import 
org.apache.shardingsphere.sqlfederation.compiler.rel.operator.logical.LogicalScan;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+class PushProjectIntoScanRuleTest {
+    
+    private PushProjectIntoScanRule rule;
+    
+    @Mock
+    private RelOptRuleCall call;
+    
+    @Mock
+    private LogicalScan logicalScan;
+    
+    @Mock
+    private LogicalProject logicalProject;
+    
+    @BeforeEach
+    void setUp() {
+        rule = PushProjectIntoScanRule.Config.DEFAULT.toRule();
+    }
+    
+    @Test
+    void assertNotMatchWhenSystemSchema() {
+        mockQualifiedName("mysql", "t_order");
+        
when(logicalProject.getProjects()).thenReturn(Collections.singletonList(mock(RexNode.class)));
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertNotMatchWhenProjectContainsSubQuery() {
+        mockQualifiedName("public", "t_order");
+        
when(logicalProject.getProjects()).thenReturn(Collections.singletonList(mock(RexSubQuery.class)));
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertNotMatchWhenProjectContainsCastFunction() {
+        mockQualifiedName("public", "t_order");
+        RexCall rexCall = mock(RexCall.class);
+        SqlOperator operator = mock(SqlOperator.class);
+        when(operator.getName()).thenReturn("CAST");
+        when(rexCall.getOperator()).thenReturn(operator);
+        
when(logicalProject.getProjects()).thenReturn(Collections.singletonList(rexCall));
+        assertFalse(rule.matches(call));
+    }
+    
+    @Test
+    void assertMatchWhenProjectContainsNonCallExpression() {
+        mockQualifiedName("public", "t_order");
+        
when(logicalProject.getProjects()).thenReturn(Collections.singletonList(mock(RexNode.class)));
+        assertTrue(rule.matches(call));
+    }
+    
+    @Test
+    void assertMatchAndOnMatchWhenProjectWithoutSubQueryOrCast() {
+        mockQualifiedName("public", "t_order");
+        RexCall rexCall = mock(RexCall.class);
+        SqlOperator operator = mock(SqlOperator.class);
+        when(operator.getName()).thenReturn("TRIM");
+        when(rexCall.getOperator()).thenReturn(operator);
+        
when(logicalProject.getProjects()).thenReturn(Collections.singletonList(rexCall));
+        assertTrue(rule.matches(call));
+        rule.onMatch(call);
+        verify(logicalScan).pushDown(logicalProject);
+        verify(call).transformTo(logicalScan);
+    }
+    
+    private void mockQualifiedName(final String... names) {
+        RelOptTable relOptTable = mock(RelOptTable.class);
+        when(relOptTable.getQualifiedName()).thenReturn(Arrays.asList(names));
+        when(logicalScan.getTable()).thenReturn(relOptTable);
+        when(call.rel(1)).thenReturn(logicalScan);
+        when(call.rel(0)).thenReturn(logicalProject);
+    }
+}

Reply via email to