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


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/ProjectWatermarkAssignerTransposeRule.java:
##########
@@ -61,97 +60,81 @@ public boolean matches(RelOptRuleCall call) {
         LogicalProject project = call.rel(0);
         LogicalWatermarkAssigner watermarkAssigner = call.rel(1);
 
-        NestedSchema usedFieldInProjectIncludingRowTimeFields =
-                getUsedFieldsInTopLevelProjectAndWatermarkAssigner(project, 
watermarkAssigner);
-
-        // check the field number of the input in watermark assigner is equal 
to the used fields in
-        // watermark assigner and project
+        NestedSchema requiredFields = extractRequiredFields(project, 
watermarkAssigner);
 
-        return usedFieldInProjectIncludingRowTimeFields.columns().size()
-                != watermarkAssigner.getRowType().getFieldCount();
+        // check whether there are unused top-level fields or nested fields 
that can be pruned
+        return requiredFields.columns().size() != 
watermarkAssigner.getRowType().getFieldCount()
+                || requiredFields.columns().values().stream().anyMatch(column 
-> !column.isLeaf());
     }
 
     @Override
     public void onMatch(RelOptRuleCall call) {
         LogicalProject project = call.rel(0);
         LogicalWatermarkAssigner watermarkAssigner = call.rel(1);
 
-        // NOTES: DON'T use the nestedSchema datatype to build the transposed 
project.
-        NestedSchema nestedSchema =
-                getUsedFieldsInTopLevelProjectAndWatermarkAssigner(project, 
watermarkAssigner);
+        NestedSchema requiredFields = extractRequiredFields(project, 
watermarkAssigner);
         FlinkRelBuilder builder =
                 (FlinkRelBuilder) 
call.builder().push(watermarkAssigner.getInput());
-        List<RexInputRef> transposedProjects = new LinkedList<>();
-        List<String> usedNames = new LinkedList<>();
-
-        // TODO: support nested projection push down in transpose
-        // add the used column RexInputRef and names into list
-        for (NestedColumn column : nestedSchema.columns().values()) {
-            // mark by hand
-            column.setIndexOfLeafInNewSchema(transposedProjects.size());
-            column.markLeaf();
-
-            usedNames.add(column.name());
-            
transposedProjects.add(builder.field(column.indexInOriginSchema()));
-        }
-
-        // get the rowtime field index in the transposed project
-        String rowTimeName =
-                watermarkAssigner
-                        .getRowType()
-                        .getFieldNames()
-                        .get(watermarkAssigner.rowtimeFieldIndex());
-        int indexOfRowTimeInTransposedProject;
-        if (nestedSchema.columns().get(rowTimeName) == null) {
-            // push the RexInputRef of the rowtime into the list
-            int rowTimeIndexInInput = watermarkAssigner.rowtimeFieldIndex();
-            indexOfRowTimeInTransposedProject = transposedProjects.size();
-            transposedProjects.add(builder.field(rowTimeIndexInInput));
-            usedNames.add(rowTimeName);
-        } else {
-            // find rowtime ref in the list and mark the location
-            indexOfRowTimeInTransposedProject =
-                    
nestedSchema.columns().get(rowTimeName).indexOfLeafInNewSchema();
+        RexBuilder rexBuilder = builder.getRexBuilder();
+        List<String> inputFieldNames = 
watermarkAssigner.getRowType().getFieldNames();
+
+        List<RexNode> transposedProjects = new ArrayList<>();
+        List<String> transposedNames = new ArrayList<>();
+        // NestedColumn#originFieldType ignores the nullability of enclosing 
rows, don't use it.
+        // convertToIndexArray() also sets indexOfLeafInNewSchema, which is 
used below.
+        for (int[] path : 
NestedProjectionUtil.convertToIndexArray(requiredFields)) {
+            RexNode expr = builder.field(path[0]);
+            StringBuilder name = new 
StringBuilder(inputFieldNames.get(path[0]));
+            for (int i = 1; i < path.length; i++) {
+                
name.append("_").append(expr.getType().getFieldNames().get(path[i]));
+                expr = rexBuilder.makeFieldAccess(expr, path[i]);

Review Comment:
   Thanks for the review!
   
   You are right, the flattened names can collide, but this doesn't affect 
correctness, since the projections are resolved by index. `RelBuilder#project` 
uniquifies the names. For `SELECT c_d.e, c.d_e FROM T`, the transposed project 
is `LogicalProject(c_d_e=[$1.e], c_d_e0=[$2.d_e], ts=[$3])`. After the 
projection is pushed into the source, `PushProjectIntoTableSourceScanRule` 
names them `project=[c_d_e, c_d_e_$0, ts]`, as it does for any nested 
projection push-down. 
   
   I added a test for this case (`transposeWithCollidingNestedFieldNames`).



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