Chengcheng Jin created FLINK-41765:
--------------------------------------

             Summary: arr[-1] behavior for constant and runtime
                 Key: FLINK-41765
                 URL: https://issues.apache.org/jira/browse/FLINK-41765
             Project: Flink
          Issue Type: Improvement
          Components: Stateful Functions
    Affects Versions: 2.3.0
            Reporter: Chengcheng Jin


Array element access arr[i] is inconsistent for literal vs. non-literal 
indexes: literal i < 1 throws, non-literal returns a default value instead of 
NULL for NOT NULL element types

Array element access (ITEM) behaves differently depending on whether the index 
is a literal, and the non-literal path returns a wrong value for NOT NULL 
element types.

1. Literal index < 1 fails at planning time. generateArrayElementAt throws 
ValidationException when the index is a literal smaller than 1. The same index 
passed as a non-literal expression does not fail.
2. Non-literal index < 1 or out of bounds returns a placeholder instead of 
NULL. When the element type is NOT NULL, the result is the type's internal 
default value, not NULL. CodeGenUtils.primitiveDefaultValue gives -1 for 
INT/BIGINT, -1.0 for DOUBLE, false for BOOLEAN and an empty string for STRING. 
When the element type is nullable, the result is NULL.
3. Literal out-of-bounds indexes are not validated. arr[4] on a 3-element array 
is not rejected and returns the same placeholder value as case 2.

Steps to reproduce

-- Literal index < 1: fails at planning time
SELECT ARRAY[10, 20, 30][-1];
-- ValidationException: Array element access needs an index starting at 1 but 
was -1.
SELECT ARRAY[10, 20, 30][0];
-- ValidationException: Array element access needs an index starting at 1 but 
was 0.

-- Non-literal index (RAND_INTEGER prevents constant folding):
SELECT ARRAY[10, 20, 30][RAND_INTEGER(1) - 1];   -- index -1 -> -1   (expected 
NULL)
SELECT ARRAY[10, 20, 30][RAND_INTEGER(1)];       -- index  0 -> -1   (expected 
NULL)
SELECT ARRAY[10, 20, 30][RAND_INTEGER(1) + 4];   -- index  4 -> -1   (expected 
NULL)

-- Literal out of bounds: no error, same placeholder
SELECT ARRAY[10, 20, 30][4];                     -- -1               (expected 
NULL)

-- Other NOT NULL element types, index -1 via RAND_INTEGER(1) - 1
SELECT ARRAY[CAST(10 AS BIGINT)][RAND_INTEGER(1) - 1];  -- -1
SELECT ARRAY[1.5E0][RAND_INTEGER(1) - 1];               -- -1.0
SELECT ARRAY['a', 'b'][RAND_INTEGER(1) - 1];            -- '' (empty string)
SELECT ARRAY[true][RAND_INTEGER(1) - 1];                -- false

-- Nullable element type: returns NULL as expected
SELECT ARRAY[10, 20, CAST(NULL AS INT)][RAND_INTEGER(1) - 1];  -- NULL
SELECT ARRAY[10, 20, CAST(NULL AS INT)][4];                    -- NULL

Note: a VALUES-backed view with the index in a column does not reproduce the 
non-literal behavior. The planner inlines and constant-folds it, so it fails 
with the literal-index error.

Expected behavior

The same index value should give the same result however it is produced. Either:

- A) A non-literal index < 1 or out of bounds returns NULL, as documented for 
out-of-bounds access. A literal < 1 should then also return NULL instead of 
throwing. Or:
- B) Index < 1 is an error in both cases. The non-literal case would then need 
a runtime check.

Whichever is chosen, an out-of-bounds access must never return an internal 
default such as -1, -1.0, false or ''. These are indistinguishable from real 
data.

Analysis

>From the disassembly of ScalarOperatorGens.generateArrayElementAt:

- The ValidationException("Array element access needs an index starting at 1 
but was N.") is thrown only when index.literalValue is defined and < 1. The 
non-literal path generates idx - 1 and array.isNullAt(...).
- The placeholder values match CodeGenUtils.primitiveDefaultValue (-1, -1L, 
-1.0d, false, EMPTY_UTF8).
- The wrong value appears only for NOT NULL element types. With a nullable 
element type, the NULL result is honored. This suggests the generated result is 
treated as non-nullable, so the isNull flag set on an out-of-bounds access is 
ignored.

(This analysis is from bytecode and observed behavior, not from reading the 
source. Please correct it if it is wrong.)

Impact

- Silent wrong results: out-of-bounds access returns plausible-looking values 
such as -1.
- Engines that reimplement Flink semantics (for example, native execution 
backends) have to reproduce an undocumented literal/non-literal split, and 
possibly the placeholder values.



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

Reply via email to