Skip to content

opt: expand labeled tuple fields for table-source unnest - #174980

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-98352-unnest-tuple-source-columns
Open

Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-98352-unnest-tuple-source-columns

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 10, 2026 •

Copy link
Copy Markdown

When unnest(ARRAY[t.*]) is used as a table source, a labeled row value should expose its fields as separate columns. The query in #98352 currently returns the two columns from t plus one tuple-valued unnest column. This change returns four scalar columns while preserving the three result rows.

The single-array unnest generator still emits one physical datum per array element. Changing only the declared output columns would leave the optimizer's column IDs out of sync with the executor. Instead, this patch keeps the single tuple column in ProjectSet and adds a projection above it to extract each labeled field. The scope exposes those projected fields as the table-source columns.

The special case is selected using the resolved built-in generator overload and its single array argument. A user-defined function named unnest therefore does not acquire the built-in's output layout. An assertion checks that the Zip output and scope refer to the same single physical column before the projection is constructed.

The data-driven regression records the expanded columns and the projection above ProjectSet for the reported lateral query.

Validation

  • bazel test //pkg/sql/opt/optbuilder:optbuilder_test --test_arg=-test.run=^TestBuilder$/^srfs$ passed.
  • Adding the same regression to unmodified 8812064a failed: the plan retained one tuple column instead of the two field projections.
  • bazel test //pkg/sql/opt/optbuilder:optbuilder_test passed.
  • Runs used two build jobs, disabled sharding, one Go-test parallel worker, and a fixed seed, with CockroachDB's randomized test knobs, test tenants and DRPC disabled.

The repair harness also executed the reported query and equivalents, checking all four INT8 columns and the three ordered result rows. The checks do not establish exhaustive compatibility for NULL composite values, alias lists or arbitrary same-named UDFs.

Fixes #98352

Release note (backward-incompatible change): A single-array unnest call used as a table source now expands labeled composite values into individual columns, rather than returning one tuple-valued column. Queries using SELECT * from that table source may therefore return more columns. The number of array elements returned is unchanged.

A table-source unnest over a labeled composite array should expose the
individual tuple fields. Keep one physical ProjectSet output per array
element and project the labeled fields into the table-source scope.

Restrict this to the resolved built-in single-array generator overload so
same-named UDFs retain their output contract. Add a data-driven regression
that fails on the unmodified baseline and passes with the change; the
optbuilder suite passes.

Production patch generated with Codex CLI through MetamorphicRepair.

Fixes cockroachdb#98352

Release note (backward-incompatible change): A single-array unnest call used as a table
source now expands labeled composite values into individual columns, rather than
returning one tuple-valued column. Queries using SELECT * from that table source may
therefore return more columns. The number of array elements returned is unchanged.
@Alignyx
Alignyx requested a review from a team as a code owner September 10, 2026 11:39
@Alignyx
Alignyx requested review from mw5h and removed request for a team September 10, 2026 11:40
@blathers-crl

blathers-crl Bot commented Sep 10, 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 10, 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.

opt: unnest as table source should expand tuple return values

1 participant