[ 
https://issues.apache.org/jira/browse/FLINK-41766?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Fabian Hueske updated FLINK-41766:
----------------------------------
    Fix Version/s: 2.4.0

> 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
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 2.4.0, 2.5.0
>
>
> {{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*
> * {{Projection#project}} gives a projected nested field its declared type. A 
> {{NOT NULL}} field below a nullable {{ROW}} is nullable, but keeps its {{NOT 
> NULL}} type. {{PushProjectIntoTableSourceScanRule}} uses it for the new 
> produced type and for {{ProjectPushDownSpec#projectedPhysicalType}}, so the 
> source gets the wrong type too.
> * {{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*
> * {{Projection#project}} makes a projected field nullable if any {{ROW}} on 
> its path is nullable. All callers get the correct type 
> ({{PushProjectIntoTableSourceScanRule}}, {{ScanReuser}}, 
> {{ProjectableDecodingFormat}} implementations such as JSON).
> * {{PushProjectIntoTableSourceScanRule}} uses the typed {{rewrite}} overload, 
> as the join and filter rewrites already do.
> *Tests:* {{ProjectionTest}}, {{JsonFormatFactoryTest}}, plan tests 
> ({{PushProjectIntoTableSourceScanRuleTest}} with {{verifyRelPlanWithType}}, 
> {{ScanReuseTest}}) and streaming/batch {{TableSourceITCase}} with NULL 
> container rows. {{TestValuesTableFactory#projectRow}} needs to be null-safe 
> for these.
> *Not covered:* 
> {{NestedProjectionUtilJoinTest#testNotNullLeafOfNullableStructIsNullable}} 
> should 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