2015-10-12 07:05:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Field.h>
|
2017-05-24 20:25:36 +00:00
|
|
|
#include <Core/AccurateComparison.h>
|
2018-04-09 13:52:39 +00:00
|
|
|
#include <common/demangle.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
|
|
|
|
class SipHash;
|
2015-10-12 07:05:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int CANNOT_CONVERT_TYPE;
|
2018-08-28 18:36:41 +00:00
|
|
|
extern const int BAD_TYPE_OF_FIELD;
|
2017-09-18 15:41:07 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wredundant-decls"
|
|
|
|
// Just dont mess with it. If the redundant redeclaration is removed then ReaderHelpers.h should be included.
|
|
|
|
// This leads to Arena.h inclusion which has a problem with ASAN stuff included properly and messing macro definition
|
|
|
|
// which intefrers with... You dont want to know, really.
|
|
|
|
UInt128 stringToUUID(const String & str);
|
|
|
|
#pragma GCC diagnostic pop
|
2018-08-28 18:36:41 +00:00
|
|
|
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** StaticVisitor (and its descendants) - class with overloaded operator() for all types of fields.
|
|
|
|
* You could call visitor for field using function 'applyVisitor'.
|
|
|
|
* Also "binary visitor" is supported - its operator() takes two arguments.
|
2015-10-12 07:05:54 +00:00
|
|
|
*/
|
|
|
|
template <typename R = void>
|
|
|
|
struct StaticVisitor
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = R;
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/// F is template parameter, to allow universal reference for field, that is useful for const and non-const values.
|
2015-10-12 07:05:54 +00:00
|
|
|
template <typename Visitor, typename F>
|
2019-12-10 13:40:45 +00:00
|
|
|
auto applyVisitor(Visitor && visitor, F && field)
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
2019-12-10 13:40:45 +00:00
|
|
|
return Field::dispatch(visitor, field);
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Visitor, typename F1, typename F2>
|
2019-12-10 13:40:45 +00:00
|
|
|
auto applyVisitor(Visitor && visitor, F1 && field1, F2 && field2)
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
2019-12-10 13:40:45 +00:00
|
|
|
return Field::dispatch([&](auto & field1_value)
|
|
|
|
{
|
|
|
|
return Field::dispatch([&](auto & field2_value)
|
|
|
|
{
|
|
|
|
return visitor(field1_value, field2_value);
|
|
|
|
},
|
|
|
|
field2);
|
|
|
|
},
|
|
|
|
field1);
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** Prints Field as literal in SQL query */
|
|
|
|
class FieldVisitorToString : public StaticVisitor<String>
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-12 20:41:50 +00:00
|
|
|
String operator() (const Null & x) const;
|
|
|
|
String operator() (const UInt64 & x) const;
|
2018-07-09 21:25:56 +00:00
|
|
|
String operator() (const UInt128 & x) const;
|
2017-05-12 20:41:50 +00:00
|
|
|
String operator() (const Int64 & x) const;
|
|
|
|
String operator() (const Float64 & x) const;
|
|
|
|
String operator() (const String & x) const;
|
|
|
|
String operator() (const Array & x) const;
|
|
|
|
String operator() (const Tuple & x) const;
|
2018-08-23 19:11:31 +00:00
|
|
|
String operator() (const DecimalField<Decimal32> & x) const;
|
|
|
|
String operator() (const DecimalField<Decimal64> & x) const;
|
|
|
|
String operator() (const DecimalField<Decimal128> & x) const;
|
2019-02-11 11:19:56 +00:00
|
|
|
String operator() (const AggregateFunctionStateData & x) const;
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** Print readable and unique text dump of field type and value. */
|
|
|
|
class FieldVisitorDump : public StaticVisitor<String>
|
2015-10-12 07:05:54 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-12 20:41:50 +00:00
|
|
|
String operator() (const Null & x) const;
|
|
|
|
String operator() (const UInt64 & x) const;
|
2018-07-09 21:25:56 +00:00
|
|
|
String operator() (const UInt128 & x) const;
|
2017-05-12 20:41:50 +00:00
|
|
|
String operator() (const Int64 & x) const;
|
|
|
|
String operator() (const Float64 & x) const;
|
|
|
|
String operator() (const String & x) const;
|
|
|
|
String operator() (const Array & x) const;
|
|
|
|
String operator() (const Tuple & x) const;
|
2018-08-23 19:11:31 +00:00
|
|
|
String operator() (const DecimalField<Decimal32> & x) const;
|
|
|
|
String operator() (const DecimalField<Decimal64> & x) const;
|
|
|
|
String operator() (const DecimalField<Decimal128> & x) const;
|
2019-02-11 11:19:56 +00:00
|
|
|
String operator() (const AggregateFunctionStateData & x) const;
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** Converts numberic value of any type to specified type. */
|
2015-10-12 07:05:54 +00:00
|
|
|
template <typename T>
|
|
|
|
class FieldVisitorConvertToNumber : public StaticVisitor<T>
|
|
|
|
{
|
|
|
|
public:
|
2017-12-01 17:49:12 +00:00
|
|
|
T operator() (const Null &) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
throw Exception("Cannot convert NULL to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
T operator() (const String &) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
throw Exception("Cannot convert String to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
T operator() (const Array &) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
throw Exception("Cannot convert Array to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
T operator() (const Tuple &) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
throw Exception("Cannot convert Tuple to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 20:41:50 +00:00
|
|
|
T operator() (const UInt64 & x) const { return x; }
|
|
|
|
T operator() (const Int64 & x) const { return x; }
|
2017-04-01 07:20:54 +00:00
|
|
|
T operator() (const Float64 & x) const { return x; }
|
2018-08-03 20:25:47 +00:00
|
|
|
|
|
|
|
T operator() (const UInt128 &) const
|
|
|
|
{
|
|
|
|
throw Exception("Cannot convert UInt128 to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
|
|
|
}
|
2018-08-23 19:11:31 +00:00
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
T operator() (const DecimalField<U> & x) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_floating_point_v<T>)
|
|
|
|
return static_cast<T>(x.getValue()) / x.getScaleMultiplier();
|
|
|
|
else
|
|
|
|
return x.getValue() / x.getScaleMultiplier();
|
|
|
|
}
|
2019-02-11 11:19:56 +00:00
|
|
|
|
|
|
|
T operator() (const AggregateFunctionStateData &) const
|
|
|
|
{
|
2019-02-11 13:11:52 +00:00
|
|
|
throw Exception("Cannot convert AggregateFunctionStateData to " + demangle(typeid(T).name()), ErrorCodes::CANNOT_CONVERT_TYPE);
|
2019-02-11 11:19:56 +00:00
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
/** Updates SipHash by type and value of Field */
|
|
|
|
class FieldVisitorHash : public StaticVisitor<>
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
SipHash & hash;
|
2017-01-06 17:41:19 +00:00
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
FieldVisitorHash(SipHash & hash_);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-12 20:41:50 +00:00
|
|
|
void operator() (const Null & x) const;
|
|
|
|
void operator() (const UInt64 & x) const;
|
2018-07-09 21:25:56 +00:00
|
|
|
void operator() (const UInt128 & x) const;
|
2017-05-12 20:41:50 +00:00
|
|
|
void operator() (const Int64 & x) const;
|
|
|
|
void operator() (const Float64 & x) const;
|
|
|
|
void operator() (const String & x) const;
|
|
|
|
void operator() (const Array & x) const;
|
2019-10-18 15:57:05 +00:00
|
|
|
void operator() (const Tuple & x) const;
|
2018-08-23 19:11:31 +00:00
|
|
|
void operator() (const DecimalField<Decimal32> & x) const;
|
|
|
|
void operator() (const DecimalField<Decimal64> & x) const;
|
|
|
|
void operator() (const DecimalField<Decimal128> & x) const;
|
2019-02-11 11:19:56 +00:00
|
|
|
void operator() (const AggregateFunctionStateData & x) const;
|
2017-01-06 17:41:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
template <typename T> constexpr bool isDecimalField() { return false; }
|
|
|
|
template <> constexpr bool isDecimalField<DecimalField<Decimal32>>() { return true; }
|
|
|
|
template <> constexpr bool isDecimalField<DecimalField<Decimal64>>() { return true; }
|
|
|
|
template <> constexpr bool isDecimalField<DecimalField<Decimal128>>() { return true; }
|
|
|
|
|
|
|
|
|
2016-07-08 21:47:54 +00:00
|
|
|
/** More precise comparison, used for index.
|
|
|
|
* Differs from Field::operator< and Field::operator== in that it also compares values of different types.
|
|
|
|
* Comparison rules are same as in FunctionsComparison (to be consistent with expression evaluation in query).
|
2015-10-12 07:05:54 +00:00
|
|
|
*/
|
|
|
|
class FieldVisitorAccurateEquals : public StaticVisitor<bool>
|
|
|
|
{
|
|
|
|
public:
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const UInt64 &, const Null &) const { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator() (const UInt64 & l, const UInt64 & r) const { return l == r; }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const UInt64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
2017-05-23 14:38:14 +00:00
|
|
|
bool operator() (const UInt64 & l, const Int64 & r) const { return accurate::equalsOp(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Float64 & r) const { return accurate::equalsOp(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const UInt64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const UInt64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const Int64 &, const Null &) const { return false; }
|
2017-05-23 14:38:14 +00:00
|
|
|
bool operator() (const Int64 & l, const UInt64 & r) const { return accurate::equalsOp(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Int64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator() (const Int64 & l, const Int64 & r) const { return l == r; }
|
2017-05-23 14:38:14 +00:00
|
|
|
bool operator() (const Int64 & l, const Float64 & r) const { return accurate::equalsOp(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Int64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const Int64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const Float64 &, const Null &) const { return false; }
|
2017-05-23 14:38:14 +00:00
|
|
|
bool operator() (const Float64 & l, const UInt64 & r) const { return accurate::equalsOp(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Float64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
2017-05-23 14:38:14 +00:00
|
|
|
bool operator() (const Float64 & l, const Int64 & r) const { return accurate::equalsOp(l, r); }
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator() (const Float64 & l, const Float64 & r) const { return l == r; }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Float64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const Float64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2017-12-01 17:49:12 +00:00
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Null &, const T &) const
|
|
|
|
{
|
|
|
|
return std::is_same_v<T, Null>;
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:36:41 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const String & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, String>)
|
|
|
|
return l == r;
|
|
|
|
if constexpr (std::is_same_v<T, UInt128>)
|
|
|
|
return stringToUUID(l) == r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator() (const UInt128 & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, UInt128>)
|
|
|
|
return l == r;
|
|
|
|
if constexpr (std::is_same_v<T, String>)
|
|
|
|
return l == stringToUUID(r);
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Array & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, Array>)
|
|
|
|
return l == r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Tuple & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, Tuple>)
|
|
|
|
return l == r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool operator() (const DecimalField<T> & l, const U & r) const
|
|
|
|
{
|
|
|
|
if constexpr (isDecimalField<U>())
|
|
|
|
return l == r;
|
2018-08-28 18:36:41 +00:00
|
|
|
if constexpr (std::is_same_v<U, Int64> || std::is_same_v<U, UInt64>)
|
2018-08-23 19:11:31 +00:00
|
|
|
return l == DecimalField<Decimal128>(r, 0);
|
2020-01-28 12:33:25 +00:00
|
|
|
if constexpr (std::is_same_v<U, Null>)
|
2020-01-25 22:21:12 +00:00
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool operator() (const UInt64 & l, const DecimalField<T> & r) const { return DecimalField<Decimal128>(l, 0) == r; }
|
|
|
|
template <typename T> bool operator() (const Int64 & l, const DecimalField<T> & r) const { return DecimalField<Decimal128>(l, 0) == r; }
|
2018-08-28 18:36:41 +00:00
|
|
|
template <typename T> bool operator() (const Float64 & l, const DecimalField<T> & r) const { return cantCompare(l, r); }
|
|
|
|
|
2019-02-11 11:19:56 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const AggregateFunctionStateData & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, AggregateFunctionStateData>)
|
|
|
|
return l == r;
|
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:36:41 +00:00
|
|
|
private:
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool cantCompare(const T &, const U &) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<U, Null>)
|
|
|
|
return false;
|
|
|
|
throw Exception("Cannot compare " + demangle(typeid(T).name()) + " with " + demangle(typeid(U).name()),
|
|
|
|
ErrorCodes::BAD_TYPE_OF_FIELD);
|
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FieldVisitorAccurateLess : public StaticVisitor<bool>
|
|
|
|
{
|
|
|
|
public:
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const UInt64 &, const Null &) const { return false; }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const UInt64 & l, const UInt64 & r) const { return l < r; }
|
|
|
|
bool operator() (const UInt64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Int64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Float64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const UInt64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const UInt64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const Int64 &, const Null &) const { return false; }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Int64 & l, const UInt64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const Int64 & r) const { return l < r; }
|
|
|
|
bool operator() (const Int64 & l, const Float64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Int64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const Int64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2018-08-28 18:36:41 +00:00
|
|
|
|
2020-01-25 22:21:12 +00:00
|
|
|
bool operator() (const Float64 &, const Null &) const { return false; }
|
2018-08-28 18:36:41 +00:00
|
|
|
bool operator() (const Float64 & l, const UInt64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const UInt128 & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Int64 & r) const { return accurate::lessOp(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Float64 & r) const { return l < r; }
|
|
|
|
bool operator() (const Float64 & l, const String & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Array & r) const { return cantCompare(l, r); }
|
|
|
|
bool operator() (const Float64 & l, const Tuple & r) const { return cantCompare(l, r); }
|
2019-02-11 13:11:52 +00:00
|
|
|
bool operator() (const Float64 & l, const AggregateFunctionStateData & r) const { return cantCompare(l, r); }
|
2017-12-01 17:49:12 +00:00
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Null &, const T &) const
|
|
|
|
{
|
|
|
|
return !std::is_same_v<T, Null>;
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:36:41 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const String & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, String>)
|
|
|
|
return l < r;
|
|
|
|
if constexpr (std::is_same_v<T, UInt128>)
|
|
|
|
return stringToUUID(l) < r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator() (const UInt128 & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, UInt128>)
|
|
|
|
return l < r;
|
|
|
|
if constexpr (std::is_same_v<T, String>)
|
|
|
|
return l < stringToUUID(r);
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Array & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, Array>)
|
|
|
|
return l < r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator() (const Tuple & l, const T & r) const
|
|
|
|
{
|
|
|
|
if constexpr (std::is_same_v<T, Tuple>)
|
|
|
|
return l < r;
|
2020-01-25 22:21:12 +00:00
|
|
|
if constexpr (std::is_same_v<T, Null>)
|
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool operator() (const DecimalField<T> & l, const U & r) const
|
|
|
|
{
|
|
|
|
if constexpr (isDecimalField<U>())
|
|
|
|
return l < r;
|
2020-01-28 12:31:17 +00:00
|
|
|
if constexpr (std::is_same_v<U, Int64> || std::is_same_v<U, UInt64>)
|
2018-08-23 19:11:31 +00:00
|
|
|
return l < DecimalField<Decimal128>(r, 0);
|
2020-01-28 12:31:17 +00:00
|
|
|
if constexpr (std::is_same_v<U, Null>)
|
2020-01-25 22:21:12 +00:00
|
|
|
return false;
|
2018-08-28 18:36:41 +00:00
|
|
|
return cantCompare(l, r);
|
2018-08-23 19:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> bool operator() (const UInt64 & l, const DecimalField<T> & r) const { return DecimalField<Decimal128>(l, 0) < r; }
|
|
|
|
template <typename T> bool operator() (const Int64 & l, const DecimalField<T> & r) const { return DecimalField<Decimal128>(l, 0) < r; }
|
|
|
|
template <typename T> bool operator() (const Float64 &, const DecimalField<T> &) const { return false; }
|
2018-08-28 18:36:41 +00:00
|
|
|
|
2019-02-11 11:19:56 +00:00
|
|
|
template <typename T>
|
|
|
|
bool operator() (const AggregateFunctionStateData & l, const T & r) const
|
|
|
|
{
|
|
|
|
return cantCompare(l, r);
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:36:41 +00:00
|
|
|
private:
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool cantCompare(const T &, const U &) const
|
|
|
|
{
|
|
|
|
throw Exception("Cannot compare " + demangle(typeid(T).name()) + " with " + demangle(typeid(U).name()),
|
|
|
|
ErrorCodes::BAD_TYPE_OF_FIELD);
|
|
|
|
}
|
2015-10-12 07:05:54 +00:00
|
|
|
};
|
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
|
2017-09-18 15:41:07 +00:00
|
|
|
/** Implements `+=` operation.
|
|
|
|
* Returns false if the result is zero.
|
|
|
|
*/
|
|
|
|
class FieldVisitorSum : public StaticVisitor<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const Field & rhs;
|
|
|
|
public:
|
|
|
|
explicit FieldVisitorSum(const Field & rhs_) : rhs(rhs_) {}
|
|
|
|
|
2019-12-10 13:40:45 +00:00
|
|
|
// We can add all ints as unsigned regardless of their actual signedness.
|
|
|
|
bool operator() (Int64 & x) const { return this->operator()(reinterpret_cast<UInt64 &>(x)); }
|
|
|
|
bool operator() (UInt64 & x) const
|
|
|
|
{
|
|
|
|
x += rhs.reinterpret<UInt64>();
|
|
|
|
return x != 0;
|
|
|
|
}
|
|
|
|
|
2017-09-18 15:41:07 +00:00
|
|
|
bool operator() (Float64 & x) const { x += get<Float64>(rhs); return x != 0; }
|
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
bool operator() (Null &) const { throw Exception("Cannot sum Nulls", ErrorCodes::LOGICAL_ERROR); }
|
|
|
|
bool operator() (String &) const { throw Exception("Cannot sum Strings", ErrorCodes::LOGICAL_ERROR); }
|
|
|
|
bool operator() (Array &) const { throw Exception("Cannot sum Arrays", ErrorCodes::LOGICAL_ERROR); }
|
2019-10-18 15:57:05 +00:00
|
|
|
bool operator() (Tuple &) const { throw Exception("Cannot sum Tuples", ErrorCodes::LOGICAL_ERROR); }
|
2018-07-09 21:25:56 +00:00
|
|
|
bool operator() (UInt128 &) const { throw Exception("Cannot sum UUIDs", ErrorCodes::LOGICAL_ERROR); }
|
2019-02-11 11:19:56 +00:00
|
|
|
bool operator() (AggregateFunctionStateData &) const { throw Exception("Cannot sum AggregateFunctionStates", ErrorCodes::LOGICAL_ERROR); }
|
2018-08-23 19:11:31 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator() (DecimalField<T> & x) const
|
|
|
|
{
|
|
|
|
x += get<DecimalField<T>>(rhs);
|
|
|
|
return x.getValue() != 0;
|
|
|
|
}
|
2017-09-18 15:41:07 +00:00
|
|
|
};
|
|
|
|
|
2015-10-12 07:05:54 +00:00
|
|
|
}
|