From bb20733120e7fe092cf68db28d15a6fdba89a0d5 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 13 Dec 2022 19:13:37 +0100 Subject: [PATCH] Fix documentation after #42438 --- src/Functions/FunctionsDecimalArithmetics.cpp | 17 --- src/Functions/FunctionsDecimalArithmetics.h | 90 +----------- src/Functions/divideDecimal.cpp | 120 ++++++++++++++++ src/Functions/multiplyDecimal.cpp | 129 ++++++++++++++++++ 4 files changed, 252 insertions(+), 104 deletions(-) delete mode 100644 src/Functions/FunctionsDecimalArithmetics.cpp create mode 100644 src/Functions/divideDecimal.cpp create mode 100644 src/Functions/multiplyDecimal.cpp diff --git a/src/Functions/FunctionsDecimalArithmetics.cpp b/src/Functions/FunctionsDecimalArithmetics.cpp deleted file mode 100644 index f275f169914..00000000000 --- a/src/Functions/FunctionsDecimalArithmetics.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include - -namespace DB -{ -REGISTER_FUNCTION(DivideDecimals) -{ - factory.registerFunction>(Documentation( - "Decimal division with given precision. Slower than simple `divide`, but has controlled precision and no sound overflows")); -} - -REGISTER_FUNCTION(MultiplyDecimals) -{ - factory.registerFunction>(Documentation( - "Decimal multiplication with given precision. Slower than simple `divide`, but has controlled precision and no sound overflows")); -} -} diff --git a/src/Functions/FunctionsDecimalArithmetics.h b/src/Functions/FunctionsDecimalArithmetics.h index 9806d13ed30..51cb50690f6 100644 --- a/src/Functions/FunctionsDecimalArithmetics.h +++ b/src/Functions/FunctionsDecimalArithmetics.h @@ -1,4 +1,5 @@ #pragma once + #include #include @@ -140,91 +141,6 @@ struct DecimalOpHelpers }; -struct DivideDecimalsImpl -{ - static constexpr auto name = "divideDecimal"; - - template - static inline Decimal256 - execute(FirstType a, SecondType b, UInt16 scale_a, UInt16 scale_b, UInt16 result_scale) - { - if (b.value == 0) - throw DB::Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION); - if (a.value == 0) - return Decimal256(0); - - Int256 sign_a = a.value < 0 ? -1 : 1; - Int256 sign_b = b.value < 0 ? -1 : 1; - - std::vector a_digits = DecimalOpHelpers::toDigits(a.value * sign_a); - - while (scale_a < scale_b + result_scale) - { - a_digits.push_back(0); - ++scale_a; - } - - while (scale_a > scale_b + result_scale && !a_digits.empty()) - { - a_digits.pop_back(); - --scale_a; - } - - if (a_digits.empty()) - return Decimal256(0); - - std::vector divided = DecimalOpHelpers::divide(a_digits, b.value * sign_b); - - if (divided.size() > DecimalUtils::max_precision) - throw DB::Exception("Numeric overflow: result bigger that Decimal256", ErrorCodes::DECIMAL_OVERFLOW); - return Decimal256(sign_a * sign_b * DecimalOpHelpers::fromDigits(divided)); - } -}; - - -struct MultiplyDecimalsImpl -{ - static constexpr auto name = "multiplyDecimal"; - - template - static inline Decimal256 - execute(FirstType a, SecondType b, UInt16 scale_a, UInt16 scale_b, UInt16 result_scale) - { - if (a.value == 0 || b.value == 0) - return Decimal256(0); - - Int256 sign_a = a.value < 0 ? -1 : 1; - Int256 sign_b = b.value < 0 ? -1 : 1; - - std::vector a_digits = DecimalOpHelpers::toDigits(a.value * sign_a); - std::vector b_digits = DecimalOpHelpers::toDigits(b.value * sign_b); - - std::vector multiplied = DecimalOpHelpers::multiply(a_digits, b_digits); - - UInt16 product_scale = scale_a + scale_b; - while (product_scale < result_scale) - { - multiplied.push_back(0); - ++product_scale; - } - - while (product_scale > result_scale&& !multiplied.empty()) - { - multiplied.pop_back(); - --product_scale; - } - - if (multiplied.empty()) - return Decimal256(0); - - if (multiplied.size() > DecimalUtils::max_precision) - throw DB::Exception("Numeric overflow: result bigger that Decimal256", ErrorCodes::DECIMAL_OVERFLOW); - - return Decimal256(sign_a * sign_b * DecimalOpHelpers::fromDigits(multiplied)); - } -}; - - template struct Processor { @@ -388,11 +304,12 @@ public: } private: - //long resolver to call proper templated func + // long resolver to call proper templated func ColumnPtr resolveOverload(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type) const { WhichDataType which_dividend(arguments[0].type.get()); WhichDataType which_divisor(arguments[1].type.get()); + if (which_dividend.isDecimal32()) { using DividendType = DataTypeDecimal32; @@ -454,4 +371,3 @@ private: }; } - diff --git a/src/Functions/divideDecimal.cpp b/src/Functions/divideDecimal.cpp new file mode 100644 index 00000000000..2c82f9d3dcf --- /dev/null +++ b/src/Functions/divideDecimal.cpp @@ -0,0 +1,120 @@ +#include +#include + +namespace DB +{ + +namespace +{ + +struct DivideDecimalsImpl +{ + static constexpr auto name = "divideDecimal"; + + template + static inline Decimal256 + execute(FirstType a, SecondType b, UInt16 scale_a, UInt16 scale_b, UInt16 result_scale) + { + if (b.value == 0) + throw DB::Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION); + if (a.value == 0) + return Decimal256(0); + + Int256 sign_a = a.value < 0 ? -1 : 1; + Int256 sign_b = b.value < 0 ? -1 : 1; + + std::vector a_digits = DecimalOpHelpers::toDigits(a.value * sign_a); + + while (scale_a < scale_b + result_scale) + { + a_digits.push_back(0); + ++scale_a; + } + + while (scale_a > scale_b + result_scale && !a_digits.empty()) + { + a_digits.pop_back(); + --scale_a; + } + + if (a_digits.empty()) + return Decimal256(0); + + std::vector divided = DecimalOpHelpers::divide(a_digits, b.value * sign_b); + + if (divided.size() > DecimalUtils::max_precision) + throw DB::Exception("Numeric overflow: result bigger that Decimal256", ErrorCodes::DECIMAL_OVERFLOW); + return Decimal256(sign_a * sign_b * DecimalOpHelpers::fromDigits(divided)); + } +}; + +} + +REGISTER_FUNCTION(DivideDecimals) +{ + factory.registerFunction>(Documentation( + R"( +Performs division on two decimals. Result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md). +Result scale can be explicitly specified by `result_scale` argument (const Integer in range `[0, 76]`). If not specified, the result scale is the max scale of given arguments. + +:::note +These function work significantly slower than usual `divide`. +In case you don't really need controlled precision and/or need fast computation, consider using [divide](#divide). +::: + +**Syntax** + +```sql +divideDecimal(a, b[, result_scale]) +``` + +**Arguments** + +- `a` — First value: [Decimal](../../sql-reference/data-types/decimal.md). +- `b` — Second value: [Decimal](../../sql-reference/data-types/decimal.md). +- `result_scale` — Scale of result: [Int/UInt](../../sql-reference/data-types/int-uint.md). + +**Returned value** + +- The result of division with given scale. + +Type: [Decimal256](../../sql-reference/data-types/decimal.md). + +**Example** + +```text +┌─divideDecimal(toDecimal256(-12, 0), toDecimal32(2.1, 1), 10)─┐ +│ -5.7142857142 │ +└──────────────────────────────────────────────────────────────┘ +``` + +**Difference from regular division:** +```sql +SELECT toDecimal64(-12, 1) / toDecimal32(2.1, 1); +SELECT toDecimal64(-12, 1) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5); +``` + +```text +┌─divide(toDecimal64(-12, 1), toDecimal32(2.1, 1))─┐ +│ -5.7 │ +└──────────────────────────────────────────────────┘ +┌───a─┬───b─┬─divideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 1)─┬─divideDecimal(toDecimal64(-12, 1), toDecimal32(2.1, 1), 5)─┐ +│ -12 │ 2.1 │ -5.7 │ -5.71428 │ +└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘ +``` + +```sql +SELECT toDecimal64(-12, 0) / toDecimal32(2.1, 1); +SELECT toDecimal64(-12, 0) as a, toDecimal32(2.1, 1) as b, divideDecimal(a, b, 1), divideDecimal(a, b, 5); +``` + +```text +DB::Exception: Decimal result's scale is less than argument's one: While processing toDecimal64(-12, 0) / toDecimal32(2.1, 1). (ARGUMENT_OUT_OF_BOUND) +┌───a─┬───b─┬─divideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 1)─┬─divideDecimal(toDecimal64(-12, 0), toDecimal32(2.1, 1), 5)─┐ +│ -12 │ 2.1 │ -5.7 │ -5.71428 │ +└─────┴─────┴────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────┘ +``` +)")); +} + +} diff --git a/src/Functions/multiplyDecimal.cpp b/src/Functions/multiplyDecimal.cpp new file mode 100644 index 00000000000..1e7e4f53784 --- /dev/null +++ b/src/Functions/multiplyDecimal.cpp @@ -0,0 +1,129 @@ +#include +#include + +namespace DB +{ + +namespace +{ + +struct MultiplyDecimalsImpl +{ + static constexpr auto name = "multiplyDecimal"; + + template + static inline Decimal256 + execute(FirstType a, SecondType b, UInt16 scale_a, UInt16 scale_b, UInt16 result_scale) + { + if (a.value == 0 || b.value == 0) + return Decimal256(0); + + Int256 sign_a = a.value < 0 ? -1 : 1; + Int256 sign_b = b.value < 0 ? -1 : 1; + + std::vector a_digits = DecimalOpHelpers::toDigits(a.value * sign_a); + std::vector b_digits = DecimalOpHelpers::toDigits(b.value * sign_b); + + std::vector multiplied = DecimalOpHelpers::multiply(a_digits, b_digits); + + UInt16 product_scale = scale_a + scale_b; + while (product_scale < result_scale) + { + multiplied.push_back(0); + ++product_scale; + } + + while (product_scale > result_scale&& !multiplied.empty()) + { + multiplied.pop_back(); + --product_scale; + } + + if (multiplied.empty()) + return Decimal256(0); + + if (multiplied.size() > DecimalUtils::max_precision) + throw DB::Exception("Numeric overflow: result bigger that Decimal256", ErrorCodes::DECIMAL_OVERFLOW); + + return Decimal256(sign_a * sign_b * DecimalOpHelpers::fromDigits(multiplied)); + } +}; + +} + +REGISTER_FUNCTION(MultiplyDecimals) +{ + factory.registerFunction>(Documentation( + R"( +Performs multiplication on two decimals. Result value will be of type [Decimal256](../../sql-reference/data-types/decimal.md). +Result scale can be explicitly specified by `result_scale` argument (const Integer in range `[0, 76]`). If not specified, the result scale is the max scale of given arguments. + +:::note +These functions work significantly slower than usual `multiply`. +In case you don't really need controlled precision and/or need fast computation, consider using [multiply](#multiply) +::: + +**Syntax** + +```sql +multiplyDecimal(a, b[, result_scale]) +``` + +**Arguments** + +- `a` — First value: [Decimal](../../sql-reference/data-types/decimal.md). +- `b` — Second value: [Decimal](../../sql-reference/data-types/decimal.md). +- `result_scale` — Scale of result: [Int/UInt](../../sql-reference/data-types/int-uint.md). + +**Returned value** + +- The result of multiplication with given scale. + +Type: [Decimal256](../../sql-reference/data-types/decimal.md). + +**Example** + +```text +┌─multiplyDecimal(toDecimal256(-12, 0), toDecimal32(-2.1, 1), 1)─┐ +│ 25.2 │ +└────────────────────────────────────────────────────────────────┘ +``` + +**Difference from regular multiplication:** +```sql +SELECT toDecimal64(-12.647, 3) * toDecimal32(2.1239, 4); +SELECT toDecimal64(-12.647, 3) as a, toDecimal32(2.1239, 4) as b, multiplyDecimal(a, b); +``` + +```text +┌─multiply(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐ +│ -26.8609633 │ +└───────────────────────────────────────────────────────────┘ +┌─multiplyDecimal(toDecimal64(-12.647, 3), toDecimal32(2.1239, 4))─┐ +│ -26.8609 │ +└──────────────────────────────────────────────────────────────────┘ +``` + +```sql +SELECT + toDecimal64(-12.647987876, 9) AS a, + toDecimal64(123.967645643, 9) AS b, + multiplyDecimal(a, b); +SELECT + toDecimal64(-12.647987876, 9) AS a, + toDecimal64(123.967645643, 9) AS b, + a * b; +``` + +```text +┌─────────────a─┬─────────────b─┬─multiplyDecimal(toDecimal64(-12.647987876, 9), toDecimal64(123.967645643, 9))─┐ +│ -12.647987876 │ 123.967645643 │ -1567.941279108 │ +└───────────────┴───────────────┴───────────────────────────────────────────────────────────────────────────────┘ +Received exception from server (version 22.11.1): +Code: 407. DB::Exception: Received from localhost:9000. DB::Exception: Decimal math overflow: While processing toDecimal64(-12.647987876, 9) AS a, toDecimal64(123.967645643, 9) AS b, a * b. (DECIMAL_OVERFLOW) +``` +)")); + +} + +}