← 输入框|github.com|刷新
Skip to content

[SQL] Reduce SQL compiler stack usage - #7938

Open
mihaibudiu wants to merge 2 commits into
mainfrom
issue7931
Open

mihaibudiu wants to merge 2 commits into
mainfrom
issue7931

Conversation

@mihaibudiu

@mihaibudiu mihaibudiu commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Linked Issue

Fixes #7931

There are two commits:

  • increase stack size for the SQL compiler from 2MB to 512MB. This seems much, but the compiler is single-threaded.
  • convert long chains of UNION/INTERSECT into balanced trees (only if this change does not modify the semantics of the query) after parsing

Checklist

  • Unit tests added/updated

@mythical-fred-oss

Copy link
Copy Markdown
Contributor

What I ran: ./build.sh, then mvn test -Dtest=BalanceSetOperationsTests,NegativeParserTests: all pass. I also ran the jar on the issue's 3000-branch repro and on a chain of 3000 string literals, with and without -Xss512m. I did not run cargo check on pipeline-manager; that change is one argument.

QA case (not covered by the PR's tests) Result
Issue repro, 3000-branch UNION ALL, default test stack ✅ compiles (~14 s)
300-branch INTERSECT / INTERSECT ALL ✅ correct output
UNION chain mixing NOT NULL and nullable columns ✅
Long chain with mixed types inside an IN (…) subquery (fallback path) ✅
Long chain inside a CTE (CteToLocalViews path) ✅
3000 string literals of different lengths (CHAR(n)) ❌ Stack overflow at default stack; ✅ with -Xss512m (see inline)
  • missing (medium): None of the cases in the table are in the PR's tests. INTERSECT/INTERSECT ALL in particular now go through a new N-ary LogicalIntersect path with no test. Please add at least the issue repro, INTERSECT, and the fallback-in-subquery case. The tests I used:
@Test public void intersectLong() {
    var ccs = this.getCCS("CREATE TABLE t (x INT);\nCREATE VIEW v AS " +
            chain(300, i -> "SELECT x FROM t", "INTERSECT") + ";");
    ccs.stepWeightOne("INSERT INTO t VALUES(1),(1),(2);", " x\n---\n 1\n 2");
}
@Test public void mixedInSubquery() {
    var ccs = this.getCCS("CREATE TABLE t (b BIGINT, d DOUBLE);\nCREATE VIEW v AS SELECT b FROM t WHERE b IN (" +
            chain(200, i -> i == 150 ? "SELECT d FROM t" : "SELECT b FROM t", "UNION ALL") + ");");
    ccs.stepWeightOne("INSERT INTO t VALUES(5, 1);", " b\n---\n 5");
}
@Test public void issue3000() {
    this.getCC("CREATE TABLE t (id BIGINT NOT NULL, v BIGINT);\nCREATE MATERIALIZED VIEW u AS " +
            chain(3000, i -> "SELECT id, v + " + i + " AS v FROM t", "UNION ALL") + ";");
}
  • commit quality (medium): The subject of the first commit runs past 72 characters (GitHub cuts it off with …). The body of the second commit is only Fixes #7931; it should say why the chains are balanced and why mixed types fall back to the left-deep form. The PR description says the stack is 256 MB, but the code and the commit both use 512 MB.

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
The parser builds a left-deep tree for a chain of set operations, and
the compiler and Calcite recurse once per level, so a view with a
UNION ALL of about 2,500 queries overflows the default stack.  Chains
of more than 100 queries become balanced trees, log2(N) levels deep,
and each tree becomes one n-ary set operation.

SQL types a chain in the left-deep grouping.  When the column types
need casts that depend on the grouping, the statement is validated
again in the left-deep form.  Types that differ only in nullability,
and strings of different lengths, give the same result in every
grouping.

Fixes #7931

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SQL] StackOverflowError when a view is a UNION ALL of about 2,500 or more branches

1 participant