tkalkirill commented on code in PR #5144:
URL: https://github.com/apache/calcite/pull/5144#discussion_r3727183660


##########
core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java:
##########
@@ -1017,6 +1023,66 @@ private static void matchFilter(SubQueryRemoveRule rule,
     call.transformTo(builder.build());
   }
 
+  private static void matchTableFunctionScan(SubQueryRemoveRule rule,

Review Comment:
   I tried to fix this.



##########
core/src/main/java/org/apache/calcite/rel/rules/SubQueryRemoveRule.java:
##########
@@ -1017,6 +1023,66 @@ private static void matchFilter(SubQueryRemoveRule rule,
     call.transformTo(builder.build());
   }
 
+  private static void matchTableFunctionScan(SubQueryRemoveRule rule,
+      RelOptRuleCall call) {
+    final TableFunctionScan scan = call.rel(0);
+    final RexSubQuery e =
+        requireNonNull(findScalarQuery(scan.getCall()));
+
+    final RelBuilder builder = call.builder();
+    builder.push(e.rel);
+    builder.aggregate(builder.groupKey(),
+        builder.aggregateCall(SqlStdOperatorTable.SINGLE_VALUE,
+            builder.field(0)));
+    final RelNode scalarValue = builder.build();
+
+    final CorrelationId correlationId =
+        scan.getCluster().createCorrel();
+    final RexCorrelVariable correlationVariable =
+        (RexCorrelVariable) scan.getCluster().getRexBuilder()
+            .makeCorrel(scalarValue.getRowType(), correlationId);
+    final RexNode target =
+        scan.getCluster().getRexBuilder()
+            .makeFieldAccess(correlationVariable, 0);
+    final RexNode newCall =
+        scan.getCall().accept(new ReplaceSubQueryShuttle(e, target));
+    final TableFunctionScan newScan =
+        (TableFunctionScan) scan.copy(scan.getTraitSet(), scan.getInputs(),
+            newCall, scan.getElementType(), scan.getRowType(),
+            scan.getColumnMappings())
+            .withHints(scan.getHints());
+
+    final RelNode correlate =
+        LogicalCorrelate.create(scalarValue, newScan, ImmutableList.of(),
+            correlationId, ImmutableBitSet.of(0), JoinRelType.INNER);
+    builder.push(correlate);
+    final int scalarFieldCount =
+        scalarValue.getRowType().getFieldCount();
+    builder.project(
+        IntStream.range(0, scan.getRowType().getFieldCount())
+            .mapToObj(i -> builder.field(scalarFieldCount + i))
+            .collect(Collectors.toList()),
+        scan.getRowType().getFieldNames());
+    call.transformTo(builder.build());
+  }
+
+  private static @Nullable RexSubQuery findScalarQuery(RexNode node) {

Review Comment:
   I tried to fix this.



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