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


##########
core/src/test/java/org/apache/calcite/rex/RexUtilTest.java:
##########
@@ -43,4 +54,116 @@ class RexUtilTest {
         RexUtil.SubQueryFinder.find(expression, SqlKind.SCALAR_QUERY));
     assertNull(RexUtil.SubQueryFinder.find(expression, SqlKind.EXISTS));
   }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5845";>[CALCITE-5845]
+   * StackOverflowError when SQL has a very large predicate with many ANDs</a>.
+   *
+   * <p>Verifies that {@link RexUtil#flattenAnd} does not recurse when given a
+   * deeply nested binary AND tree. */
+  @Test void testFlattenAndDeeplyNested() {
+    final RelBuilder builder =
+        RelBuilder.create(Frameworks.newConfigBuilder().build());
+    final RexBuilder rexBuilder = builder.getCluster().getRexBuilder();
+    final RelDataType booleanType =
+        builder.getTypeFactory().createSqlType(SqlTypeName.BOOLEAN);
+    final int depth = 20_000;
+    // Build AND(AND(AND(..., $1), $2), $3, ...) as a left-deep binary tree,
+    // iteratively. Hash codes are pre-warmed bottom-up so that the
+    // de-duplication set inside flattenAnd does not itself recurse deeply.
+    RexNode node = rexBuilder.makeInputRef(booleanType, 0);
+    node.hashCode();
+    for (int i = 1; i < depth; i++) {
+      node =
+          rexBuilder.makeCall(SqlStdOperatorTable.AND, node,
+              rexBuilder.makeInputRef(booleanType, i));
+      node.hashCode();
+    }
+    final RexNode deepTree = node;
+    final List<RexNode> flat =
+        runInSmallStack(() -> RexUtil.flattenAnd(ImmutableList.of(deepTree)));
+    assertThat(flat, hasSize(depth));
+  }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5845";>[CALCITE-5845]
+   * StackOverflowError when SQL has a very large predicate with many ANDs</a>.
+   *
+   * <p>Verifies that {@link RexUtil#flatten} does not recurse when given a
+   * deeply nested binary AND tree. */
+  @Test void testFlattenDeeplyNested() {
+    final RelBuilder builder =
+        RelBuilder.create(Frameworks.newConfigBuilder().build());
+    final RexBuilder rexBuilder = builder.getCluster().getRexBuilder();
+    final RelDataType booleanType =
+        builder.getTypeFactory().createSqlType(SqlTypeName.BOOLEAN);
+    final int depth = 20_000;
+    RexNode node = rexBuilder.makeInputRef(booleanType, 0);
+    for (int i = 1; i < depth; i++) {
+      node =
+          rexBuilder.makeCall(SqlStdOperatorTable.AND, node,
+              rexBuilder.makeInputRef(booleanType, i));
+    }
+    final RexNode deepTree = node;
+    final List<RexNode> flat =
+        runInSmallStack(
+            () -> RexUtil.flatten(ImmutableList.of(deepTree),
+            SqlStdOperatorTable.AND));
+    assertThat(flat, hasSize(depth));
+  }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-5845";>[CALCITE-5845]
+   * StackOverflowError when SQL has a very large predicate with many ANDs</a>.
+   *
+   * <p>Verifies that {@link RexUtil#isFlat(RexNode)} does not recurse when
+   * given a deeply nested tree of a non-associative operator (NOT), where
+   * short-circuiting at the top level is not possible. */
+  @Test void testIsFlatDeeplyNested() {
+    final RelBuilder builder =
+        RelBuilder.create(Frameworks.newConfigBuilder().build());
+    final RexBuilder rexBuilder = builder.getCluster().getRexBuilder();
+    final RelDataType booleanType =
+        builder.getTypeFactory().createSqlType(SqlTypeName.BOOLEAN);
+    final int depth = 20_000;
+    RexNode node = rexBuilder.makeInputRef(booleanType, 0);
+    for (int i = 0; i < depth; i++) {
+      node = rexBuilder.makeCall(SqlStdOperatorTable.NOT, node);
+    }
+    final RexNode deepTree = node;
+    assertThat(runInSmallStack(() -> RexUtil.isFlat(deepTree)), is(true));
+  }
+
+  /** Runs {@code callable} on a thread with a small stack, so that a recursive
+   * implementation of the callee overflows quickly, while an iterative
+   * implementation completes. */
+  private static <T> T runInSmallStack(Callable<T> callable) {

Review Comment:
   It doesn't differ, because they were identical copies. Extracted into a 
shared org.apache.calcite.test.SmallStack used by both classes.



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