Development stories

Stop nested SQL rewrites from multiplying

KratosForge Editorial · 2026-12-03

Kratos and his wolf inspect a shared central forge mold beside a pile of repeated molds.
Custom illustration · Reuse a shared input instead of making copies at every level.

A short SQL expression can become a surprisingly large query before the database executes it. KratosForge’s local Spark translation hit that problem with nested array set functions: the template for one call repeated its input, and the next outer call copied those repetitions again. Adding a level multiplied work instead of adding a small amount.

PR #3595 fixes that growth for array_distinct, array_union, array_intersect and array_except in the local Spark SQL and Databricks SQL paths. The previous array_distinct template named its argument five times, including checks and position lookups. A nested argument therefore carried a growing tree of repeated SQL. The pull request records about 500,000 characters for eight nested array_union calls before the repair.

The repair reuses an existing argument-binding helper. When a template refers to a complex argument more than once, the generated SQL binds that argument through a lambda over a one-element list and reuses the bound value. Columns and literals keep the simpler expansion. The change targets these four functions; it does not replace every translation template or introduce a general query optimizer.

Translation boundary: the complex argument appears once before its bound value is reused.
  1. Complex array expression
  2. Bind once in generated SQL
  3. Reuse the bound value in checks and result

The important constraint is preserving answers. Removing checks to shorten a query could change null handling or array contents. The regression suite checks null arrays, repeated null elements, positive and negative zero, aggregate arguments and combinations of different set functions. The intended improvement is bounded translation growth while those behaviors remain intact.

We ran all 28 tests in the new regression file at app commit 20cf7ab. They passed on the local environment using DuckDB 1.5.6. For eight nested array_distinct calls, the generated SQL measured 2,900 characters on the Spark path and 3,228 on the Databricks path. The tests also bound each eight-level case below 20,000 characters and five seconds of thread CPU time. Those are local regression budgets, not a remote execution speed guarantee.

This deliberately nested notebook probe checks an answer through the local Spark-compatible session. It is a small test case, not a suggestion to repeat deduplication in application queries. A correct answer alone would miss the original problem: pair semantic assertions with a bound on the compiler’s output or work when you maintain a translator.

PYTHON · Local notebook regression probe at the cited app revision; the existing spark session is supplied by KratosForge.
# Regression probe for nested translation, not a recommended query style.
expression = "array(1, 1, NULL, NULL, 2)"
for _ in range(8):
    expression = f"array_distinct({expression})"

items = spark.sql(f"SELECT {expression} AS items").collect()[0][0]
assert items == [1, None, 2]
print(items)

The fix is merged source work. It is not a new Stable announcement, and these checks did not run on a Fabric Spark cluster or Databricks warehouse. The official Stable release record was still last updated November 2 when checked. For a template-based compiler, add nested compositions to the tests—not just one example of each operation. Repeated references that look harmless in a single template can multiply at the next level.

← All publications

Search documentation

Start typing to search all 115 guides.

Search runs in your browser esc to close