Skip to content

fix: evaluate scalar subqueries in VALUES rows at execution time - #27329

Open
v28k wants to merge 2 commits into
apache:mainfrom
v28k:fix-values-scalar-subquery
Open

v28k wants to merge 2 commits into
apache:mainfrom
v28k:fix-values-scalar-subquery

Conversation

@v28k

@v28k v28k commented Nov 11, 2026

Copy link
Copy Markdown

Which issue does this PR close?

Closes #27328

Rationale for this change

Queries such as VALUES ((SELECT 1), 5) and INSERT INTO t VALUES ((SELECT 1), 5) fail with Internal error: ScalarSubqueryExpr evaluated before the subquery was executed. The default VALUES planner evaluates row expressions while the physical plan is built, but an uncorrelated scalar subquery produces a ScalarSubqueryExpr whose value is only populated at runtime by ScalarSubqueryExec.

What changes are included in this PR?

In physical_planner.rs, when planning LogicalPlan::Values, we check whether any row contains an Expr::ScalarSubquery. If so, each row is planned as a ProjectionExec over a PlaceholderRowExec, combined across rows with UnionExec::try_new (or the single projection for a one-row VALUES). This defers evaluation to execution time inside ScalarSubqueryExec. Standard VALUES lists without scalar subqueries continue using the existing optimized MemorySourceConfig::try_new_as_values path.

What is the testing strategy for this PR?

  • Added test cases in subquery.slt verifying single-row and multi-row VALUES with scalar subqueries in different column positions.
  • Ran cargo test --profile ci -p datafusion-sqllogictest --test sqllogictests -- subquery values insert explain.
  • Passed cargo fmt and cargo clippy.

Are there any user-facing changes?

No API changes. Valid queries containing scalar subqueries within VALUES clauses now execute successfully instead of panicking.

v28k added 2 commits November 8, 2026 19:05
`VALUES ((SELECT 1), 5)` failed with "Internal error: ScalarSubqueryExpr
evaluated before the subquery was executed": the values exec evaluates
its rows while the plan is built, but the value of an uncorrelated scalar
subquery only exists once ScalarSubqueryExec has run.

Plan each row of such a VALUES list as a projection over a placeholder
row and union the rows, so they are evaluated at execution time.
@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scalar subquery in a VALUES row fails with "Internal error: ScalarSubqueryExpr evaluated before the subquery was executed"

1 participant