mihaibudiu commented on code in PR #4301:
URL: https://github.com/apache/calcite/pull/4301#discussion_r2043176782


##########
core/src/test/resources/sql/planner.iq:
##########
@@ -0,0 +1,84 @@
+# planner.iq - planner tests can customizable optimization rules
+#
+# 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.
+#
+!use post
+!set outputformat mysql
+!set update-rules 
"-INTERSECT_TO_DISTINCT,-EnumerableRules.ENUMERABLE_INTERSECT_RULE,+CoreRules.INTERSECT_TO_SEMI_JOIN"
+
+# Test INTERSECT_TO_SEMI_JOIN
+with t (i) as (values (0), (1))
+select * from t as t1
+intersect
+select * from t as t2 where t2.i > 0;
++---+
+| I |
++---+
+| 1 |
++---+
+(1 row)
+
+!ok
+
+EnumerableNestedLoopJoin(condition=[IS NOT DISTINCT FROM($0, $1)], 
joinType=[semi])
+  EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+  EnumerableCalc(expr#0=[{inputs}], expr#1=[0], expr#2=[>($t0, $t1)], 
EXPR$0=[$t0], $condition=[$t2])
+    EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+!plan
+
+# Test no reset-rules can keep rules
+with t (i) as (values (0), (1))
+select * from t as t1
+intersect
+select * from t as t2 where t2.i > 0;
++---+
+| I |
++---+
+| 1 |
++---+
+(1 row)
+
+!ok
+
+EnumerableNestedLoopJoin(condition=[IS NOT DISTINCT FROM($0, $1)], 
joinType=[semi])
+  EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+  EnumerableCalc(expr#0=[{inputs}], expr#1=[0], expr#2=[>($t0, $t1)], 
EXPR$0=[$t0], $condition=[$t2])
+    EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+!plan
+
+!set reset-rules ""

Review Comment:
   this is a bit ugly
   if you want to reuse "!set", maybe you can say "!set optimization-rules 
original". 
   Here "original" is not the name of any rule



##########
testkit/src/main/java/org/apache/calcite/test/QuidemTest.java:
##########
@@ -71,6 +77,9 @@ public abstract class QuidemTest {
 
   private static final Pattern PATTERN = Pattern.compile("\\.iq$");
 
+  // cache original rules

Review Comment:
   "Saved original planner rules". 
   



##########
testkit/src/main/java/org/apache/calcite/test/QuidemTest.java:
##########
@@ -171,6 +182,33 @@ protected void checkRun(String path) throws Exception {
               int thresholdValue = ((BigDecimal) value).intValue();
               
closer.add(Prepare.THREAD_INSUBQUERY_THRESHOLD.push(thresholdValue));
             }
+            if (propertyName.equals("rules")) {
+              parseRules((String) value);
+              closer.add(
+                  Hook.PLANNER.addThread((Consumer<RelOptPlanner>)
+                      planner -> {
+                          for (RelOptRule rule : addRulesCache) {
+                            planner.addRule(rule);
+                          }
+                          for (RelOptRule rule : removeRulesCache) {
+                            planner.removeRule(rule);
+                          }
+                      }));
+            }
+            if (propertyName.equals("reset-rules")) {

Review Comment:
   can you write a test to verify that the planner rules are the same after a 
reset?
   you don't have to do it from the quidem tests, it can be a separate test 
that just uses the hook



##########
core/src/test/resources/sql/planner.iq:
##########
@@ -0,0 +1,84 @@
+# planner.iq - planner tests can customizable optimization rules
+#
+# 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.
+#
+!use post
+!set outputformat mysql
+!set update-rules 
"-INTERSECT_TO_DISTINCT,-EnumerableRules.ENUMERABLE_INTERSECT_RULE,+CoreRules.INTERSECT_TO_SEMI_JOIN"

Review Comment:
   let's call this "optimization-rules" instead of "update-rules"



##########
core/src/test/resources/sql/planner.iq:
##########
@@ -0,0 +1,84 @@
+# planner.iq - planner tests can customizable optimization rules
+#
+# 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.
+#
+!use post
+!set outputformat mysql
+!set update-rules 
"-INTERSECT_TO_DISTINCT,-EnumerableRules.ENUMERABLE_INTERSECT_RULE,+CoreRules.INTERSECT_TO_SEMI_JOIN"
+
+# Test INTERSECT_TO_SEMI_JOIN
+with t (i) as (values (0), (1))
+select * from t as t1
+intersect
+select * from t as t2 where t2.i > 0;
++---+
+| I |
++---+
+| 1 |
++---+
+(1 row)
+
+!ok
+
+EnumerableNestedLoopJoin(condition=[IS NOT DISTINCT FROM($0, $1)], 
joinType=[semi])
+  EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+  EnumerableCalc(expr#0=[{inputs}], expr#1=[0], expr#2=[>($t0, $t1)], 
EXPR$0=[$t0], $condition=[$t2])
+    EnumerableValues(tuples=[[{ 0 }, { 1 }]])
+!plan
+
+# Test no reset-rules can keep rules

Review Comment:
   test that rules set by "!set update-rules" are sticky



##########
testkit/src/main/java/org/apache/calcite/test/QuidemTest.java:
##########
@@ -187,6 +209,57 @@ protected void checkRun(String path) throws Exception {
     }
   }
 
+  private void updatePlanner(RelOptPlanner planner, String value) {
+    List<RelOptRule> rulesAdd = new ArrayList<>();
+    List<RelOptRule> rulesRemove = new ArrayList<>();
+    parseRules(value, rulesAdd, rulesRemove);
+    rulesAdd.forEach(planner::addRule);
+    rulesRemove.forEach(planner::removeRule);

Review Comment:
   I don't know if the order of rules in the planner matters.
   but just in case, perhaps it's safer to first remove, then add



-- 
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