sql: reject out-of-range routine placeholders - #174718
Open
Alignyx wants to merge 1 commit into
Open
Conversation
When a routine body was built while an enclosing prepared query's values were active, an invalid ordinal fell through function argument resolution. Scalar building could then fold it from the caller's evaluation context. Preserve the distinction between no function-argument scope and a routine ordinal known to be out of range. Reject the latter with the existing undefined-parameter error before generic placeholder construction. Fixes cockroachdb#172995 Release note (bug fix): Fixed PL/pgSQL routine bodies with out-of-range ordinal parameter references reading placeholder values from an enclosing prepared query when the body was built lazily.
|
Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR. My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI. 🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
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.
Fixes #172995.
Root cause
Routine placeholder lookup returned
nilfor two different states: no active routine-argument scope, and an active scope in which the requested ordinal was out of range. The placeholder visitor could not distinguish those states, so an invalid routine placeholder fell through to ordinary prepared-statement placeholder construction. When the surrounding statement had a value for that ordinal, the routine body could read and fold the caller's placeholder value instead of raising an undefined-parameter error.How I tracked it down
The two higher-level lazy routine paths from the report are not currently executable on master: nested routine DDL is rejected by the routine DDL safety check, and dynamic
EXECUTEis not implemented. I therefore isolated the shared optbuilder transition under an active outer placeholder assignment. The control cases showed that an ordinary prepared placeholder correctly became a constant and a valid routine ordinal correctly became a routine parameter column, while an out-of-range routine ordinal incorrectly became the caller's constant. That placed the first invalid transition at the boundary between routine scope lookup and generic placeholder handling rather than in lazy body construction itself.Fix
The existing single-value scope lookup remains available to its other callers, and a companion lookup now reports whether an ordinal is definitively out of range. Placeholder resolution uses that result to preserve valid routine parameter columns and to raise the existing
no value provided for placeholdererror before generic placeholder construction when an active routine scope rejects the ordinal.Test coverage
TestRoutineParamOrdinalIsolationwith three arms: ordinary prepared placeholders still use their assigned values, valid routine ordinals still resolve to parameter columns, and invalid routine ordinals return SQLSTATE42P02even when the caller has a value at that position.regression_143887andregression_144020routine-placeholder logic regressions.