Skip to content

Commit

Permalink
Merge pull request #3366 from 4ertus2/decimal
Browse files Browse the repository at this point in the history
Conditional for UUID [CLICKHOUSE-4016]
  • Loading branch information
alexey-milovidov authored Oct 12, 2018
2 parents b1ceb4b + 688b5aa commit 8b4e584
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 4 deletions.
9 changes: 7 additions & 2 deletions dbms/src/DataTypes/NumberTraits.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <Core/Types.h>
#include <type_traits>

#include <Core/Types.h>
#include <Common/UInt128.h>


namespace DB
{
Expand Down Expand Up @@ -146,6 +148,7 @@ template <typename A> struct ResultOfBitNot
* UInt<x>, Int<y> -> Int<max(x*2, y)>
* Float<x>, [U]Int<y> -> Float<max(x, y*2)>
* Decimal<x>, Decimal<y> -> Decimal<max(x,y)>
* UUID, UUID -> UUID
* UInt64 , Int<x> -> Error
* Float<x>, [U]Int64 -> Error
*/
Expand All @@ -168,7 +171,9 @@ struct ResultOfIf
? max(sizeof(A), sizeof(B)) * 2
: max(sizeof(A), sizeof(B))>::Type;

using Type = std::conditional_t<!IsDecimalNumber<A> && !IsDecimalNumber<B>, ConstructedType,
using ConstructedWithUUID = std::conditional_t<std::is_same_v<A, UInt128> && std::is_same_v<B, UInt128>, A, ConstructedType>;

using Type = std::conditional_t<!IsDecimalNumber<A> && !IsDecimalNumber<B>, ConstructedWithUUID,
std::conditional_t<IsDecimalNumber<A> && IsDecimalNumber<B>, std::conditional_t<(sizeof(A) > sizeof(B)), A, B>, Error>>;
};

Expand Down
10 changes: 8 additions & 2 deletions dbms/src/Functions/FunctionsConditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ class FunctionIf : public FunctionIfBase</*null_is_false=*/false>
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIf>(); }

private:
template <typename T0, typename T1>
static constexpr bool allow_arrays =
!IsDecimalNumber<T0> && !IsDecimalNumber<T1> &&
!std::is_same_v<T0, UInt128> && !std::is_same_v<T1, UInt128>;

template <typename T0, typename T1>
static UInt32 decimalScale(Block & block [[maybe_unused]], const ColumnNumbers & arguments [[maybe_unused]])
{
Expand Down Expand Up @@ -314,7 +319,7 @@ class FunctionIf : public FunctionIfBase</*null_is_false=*/false>
{
if constexpr (std::is_same_v<NumberTraits::Error, typename NumberTraits::ResultOfIf<T0, T1>::Type>)
return false;
else if constexpr (!IsDecimalNumber<T0> && !IsDecimalNumber<T1>)
else if constexpr (allow_arrays<T0, T1>)
{
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;

Expand Down Expand Up @@ -370,7 +375,7 @@ class FunctionIf : public FunctionIfBase</*null_is_false=*/false>
{
if constexpr (std::is_same_v<NumberTraits::Error, typename NumberTraits::ResultOfIf<T0, T1>::Type>)
return false;
else if constexpr (!IsDecimalNumber<T0> && !IsDecimalNumber<T1>)
else if constexpr (allow_arrays<T0, T1>)
{
using ResultType = typename NumberTraits::ResultOfIf<T0, T1>::Type;

Expand Down Expand Up @@ -978,6 +983,7 @@ class FunctionIf : public FunctionIfBase</*null_is_false=*/false>
bool executed_with_nums = callOnBasicTypes<true, true, true, true>(left_id, right_id, call);

if (!( executed_with_nums
|| executeTyped<UInt128, UInt128>(cond_col, block, arguments, result, input_rows_count)
|| executeString(cond_col, block, arguments, result)
|| executeGenericArray(cond_col, block, arguments, result)
|| executeTuple(block, arguments, result, input_rows_count)))
Expand Down
148 changes: 148 additions & 0 deletions dbms/tests/queries/0_stateless/00735_conditional.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
value vs value
0 1 1 Int8 Int8 Int8
0 1 1 Int8 Int16 Int16
0 1 1 Int8 Int32 Int32
0 1 1 Int8 Int64 Int64
0 1 1 Int8 UInt8 Int16
0 1 1 Int8 UInt16 Int32
0 1 1 Int8 UInt32 Int64
0 1 1 Int8 Float32 Float32
0 1 1 Int8 Float64 Float64
0 1 1 Int16 Int8 Int16
0 1 1 Int16 Int16 Int16
0 1 1 Int16 Int32 Int32
0 1 1 Int16 Int64 Int64
0 1 1 Int16 UInt8 Int16
0 1 1 Int16 UInt16 Int32
0 1 1 Int16 UInt32 Int64
0 1 1 Int16 Float32 Float32
0 1 1 Int16 Float64 Float64
0 1 1 Int32 Int8 Int32
0 1 1 Int32 Int16 Int32
0 1 1 Int32 Int32 Int32
0 1 1 Int32 Int64 Int64
0 1 1 Int32 UInt8 Int32
0 1 1 Int32 UInt16 Int32
0 1 1 Int32 UInt32 Int64
0 1 1 Int32 Float32 Float64
0 1 1 Int32 Float64 Float64
0 1 1 Int64 Int8 Int64
0 1 1 Int64 Int16 Int64
0 1 1 Int64 Int32 Int64
0 1 1 Int64 Int64 Int64
0 1 1 Int64 UInt8 Int64
0 1 1 Int64 UInt16 Int64
0 1 1 Int64 UInt32 Int64
0 1 1 UInt8 Int8 Int16
0 1 1 UInt8 Int16 Int16
0 1 1 UInt8 Int32 Int32
0 1 1 UInt8 Int64 Int64
0 1 1 UInt8 UInt8 UInt8
0 1 1 UInt8 UInt16 UInt16
0 1 1 UInt8 UInt32 UInt32
0 1 1 UInt8 UInt64 UInt64
0 1 1 UInt8 Float32 Float32
0 1 1 UInt8 Float64 Float64
0 1 1 UInt16 Int8 Int32
0 1 1 UInt16 Int16 Int32
0 1 1 UInt16 Int32 Int32
0 1 1 UInt16 Int64 Int64
0 1 1 UInt16 UInt8 UInt16
0 1 1 UInt16 UInt16 UInt16
0 1 1 UInt16 UInt32 UInt32
0 1 1 UInt16 UInt64 UInt64
0 1 1 UInt16 Float32 Float32
0 1 1 UInt16 Float64 Float64
0 1 1 UInt32 Int8 Int64
0 1 1 UInt32 Int16 Int64
0 1 1 UInt32 Int32 Int64
0 1 1 UInt32 Int64 Int64
0 1 1 UInt32 UInt8 UInt32
0 1 1 UInt32 UInt16 UInt32
0 1 1 UInt32 UInt32 UInt32
0 1 1 UInt32 UInt64 UInt64
0 1 1 UInt32 Float32 Float64
0 1 1 UInt32 Float64 Float64
0 1 1 UInt64 UInt8 UInt64
0 1 1 UInt64 UInt16 UInt64
0 1 1 UInt64 UInt32 UInt64
0 1 1 UInt64 UInt64 UInt64
0000-00-00 1970-01-02 1970-01-02 Date Date Date
0000-00-00 1970-01-01 03:00:01 1970-01-01 03:00:01 Date DateTime DateTime
0000-00-00 00:00:00 1970-01-02 1970-01-01 03:00:01 DateTime Date DateTime
0000-00-00 00:00:00 1970-01-01 03:00:01 1970-01-01 03:00:01 DateTime DateTime DateTime
00000000-0000-0000-0000-000000000000 00000000-0000-0001-0000-000000000000 00000000-0000-0001-0000-000000000000 UUID UUID UUID
column vs value
0 1 1 Int8 Int8 Int8
0 1 1 Int8 Int16 Int16
0 1 1 Int8 Int32 Int32
0 1 1 Int8 Int64 Int64
0 1 1 Int8 UInt8 Int16
0 1 1 Int8 UInt16 Int32
0 1 1 Int8 UInt32 Int64
0 1 1 Int8 Float32 Float32
0 1 1 Int8 Float64 Float64
0 1 1 Int16 Int8 Int16
0 1 1 Int16 Int16 Int16
0 1 1 Int16 Int32 Int32
0 1 1 Int16 Int64 Int64
0 1 1 Int16 UInt8 Int16
0 1 1 Int16 UInt16 Int32
0 1 1 Int16 UInt32 Int64
0 1 1 Int16 Float32 Float32
0 1 1 Int16 Float64 Float64
0 1 1 Int32 Int8 Int32
0 1 1 Int32 Int16 Int32
0 1 1 Int32 Int32 Int32
0 1 1 Int32 Int64 Int64
0 1 1 Int32 UInt8 Int32
0 1 1 Int32 UInt16 Int32
0 1 1 Int32 UInt32 Int64
0 1 1 Int32 Float32 Float64
0 1 1 Int32 Float64 Float64
0 1 1 Int64 Int8 Int64
0 1 1 Int64 Int16 Int64
0 1 1 Int64 Int32 Int64
0 1 1 Int64 Int64 Int64
0 1 1 Int64 UInt8 Int64
0 1 1 Int64 UInt16 Int64
0 1 1 Int64 UInt32 Int64
0 1 1 UInt8 Int8 Int16
0 1 1 UInt8 Int16 Int16
0 1 1 UInt8 Int32 Int32
0 1 1 UInt8 Int64 Int64
0 1 1 UInt8 UInt8 UInt8
0 1 1 UInt8 UInt16 UInt16
0 1 1 UInt8 UInt32 UInt32
0 1 1 UInt8 UInt64 UInt64
0 1 1 UInt8 Float32 Float32
0 1 1 UInt8 Float64 Float64
0 1 1 UInt16 Int8 Int32
0 1 1 UInt16 Int16 Int32
0 1 1 UInt16 Int32 Int32
0 1 1 UInt16 Int64 Int64
0 1 1 UInt16 UInt8 UInt16
0 1 1 UInt16 UInt16 UInt16
0 1 1 UInt16 UInt32 UInt32
0 1 1 UInt16 UInt64 UInt64
0 1 1 UInt16 Float32 Float32
0 1 1 UInt16 Float64 Float64
0 1 1 UInt32 Int8 Int64
0 1 1 UInt32 Int16 Int64
0 1 1 UInt32 Int32 Int64
0 1 1 UInt32 Int64 Int64
0 1 1 UInt32 UInt8 UInt32
0 1 1 UInt32 UInt16 UInt32
0 1 1 UInt32 UInt32 UInt32
0 1 1 UInt32 UInt64 UInt64
0 1 1 UInt32 Float32 Float64
0 1 1 UInt32 Float64 Float64
0 1 1 UInt64 UInt8 UInt64
0 1 1 UInt64 UInt16 UInt64
0 1 1 UInt64 UInt32 UInt64
0 1 1 UInt64 UInt64 UInt64
0000-00-00 1970-01-02 1970-01-02 Date Date Date
0000-00-00 1970-01-01 03:00:01 1970-01-01 03:00:01 Date DateTime DateTime
0000-00-00 00:00:00 1970-01-02 1970-01-01 03:00:01 DateTime Date DateTime
0000-00-00 00:00:00 1970-01-01 03:00:01 1970-01-01 03:00:01 DateTime DateTime DateTime
00000000-0000-0000-0000-000000000000 00000000-0000-0001-0000-000000000000 00000000-0000-0001-0000-000000000000 UUID UUID UUID
Loading

0 comments on commit 8b4e584

Please sign in to comment.