xuzifu666 commented on code in PR #5384:
URL: https://github.com/apache/calcite/pull/5384#discussion_r4455243398


##########
core/src/main/java/org/apache/calcite/rex/RexUtil.java:
##########
@@ -1823,20 +1832,44 @@ private static boolean isFlat(
    * calls to an associative operator such as AND and OR.
    */
   public static boolean isFlat(RexNode expr) {
-    if (!(expr instanceof RexCall)) {
-      return true;
+    // Use an explicit stack to avoid StackOverflowError on deeply nested
+    // expressions (e.g. CALCITE-5845)
+    final Deque<RexNode> stack = new ArrayDeque<>();
+    stack.push(expr);
+    while (!stack.isEmpty()) {
+      final RexNode current = stack.pop();
+      if (!(current instanceof RexCall)) {
+        continue;
+      }
+      final RexCall call = (RexCall) current;
+      if (!isFlat(call.getOperands(), call.getOperator())) {

Review Comment:
   It's non-recursive and it only loops over an explicit `ArrayDeque`, and the 
private list overload is the original non-recursive exists() check. 
testIsFlatDeeplyNested verifies it: overflows against the old code on a 256KB 
stack, passes in latest commit.



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