Skip to content

Commit de1eb4f

Browse files
rich7420andygrove
andauthored
test: restore ANSI array access error coverage (#5798)
* test: restore ANSI array index error coverage * test: restore ANSI element_at error coverage * test: strengthen ANSI array access regression coverage --------- Co-authored-by: Andy Grove <agrove@apache.org>
1 parent 3346176 commit de1eb4f

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

spark/src/test/resources/sql-tests/expressions/array/element_at_ansi.sql

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,58 @@ CREATE TABLE ansi_element_at_oob(arr array<int>) USING parquet
3131
statement
3232
INSERT INTO ansi_element_at_oob VALUES (array(1, 2, 3))
3333

34+
-- Valid positive and negative boundary indices must run natively and match Spark.
35+
query
36+
SELECT element_at(arr, 1), element_at(arr, 3), element_at(arr, -1), element_at(arr, -3)
37+
FROM ansi_element_at_oob
38+
39+
-- A NULL index returns NULL even in ANSI mode.
40+
query
41+
SELECT element_at(arr, CAST(NULL AS INT)) FROM ansi_element_at_oob
42+
3443
-- ============================================================================
3544
-- element_at index out of bounds (positive index)
36-
-- Spark throws: [INVALID_ARRAY_INDEX_IN_ELEMENT_AT] ...
37-
-- Comet throws: Index out of bounds for array
38-
-- See https://github.com/apache/datafusion-comet/issues/3375
45+
-- Spark and Comet throw INVALID_ARRAY_INDEX_IN_ELEMENT_AT in ANSI mode.
3946
-- ============================================================================
4047

4148
-- index beyond array length should throw (1-based indexing)
42-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
49+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
50+
SELECT element_at(arr, 4) FROM ansi_element_at_oob
51+
52+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
4353
SELECT element_at(arr, 10) FROM ansi_element_at_oob
4454

4555
-- literal array with out of bounds access
46-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
47-
SELECT element_at(array(1, 2, 3), 5)
56+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
57+
SELECT element_at(array(1, 2, 3), 5) FROM ansi_element_at_oob
4858

4959
-- ============================================================================
5060
-- element_at with index 0 (invalid)
51-
-- Spark throws: [INVALID_INDEX_OF_ZERO] The index 0 is invalid
52-
-- Comet throws: different error message
53-
-- See https://github.com/apache/datafusion-comet/issues/3375
61+
-- Spark and Comet throw INVALID_INDEX_OF_ZERO.
5462
-- ============================================================================
5563

5664
-- index 0 is not valid for element_at (1-based indexing)
57-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
65+
query expect_error([INVALID_INDEX_OF_ZERO])
5866
SELECT element_at(arr, 0) FROM ansi_element_at_oob
5967

6068
-- literal with index 0
61-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
62-
SELECT element_at(array(1, 2, 3), 0)
69+
query expect_error([INVALID_INDEX_OF_ZERO])
70+
SELECT element_at(array(1, 2, 3), 0) FROM ansi_element_at_oob
6371

6472
-- ============================================================================
6573
-- element_at index out of bounds (negative index beyond array)
6674
-- ============================================================================
6775

6876
-- negative index beyond array size should throw
69-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
77+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
78+
SELECT element_at(arr, -4) FROM ansi_element_at_oob
79+
80+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
7081
SELECT element_at(arr, -10) FROM ansi_element_at_oob
7182

7283
-- literal with negative out of bounds
73-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
74-
SELECT element_at(array(1, 2, 3), -5)
84+
query expect_error([INVALID_ARRAY_INDEX_IN_ELEMENT_AT])
85+
SELECT element_at(array(1, 2, 3), -5) FROM ansi_element_at_oob
7586

7687
-- ============================================================================
7788
-- ANSI short-circuit over a NULL array

spark/src/test/resources/sql-tests/expressions/array/get_array_item_ansi.sql

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,39 @@
2525
-- ============================================================================
2626

2727
statement
28-
CREATE TABLE ansi_array_oob(arr array<int>) USING parquet
28+
CREATE TABLE ansi_array_oob(arr array<int>, positive_idx int, negative_idx int) USING parquet
2929

3030
statement
31-
INSERT INTO ansi_array_oob VALUES (array(1, 2, 3))
31+
INSERT INTO ansi_array_oob VALUES (array(1, 2, 3), 5, -1)
32+
33+
-- Valid boundary indices must run natively as well as match Spark.
34+
query
35+
SELECT arr[0], arr[2] FROM ansi_array_oob
3236

3337
-- ============================================================================
3438
-- Array index out of bounds (positive index)
35-
-- Spark throws: [INVALID_ARRAY_INDEX] The index X is out of bounds
36-
-- Comet throws: Index out of bounds for array
37-
-- See https://github.com/apache/datafusion-comet/issues/3375
39+
-- Spark and Comet throw INVALID_ARRAY_INDEX in ANSI mode.
3840
-- ============================================================================
3941

4042
-- index beyond array length should throw (0-based indexing)
41-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
43+
query expect_error([INVALID_ARRAY_INDEX])
44+
SELECT arr[3] FROM ansi_array_oob
45+
46+
query expect_error([INVALID_ARRAY_INDEX])
4247
SELECT arr[10] FROM ansi_array_oob
4348

44-
-- literal array with out of bounds access
45-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
46-
SELECT array(1, 2, 3)[5]
49+
-- Use a column index so SimplifyExtractValueOps cannot replace the lookup with NULL.
50+
query expect_error([INVALID_ARRAY_INDEX])
51+
SELECT array(1, 2, 3)[positive_idx] FROM ansi_array_oob
4752

4853
-- ============================================================================
4954
-- Array index out of bounds (negative index)
5055
-- ============================================================================
5156

5257
-- negative index should throw
53-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
58+
query expect_error([INVALID_ARRAY_INDEX])
5459
SELECT arr[-1] FROM ansi_array_oob
5560

56-
-- literal with negative index
57-
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
58-
SELECT array(1, 2, 3)[-1]
61+
-- literal array with a negative column index
62+
query expect_error([INVALID_ARRAY_INDEX])
63+
SELECT array(1, 2, 3)[negative_idx] FROM ansi_array_oob

0 commit comments

Comments
 (0)