|
| 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 | +suite("decimal_sum_multiplicity") { |
| 19 | + sql """ |
| 20 | + DROP TABLE IF EXISTS decimal_sum_l; |
| 21 | + DROP TABLE IF EXISTS decimal_sum_r; |
| 22 | + DROP TABLE IF EXISTS decimal_sum_out; |
| 23 | + CREATE TABLE decimal_sum_l (k VARCHAR(20) NOT NULL, v DECIMAL(38,18) NULL) |
| 24 | + DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 25 | + PROPERTIES ("replication_num"="1"); |
| 26 | + CREATE TABLE decimal_sum_r (k VARCHAR(20) NOT NULL, w BIGINT NULL) |
| 27 | + DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 28 | + PROPERTIES ("replication_num"="1"); |
| 29 | + CREATE TABLE decimal_sum_out (k VARCHAR(20), sv DECIMAL(38,18), sw BIGINT) |
| 30 | + DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 |
| 31 | + PROPERTIES ("replication_num"="1"); |
| 32 | + INSERT INTO decimal_sum_l VALUES |
| 33 | + ('x', 0.000000000100000000), ('x', 0.000000000100000000), |
| 34 | + ('negative', -1.123456789012345678), ('negative', NULL), |
| 35 | + ('null', NULL), ('null', NULL); |
| 36 | + INSERT INTO decimal_sum_r VALUES |
| 37 | + ('x', 1), ('x', 1), ('negative', 1), ('negative', 2), ('null', 3); |
| 38 | + """ |
| 39 | + sql "SET disable_join_reorder=true" |
| 40 | + sql "SET decimal_overflow_scale=6" |
| 41 | + for (def decimal256 : [false, true]) { |
| 42 | + sql "SET enable_decimal256=${decimal256}" |
| 43 | + for (def checkOverflow : [false, true]) { |
| 44 | + sql "SET check_overflow_for_decimal=${checkOverflow}" |
| 45 | + for (def mode : [-1, 1]) { |
| 46 | + sql "SET eager_aggregation_mode=${mode}" |
| 47 | + def suffix = "${decimal256}_${checkOverflow}_${mode == -1 ? 'off' : 'on'}" |
| 48 | + // The alias used to expose the stale output type; without it, scale could be lost silently. |
| 49 | + "order_qt_alias_${suffix}" """ |
| 50 | + SELECT l.k AS g, SUM(l.v) AS sv, SUM(r.w) AS sw |
| 51 | + FROM decimal_sum_l l JOIN [shuffle] decimal_sum_r r ON l.k=r.k GROUP BY l.k |
| 52 | + """ |
| 53 | + "order_qt_no_alias_${suffix}" """ |
| 54 | + SELECT l.k, SUM(l.v), SUM(r.w) |
| 55 | + FROM decimal_sum_l l JOIN [shuffle] decimal_sum_r r ON l.k=r.k GROUP BY l.k |
| 56 | + """ |
| 57 | + sql """ |
| 58 | + INSERT OVERWRITE TABLE decimal_sum_out |
| 59 | + SELECT l.k AS g, CAST(SUM(l.v) AS DECIMAL(38,18)), SUM(r.w) |
| 60 | + FROM decimal_sum_l l JOIN [shuffle] decimal_sum_r r ON l.k=r.k GROUP BY l.k |
| 61 | + """ |
| 62 | + "order_qt_insert_${suffix}" "SELECT * FROM decimal_sum_out" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + // The compensation products exceed DECIMAL(38,18), but fit Int128 and cancel in the final SUM. |
| 67 | + sql """INSERT INTO decimal_sum_l VALUES |
| 68 | + ('cancel_positive', 60000000000000000000.000000000000000000), |
| 69 | + ('cancel_negative', -60000000000000000000.000000000000000000)""" |
| 70 | + sql """INSERT INTO decimal_sum_r VALUES |
| 71 | + ('cancel_positive', 1), ('cancel_positive', 1), |
| 72 | + ('cancel_negative', 1), ('cancel_negative', 1)""" |
| 73 | + for (def decimal256 : [false, true]) { |
| 74 | + sql "SET enable_decimal256=${decimal256}" |
| 75 | + sql "SET check_overflow_for_decimal=true" |
| 76 | + for (def mode : [-1, 1]) { |
| 77 | + sql "SET eager_aggregation_mode=${mode}" |
| 78 | + "order_qt_cancellation_${decimal256}_${mode == -1 ? 'off' : 'on'}" """ |
| 79 | + SELECT SUBSTRING(l.k, 1, 6), SUM(l.v), SUM(r.w) |
| 80 | + FROM decimal_sum_l l JOIN [shuffle] decimal_sum_r r ON l.k=r.k |
| 81 | + WHERE l.k IN ('cancel_positive', 'cancel_negative') |
| 82 | + GROUP BY SUBSTRING(l.k, 1, 6) |
| 83 | + """ |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments