|
| 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 | +-- Decimal division (/) and integral division (div) in legacy (non-ANSI) mode. |
| 19 | +-- Also covers try_divide (TRY mode semantics). |
| 20 | +-- See decimal_div_ansi.sql for ANSI mode tests. |
| 21 | + |
| 22 | +-- ConfigMatrix: parquet.enable.dictionary=false,true |
| 23 | + |
| 24 | +-- ============================================================================ |
| 25 | +-- Basic divide and integral divide |
| 26 | +-- ============================================================================ |
| 27 | + |
| 28 | +statement |
| 29 | +CREATE TABLE test_decimal_div(a decimal(18,6), b decimal(18,6)) USING parquet |
| 30 | + |
| 31 | +statement |
| 32 | +INSERT INTO test_decimal_div VALUES |
| 33 | + (10.000000, 3.000000), |
| 34 | + (7.500000, 2.500000), |
| 35 | + (-10.000000, 3.000000), |
| 36 | + (10.000000, -3.000000), |
| 37 | + (-10.000000,-3.000000), |
| 38 | + (0.000000, 5.000000), |
| 39 | + (1.000000, 3.000000), |
| 40 | + (NULL, 3.000000), |
| 41 | + (10.000000, NULL) |
| 42 | + |
| 43 | +-- column / column |
| 44 | +query |
| 45 | +SELECT a / b FROM test_decimal_div |
| 46 | + |
| 47 | +-- column div column (integral division: drops fractional part) |
| 48 | +query |
| 49 | +SELECT a div b FROM test_decimal_div |
| 50 | + |
| 51 | +-- ============================================================================ |
| 52 | +-- Divide by zero: legacy mode returns NULL, not an error |
| 53 | +-- ============================================================================ |
| 54 | + |
| 55 | +statement |
| 56 | +CREATE TABLE test_decimal_div_zero(a decimal(18,6)) USING parquet |
| 57 | + |
| 58 | +statement |
| 59 | +INSERT INTO test_decimal_div_zero VALUES |
| 60 | + (10.000000), |
| 61 | + (-5.000000), |
| 62 | + (0.000000), |
| 63 | + (NULL) |
| 64 | + |
| 65 | +-- a / 0 returns null in legacy mode |
| 66 | +query |
| 67 | +SELECT a / cast(0 as decimal(18,6)) FROM test_decimal_div_zero |
| 68 | + |
| 69 | +-- a div 0 returns null in legacy mode |
| 70 | +query |
| 71 | +SELECT a div cast(0 as decimal(18,6)) FROM test_decimal_div_zero |
| 72 | + |
| 73 | +-- ============================================================================ |
| 74 | +-- TRY mode: try_divide always returns NULL on zero divisor |
| 75 | +-- ============================================================================ |
| 76 | + |
| 77 | +-- try_divide by zero returns null even when ANSI mode is on externally |
| 78 | +query |
| 79 | +SELECT try_divide(a, cast(0.000000 as decimal(18,6))) FROM test_decimal_div_zero |
| 80 | + |
| 81 | +-- try_divide with non-zero divisor returns the quotient |
| 82 | +query |
| 83 | +SELECT try_divide(a, b) FROM test_decimal_div |
| 84 | + |
| 85 | +-- try_divide literal decimal by zero |
| 86 | +query |
| 87 | +SELECT try_divide(cast(10.5 as decimal(10,2)), cast(0.0 as decimal(10,2))) |
| 88 | + |
| 89 | +-- try_divide with NULL inputs |
| 90 | +query |
| 91 | +SELECT try_divide(NULL, cast(3.0 as decimal(10,2))), |
| 92 | + try_divide(cast(10.0 as decimal(10,2)), NULL) |
| 93 | + |
| 94 | +-- ============================================================================ |
| 95 | +-- Overflow in legacy mode produces NULL |
| 96 | +-- ============================================================================ |
| 97 | + |
| 98 | +statement |
| 99 | +CREATE TABLE test_decimal_overflow(a decimal(38,0)) USING parquet |
| 100 | + |
| 101 | +statement |
| 102 | +INSERT INTO test_decimal_overflow VALUES |
| 103 | + (99999999999999999999999999999999999999), |
| 104 | + (-99999999999999999999999999999999999999) |
| 105 | + |
| 106 | +-- Dividing max decimal(38,0) by a small fraction overflows the result type -> null |
| 107 | +query |
| 108 | +SELECT a / cast(0.01 as decimal(10,2)) FROM test_decimal_overflow |
| 109 | + |
| 110 | +-- Integral divide overflow -> null |
| 111 | +query |
| 112 | +SELECT a div cast(0.40 as decimal(2,2)) FROM test_decimal_overflow |
| 113 | + |
| 114 | +-- ============================================================================ |
| 115 | +-- Literal arguments (constant folding is disabled by the test runner) |
| 116 | +-- ============================================================================ |
| 117 | + |
| 118 | +-- literal / literal |
| 119 | +query |
| 120 | +SELECT cast(10.5 as decimal(10,2)) / cast(3.5 as decimal(10,2)) |
| 121 | + |
| 122 | +-- literal div literal |
| 123 | +query |
| 124 | +SELECT cast(10 as decimal(10,0)) div cast(3 as decimal(10,0)) |
| 125 | + |
| 126 | +-- literal / zero |
| 127 | +query |
| 128 | +SELECT cast(7.0 as decimal(10,2)) / cast(0.0 as decimal(10,2)) |
| 129 | + |
| 130 | +-- literal div zero |
| 131 | +query |
| 132 | +SELECT cast(7 as decimal(10,0)) div cast(0 as decimal(10,0)) |
| 133 | + |
| 134 | +-- NULL literal |
| 135 | +query |
| 136 | +SELECT cast(NULL as decimal(10,2)) / cast(3.0 as decimal(10,2)), |
| 137 | + cast(5.0 as decimal(10,2)) / cast(NULL as decimal(10,2)) |
| 138 | + |
| 139 | +-- ============================================================================ |
| 140 | +-- Mixed precision and scale |
| 141 | +-- ============================================================================ |
| 142 | + |
| 143 | +statement |
| 144 | +CREATE TABLE test_decimal_mixed(a decimal(18,6), b decimal(10,2)) USING parquet |
| 145 | + |
| 146 | +statement |
| 147 | +INSERT INTO test_decimal_mixed VALUES |
| 148 | + (123456.789012, 99.99), |
| 149 | + (0.000001, 1.00), |
| 150 | + (-123456.789012, 0.01), |
| 151 | + (NULL, NULL) |
| 152 | + |
| 153 | +-- spark_answer_only: mixed-precision division can trigger the precision-loss path in Spark's |
| 154 | +-- decimal arithmetic which Comet does not yet handle natively; see |
| 155 | +-- https://github.com/apache/datafusion-comet/issues/1526 |
| 156 | +query spark_answer_only |
| 157 | +SELECT a / b FROM test_decimal_mixed |
| 158 | + |
| 159 | +query |
| 160 | +SELECT a div b FROM test_decimal_mixed |
| 161 | + |
| 162 | +-- ============================================================================ |
| 163 | +-- Various precision/scale combinations |
| 164 | +-- ============================================================================ |
| 165 | + |
| 166 | +statement |
| 167 | +CREATE TABLE test_decimal_prec( |
| 168 | + a5_2 decimal(5,2), |
| 169 | + b5_2 decimal(5,2), |
| 170 | + a38_4 decimal(38,4), |
| 171 | + b38_4 decimal(38,4) |
| 172 | +) USING parquet |
| 173 | + |
| 174 | +statement |
| 175 | +INSERT INTO test_decimal_prec VALUES |
| 176 | + (10.50, 3.25, 9999999999999999.1234, 3.0001), |
| 177 | + (-10.50, 3.25, -9999999999999999.1234, 3.0001), |
| 178 | + (0.00, 1.00, 0.0000, 1.0000), |
| 179 | + (NULL, NULL, NULL, NULL) |
| 180 | + |
| 181 | +query |
| 182 | +SELECT a5_2 / b5_2 FROM test_decimal_prec |
| 183 | + |
| 184 | +query |
| 185 | +SELECT a5_2 div b5_2 FROM test_decimal_prec |
| 186 | + |
| 187 | +-- spark_answer_only: decimal(38,4) / decimal(38,4) produces a result type that exceeds |
| 188 | +-- Decimal(38,x) precision, triggering a precision-loss path Comet does not yet handle natively; |
| 189 | +-- see https://github.com/apache/datafusion-comet/issues/1526 |
| 190 | +query spark_answer_only |
| 191 | +SELECT a38_4 / b38_4 FROM test_decimal_prec |
| 192 | + |
| 193 | +query |
| 194 | +SELECT a38_4 div b38_4 FROM test_decimal_prec |
0 commit comments