Skip to content

[CALCITE-7546] NullPointerException in SqlToRelConverter for UNNEST(array) AS alias under conformance with allowAliasUnnestItems=true#4962

Open
takaaki7 wants to merge 2 commits into
apache:mainfrom
takaaki7:CALCITE-7546
Open

[CALCITE-7546] NullPointerException in SqlToRelConverter for UNNEST(array) AS alias under conformance with allowAliasUnnestItems=true#4962
takaaki7 wants to merge 2 commits into
apache:mainfrom
takaaki7:CALCITE-7546

Conversation

@takaaki7
Copy link
Copy Markdown

Summary

  • Fixes CALCITE-7546: SqlToRelConverter throws NullPointerException: fieldNames for UNNEST(<array>) AS <alias> (no column list) under a conformance whose allowAliasUnnestItems() returns true (SqlConformanceEnum.PRESTO, plus user conformances overriding the flag).
  • convertFrom's AS branch passes fieldNames=null to convertUnnest when the AS clause omits a column list. The PRESTO-only branch then called requireNonNull(fieldNames, "fieldNames") and threw NPE.
  • Fall back to default item aliases derived from SqlUtil#deriveAliasFromOrdinal, matching the names that SqlUnnestOperator#inferReturnType uses during validation. This keeps the relational row type aligned with the validator's underlying namespace, so both struct (e.g. UNNEST(d.employees) AS ee.empno) and scalar element types (e.g. UNNEST(d.admins) AS aa) resolve correctly.

Test plan

  • Added testAliasUnnestArrayPlanWithoutColumnList (struct array, PRESTO) — reproduces the NPE on main, passes after the fix.
  • Added testAliasUnnestScalarArrayPlanWithoutColumnList (scalar varchar array, PRESTO).
  • Existing PRESTO/UNNEST regression tests still pass: testAliasUnnestArrayPlanWithSingleColumn, testAliasUnnestArrayPlanWithDoubleColumn, testUnnestArrayPlan, testUnnestArrayPlanAs.
  • Full SqlToRelConverterTest (663) and RelToSqlConverterTest (579) suites pass.

…rray) AS alias under conformance with allowAliasUnnestItems=true

Under a SqlConformance where allowAliasUnnestItems() is true
(SqlConformanceEnum.PRESTO and user conformances overriding the flag),
the AS branch in convertFrom passes fieldNames=null to convertUnnest when
the AS clause omits a column list. The PRESTO-only branch then called
requireNonNull(fieldNames, "fieldNames") and threw NPE.

Fall back to default item aliases derived from
SqlUtil#deriveAliasFromOrdinal, matching the names that
SqlUnnestOperator#inferReturnType uses during validation. This keeps the
relational row type aligned with the validator's underlying namespace so
both struct and scalar element types resolve correctly.
* NullPointerException in SqlToRelConverter for UNNEST(array) AS alias under
* conformance with allowAliasUnnestItems=true</a>.
*/
@Test void testAliasUnnestArrayPlanWithoutColumnList() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither of the test cases here matches the one in the issue, but they look close enough.
I hope that you have validated that the test in the issue passes as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I've added the test.

… reproduction

Add testAliasUnnestArrayLiteralPlanWithoutColumnList using the exact SQL
from the issue ("SELECT t FROM UNNEST(ARRAY[1, 2, 3]) AS t"), which
exercises the same convertUnnest fallback as the column-reference cases
but through a standalone (non-correlated) Uncollect tree. Verified that
the new test reproduces "NullPointerException: fieldNames" against the
pre-fix code and passes with the fix.
@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.

2 participants