snuyanzin commented on code in PR #27250:
URL: https://github.com/apache/flink/pull/27250#discussion_r2539930231


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/physical/stream/FlinkMarkChangelogNormalizeProgram.java:
##########
@@ -149,27 +154,47 @@ public RelNode optimize(RelNode root, 
StreamOptimizeContext context) {
                 continue;
             }
 
-            final Set<RexNode> common = 
calculateCommonCondition(changelogNormalizeContexts);
+            final List<RexNode> commons =
+                    calculateCommonCondition(rexBuilder, 
changelogNormalizeContexts);
             for (ChangelogNormalizeContext ctx : changelogNormalizeContexts) {
                 ctx.getChangelogNormalize().markSourceReuse();
-                if (!common.isEmpty()) {
-                    
ctx.getChangelogNormalize().setCommonFilter(common.toArray(new RexNode[0]));
+                if (!commons.isEmpty()) {
+                    
ctx.getChangelogNormalize().setCommonFilter(commons.toArray(new RexNode[0]));
                 }
             }
         }
         return root;
     }
 
-    private Set<RexNode> calculateCommonCondition(
-            List<ChangelogNormalizeContext> changelogNormalizeContexts) {
-        changelogNormalizeContexts.sort(Comparator.comparingInt(o -> 
o.getConditions().size()));
-        final Set<RexNode> common =
-                new 
HashSet<>(changelogNormalizeContexts.get(0).getConditions());
+    private List<RexNode> calculateCommonCondition(
+            RexBuilder rexBuilder, List<ChangelogNormalizeContext> 
changelogNormalizeContexts) {
+        if (changelogNormalizeContexts.stream()
+                .map(ChangelogNormalizeContext::getConditions)
+                .anyMatch(Objects::isNull)) {
+            return List.of();
+        }
+
+        final RexNode or =
+                rexBuilder.makeCall(
+                        SqlStdOperatorTable.OR,
+                        changelogNormalizeContexts.stream()
+                                .map(ChangelogNormalizeContext::getConditions)
+                                .collect(Collectors.toList()));
+        final RexCall factors = (RexCall) RexUtil.pullFactors(rexBuilder, or);
 
-        for (int i = 1; i < changelogNormalizeContexts.size() && 
!common.isEmpty(); i++) {
-            
common.retainAll(changelogNormalizeContexts.get(i).getConditions());
+        final List<RexNode> commonCondition = new ArrayList<>();
+        // Since we are interested in factors only then look for AND
+        if (factors.getKind() == SqlKind.AND) {
+            for (RexNode node : factors.getOperands()) {
+                // If there is OR on top level then it is not a common factor 
anymore
+                if (node.getKind() == SqlKind.OR) {
+                    break;
+                }
+                
commonCondition.addAll(RelOptUtil.conjunctions(RexUtil.toCnf(rexBuilder, 
node)));

Review Comment:
   it is ok to run it on small pieces like factors, however running on huge 
conditions connected with `OR` leads to memory issues



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