julianhyde commented on code in PR #4492:
URL: https://github.com/apache/calcite/pull/4492#discussion_r2254976660


##########
core/src/main/java/org/apache/calcite/rel/RelCommonExpressionIdentitySuggester.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+import org.apache.calcite.plan.CommonRelExpressionRegistry;
+import org.apache.calcite.plan.Context;
+import org.apache.calcite.plan.Contexts;
+import org.apache.calcite.plan.hep.HepPlanner;
+import org.apache.calcite.plan.hep.HepProgram;
+import org.apache.calcite.plan.hep.HepProgramBuilder;
+import org.apache.calcite.rel.rules.CommonRelSubExprRegisterRule;
+
+import org.apiguardian.api.API;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+import java.util.Collection;
+import java.util.stream.Collectors;
+
+/**
+ * Suggester for common table expressions that appear as is (identical trees)
+ * more than once in the query plan.
+ */
+@API(since = "1.41.0", status = API.Status.EXPERIMENTAL)
+public class RelCommonExpressionIdentitySuggester implements 
RelCommonExpressionSuggester {

Review Comment:
   The name "Identity Suggester" implies that it is suggesting an identity. 
Perhaps "Basic Suggester"?



##########
core/src/test/java/org/apache/calcite/rel/RelCommonExpressionIdentitySuggesterTest.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.calcite.rel;
+
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.AbstractTable;
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.test.DiffRepository;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.calcite.tools.Planner;
+import org.apache.calcite.tools.RelConversionException;
+import org.apache.calcite.tools.ValidationException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+
+import static java.util.stream.Collectors.joining;
+
+/**
+ * Tests for {@link RelCommonExpressionIdentitySuggester}.
+ * The tests ensure that the suggester can correctly identify common 
expressions in the query plan
+ * that have a certain structure.
+ */
+public class RelCommonExpressionIdentitySuggesterTest {
+
+  @Test void testSingleFilterScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testTwoFilterScanSuggestions() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleProjectFilterScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleAggregateProjectScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleAggregateProjectFilterScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleProjectJoinScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleProjectFilterJoinScanSuggestion() {
+    checkSuggestions();
+  }
+
+  @Test void testSingleAggregateProjectFilterJoinScanSuggestion() {
+    checkSuggestions();
+  }
+
+  private void checkSuggestions() {
+    DiffRepository diffRepo = 
DiffRepository.lookup(RelCommonExpressionIdentitySuggesterTest.class);

Review Comment:
   DiffRepository is usually static. Multiple instances probably don't fare too 
well when tests are run on parallel threads.
   
   I think you should move `checkSuggestions` into a fixture class now. 
Otherwise it won't extend gracefully  when you want to test other things.



##########
core/src/test/java/org/apache/calcite/rel/RelCommonExpressionIdentitySuggesterTest.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.calcite.rel;
+
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.AbstractTable;
+import org.apache.calcite.sql.parser.SqlParseException;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.test.DiffRepository;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.calcite.tools.Planner;
+import org.apache.calcite.tools.RelConversionException;
+import org.apache.calcite.tools.ValidationException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+
+import static java.util.stream.Collectors.joining;
+
+/**
+ * Tests for {@link RelCommonExpressionIdentitySuggester}.
+ * The tests ensure that the suggester can correctly identify common 
expressions in the query plan
+ * that have a certain structure.
+ */
+public class RelCommonExpressionIdentitySuggesterTest {
+
+  @Test void testSingleFilterScanSuggestion() {

Review Comment:
   These tests are difficult to read because most of the information is in the 
XML file and a small amount of information is in the method name. Having read 
the tests, I don't understand any better what the component is doing. That's a 
problem.



##########
core/src/test/resources/org/apache/calcite/rel/RelCommonExpressionIdentitySuggesterTest.xml:
##########
@@ -0,0 +1,269 @@
+<?xml version="1.0" ?>
+<!--
+  ~ 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.
+  -->
+<Root>
+  <TestCase name="testSingleFilterScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[
+SELECT '2025' FROM emp WHERE empno = 1
+UNION
+SELECT '2024' FROM emp WHERE empno = 1
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalUnion(all=[false])
+  LogicalProject(EXPR$0=['2025'])
+    LogicalFilter(condition=[=($0, 1)])
+      LogicalTableScan(table=[[EMP]])
+  LogicalProject(EXPR$0=['2024'])
+    LogicalFilter(condition=[=($0, 1)])
+      LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalFilter(condition=[=($0, 1)])
+  LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testTwoFilterScanSuggestions">
+    <Resource name="sql">
+      <![CDATA[
+SELECT '2025' FROM emp WHERE empno = 1
+UNION
+SELECT '2024' FROM emp WHERE empno = 1
+UNION
+SELECT '2023' FROM emp WHERE empno = 2
+UNION
+SELECT '2022' FROM emp WHERE empno = 2
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalUnion(all=[false])
+  LogicalUnion(all=[false])
+    LogicalUnion(all=[false])
+      LogicalProject(EXPR$0=['2025'])
+        LogicalFilter(condition=[=($0, 1)])
+          LogicalTableScan(table=[[EMP]])
+      LogicalProject(EXPR$0=['2024'])
+        LogicalFilter(condition=[=($0, 1)])
+          LogicalTableScan(table=[[EMP]])
+    LogicalProject(EXPR$0=['2023'])
+      LogicalFilter(condition=[=($0, 2)])
+        LogicalTableScan(table=[[EMP]])
+  LogicalProject(EXPR$0=['2022'])
+    LogicalFilter(condition=[=($0, 2)])
+      LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalFilter(condition=[=($0, 1)])
+  LogicalTableScan(table=[[EMP]])
+
+LogicalFilter(condition=[=($0, 2)])
+  LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleProjectFilterScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[
+SELECT ename FROM emp WHERE empno = 1
+UNION
+SELECT ename FROM emp WHERE empno = 1
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalUnion(all=[false])
+  LogicalProject(ENAME=[$1])
+    LogicalFilter(condition=[=($0, 1)])
+      LogicalTableScan(table=[[EMP]])
+  LogicalProject(ENAME=[$1])
+    LogicalFilter(condition=[=($0, 1)])
+      LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalProject(ENAME=[$1])
+  LogicalFilter(condition=[=($0, 1)])
+    LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleAggregateProjectScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[
+SELECT COUNT(*), 'A' FROM emp GROUP BY ename
+UNION
+SELECT COUNT(*), 'B' FROM emp GROUP BY ename
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalUnion(all=[false])
+  LogicalProject(EXPR$0=[$1], EXPR$1=['A'])
+    LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+      LogicalProject(ENAME=[$1])
+        LogicalTableScan(table=[[EMP]])
+  LogicalProject(EXPR$0=[$1], EXPR$1=['B'])
+    LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+      LogicalProject(ENAME=[$1])
+        LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+  LogicalProject(ENAME=[$1])
+    LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleAggregateProjectFilterScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[
+SELECT COUNT(*), 'A' FROM emp WHERE empno > 50 GROUP BY ename
+UNION
+SELECT COUNT(*), 'B' FROM emp WHERE empno > 50 GROUP BY ename
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalUnion(all=[false])
+  LogicalProject(EXPR$0=[$1], EXPR$1=['A'])
+    LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+      LogicalProject(ENAME=[$1])
+        LogicalFilter(condition=[>($0, 50)])
+          LogicalTableScan(table=[[EMP]])
+  LogicalProject(EXPR$0=[$1], EXPR$1=['B'])
+    LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+      LogicalProject(ENAME=[$1])
+        LogicalFilter(condition=[>($0, 50)])
+          LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
+  LogicalProject(ENAME=[$1])
+    LogicalFilter(condition=[>($0, 50)])
+      LogicalTableScan(table=[[EMP]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleProjectJoinScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[WITH cx AS (SELECT e.empno, d.dname FROM emp e INNER JOIN dept 
d ON e.deptno = d.deptno)
+SELECT * FROM cx WHERE cx.empno > 50
+INTERSECT
+SELECT * FROM cx WHERE cx.empno < 100
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalIntersect(all=[false])
+  LogicalProject(EMPNO=[$0], DNAME=[$1])
+    LogicalFilter(condition=[>($0, 50)])
+      LogicalProject(EMPNO=[$0], DNAME=[$4])
+        LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+          LogicalTableScan(table=[[EMP]])
+          LogicalTableScan(table=[[DEPT]])
+  LogicalProject(EMPNO=[$0], DNAME=[$1])
+    LogicalFilter(condition=[<($0, 100)])
+      LogicalProject(EMPNO=[$0], DNAME=[$4])
+        LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+          LogicalTableScan(table=[[EMP]])
+          LogicalTableScan(table=[[DEPT]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalProject(EMPNO=[$0], DNAME=[$4])
+  LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+    LogicalTableScan(table=[[EMP]])
+    LogicalTableScan(table=[[DEPT]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleProjectFilterJoinScanSuggestion">
+    <Resource name="sql">
+      <![CDATA[WITH cx AS (SELECT e.empno, d.dname FROM emp e INNER JOIN dept 
d ON e.deptno = d.deptno WHERE d.dname = 'Sales')
+SELECT * FROM cx WHERE cx.empno > 50
+INTERSECT
+SELECT * FROM cx WHERE cx.empno < 100
+]]>
+    </Resource>
+    <Resource name="plan">
+      <![CDATA[LogicalIntersect(all=[false])
+  LogicalProject(EMPNO=[$0], DNAME=[$1])
+    LogicalFilter(condition=[>($0, 50)])
+      LogicalProject(EMPNO=[$0], DNAME=[$4])
+        LogicalFilter(condition=[=($4, 'Sales')])
+          LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+            LogicalTableScan(table=[[EMP]])
+            LogicalTableScan(table=[[DEPT]])
+  LogicalProject(EMPNO=[$0], DNAME=[$1])
+    LogicalFilter(condition=[<($0, 100)])
+      LogicalProject(EMPNO=[$0], DNAME=[$4])
+        LogicalFilter(condition=[=($4, 'Sales')])
+          LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+            LogicalTableScan(table=[[EMP]])
+            LogicalTableScan(table=[[DEPT]])
+]]>
+    </Resource>
+    <Resource name="suggestions">
+      <![CDATA[LogicalProject(EMPNO=[$0], DNAME=[$4])
+  LogicalFilter(condition=[=($4, 'Sales')])
+    LogicalJoin(condition=[=($2, $3)], joinType=[inner])
+      LogicalTableScan(table=[[EMP]])
+      LogicalTableScan(table=[[DEPT]])
+]]>
+    </Resource>
+  </TestCase>
+  <TestCase name="testSingleAggregateProjectFilterJoinScanSuggestion">

Review Comment:
   You should enforce that this file is sorted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to