soumyakanti3578 commented on code in PR #6067:
URL: https://github.com/apache/hive/pull/6067#discussion_r2396396987


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java:
##########
@@ -1079,15 +1106,110 @@ public static Pair<RelOptTable, List<Integer>> 
getColumnOriginSet(RelNode rel, I
    * to parse the string back.
    */
   public static String toJsonString(final RelNode rel) {
+    return serializeWithPlanWriter(rel, new HiveRelJsonImplWithStats());
+  }
+
+  private static String serializeWithPlanWriter(RelNode rel, HiveRelJsonImpl 
planWriter) {
     if (rel == null) {
       return null;
     }
-
-    final HiveRelJsonImpl planWriter = new HiveRelJsonImpl();
     rel.explain(planWriter);
     return planWriter.asString();
   }
 
+  public static Optional<String> serializeToJSON(RelNode plan) {
+    if (!isSerializable(plan)) {
+      return Optional.empty();
+    }
+
+    JSONObject outJSONObject = new JSONObject(new LinkedHashMap<>());
+    outJSONObject.put("CBOPlan", serializeWithPlanWriter(plan, new 
HiveRelJsonImpl()));
+    String jsonPlan = outJSONObject.toString();
+
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Plan to serialize: \n{}", RelOptUtil.toString(plan));
+      LOG.debug("JSON plan: \n{}", jsonPlan);
+    }
+
+    return Optional.of(jsonPlan);
+  }
+
+  private static boolean isSerializable(RelNode plan) {
+    return HiveRelNode.stream(plan)
+        .noneMatch(node ->
+            
Objects.requireNonNull(node.getConvention()).getName().toLowerCase().contains("jdbc")
+        );
+  }
+
+  public static RelNode deserializePlan(HiveConf conf, String jsonPlan) throws 
IOException {
+    RelOptPlanner planner = HiveRelOptUtil.createPlanner(conf, 
StatsSources.getStatsSource(conf), false);
+    RexBuilder rexBuilder = new RexBuilder(new HiveTypeFactory());
+    RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
+
+    RelPlanParser parser = new RelPlanParser(cluster, conf);
+    RelNode deserializedPlan = parser.parse(jsonPlan);
+    // Apply partition pruning to compute partition list in HiveTableScan
+    deserializedPlan = applyPartitionPruning(conf, deserializedPlan, cluster, 
planner);

Review Comment:
   We can deserialize the plan but `partitionList` of `RelOptHiveTable` will be 
empty. Also, the plans won't match up as we do print `plKey` in CBO plans for 
`HiveTableScan`.



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