fhueske commented on code in PR #30097:
URL: https://github.com/apache/flink/pull/30097#discussion_r4720980234


##########
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/NestedProjectionUtil.scala:
##########
@@ -149,6 +182,253 @@ object NestedProjectionUtil {
     new NestedColumn(name, indexInOriginSchema, originFieldType, isLeaf = true)
   }
 
+  /**
+   * Prepares pushing the nested field accesses of ROW columns to projects 
below a join.
+   *
+   * `projects` are the expressions of the project above the join and 
`condition` is the join
+   * condition. The method finds the nested fields they access and returns one 
projection per join
+   * input that outputs these nested fields as flat columns. It also rewrites 
`projects` and
+   * `condition` to read the flat columns instead of the ROW fields. The flat 
columns are numbered
+   * left input first, then right input.
+   *
+   * ROW columns from which nested fields are accessed are replaced by the 
extracted fields. All
+   * other top-level columns pass through as whole columns.
+   *
+   * The rewritten condition is typed against the join inputs. The rewritten 
projects are typed
+   * against the join output, so the columns of a side that generates nulls 
are nullable.
+   *
+   * @param generatesNullsOnLeft
+   *   true if the join type is an outer join that pads the left side with 
nulls (RIGHT or FULL)
+   * @param generatesNullsOnRight
+   *   true if the join type is an outer join that pads the right side with 
nulls (LEFT or FULL)
+   */
+  def rewriteForJoin(
+      projects: JList[RexNode],
+      condition: RexNode,
+      joinRowType: RelDataType,
+      leftRowType: RelDataType,
+      rightRowType: RelDataType,
+      generatesNullsOnLeft: Boolean,
+      generatesNullsOnRight: Boolean,
+      rexBuilder: RexBuilder): JoinNestedProjectionResult = {
+    val exprs = new JArrayList[RexNode](projects)
+    exprs.add(condition)
+    val leftFieldCount = leftRowType.getFieldCount
+    // 1. Build the used-field tree over the whole join output. Top-level 
columns that no
+    //    expression uses are added as whole columns, so that they pass 
through unchanged.
+    val usedIndices = build(exprs, 
joinRowType).columns.values().map(_.indexInOriginSchema).toSet
+    val allColumns = new JArrayList[RexNode](exprs)
+    joinRowType.getFieldList.foreach {
+      field =>
+        if (!usedIndices.contains(field.getIndex)) {
+          allColumns.add(rexBuilder.makeInputRef(field.getType, 
field.getIndex))
+        }
+    }
+    val schema = build(allColumns, joinRowType)
+

Review Comment:
   Good idea, thanks!



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