LakshSingla commented on a change in pull request #11908:
URL: https://github.com/apache/druid/pull/11908#discussion_r747798694



##########
File path: 
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidPlanner.java
##########
@@ -384,6 +399,72 @@ private PlannerResult planExplanation(
     return new PlannerResult(resultsSupplier, 
getExplainStructType(rel.getCluster().getTypeFactory()));
   }
 
+  /**
+   * Construct a {@link PlannerResult} for an 'explain' query from a {@link 
RelNode}
+   */
+  private PlannerResult planDruidExplanation(
+      final DruidRel<?> rel
+  )
+  {
+    String resources = null;
+    String explanation = null;
+    try {
+      explanation = dumpDruidPlan(rel);
+      resources = jsonMapper.writeValueAsString(plannerContext.getResources());
+    }
+    catch (JsonProcessingException jpe) {
+      // this should never happen, we create the Resources here, not a user
+      log.error(jpe, "Encountered exception while serializing Resources for 
explain output");
+    }
+    final Supplier<Sequence<Object[]>> resultsSupplier = Suppliers.ofInstance(
+        Sequences.simple(ImmutableList.of(new Object[]{explanation, 
resources})));
+    return new PlannerResult(resultsSupplier, 
getExplainStructType(rel.getCluster().getTypeFactory()));
+  }
+
+  /**
+   * This method doesn't utilize the Calcite's internal {@link 
RelOptUtil#dumpPlan} since that tends to be verbose
+   * and not indicative of the native Druid Queries which will get executed
+   * This method assumes that the Planner has converted the RelNodes to 
DruidRels, and thereby we can implictly cast it
+   *
+   * @param rel Instance of the root {@link DruidRel} which is formed by 
running the planner transformations on it
+   * @return A string representing an array of native queries that correspond 
to the given SQL query, in JSON format
+   * @throws JsonProcessingException
+   */
+  public static String dumpDruidPlan(DruidRel<?> rel) throws 
JsonProcessingException

Review comment:
       Refactored the code, to reuse the original methods wherever possible. 
   As per the JSON Mapper used, I was trying to keep the code as similar to 
what was used in `explainTerms`. Makes sense to use the JSON Mapper given to 
the DruidPlanner.




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