|
| 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 edge cases for Boolean -> Decimal. Comet has no native path for this cast; the |
| 19 | +-- `CodegenDispatchFallback` mixin runs Spark's own generated code inside the Comet pipeline, so |
| 20 | +-- the ANSI overflow errors must match Spark exactly. The non-error queries act as sentinels |
| 21 | +-- proving the cast really executed natively rather than falling the whole plan back to Spark. |
| 22 | + |
| 23 | +-- Config: spark.sql.ansi.enabled=true |
| 24 | + |
| 25 | +statement |
| 26 | +CREATE TABLE test_cast_bool_to_decimal_ansi(id int, b boolean) USING parquet |
| 27 | + |
| 28 | +statement |
| 29 | +INSERT INTO test_cast_bool_to_decimal_ansi VALUES (1, true), (2, false), (3, NULL) |
| 30 | + |
| 31 | +-- sentinel: a cast that always fits must run natively under ANSI mode |
| 32 | +query |
| 33 | +SELECT id, cast(b as decimal(10,2)), cast(b as decimal(38,0)) |
| 34 | +FROM test_cast_bool_to_decimal_ansi ORDER BY id |
| 35 | + |
| 36 | +-- sentinel: decimal(1,0) is the tightest type that can hold 1 |
| 37 | +query |
| 38 | +SELECT id, cast(b as decimal(1,0)), cast(b as decimal(2,1)) |
| 39 | +FROM test_cast_bool_to_decimal_ansi ORDER BY id |
| 40 | + |
| 41 | +-- decimal(1,1) holds at most 0.9, so casting true overflows and must throw under ANSI mode |
| 42 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 43 | +SELECT cast(b as decimal(1,1)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 |
| 44 | + |
| 45 | +-- decimal(2,2) holds at most 0.99: same overflow, larger precision |
| 46 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 47 | +SELECT cast(b as decimal(2,2)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 |
| 48 | + |
| 49 | +-- all-scale decimal at maximum precision still cannot represent 1 |
| 50 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 51 | +SELECT cast(b as decimal(38,38)) FROM test_cast_bool_to_decimal_ansi WHERE id = 1 |
| 52 | + |
| 53 | +-- literal true overflows the same way |
| 54 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 55 | +SELECT cast(true as decimal(1,1)) |
| 56 | + |
| 57 | +-- overflow is value dependent: false scales to 0, which fits every decimal type, so rows that |
| 58 | +-- only contain false must not throw |
| 59 | +query |
| 60 | +SELECT cast(b as decimal(1,1)), cast(b as decimal(38,38)) |
| 61 | +FROM test_cast_bool_to_decimal_ansi WHERE id = 2 |
| 62 | + |
| 63 | +-- NULL input short-circuits before the precision check, so it must not throw either |
| 64 | +query |
| 65 | +SELECT cast(b as decimal(1,1)), cast(b as decimal(38,38)) |
| 66 | +FROM test_cast_bool_to_decimal_ansi WHERE id = 3 |
| 67 | + |
| 68 | +-- try_cast suppresses the ANSI overflow and yields NULL |
| 69 | +query |
| 70 | +SELECT id, try_cast(b as decimal(1,1)), try_cast(b as decimal(38,38)) |
| 71 | +FROM test_cast_bool_to_decimal_ansi ORDER BY id |
| 72 | + |
| 73 | +-- overflow raised from inside an aggregate input |
| 74 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 75 | +SELECT sum(cast(b as decimal(1,1))) FROM test_cast_bool_to_decimal_ansi |
| 76 | + |
| 77 | +-- overflow raised from inside a filter predicate |
| 78 | +query expect_error(NUMERIC_VALUE_OUT_OF_RANGE) |
| 79 | +SELECT id FROM test_cast_bool_to_decimal_ansi WHERE cast(b as decimal(1,1)) > 0.5 |
0 commit comments