alex-plekhanov commented on code in PR #11957:
URL: https://github.com/apache/ignite/pull/11957#discussion_r2017482987


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/JoinCommutePlannerTest.java:
##########
@@ -76,6 +86,105 @@ public class JoinCommutePlannerTest extends 
AbstractPlannerTest {
         );
     }
 
+    /**
+     * Ensures that join commute rules are disabled if joins count is too high.
+     *
+     * @see JoinCommuteRule
+     * @see JoinPushThroughJoinRule
+     */
+    @Test
+    public void testCommuteDisabledForManyJoins() throws Exception {
+        int maxJoinsToOptimize = 
Math.max(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+        int minJoins = Math.min(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+
+        // +2 tables because we use 1 more join in the query and 1 extra table 
for the correlated query.
+        int tablesCnt = maxJoinsToOptimize + 3;
+
+        try {
+            for (int i = 0; i < tablesCnt; ++i) {
+                TestTable tbl = createTable("TBL" + i, (int)Math.pow(100, (1 + 
(i % 3))),
+                    IgniteDistributions.affinity(0, "TEST_CACHE", "hash"),
+                    "ID", Integer.class);
+
+                publicSchema.addTable(tbl.name(), tbl);
+            }
+
+            // Rule names to check.
+            Collection<String> commuteJoins = 
Collections.singletonList(CoreRules.JOIN_COMMUTE.toString());
+            Collection<String> commuteSubInputs = 
Stream.of(JoinPushThroughJoinRule.LEFT.toString(),
+                
JoinPushThroughJoinRule.RIGHT.toString()).collect(Collectors.toSet());
+            Collection<String> allRules = Stream.concat(commuteJoins.stream(), 
commuteSubInputs.stream()).collect(Collectors.toSet());
+
+            // With the minimal joins number all the rules are expected to 
launch.
+            checkJoinCommutes(minJoins, false, true, allRules);
+            checkJoinCommutes(minJoins, true, true, allRules);
+
+            // Checks only JoinCommuteRule rule.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, false, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, true, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE + 1, false, 
false, commuteJoins);
+
+            // Checks only JoinPushThroughJoinRule rules.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
false, true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, true, 
true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS + 1, 
false, false, commuteSubInputs);
+
+            // From this joins count all the rules must not work.
+            checkJoinCommutes(maxJoinsToOptimize + 1, false, false, allRules);
+        }
+        finally {
+            for (int i = 0; i < tablesCnt; ++i)
+                publicSchema.removeTable("TBL" + i);
+        }
+    }
+
+    /** */
+    private void checkJoinCommutes(int jCnt, boolean addCorrelated, Boolean 
rulesExpected, Collection<String> rules) throws Exception {

Review Comment:
   Boolean -> boolean



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/JoinCommutePlannerTest.java:
##########
@@ -76,6 +86,105 @@ public class JoinCommutePlannerTest extends 
AbstractPlannerTest {
         );
     }
 
+    /**
+     * Ensures that join commute rules are disabled if joins count is too high.
+     *
+     * @see JoinCommuteRule
+     * @see JoinPushThroughJoinRule
+     */
+    @Test
+    public void testCommuteDisabledForManyJoins() throws Exception {
+        int maxJoinsToOptimize = 
Math.max(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+        int minJoins = Math.min(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+
+        // +2 tables because we use 1 more join in the query and 1 extra table 
for the correlated query.
+        int tablesCnt = maxJoinsToOptimize + 3;
+
+        try {
+            for (int i = 0; i < tablesCnt; ++i) {
+                TestTable tbl = createTable("TBL" + i, (int)Math.pow(100, (1 + 
(i % 3))),
+                    IgniteDistributions.affinity(0, "TEST_CACHE", "hash"),
+                    "ID", Integer.class);
+
+                publicSchema.addTable(tbl.name(), tbl);
+            }
+
+            // Rule names to check.
+            Collection<String> commuteJoins = 
Collections.singletonList(CoreRules.JOIN_COMMUTE.toString());
+            Collection<String> commuteSubInputs = 
Stream.of(JoinPushThroughJoinRule.LEFT.toString(),
+                
JoinPushThroughJoinRule.RIGHT.toString()).collect(Collectors.toSet());
+            Collection<String> allRules = Stream.concat(commuteJoins.stream(), 
commuteSubInputs.stream()).collect(Collectors.toSet());
+
+            // With the minimal joins number all the rules are expected to 
launch.
+            checkJoinCommutes(minJoins, false, true, allRules);
+            checkJoinCommutes(minJoins, true, true, allRules);
+
+            // Checks only JoinCommuteRule rule.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, false, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, true, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE + 1, false, 
false, commuteJoins);
+
+            // Checks only JoinPushThroughJoinRule rules.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
false, true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, true, 
true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS + 1, 
false, false, commuteSubInputs);
+
+            // From this joins count all the rules must not work.
+            checkJoinCommutes(maxJoinsToOptimize + 1, false, false, allRules);
+        }
+        finally {
+            for (int i = 0; i < tablesCnt; ++i)
+                publicSchema.removeTable("TBL" + i);
+        }
+    }
+
+    /** */
+    private void checkJoinCommutes(int jCnt, boolean addCorrelated, Boolean 
rulesExpected, Collection<String> rules) throws Exception {
+        StringBuilder select = new StringBuilder();
+        StringBuilder joins = new StringBuilder();
+
+        for (int i = 0; i < jCnt + 1; ++i) {
+            select.append("\nt").append(i).append(".id, ");
+
+            if (i == 0)
+                joins.append("TBL0 t0");
+            else {
+                joins.append(" LEFT JOIN TBL").append(i).append(" 
t").append(i).append(" ON t").append(i - 1).append(".id=t")
+                    .append(i).append(".id");
+            }
+        }
+
+        if (addCorrelated) {
+            int tblNum = jCnt + 1;
+            String alias = "t" + tblNum;
+
+            select.append("\n(SELECT MAX(").append(alias).append(".id) FROM 
TBL").append(tblNum).append(" ").append(alias)
+                .append(" WHERE 
").append(alias).append(".id>").append("t0").append(".id + 1) as corr")
+                .append(tblNum).append(", ");
+        }
+
+        String sql = "SELECT " + select.substring(0, select.length() - 2) + 
"\nFROM " + joins;
+
+        PlanningContext ctx = plannerCtx(sql, publicSchema);
+
+        Map<String, Boolean> ruleTries = new 
HashMap<>(rules.stream().collect(Collectors.toMap(r -> r, r -> false)));
+
+        RuleMatchVisualizer lsnr = new RuleMatchVisualizer() {
+            @Override public void ruleAttempted(RuleAttemptedEvent evt) {
+                if (rules.contains(evt.getRuleCall().getRule().toString()))

Review Comment:
   rules can be List<RelOptRule> (without toString())



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/JoinCommutePlannerTest.java:
##########
@@ -76,6 +86,105 @@ public class JoinCommutePlannerTest extends 
AbstractPlannerTest {
         );
     }
 
+    /**
+     * Ensures that join commute rules are disabled if joins count is too high.
+     *
+     * @see JoinCommuteRule
+     * @see JoinPushThroughJoinRule
+     */
+    @Test
+    public void testCommuteDisabledForManyJoins() throws Exception {
+        int maxJoinsToOptimize = 
Math.max(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+        int minJoins = Math.min(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
PlannerHelper.MAX_JOINS_TO_COMMUTE);
+
+        // +2 tables because we use 1 more join in the query and 1 extra table 
for the correlated query.
+        int tablesCnt = maxJoinsToOptimize + 3;
+
+        try {
+            for (int i = 0; i < tablesCnt; ++i) {
+                TestTable tbl = createTable("TBL" + i, (int)Math.pow(100, (1 + 
(i % 3))),
+                    IgniteDistributions.affinity(0, "TEST_CACHE", "hash"),
+                    "ID", Integer.class);
+
+                publicSchema.addTable(tbl.name(), tbl);
+            }
+
+            // Rule names to check.
+            Collection<String> commuteJoins = 
Collections.singletonList(CoreRules.JOIN_COMMUTE.toString());
+            Collection<String> commuteSubInputs = 
Stream.of(JoinPushThroughJoinRule.LEFT.toString(),
+                
JoinPushThroughJoinRule.RIGHT.toString()).collect(Collectors.toSet());
+            Collection<String> allRules = Stream.concat(commuteJoins.stream(), 
commuteSubInputs.stream()).collect(Collectors.toSet());
+
+            // With the minimal joins number all the rules are expected to 
launch.
+            checkJoinCommutes(minJoins, false, true, allRules);
+            checkJoinCommutes(minJoins, true, true, allRules);
+
+            // Checks only JoinCommuteRule rule.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, false, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE, true, true, 
commuteJoins);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE + 1, false, 
false, commuteJoins);
+
+            // Checks only JoinPushThroughJoinRule rules.
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, 
false, true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS, true, 
true, commuteSubInputs);
+            checkJoinCommutes(PlannerHelper.MAX_JOINS_TO_COMMUTE_INPUTS + 1, 
false, false, commuteSubInputs);
+
+            // From this joins count all the rules must not work.
+            checkJoinCommutes(maxJoinsToOptimize + 1, false, false, allRules);
+        }
+        finally {
+            for (int i = 0; i < tablesCnt; ++i)
+                publicSchema.removeTable("TBL" + i);
+        }
+    }
+
+    /** */
+    private void checkJoinCommutes(int jCnt, boolean addCorrelated, Boolean 
rulesExpected, Collection<String> rules) throws Exception {
+        StringBuilder select = new StringBuilder();
+        StringBuilder joins = new StringBuilder();
+
+        for (int i = 0; i < jCnt + 1; ++i) {
+            select.append("\nt").append(i).append(".id, ");
+
+            if (i == 0)
+                joins.append("TBL0 t0");
+            else {
+                joins.append(" LEFT JOIN TBL").append(i).append(" 
t").append(i).append(" ON t").append(i - 1).append(".id=t")
+                    .append(i).append(".id");
+            }
+        }
+
+        if (addCorrelated) {
+            int tblNum = jCnt + 1;
+            String alias = "t" + tblNum;
+
+            select.append("\n(SELECT MAX(").append(alias).append(".id) FROM 
TBL").append(tblNum).append(" ").append(alias)
+                .append(" WHERE 
").append(alias).append(".id>").append("t0").append(".id + 1) as corr")
+                .append(tblNum).append(", ");
+        }
+
+        String sql = "SELECT " + select.substring(0, select.length() - 2) + 
"\nFROM " + joins;
+
+        PlanningContext ctx = plannerCtx(sql, publicSchema);
+
+        Map<String, Boolean> ruleTries = new 
HashMap<>(rules.stream().collect(Collectors.toMap(r -> r, r -> false)));
+
+        RuleMatchVisualizer lsnr = new RuleMatchVisualizer() {
+            @Override public void ruleAttempted(RuleAttemptedEvent evt) {
+                if (rules.contains(evt.getRuleCall().getRule().toString()))
+                    ruleTries.put(evt.getRuleCall().getRule().toString(), 
true);

Review Comment:
   ```
                       assertTrue(rulesExpected);
   ```
   And remove `ruleTries`?



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to