xuyangzhong commented on code in PR #23620:
URL: https://github.com/apache/flink/pull/23620#discussion_r1384578763


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/hint/FlinkHints.java:
##########
@@ -234,4 +232,112 @@ private RelNode visitBiRel(BiRel biRel) {
             }
         }
     }
+
+    /** Resolve the RelNode of the sub query in the node and return a new 
node. */
+    public static RelNode resolveSubQuery(RelNode node, Function<RelNode, 
RelNode> resolver) {
+        if (node instanceof LogicalProject) {
+            LogicalProject project = (LogicalProject) node;
+            List<RexNode> newProjects = resolveSubQuery(project::getProjects, 
resolver);
+            return project.copy(
+                    project.getTraitSet(), project.getInput(), newProjects, 
project.getRowType());
+
+        } else if (node instanceof LogicalFilter) {
+            LogicalFilter filter = (LogicalFilter) node;
+            RexNode newCondition =
+                    resolveSubQuery(
+                                    () -> 
Collections.singletonList(filter.getCondition()),
+                                    resolver)
+                            .get(0);
+            return filter.copy(filter.getTraitSet(), filter.getInput(), 
newCondition);
+        } else if (node instanceof LogicalJoin) {
+            LogicalJoin join = (LogicalJoin) node;
+            RexNode newCondition =
+                    resolveSubQuery(() -> 
Collections.singletonList(join.getCondition()), resolver)
+                            .get(0);
+            return join.copy(
+                    join.getTraitSet(),
+                    newCondition,
+                    join.getLeft(),
+                    join.getRight(),
+                    join.getJoinType(),
+                    join.isSemiJoinDone());
+        } else {
+            return node;
+        }
+    }
+
+    /** Resolve the RelNode of the sub query in conditions. */
+    private static List<RexNode> resolveSubQuery(
+            Supplier<List<RexNode>> conditionSupplier, Function<RelNode, 
RelNode> resolver) {
+        return conditionSupplier.get().stream()
+                .map(
+                        rexNode ->
+                                rexNode.accept(
+                                        new RexShuttle() {
+                                            @Override
+                                            public RexNode 
visitSubQuery(RexSubQuery subQuery) {
+                                                RelNode oldRel = subQuery.rel;
+                                                RelNode newRel = 
resolver.apply(oldRel);
+                                                RexSubQuery newSubQuery = 
subQuery.clone(newRel);
+                                                return 
super.visitSubQuery(newSubQuery);
+                                            }
+                                        }))
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Clear the join hints and lookup hints on some nodes where these hints 
should not be attached.
+     */
+    public static RelNode clearJoinLookupHintsOnUnmatchedNodes(RelNode root) {
+        return root.accept(
+                new ClearJoinLookupHintsOnUnmatchedNodesShuttle(
+                        root.getCluster().getHintStrategies()));
+    }
+
+    /**
+     * Clear the invalid join hints and lookup hints in the unmatched nodes. 
For example, a join
+     * hint may be attached in the Project node at first. After accepting this 
shuttle, the join
+     * hint in the Project node will be clear.
+     *
+     * <p>See more at {@code FlinkHintStrategies}.
+     *
+     * <p>Tips, hints about view and alias will not be cleared.
+     */
+    private static class ClearJoinLookupHintsOnUnmatchedNodesShuttle extends 
RelHomogeneousShuttle {

Review Comment:
   I'll unify the name to `XXHintsShuttle`.



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