Repository navigation
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #27328
Rationale for this change
Queries such as
VALUES ((SELECT 1), 5)andINSERT INTO t VALUES ((SELECT 1), 5)fail withInternal 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 aScalarSubqueryExprwhose value is only populated at runtime byScalarSubqueryExec.What changes are included in this PR?
In
physical_planner.rs, when planningLogicalPlan::Values, we check whether any row contains anExpr::ScalarSubquery. If so, each row is planned as aProjectionExecover aPlaceholderRowExec, combined across rows withUnionExec::try_new(or the single projection for a one-row VALUES). This defers evaluation to execution time insideScalarSubqueryExec. Standard VALUES lists without scalar subqueries continue using the existing optimizedMemorySourceConfig::try_new_as_valuespath.What is the testing strategy for this PR?
subquery.sltverifying single-row and multi-row VALUES with scalar subqueries in different column positions.cargo test --profile ci -p datafusion-sqllogictest --test sqllogictests -- subquery values insert explain.cargo fmtandcargo clippy.Are there any user-facing changes?
No API changes. Valid queries containing scalar subqueries within VALUES clauses now execute successfully instead of panicking.