Fabian Hueske created FLINK-41766:
-------------------------------------

             Summary: Nested projection pushdown types NOT NULL field of 
nullable ROW as NOT NULL
                 Key: FLINK-41766
                 URL: https://issues.apache.org/jira/browse/FLINK-41766
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
            Reporter: Fabian Hueske
            Assignee: Fabian Hueske


`PushProjectIntoTableSourceScanRule` gives a pushed nested field its declared 
type. If that field is `NOT NULL` but sits inside a nullable `ROW` column, the 
field is actually nullable, so the type is wrong.

*Reproduction* (source with nested projection support, e.g. `values` with 
`'nested-projection-supported' = 'true'`):

{code:sql}
CREATE TABLE T (id INT, ns ROW<v INT NOT NULL, w STRING, u INT>) WITH (...);
-- data: (1, NULL), (2, ROW(5,'x',7))

SELECT id, ns.v, ns.w FROM T;
SELECT id, ns.v IS NULL FROM T;
{code}

*Actual*
* The first query fails during planning:
{noformat}
AssertionError: type mismatch: type1: INTEGER NOT NULL, type2: INTEGER
  at Project.isValid
  at PushProjectIntoTableSourceScanRule.onMatch(...:186)
{noformat}
* The second query plans, but {{ns.v IS NULL}} returns {{false}} for the row 
with {{ns = NULL}}. It should return {{true}}.

*Expected:* both queries plan and return correct results. A pushed field is 
nullable if the field or any container on its path is nullable.

*Cause*
* {{performPushDown}} builds the new produced type with {{Projection.project}}. 
The pushed leaf keeps its declared {{NOT NULL}} type, and that type also goes 
into {{ProjectPushDownSpec#projectedPhysicalType}}.
* {{rewriteProjections}} uses the untyped {{NestedProjectionUtil.rewrite}} 
overload, so the new ref is also {{NOT NULL}}. The old project row type stays 
nullable, which fails the first query. When the leaf is only used inside an 
expression (second query), nothing checks it, and null checks are optimized 
away.

*Fix:* use the typed {{rewrite}} overload (as the join and filter rewrites 
already do) and make pushed leaf types nullable in {{performPushDown}} when the 
path has a nullable container.

*Tests:* plan test with {{verifyRelPlanWithType}} and streaming/batch 
{{TableSourceITCase}} with NULL container rows. 
{{TestValuesTableFactory#projectRow}} needs to be null-safe for these. 
{{NestedProjectionUtilJoinTest#testNotNullLeafOfNullableStructIsNullable}} 
should also use {{StructKind.PEEK_FIELDS_NO_EXPAND}}, because with 
{{FULLY_QUALIFIED}} it never has a {{NOT NULL}} leaf inside a nullable struct.

Reproduces on clean master (78aeda2b289). Found while working on FLINK-39804, 
which makes more queries reach this path but doesn't cause it.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to