thomasrebele commented on code in PR #6889:
URL: https://github.com/apache/hive/pull/6889#discussion_r4294593151


##########
ql/src/test/org/apache/hadoop/hive/ql/optimizer/calcite/TestCBORuleFiredOnlyOnce.java:
##########
@@ -91,6 +102,65 @@ public void testRuleFiredOnlyOnce() {
     assertEquals(1, DummyRule.INSTANCE.numberOnMatch);
   }
 
+  @Test
+  public void testHivePreFilteringRuleFiredOnlyOnce() {
+    // Create a program with the rule
+    HepProgramBuilder programBuilder = new HepProgramBuilder();
+    programBuilder.addMatchOrder(HepMatchOrder.DEPTH_FIRST);
+    programBuilder.addRuleInstance(new HivePreFilteringRuleWithCountMatches());
+    HepProgram prog = programBuilder.build();
+
+    // Create a plan
+    final RelNode node = getFilterOnJoinNode();
+    RelMetadataProvider mdProvider = relOptCluster.getMetadataProvider();
+    HepPlanner planner =
+        CalcitePlanner.CalcitePlannerAction.getHepPlanner(node, prog, 
mdProvider, false, relOptCluster);
+    planner.setRoot(node);
+    RelNode relNode = planner.findBestExp();
+
+    // Check that the rule changed the original plan, because only then there 
is a risk that the rule matches again
+    String plan = RelOptUtil.toString(relNode);
+    assertTrue(
+        "HiveFilter(condition=[>($5, RAND_INTEGER(100))]) expected, but not 
found in the following plan: " + plan,
+        plan.contains("HiveFilter(condition=[>($5, RAND_INTEGER(100))])"));
+    assertTrue("HiveFilter(condition=[>($2, 1)]) expected, but not found in 
the following plan: " + plan,
+        plan.contains("HiveFilter(condition=[>($2, 1)])"));
+
+    // HivePreFilteringRule adds its result to the registry, so the rule 
should not be applied again to its own output
+    assertEquals(1, HivePreFilteringRuleWithCountMatches.countOnMatches);
+  }
+
+  private RelNode getFilterOnJoinNode() {
+    RelNode ts1 = createTS(t1NativeMock, "t1");
+    RelNode ts2 = createTS(t2NativeMock, "t2");
+    RexNode joinCondition = REX_BUILDER.makeCall(SqlStdOperatorTable.EQUALS,
+        
REX_BUILDER.makeInputRef(ts1.getRowType().getFieldList().get(0).getType(), 0),
+        
REX_BUILDER.makeInputRef(ts2.getRowType().getFieldList().get(0).getType(), 0));
+
+    REL_BUILDER.push(ts1).push(ts2).join(JoinRelType.INNER, joinCondition);
+    RexNode deterministic = REL_BUILDER.greaterThan(REL_BUILDER.field("c"), 
REL_BUILDER.literal(1));
+    RexNode randomNumber = REL_BUILDER.call(SqlStdOperatorTable.RAND_INTEGER, 
REL_BUILDER.literal(100));
+    RexNode nonDeterministic = 
REL_BUILDER.call(SqlStdOperatorTable.GREATER_THAN, REL_BUILDER.field("d"), 
randomNumber);
+    REL_BUILDER.filter(REL_BUILDER.and(deterministic, nonDeterministic));
+    return REL_BUILDER.build();
+  }
+
+  private static class HivePreFilteringRuleWithCountMatches extends 
HivePreFilteringRule {

Review Comment:
   I'll add `final` in case I need to update the PR.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to