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


##########
core/src/test/java/org/apache/calcite/test/TableFunctionTest.java:
##########
@@ -400,6 +411,139 @@ private Connection getConnectionWithMultiplyFunction() 
throws SQLException {
             "row_name=row 2; c1=103; c2=106");
   }
 
+  @Test void testTableFunctionWithScalarQueryLiteralAndColumnArguments() {

Review Comment:
   I hope that you have validated these results in some way.
   Maybe you can say how.
   Ideally you wrote a test oracle (a separate program) which verified these 
results.



##########
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:
   Isn't there another public implementation of a similar visitor you can reuse?



##########
testkit/src/main/java/org/apache/calcite/util/Smalls.java:
##########
@@ -111,6 +111,12 @@ public class Smalls {
   public static final Method MULTIPLICATION_TABLE_METHOD =
       Types.lookupMethod(Smalls.class, "multiplicationTable", int.class,
         int.class, Integer.class);
+  public static final Method SCALAR_QUERY_ARGUMENTS_TABLE_METHOD =

Review Comment:
   I wonder why this file is named Smalls.java.



##########
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:
   please add some javadoc explaining the shape of the rewriting that goes on 
here. 
   an example input and output plan would work great.



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