Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiplication of Decimal and Float #18145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
c906b1c
added the initial solution
myrrc Dec 16, 2020
d7145a5
working
myrrc Dec 16, 2020
f143a37
simplified case to multiply only
myrrc Dec 16, 2020
02baadd
removed some casts
myrrc Dec 16, 2020
17d9960
style fix
myrrc Dec 16, 2020
9d5998f
added debug info
myrrc Dec 17, 2020
0d319c8
fix
myrrc Dec 17, 2020
49f3b71
another fix
myrrc Dec 17, 2020
fcacf5c
another try
myrrc Dec 17, 2020
737d4fc
debug fix
myrrc Dec 17, 2020
af8a72a
fix
myrrc Dec 17, 2020
fa6a1d3
fix
myrrc Dec 17, 2020
9f217c5
case fix
myrrc Dec 17, 2020
d24fc58
case fix 2
myrrc Dec 17, 2020
494e906
fixing first compile bug
myrrc Dec 17, 2020
1f67b0c
reverting if constexpr
myrrc Dec 17, 2020
ea2da76
fixed result data type init
myrrc Dec 17, 2020
3e94285
fix deleted constuctor
myrrc Dec 17, 2020
54887da
fix scale init
myrrc Dec 17, 2020
39c82fe
fixed result type
myrrc Dec 17, 2020
b428f8f
fixed scales
myrrc Dec 17, 2020
fd1d24c
fixed overflow check
myrrc Dec 17, 2020
c61af32
fixed const column branch
myrrc Dec 17, 2020
1c52842
simplified the condition
myrrc Dec 17, 2020
613f4e0
removed explicit types
myrrc Dec 17, 2020
31e3ca1
restored type signature
myrrc Dec 17, 2020
6eecef3
fixed result type trait
myrrc Dec 17, 2020
5974e78
added the conversion func
myrrc Dec 17, 2020
f93385d
replaced lambda with helper function
myrrc Dec 17, 2020
e50af17
fixed template args1
myrrc Dec 17, 2020
488f4ef
fixed compile condition
myrrc Dec 17, 2020
73f3195
fixed variable init
myrrc Dec 17, 2020
61d9d21
fixed another variable init
myrrc Dec 17, 2020
4f7d019
Squashed commit of the following:
myrrc Dec 17, 2020
b5e6b23
Merge branch 'improvement/multiply-decimals-and-floats' of github:myr…
myrrc Dec 17, 2020
205db4f
fixed the scale bug, added the test (wo reference)
myrrc Dec 17, 2020
7db7a9e
added another constexpr branch
myrrc Dec 17, 2020
bf5e836
updated the test
myrrc Dec 17, 2020
fe1b396
added test reference
myrrc Dec 17, 2020
40c702c
Update multiply.cpp
alexey-milovidov Dec 17, 2020
5a78eaf
Update FunctionBinaryArithmetic.h
alexey-milovidov Dec 17, 2020
0462340
Update FunctionBinaryArithmetic.h
alexey-milovidov Dec 17, 2020
43348bd
fixed the possible values case
myrrc Dec 17, 2020
a50e954
Merge branch 'improvement/multiply-decimals-and-floats' of github:myr…
myrrc Dec 17, 2020
6a9397a
Update FunctionBinaryArithmetic.h
alexey-milovidov Dec 17, 2020
05abfb0
Merge remote-tracking branch 'upstream/master' into improvement/multi…
myrrc Dec 18, 2020
5b6143b
fix style error
myrrc Dec 18, 2020
283268c
Merge branch 'improvement/multiply-decimals-and-floats' of github:myr…
myrrc Dec 18, 2020
d4f8c46
fixed the test
myrrc Dec 18, 2020
f34c050
enlarged the test, updated scales calc
myrrc Dec 18, 2020
84013d2
fix: ternary if
myrrc Dec 18, 2020
15f0915
replaced ternary if
myrrc Dec 18, 2020
c4ea410
updated the comments and branches
myrrc Dec 18, 2020
07bc3a2
reverted the overengineering
myrrc Dec 18, 2020
1f10514
another attempt
myrrc Dec 18, 2020
6c91b3d
added explicit scale calc
myrrc Dec 18, 2020
611d400
fixed compile error
myrrc Dec 18, 2020
764d462
updated test with possible right values
myrrc Dec 18, 2020
bc651b3
added the suboptimal solution
myrrc Dec 18, 2020
6488846
signature fix
myrrc Dec 18, 2020
fa1f7f4
another try
myrrc Dec 18, 2020
3c01cfe
no scale added
myrrc Dec 18, 2020
58ef4c3
prep for debug version
myrrc Dec 18, 2020
6b956a2
possible fix for non-const mult
myrrc Dec 18, 2020
7611f8d
compile fix
myrrc Dec 18, 2020
9d24caf
added vector mult branch
myrrc Dec 18, 2020
d22d045
fixed the impl
myrrc Dec 18, 2020
0dca856
solution fix
myrrc Dec 18, 2020
6f1c0b1
typo
myrrc Dec 18, 2020
ed6d74b
another bad solution
myrrc Dec 18, 2020
fbaa8d3
fix
myrrc Dec 18, 2020
a199348
fix2
myrrc Dec 18, 2020
e255665
fix3
myrrc Dec 18, 2020
985876b
another fix
myrrc Dec 18, 2020
0e235d3
aa
myrrc Dec 18, 2020
cb2ddc7
aMerge branch 'improvement/multiply-decimals-and-floats' of github:my…
myrrc Dec 18, 2020
61e189d
another fix
myrrc Dec 18, 2020
17331d2
another fix
myrrc Dec 18, 2020
2c8582a
Squashed commit of the following:
myrrc Dec 18, 2020
259ef9e
corrected the test
myrrc Dec 18, 2020
ad1aa59
Merge branch 'improvement/multiply-decimals-and-floats' of github:myr…
myrrc Dec 18, 2020
881ce40
rearranged some code parts, unified the impls::process method
myrrc Dec 21, 2020
2db721b
further rearrangement
myrrc Dec 21, 2020
57bb75b
finished rearranging the code
myrrc Dec 22, 2020
bb50329
fixed the test and the code
myrrc Dec 22, 2020
ba33b81
updated the tests
myrrc Dec 22, 2020
473aef5
reverted 1 scale to fix 3 tests
myrrc Dec 23, 2020
fec0a59
transfered scale factor calc for multiply from the impl
myrrc Dec 23, 2020
fcfe76d
trying to fix different types of *
myrrc Dec 23, 2020
0ab1982
fix the compile error
myrrc Dec 23, 2020
2516f84
fix initialization
myrrc Dec 23, 2020
4988e05
fixing the Decimal * Float aggregate case
myrrc Dec 23, 2020
631a2bc
reverted the tests to initial state
myrrc Dec 23, 2020
01b311c
over-scale fix
myrrc Dec 23, 2020
b48370d
fixed explicit type
myrrc Dec 23, 2020
4c4f727
replaced name shadowing in gcc
myrrc Dec 24, 2020
fe80f98
last fix add
myrrc Dec 24, 2020
8e5279c
fix gcc explicit namespace bug
myrrc Dec 24, 2020
e335ba2
fixing build on gcc
myrrc Dec 25, 2020
80b96f2
Merge remote-tracking branch 'upstream/master' into improvement/multi…
myrrc Dec 25, 2020
b086a78
Update FunctionBinaryArithmetic.h
alexey-milovidov Dec 27, 2020
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
1 change: 1 addition & 0 deletions src/Core/DecimalFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <common/arithmeticOverflow.h>

#include <limits>
#include <type_traits>


namespace DB
Expand Down
907 changes: 534 additions & 373 deletions src/Functions/FunctionBinaryArithmetic.h

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Functions/intDiv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ struct DivideIntegralByConstantImpl

}

namespace impl_
{
template <> struct BinaryOperationImpl<UInt64, UInt8, DivideIntegralImpl<UInt64, UInt8>> : DivideIntegralByConstantImpl<UInt64, UInt8> {};
template <> struct BinaryOperationImpl<UInt64, UInt16, DivideIntegralImpl<UInt64, UInt16>> : DivideIntegralByConstantImpl<UInt64, UInt16> {};
template <> struct BinaryOperationImpl<UInt64, UInt32, DivideIntegralImpl<UInt64, UInt32>> : DivideIntegralByConstantImpl<UInt64, UInt32> {};
Expand All @@ -107,7 +109,7 @@ template <> struct BinaryOperationImpl<Int32, Int8, DivideIntegralImpl<Int32, In
template <> struct BinaryOperationImpl<Int32, Int16, DivideIntegralImpl<Int32, Int16>> : DivideIntegralByConstantImpl<Int32, Int16> {};
template <> struct BinaryOperationImpl<Int32, Int32, DivideIntegralImpl<Int32, Int32>> : DivideIntegralByConstantImpl<Int32, Int32> {};
template <> struct BinaryOperationImpl<Int32, Int64, DivideIntegralImpl<Int32, Int64>> : DivideIntegralByConstantImpl<Int32, Int64> {};

}

struct NameIntDiv { static constexpr auto name = "intDiv"; };
using FunctionIntDiv = BinaryArithmeticOverloadResolver<DivideIntegralImpl, NameIntDiv, false>;
Expand Down
4 changes: 3 additions & 1 deletion src/Functions/modulo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct ModuloByConstantImpl
* Can be expanded to all possible combinations, but more code is needed.
*/

namespace impl_
{
template <> struct BinaryOperationImpl<UInt64, UInt8, ModuloImpl<UInt64, UInt8>> : ModuloByConstantImpl<UInt64, UInt8> {};
template <> struct BinaryOperationImpl<UInt64, UInt16, ModuloImpl<UInt64, UInt16>> : ModuloByConstantImpl<UInt64, UInt16> {};
template <> struct BinaryOperationImpl<UInt64, UInt32, ModuloImpl<UInt64, UInt32>> : ModuloByConstantImpl<UInt64, UInt32> {};
Expand All @@ -98,7 +100,7 @@ template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : M
template <> struct BinaryOperationImpl<Int32, Int16, ModuloImpl<Int32, Int16>> : ModuloByConstantImpl<Int32, Int16> {};
template <> struct BinaryOperationImpl<Int32, Int32, ModuloImpl<Int32, Int32>> : ModuloByConstantImpl<Int32, Int32> {};
template <> struct BinaryOperationImpl<Int32, Int64, ModuloImpl<Int32, Int64>> : ModuloByConstantImpl<Int32, Int64> {};

}

struct NameModulo { static constexpr auto name = "modulo"; };
using FunctionModulo = BinaryArithmeticOverloadResolver<ModuloImpl, NameModulo, false>;
Expand Down
11 changes: 9 additions & 2 deletions src/Functions/multiply.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <type_traits>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionBinaryArithmetic.h>
#include <common/arithmeticOverflow.h>
Expand Down Expand Up @@ -25,11 +26,17 @@ struct MultiplyImpl
return static_cast<Result>(a) * b;
}

/// Apply operation and check overflow. It's used for Deciamal operations. @returns true if overflowed, false otherwise.
/// Apply operation and check overflow. It's used for Decimal operations. @returns true if overflowed, false otherwise.
template <typename Result = ResultType>
static inline bool apply(A a, B b, Result & c)
{
return common::mulOverflow(static_cast<Result>(a), b, c);
if constexpr (std::is_same_v<Result, float> || std::is_same_v<Result, double>)
{
c = static_cast<Result>(a) * b;
return false;
}
else
return common::mulOverflow(static_cast<Result>(a), b, c);
}

#if USE_EMBEDDED_COMPILER
Expand Down
14 changes: 14 additions & 0 deletions tests/queries/0_stateless/01603_decimal_mult_float.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2.4
10.165
0.00012000000000000002
150.16500000000002
7.775900000000001
56.62269
598.8376688440277
299.41883723437786
0.7485470860550345
2.245641373854596
1.641386318314034
1.641386318314034
1.641386334333447
1.641386334333447
27 changes: 27 additions & 0 deletions tests/queries/0_stateless/01603_decimal_mult_float.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SELECT toDecimal32(2, 2) * 1.2;
SELECT toDecimal64(0.5, 2) * 20.33;
SELECT 0.00001 * toDecimal32(12, 2);
SELECT 30.033 * toDecimal32(5, 1);

CREATE TABLE IF NOT EXISTS test01603 (
f64 Float64,
d Decimal64(3) DEFAULT toDecimal32(f64, 3),
f32 Float32 DEFAULT f64
) ENGINE=MergeTree() ORDER BY f32;

INSERT INTO test01603(f64) SELECT 1 / (number + 1) FROM system.numbers LIMIT 1000;

SELECT sum(d * 1.1) FROM test01603;
SELECT sum(8.01 * d) FROM test01603;

SELECT sum(f64 * toDecimal64(80, 2)) FROM test01603;
SELECT sum(toDecimal64(40, 2) * f32) FROM test01603;
SELECT sum(f64 * toDecimal64(0.1, 2)) FROM test01603;
SELECT sum(toDecimal64(0.3, 2) * f32) FROM test01603;

SELECT sum(f64 * d) FROM test01603;
SELECT sum(d * f64) FROM test01603;
SELECT sum(f32 * d) FROM test01603;
SELECT sum(d * f32) FROM test01603;

DROP TABLE IF EXISTS test01603;