Skip to content

sql: reject qualified column names that match whole-row references - #174772

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-120625-qualified-whole-row
Open

Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-120625-qualified-whole-row

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 6, 2026

Copy link
Copy Markdown

SELECT t1.t1 FROM t1 returned a whole-row record when t1 contained only f1. A predicate such as WHERE t1.t1 IS NULL could therefore silently test the row instead of reporting a missing column.

Restrict optbuilder's whole-row fallback to unqualified references. Qualified missing columns now preserve the original undefined-column error (42703); bare whole-row references and real columns retain their behavior. Add seven native optbuilder regression cases covering qualified names, aliases, predicates, multiple sources, CTEs, derived tables, and correlation.

Fixes #120625.

Related PR: #173881 also addresses this issue. This PR contains an independently generated fix with regression coverage in optbuilder's data-driven tests.

Validation:

  • Confirmed that the new regression file fails before the fix and passes afterward.
  • Built and ran the complete //pkg/sql/opt/optbuilder:optbuilder_test executable on this branch: 109 tests and subtests passed, with no skips.
  • Executed 20 SQL cases twice against real single-node test servers and an additional exact-row assertion: 41/41 checks passed. All valid-query rows matched the original baseline, including bare rows, aliases, existing same-named columns, stars, tuple fields, and correlated queries.
  • crlfmt -fast -tab 2 and git diff --check passed.

The product fix and regression fixture were generated using DeepSeek, then reviewed and validated locally. Local native test binaries were built with Bazel using --norun_validations; the ./dev wrapper refuses to run in this root execution environment. Full-repository generation and lint are not claimed here.

Release note (bug fix): Fixed a bug where a qualified reference to a nonexistent column, such as SELECT t1.t1 FROM t1, could silently return an entire table row. Such references now report an undefined-column error (SQLSTATE 42703). Unqualified whole-row references such as SELECT t1 FROM t1 and references to existing columns remain valid.

When column lookup failed, optbuilder tried to resolve the final name as
a whole-row reference even if the original name had a table prefix.
Consequently, SELECT t1.t1 FROM t1 returned the row of t1 when t1 only
contained f1, and predicates using the same typo silently used that row.

Restrict the whole-row fallback to unqualified references. Qualified
missing columns retain the resolver's undefined-column error, while bare
whole-row references and existing columns retain their current behavior.
Add optbuilder regression cases for qualified references, aliases,
predicates, multiple sources, CTEs, derived tables, and correlation.

Fixes cockroachdb#120625
See also: cockroachdb#173881
Epic: none

Release note (bug fix): Fixed a bug where a qualified reference to a
nonexistent column, such as SELECT t1.t1 FROM t1, could silently return
an entire table row. Such references now report an undefined-column
error (SQLSTATE 42703). Unqualified whole-row references such as
SELECT t1 FROM t1 and references to existing columns remain valid.
@Alignyx
Alignyx requested a review from a team as a code owner September 6, 2026 09:28
@Alignyx
Alignyx requested review from bowencrl and removed request for a team September 6, 2026 09:28
@blathers-crl

blathers-crl Bot commented Sep 6, 2026

Copy link
Copy Markdown

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.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 6, 2026

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

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relation appears as a record field within itself

1 participant