Skip to content

[CALCITE-7547] BIG_QUERY should allow field access on UNNEST of struct array AS alias#4963

Open
takaaki7 wants to merge 1 commit into
apache:mainfrom
takaaki7:CALCITE-7547
Open

[CALCITE-7547] BIG_QUERY should allow field access on UNNEST of struct array AS alias#4963
takaaki7 wants to merge 1 commit into
apache:mainfrom
takaaki7:CALCITE-7547

Conversation

@takaaki7
Copy link
Copy Markdown

@takaaki7 takaaki7 commented May 23, 2026

Jira Link

CALCITE-7547

Changes Proposed

Under SqlConformanceEnum.BIG_QUERY, the following query failed validation with Column 'NAME' not found in table 'I', even though BigQuery itself supports the pattern:

SELECT i.name FROM t, UNNEST(t.items) AS i
-- t(items ARRAY<ROW<name VARCHAR>>), ROW kind = PEEK_FIELDS_NO_EXPAND

Root cause: SqlConformanceEnum.BIG_QUERY.allowAliasUnnestItems() returned false. With the flag off, SqlUnnestOperator#inferReturnType flattens the ROW element type into one column per field instead of producing a single struct-typed column the alias can wrap, and AliasNamespace cannot remap the flattened result back under the alias. The identical query already validates under SqlConformanceEnum.PRESTO. The flag was added in CALCITE-3789 for Presto and was never extended to BigQuery.

Extend the existing PRESTO case in SqlConformanceEnum#allowAliasUnnestItems() to also cover BIG_QUERY, matching BigQuery's actual semantics. No other conformance is affected.

Depends on #4962 (CALCITE-7546) — without that fix, enabling the flag for BIG_QUERY would shift the failure from validation to an NPE in SqlToRelConverter#convertUnnest for the 2-operand AS(UNNEST, alias) form. The tests in this PR therefore use the column-list form AS t(<col>) so they pass on main alone; the bare-alias form is covered by CALCITE-7546's own tests once both land.

Test plan

  • SqlValidatorTest#testAliasUnnestMultipleArraysBigQuery — new, mirrors the existing PRESTO test under BIG_QUERY conformance (positive struct-field access, multi-array unnest, two negative cases).
  • SqlToRelConverterTest#testAliasUnnestArrayPlanWithSingleColumnBigQuery and testAliasUnnestArrayPlanWithDoubleColumnBigQuery — new, with matching XML plan blocks. Plans are identical to the PRESTO equivalents because allowAliasUnnestItems() is the only relevant dial.
  • Existing PRESTO regression tests still pass: testAliasUnnestMultipleArrays, testAliasUnnestArrayPlanWithSingleColumn, testAliasUnnestArrayPlanWithDoubleColumn.
  • Full SqlValidatorTest (562) and SqlToRelConverterTest (663) suites pass.

…EST(array_of_struct) AS alias

Under SqlConformanceEnum.BIG_QUERY, SELECT i.name FROM t, UNNEST(t.items) AS i
where items is ARRAY<ROW<name ...>> failed validation with
"Column 'NAME' not found in table 'I'" because allowAliasUnnestItems() returned
false. With the flag off, SqlUnnestOperator#inferReturnType flattens the ROW
element type into individual columns instead of keeping it as a single
struct-typed column the alias can wrap, and AliasNamespace cannot remap the
flattened result back under the alias.

BigQuery itself supports this access pattern, so extend the existing PRESTO
case in SqlConformanceEnum#allowAliasUnnestItems() to also cover BIG_QUERY.
This complements CALCITE-7546, which removes the downstream NPE in
SqlToRelConverter#convertUnnest for the 2-operand AS(UNNEST, alias) form
when the flag is enabled.
@takaaki7 takaaki7 changed the title [CALCITE-7547] BIG_QUERY conformance should allow field access on UNN… [CALCITE-7547] BIG_QUERY conformance should allow field access on UNNEST(array_of_struct) AS alias May 23, 2026
@takaaki7 takaaki7 changed the title [CALCITE-7547] BIG_QUERY conformance should allow field access on UNNEST(array_of_struct) AS alias [CALCITE-7547] BIG_QUERY should allow field access on UNNEST of struct array AS alias May 23, 2026
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant