raminqaf opened a new pull request, #29882:
URL: https://github.com/apache/flink/pull/29882

   ## What is the purpose of the change
   
   Reading an element that doesn't exist returns a placeholder value instead of 
NULL when the element type is NOT NULL:
   
   ```sql
   SELECT ARRAY[10,20,30][4], ARRAY[10,20,30][4] IS NULL;
   ```
   
   | Expression                                       | Before  | After |
   |--------------------------------------------------|---------|-------|
   | `ARRAY[10,20,30][4]`                             | `-1`    | NULL  |
   | `ARRAY[10,20,30][4] IS NULL`                     | FALSE   | TRUE  |
   | `WHERE ARRAY[10,20,30][x] IS NULL` over x = 2, 4 | no rows | row 4 |
   | `MAP['a', 1]['b']`                               | `-1`    | NULL  |
   | `ELEMENT(ARRAY_REMOVE(ARRAY[1], 1))`             | `-1`    | NULL  |
   | `a[1]` on a NULL `ARRAY<INT NOT NULL>`           | `-1`    | NULL  |
   | `r['f']` on a NULL `ROW<f INT NOT NULL>`         | `-1`    | NULL  |
   
   The planner already types these results as nullable. The code generators set 
the null term correctly but returned the NOT NULL element type, so 
`rowSetField` skipped `setNullAt` and `IS NULL` folded to `false`. They now 
return the call's result type, as `generateArrayCardinality` does.
   
   A job that writes such a result into a NOT NULL sink column used to store 
`-1`. It now fails under the default `table.exec.sink.not-null-enforcer = 
ERROR`, or drops the row with `DROP`. NULL for an out-of-range index is the 
behavior agreed in FLINK-17847.
   
   PostgreSQL, Calcite, Spark with ANSI off, DuckDB, Hive, CockroachDB and 
StarRocks all return NULL. The SQL:1999 standard raises an error.
   
   This also fixes FLINK-34656, the NULL-array case.
   
   ## Brief change log
   
     - `generateArrayElementAt`, `generateArrayElement`, `generateMapGet` and 
`generateDot` take the call's result type and use it for the generated 
expression.
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - Extended the `AT` spec in `FieldAccessFromTableITCase` with an index 
past the end, a NULL array, a missing MAP key and a NULL ROW, all with NOT NULL 
elements or fields.
     - Extended the `ARRAY_ELEMENT` spec in `CollectionFunctionsITCase` with an 
empty `ARRAY<INT NOT NULL>`.
     - The new cases fail without the fix. 
`ArrayTypeTest#testReturnNullWhenArrayIndexOutOfBounds` couldn't catch this, 
because `ExpressionTestBase` casts every expression to VARCHAR before writing 
it.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with 
`@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): yes. The 
generated code now checks the null flag when it writes these results, one 
branch per field.
     - Anything that affects deployment or recovery: JobManager (and its 
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code 2.1.322 (Claude Opus 5.5)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to