Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/source/contributor-guide/sql-file-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,27 @@ query ignore(https://github.com/apache/datafusion-comet/issues/3326)
SELECT space(n) FROM test_space WHERE n < 0
```

#### `query expect_error(<pattern>)`

Asserts that both Spark and Comet throw an exception containing the given pattern. Use this
for ANSI mode tests where invalid operations should throw errors.

```sql
-- Config: spark.sql.ansi.enabled=true

-- integer overflow should throw in ANSI mode
query expect_error(ARITHMETIC_OVERFLOW)
SELECT 2147483647 + 1

-- division by zero should throw in ANSI mode
query expect_error(DIVIDE_BY_ZERO)
SELECT 1 / 0

-- array out of bounds should throw in ANSI mode
query expect_error(INVALID_ARRAY_INDEX)
SELECT array(1, 2, 3)[10]
```

## Adding a new test

1. Create a `.sql` file under the appropriate subdirectory in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ANSI mode element_at tests
-- Tests that element_at throws exceptions for out-of-bounds access in ANSI mode
-- Note: element_at uses 1-based indexing

-- Config: spark.sql.ansi.enabled=true

-- ============================================================================
-- Test data setup
-- ============================================================================

statement
CREATE TABLE ansi_element_at_oob(arr array<int>) USING parquet

statement
INSERT INTO ansi_element_at_oob VALUES (array(1, 2, 3))

-- ============================================================================
-- element_at index out of bounds (positive index)
-- Spark throws: [INVALID_ARRAY_INDEX_IN_ELEMENT_AT] ...
-- Comet throws: Index out of bounds for array
-- See https://github.com/apache/datafusion-comet/issues/3375
-- ============================================================================

-- index beyond array length should throw (1-based indexing)
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(arr, 10) FROM ansi_element_at_oob

-- literal array with out of bounds access
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(array(1, 2, 3), 5)

-- ============================================================================
-- element_at with index 0 (invalid)
-- Spark throws: [INVALID_INDEX_OF_ZERO] The index 0 is invalid
-- Comet throws: different error message
-- See https://github.com/apache/datafusion-comet/issues/3375
-- ============================================================================

-- index 0 is not valid for element_at (1-based indexing)
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(arr, 0) FROM ansi_element_at_oob

-- literal with index 0
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(array(1, 2, 3), 0)

-- ============================================================================
-- element_at index out of bounds (negative index beyond array)
-- ============================================================================

-- negative index beyond array size should throw
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(arr, -10) FROM ansi_element_at_oob

-- literal with negative out of bounds
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT element_at(array(1, 2, 3), -5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ANSI mode array index access tests
-- Tests that array[index] throws exceptions for out-of-bounds access in ANSI mode

-- Config: spark.sql.ansi.enabled=true

-- ============================================================================
-- Test data setup
-- ============================================================================

statement
CREATE TABLE ansi_array_oob(arr array<int>) USING parquet

statement
INSERT INTO ansi_array_oob VALUES (array(1, 2, 3))

-- ============================================================================
-- Array index out of bounds (positive index)
-- Spark throws: [INVALID_ARRAY_INDEX] The index X is out of bounds
-- Comet throws: Index out of bounds for array
-- See https://github.com/apache/datafusion-comet/issues/3375
-- ============================================================================

-- index beyond array length should throw (0-based indexing)
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT arr[10] FROM ansi_array_oob

-- literal array with out of bounds access
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT array(1, 2, 3)[5]

-- ============================================================================
-- Array index out of bounds (negative index)
-- ============================================================================

-- negative index should throw
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT arr[-1] FROM ansi_array_oob

-- literal with negative index
query ignore(https://github.com/apache/datafusion-comet/issues/3375)
SELECT array(1, 2, 3)[-1]
97 changes: 97 additions & 0 deletions spark/src/test/resources/sql-tests/expressions/math/abs_ansi.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- ANSI mode abs function tests
-- Tests that abs throws exceptions for overflow on minimum integer values

-- Config: spark.sql.ansi.enabled=true

-- ============================================================================
-- Test data setup
-- ============================================================================

statement
CREATE TABLE ansi_test_abs_int(v int) USING parquet

statement
INSERT INTO ansi_test_abs_int VALUES (-2147483648)

statement
CREATE TABLE ansi_test_abs_long(v long) USING parquet

statement
INSERT INTO ansi_test_abs_long VALUES (-9223372036854775808)

statement
CREATE TABLE ansi_test_abs_short(v short) USING parquet

statement
INSERT INTO ansi_test_abs_short VALUES (-32768)

statement
CREATE TABLE ansi_test_abs_byte(v tinyint) USING parquet

statement
INSERT INTO ansi_test_abs_byte VALUES (-128)

-- ============================================================================
-- abs(INT_MIN) overflow
-- ============================================================================

-- abs(-2147483648) cannot be represented as int (since INT_MAX = 2147483647)
query expect_error(overflow)
SELECT abs(v) FROM ansi_test_abs_int

-- literal
query expect_error(overflow)
SELECT abs(-2147483648)

-- ============================================================================
-- abs(LONG_MIN) overflow
-- ============================================================================

-- abs(-9223372036854775808) cannot be represented as long
query expect_error(overflow)
SELECT abs(v) FROM ansi_test_abs_long

-- literal
query expect_error(overflow)
SELECT abs(-9223372036854775808L)

-- ============================================================================
-- abs(SHORT_MIN) overflow
-- ============================================================================

-- abs(-32768) cannot be represented as short
query expect_error(overflow)
SELECT abs(v) FROM ansi_test_abs_short

-- literal
query expect_error(overflow)
SELECT abs(cast(-32768 as short))

-- ============================================================================
-- abs(BYTE_MIN) overflow
-- ============================================================================

-- abs(-128) cannot be represented as tinyint
query expect_error(overflow)
SELECT abs(v) FROM ansi_test_abs_byte

-- literal
query expect_error(overflow)
SELECT abs(cast(-128 as tinyint))
Loading
Loading