beyond1920 commented on code in PR #2606:
URL: https://github.com/apache/calcite/pull/2606#discussion_r906840696


##########
core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java:
##########
@@ -1362,13 +1364,98 @@ private void substituteSubQuery(Blackboard bb, SubQuery 
subQuery) {
       //
       bb.cursors.add(converted.r);
       return;
-
+    case SET_SEMANTICS_TABLE:
+      if (!config.isExpand()) {
+        return;
+      }
+      call = (SqlBasicCall) subQuery.node;
+      query = call.operand(0);
+      final SqlValidatorScope innerTableScope =
+          (query instanceof SqlSelect)
+              ? validator().getSelectScope((SqlSelect) query)
+              : null;
+      final Blackboard setSemanticsTableBb = createBlackboard(innerTableScope, 
null, false);
+      final RelNode inputOfSetSemanticsTable = convertQueryRecursive(query, 
false, null).project();
+      requireNonNull(inputOfSetSemanticsTable, () -> "input RelNode is null 
for query " + query);
+      SqlNodeList partitionList = call.operand(1);
+      final ImmutableBitSet partitionKeys = 
buildPartitionKeys(setSemanticsTableBb, partitionList);
+      // For set semantics table, distribution is singleton if does not 
specify partition keys
+      RelDistribution distribution = partitionKeys.isEmpty()
+          ? RelDistributions.SINGLETON
+          : RelDistributions.hash(partitionKeys.asList());
+      // ORDER BY
+      final SqlNodeList orderList = call.operand(2);
+      final RelCollation orders = buildCollation(setSemanticsTableBb, 
orderList);
+      relBuilder.push(inputOfSetSemanticsTable);
+      if (orderList.isEmpty()) {
+        relBuilder.exchange(distribution);
+      } else {
+        relBuilder.sortExchange(distribution, orders);
+      }
+      RelNode tableRel = relBuilder.build();
+      subQuery.expr = bb.register(tableRel, JoinRelType.LEFT);
+      // This is used when converting window table functions:
+      //
+      // select * from table(tumble(table emps, descriptor(deptno), interval 
'3' DAY))
+      //
+      bb.cursors.add(tableRel);
+      return;
     default:
       throw new AssertionError("unexpected kind of sub-query: "
           + subQuery.node);
     }
   }
 
+  private ImmutableBitSet buildPartitionKeys(Blackboard bb, SqlNodeList 
partitionList) {
+    final ImmutableBitSet.Builder partitionKeys = ImmutableBitSet.builder();
+    for (SqlNode partition : partitionList) {
+      validator().deriveType(bb.scope(), partition);
+      RexNode e = bb.convertExpression(partition);
+      partitionKeys.set(parseFieldIdx(e));
+    }
+    return partitionKeys.build();
+  }
+
+  private RelCollation buildCollation(Blackboard bb, SqlNodeList orderList) {
+    final List<RelFieldCollation> orderKeys = new ArrayList<>();

Review Comment:
   Since it is also related to convert order by clause, I would also correct 
this point at next commit of this 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]

Reply via email to