vlsi commented on a change in pull request #1910: [CALCITE-3915] Add rule 
listener to report rule attempts and time at …
URL: https://github.com/apache/calcite/pull/1910#discussion_r409389345
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/plan/AbstractRelOptPlanner.java
 ##########
 @@ -453,4 +467,63 @@ protected MulticastRelOptListener getListener() {
         : Collections.singletonList(relType);
     return Pair.of(digest, v);
   }
+
+  /** Listener for counting the attempts of each rule. Only enabled under 
DEBUG level.*/
+  private class RuleAttemptsListener implements RelOptListener {
+    private long beforeTimestamp;
+    private Map<String, Pair<Long, Long>> ruleAttempts;
+
+    RuleAttemptsListener() {
+      ruleAttempts = new HashMap<>();
+    }
+
+    @Override public void relEquivalenceFound(RelEquivalenceEvent event) {
+    }
+
+    @Override public void ruleAttempted(RuleAttemptedEvent event) {
+      if (event.isBefore()) {
+        this.beforeTimestamp = System.nanoTime();
+      } else {
+        long elapsed = (System.nanoTime() - this.beforeTimestamp) / 1000;
+        String rule = event.getRuleCall().getRule().toString();
+        if (ruleAttempts.containsKey(rule)) {
+          Pair<Long, Long> p = ruleAttempts.get(rule);
+          ruleAttempts.put(rule, Pair.of(p.left + 1, p.right + elapsed));
+        } else {
+          ruleAttempts.put(rule, Pair.of(1L,  elapsed));
+        }
+      }
+    }
+
+    @Override public void ruleProductionSucceeded(RuleProductionEvent event) {
+    }
+
+    @Override public void relDiscarded(RelDiscardedEvent event) {
+    }
+
+    @Override public void relChosen(RelChosenEvent event) {
+    }
+
+    public String dump() {
+      // Sort rules by number of attempts
+      List<Map.Entry<String, Pair<Long, Long>>> list =
+          new ArrayList<>(this.ruleAttempts.entrySet());
+      Collections.sort(list,
+          (left, right) -> 
right.getValue().left.compareTo(left.getValue().left));
 
 Review comment:
   Can you please use Comparators.... then then then to make the output stable?
   
   Currently, you use a single field only for the sort, so the ties will appear 
as randomly sorted rows :-/

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to