Skip to content

Commit 67d575d

Browse files
andygroveclaude
andcommitted
Add ANSI mode SQL test files for expressions that throw on invalid input
This PR adds: 1. Framework support for `query expect_error(<pattern>)` mode in the SQL test framework, which verifies both Spark and Comet throw exceptions containing the given pattern. 2. New ANSI mode test files: - `math/abs_ansi.sql` - Tests abs overflow on INT_MIN, LONG_MIN, etc. - `math/arithmetic_ansi.sql` - Tests arithmetic overflow and divide-by-zero - `array/get_array_item_ansi.sql` - Tests out-of-bounds array access (ignored pending #3375) - `array/element_at_ansi.sql` - Tests out-of-bounds element_at (ignored pending #3375) 3. Documentation for the new `expect_error` query mode. Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent c520d7b commit 67d575d

File tree

7 files changed

+435
-0
lines changed

7 files changed

+435
-0
lines changed

docs/source/contributor-guide/sql-file-tests.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ query ignore(https://github.com/apache/datafusion-comet/issues/3326)
183183
SELECT space(n) FROM test_space WHERE n < 0
184184
```
185185

186+
#### `query expect_error(<pattern>)`
187+
188+
Asserts that both Spark and Comet throw an exception containing the given pattern. Use this
189+
for ANSI mode tests where invalid operations should throw errors.
190+
191+
```sql
192+
-- Config: spark.sql.ansi.enabled=true
193+
194+
-- integer overflow should throw in ANSI mode
195+
query expect_error(ARITHMETIC_OVERFLOW)
196+
SELECT 2147483647 + 1
197+
198+
-- division by zero should throw in ANSI mode
199+
query expect_error(DIVIDE_BY_ZERO)
200+
SELECT 1 / 0
201+
202+
-- array out of bounds should throw in ANSI mode
203+
query expect_error(INVALID_ARRAY_INDEX)
204+
SELECT array(1, 2, 3)[10]
205+
```
206+
186207
## Adding a new test
187208

188209
1. Create a `.sql` file under the appropriate subdirectory in
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- ANSI mode element_at tests
19+
-- Tests that element_at throws exceptions for out-of-bounds access in ANSI mode
20+
-- Note: element_at uses 1-based indexing
21+
22+
-- Config: spark.sql.ansi.enabled=true
23+
24+
-- ============================================================================
25+
-- Test data setup
26+
-- ============================================================================
27+
28+
statement
29+
CREATE TABLE ansi_element_at_oob(arr array<int>) USING parquet
30+
31+
statement
32+
INSERT INTO ansi_element_at_oob VALUES (array(1, 2, 3))
33+
34+
-- ============================================================================
35+
-- 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
39+
-- ============================================================================
40+
41+
-- index beyond array length should throw (1-based indexing)
42+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
43+
SELECT element_at(arr, 10) FROM ansi_element_at_oob
44+
45+
-- 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)
48+
49+
-- ============================================================================
50+
-- 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
54+
-- ============================================================================
55+
56+
-- index 0 is not valid for element_at (1-based indexing)
57+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
58+
SELECT element_at(arr, 0) FROM ansi_element_at_oob
59+
60+
-- literal with index 0
61+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
62+
SELECT element_at(array(1, 2, 3), 0)
63+
64+
-- ============================================================================
65+
-- element_at index out of bounds (negative index beyond array)
66+
-- ============================================================================
67+
68+
-- negative index beyond array size should throw
69+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
70+
SELECT element_at(arr, -10) FROM ansi_element_at_oob
71+
72+
-- 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)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- ANSI mode array index access tests
19+
-- Tests that array[index] throws exceptions for out-of-bounds access in ANSI mode
20+
21+
-- Config: spark.sql.ansi.enabled=true
22+
23+
-- ============================================================================
24+
-- Test data setup
25+
-- ============================================================================
26+
27+
statement
28+
CREATE TABLE ansi_array_oob(arr array<int>) USING parquet
29+
30+
statement
31+
INSERT INTO ansi_array_oob VALUES (array(1, 2, 3))
32+
33+
-- ============================================================================
34+
-- 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
38+
-- ============================================================================
39+
40+
-- index beyond array length should throw (0-based indexing)
41+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
42+
SELECT arr[10] FROM ansi_array_oob
43+
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]
47+
48+
-- ============================================================================
49+
-- Array index out of bounds (negative index)
50+
-- ============================================================================
51+
52+
-- negative index should throw
53+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
54+
SELECT arr[-1] FROM ansi_array_oob
55+
56+
-- literal with negative index
57+
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
58+
SELECT array(1, 2, 3)[-1]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- ANSI mode abs function tests
19+
-- Tests that abs throws exceptions for overflow on minimum integer values
20+
21+
-- Config: spark.sql.ansi.enabled=true
22+
23+
-- ============================================================================
24+
-- Test data setup
25+
-- ============================================================================
26+
27+
statement
28+
CREATE TABLE ansi_test_abs_int(v int) USING parquet
29+
30+
statement
31+
INSERT INTO ansi_test_abs_int VALUES (-2147483648)
32+
33+
statement
34+
CREATE TABLE ansi_test_abs_long(v long) USING parquet
35+
36+
statement
37+
INSERT INTO ansi_test_abs_long VALUES (-9223372036854775808)
38+
39+
statement
40+
CREATE TABLE ansi_test_abs_short(v short) USING parquet
41+
42+
statement
43+
INSERT INTO ansi_test_abs_short VALUES (-32768)
44+
45+
statement
46+
CREATE TABLE ansi_test_abs_byte(v tinyint) USING parquet
47+
48+
statement
49+
INSERT INTO ansi_test_abs_byte VALUES (-128)
50+
51+
-- ============================================================================
52+
-- abs(INT_MIN) overflow
53+
-- ============================================================================
54+
55+
-- abs(-2147483648) cannot be represented as int (since INT_MAX = 2147483647)
56+
query expect_error(overflow)
57+
SELECT abs(v) FROM ansi_test_abs_int
58+
59+
-- literal
60+
query expect_error(overflow)
61+
SELECT abs(-2147483648)
62+
63+
-- ============================================================================
64+
-- abs(LONG_MIN) overflow
65+
-- ============================================================================
66+
67+
-- abs(-9223372036854775808) cannot be represented as long
68+
query expect_error(overflow)
69+
SELECT abs(v) FROM ansi_test_abs_long
70+
71+
-- literal
72+
query expect_error(overflow)
73+
SELECT abs(-9223372036854775808L)
74+
75+
-- ============================================================================
76+
-- abs(SHORT_MIN) overflow
77+
-- ============================================================================
78+
79+
-- abs(-32768) cannot be represented as short
80+
query expect_error(overflow)
81+
SELECT abs(v) FROM ansi_test_abs_short
82+
83+
-- literal
84+
query expect_error(overflow)
85+
SELECT abs(cast(-32768 as short))
86+
87+
-- ============================================================================
88+
-- abs(BYTE_MIN) overflow
89+
-- ============================================================================
90+
91+
-- abs(-128) cannot be represented as tinyint
92+
query expect_error(overflow)
93+
SELECT abs(v) FROM ansi_test_abs_byte
94+
95+
-- literal
96+
query expect_error(overflow)
97+
SELECT abs(cast(-128 as tinyint))

0 commit comments

Comments
 (0)