2011-08-09 15:57:33 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2017-10-30 05:26:31 +00:00
|
|
|
#include <DataTypes/DataTypeInterval.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2017-10-30 05:26:31 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2017-06-13 04:45:30 +00:00
|
|
|
#include <DataTypes/NumberTraits.h>
|
2017-05-24 20:25:36 +00:00
|
|
|
#include <Core/AccurateComparison.h>
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-11-06 01:22:25 +00:00
|
|
|
#include <Interpreters/ExpressionActions.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <ext/range.h>
|
2017-11-22 09:25:36 +00:00
|
|
|
#include <boost/math/common_factor.hpp>
|
2011-08-09 15:57:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-12 02:21:15 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int ILLEGAL_DIVISION;
|
2017-06-13 02:06:53 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-07-21 06:35:58 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int TOO_LESS_ARGUMENTS_FOR_FUNCTION;
|
2016-01-12 02:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-20 19:16:02 +00:00
|
|
|
/** Arithmetic operations: +, -, *, /, %,
|
|
|
|
* intDiv (integer division), unary minus.
|
|
|
|
* Bitwise operations: |, &, ^, ~.
|
|
|
|
* Etc.
|
2011-08-09 15:57:33 +00:00
|
|
|
*/
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B, typename Op, typename ResultType_ = typename Op::ResultType>
|
2014-08-17 02:13:40 +00:00
|
|
|
struct BinaryOperationImplBase
|
2011-08-09 15:57:33 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = ResultType_;
|
|
|
|
|
2017-09-16 22:24:48 +00:00
|
|
|
static void NO_INLINE vector_vector(const PaddedPODArray<A> & a, const PaddedPODArray<B> & b, PaddedPODArray<ResultType> & c)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::template apply<ResultType>(a[i], b[i]);
|
|
|
|
}
|
|
|
|
|
2017-09-16 22:24:48 +00:00
|
|
|
static void NO_INLINE vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::template apply<ResultType>(a[i], b);
|
|
|
|
}
|
|
|
|
|
2017-09-16 22:24:48 +00:00
|
|
|
static void NO_INLINE constant_vector(A a, const PaddedPODArray<B> & b, PaddedPODArray<ResultType> & c)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t size = b.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::template apply<ResultType>(a, b[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void constant_constant(A a, B b, ResultType & c)
|
|
|
|
{
|
|
|
|
c = Op::template apply<ResultType>(a, b);
|
|
|
|
}
|
2011-08-09 15:57:33 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B, typename Op, typename ResultType = typename Op::ResultType>
|
2014-09-08 10:46:30 +00:00
|
|
|
struct BinaryOperationImpl : BinaryOperationImplBase<A, B, Op, ResultType>
|
2014-08-17 02:13:40 +00:00
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2014-09-08 10:46:30 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename Op>
|
2012-12-20 20:01:18 +00:00
|
|
|
struct UnaryOperationImpl
|
2011-08-09 17:24:17 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename Op::ResultType;
|
|
|
|
|
2017-09-16 22:24:48 +00:00
|
|
|
static void NO_INLINE vector(const PaddedPODArray<A> & a, PaddedPODArray<ResultType> & c)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = Op::apply(a[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void constant(A a, ResultType & c)
|
|
|
|
{
|
|
|
|
c = Op::apply(a);
|
|
|
|
}
|
2012-12-20 20:01:18 +00:00
|
|
|
};
|
2011-08-09 17:24:17 +00:00
|
|
|
|
2011-08-20 23:56:07 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2012-12-20 20:01:18 +00:00
|
|
|
struct PlusImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Next everywhere, static_cast - so that there is no wrong result in expressions of the form Int64 c = UInt32(a) * Int32(-1).
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<Result>(a) + b;
|
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
};
|
|
|
|
|
2014-09-08 10:46:30 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2012-12-20 20:01:18 +00:00
|
|
|
struct MultiplyImpl
|
2011-08-09 17:24:17 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
|
2011-08-09 17:24:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a) * b;
|
|
|
|
}
|
2012-12-20 20:01:18 +00:00
|
|
|
};
|
2011-08-09 17:24:17 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2012-12-20 20:01:18 +00:00
|
|
|
struct MinusImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfSubtraction<A, B>::Type;
|
2011-08-20 23:56:07 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a) - b;
|
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2011-08-09 17:24:17 +00:00
|
|
|
struct DivideFloatingImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfFloatingPointDivision<A, B>::Type;
|
2011-08-09 17:24:17 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a) / b;
|
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
};
|
|
|
|
|
2012-12-18 20:07:26 +00:00
|
|
|
|
2013-06-07 22:12:58 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
2013-02-16 21:23:55 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
inline void throwIfDivisionLeadsToFPE(A a, B b)
|
2012-12-18 20:07:26 +00:00
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Is it better to use siglongjmp instead of checks?
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely(b == 0))
|
|
|
|
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
|
2013-02-16 21:23:55 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// http://avva.livejournal.com/2548306.html
|
|
|
|
if (unlikely(std::is_signed<A>::value && std::is_signed<B>::value && a == std::numeric_limits<A>::min() && b == -1))
|
|
|
|
throw Exception("Division of minimal signed number by minus one", ErrorCodes::ILLEGAL_DIVISION);
|
2013-06-07 22:12:58 +00:00
|
|
|
}
|
2013-06-07 17:32:12 +00:00
|
|
|
|
2014-11-21 14:28:39 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
inline bool divisionLeadsToFPE(A a, B b)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely(b == 0))
|
|
|
|
return true;
|
2014-11-21 14:28:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely(std::is_signed<A>::value && std::is_signed<B>::value && a == std::numeric_limits<A>::min() && b == -1))
|
|
|
|
return true;
|
2014-11-21 14:28:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
2014-11-21 14:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-07 17:32:12 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2013-06-07 22:12:58 +00:00
|
|
|
|
2012-12-18 20:07:26 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2011-08-09 17:24:17 +00:00
|
|
|
struct DivideIntegralImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIntegerDivision<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
throwIfDivisionLeadsToFPE(a, b);
|
|
|
|
return a / b;
|
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2014-11-21 14:28:39 +00:00
|
|
|
struct DivideIntegralOrZeroImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfIntegerDivision<A, B>::Type;
|
2014-11-21 14:28:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return unlikely(divisionLeadsToFPE(a, b)) ? 0 : a / b;
|
|
|
|
}
|
2014-11-21 14:28:39 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2011-08-09 17:24:17 +00:00
|
|
|
struct ModuloImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfModulo<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2017-11-24 08:17:49 +00:00
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
|
2017-04-01 07:20:54 +00:00
|
|
|
return typename NumberTraits::ToInteger<A>::Type(a)
|
2017-11-24 08:17:49 +00:00
|
|
|
% typename NumberTraits::ToInteger<B>::Type(b);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2011-08-09 17:24:17 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitAndImpl
|
2012-12-20 13:41:55 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a)
|
|
|
|
& static_cast<Result>(b);
|
|
|
|
}
|
2012-12-20 13:41:55 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitOrImpl
|
2012-12-20 13:41:55 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a)
|
|
|
|
| static_cast<Result>(b);
|
|
|
|
}
|
2012-12-20 20:01:18 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitXorImpl
|
2012-12-20 20:01:18 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a)
|
|
|
|
^ static_cast<Result>(b);
|
|
|
|
}
|
2012-12-20 13:41:55 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitShiftLeftImpl
|
2013-12-17 13:49:21 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a)
|
|
|
|
<< static_cast<Result>(b);
|
|
|
|
}
|
2013-12-17 13:49:21 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitShiftRightImpl
|
2013-12-17 13:49:21 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a)
|
|
|
|
>> static_cast<Result>(b);
|
|
|
|
}
|
2013-12-17 13:49:21 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-03-20 19:16:02 +00:00
|
|
|
struct BitRotateLeftImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return (static_cast<Result>(a) << static_cast<Result>(b))
|
|
|
|
| (static_cast<Result>(a) >> ((sizeof(Result) * 8) - static_cast<Result>(b)));
|
|
|
|
}
|
2017-03-20 19:16:02 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-03-20 19:16:02 +00:00
|
|
|
struct BitRotateRightImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBit<A, B>::Type;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return (static_cast<Result>(a) >> static_cast<Result>(b))
|
|
|
|
| (static_cast<Result>(a) << ((sizeof(Result) * 8) - static_cast<Result>(b)));
|
|
|
|
}
|
2017-03-20 19:16:02 +00:00
|
|
|
};
|
|
|
|
|
2015-07-10 03:06:00 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_integral<T>::value, T> toInteger(T x) { return x; }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_floating_point<T>::value, Int64> toInteger(T x) { return Int64(x); }
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct BitTestImpl
|
|
|
|
{
|
|
|
|
using ResultType = UInt8;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b) { return (toInteger(a) >> toInteger(b)) & 1; };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
2017-01-13 18:15:12 +00:00
|
|
|
struct LeastBaseImpl
|
2015-07-10 02:57:32 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = NumberTraits::ResultOfLeast<A, B>;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2017-05-13 22:19:04 +00:00
|
|
|
/** gcc 4.9.2 successfully vectorizes a loop from this function. */
|
2017-04-01 07:20:54 +00:00
|
|
|
return static_cast<Result>(a) < static_cast<Result>(b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
|
|
}
|
2015-07-10 02:57:32 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-01-13 18:15:12 +00:00
|
|
|
struct LeastSpecialImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = std::make_signed_t<A>;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
static_assert(std::is_same<Result, ResultType>::value, "ResultType != Result");
|
|
|
|
return accurate::lessOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
|
|
}
|
2017-01-13 18:15:12 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-01-13 19:45:18 +00:00
|
|
|
using LeastImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>::value, LeastBaseImpl<A, B>, LeastSpecialImpl<A, B>>;
|
2017-01-13 18:15:12 +00:00
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-01-13 18:15:12 +00:00
|
|
|
struct GreatestBaseImpl
|
2015-07-10 02:57:32 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = NumberTraits::ResultOfGreatest<A, B>;
|
2015-07-10 02:57:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
return static_cast<Result>(a) > static_cast<Result>(b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
|
|
}
|
2015-07-10 02:57:32 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-01-13 18:15:12 +00:00
|
|
|
struct GreatestSpecialImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = std::make_unsigned_t<A>;
|
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
|
|
|
static_assert(std::is_same<Result, ResultType>::value, "ResultType != Result");
|
|
|
|
return accurate::greaterOp(a, b) ? static_cast<Result>(a) : static_cast<Result>(b);
|
|
|
|
}
|
2017-01-13 18:15:12 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A, typename B>
|
2017-01-13 19:45:18 +00:00
|
|
|
using GreatestImpl = std::conditional_t<!NumberTraits::LeastGreatestSpecialCase<A, B>::value, GreatestBaseImpl<A, B>, GreatestSpecialImpl<A, B>>;
|
2017-01-13 18:15:12 +00:00
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A>
|
2011-09-05 00:32:22 +00:00
|
|
|
struct NegateImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfNegate<A>::Type;
|
2011-09-05 00:32:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static inline ResultType apply(A a)
|
|
|
|
{
|
|
|
|
return -static_cast<ResultType>(a);
|
|
|
|
}
|
2012-12-20 20:01:18 +00:00
|
|
|
};
|
2011-09-05 00:32:22 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A>
|
2013-12-17 16:06:52 +00:00
|
|
|
struct BitNotImpl
|
2012-12-20 20:01:18 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfBitNot<A>::Type;
|
2012-12-20 20:01:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static inline ResultType apply(A a)
|
|
|
|
{
|
|
|
|
return ~static_cast<ResultType>(a);
|
|
|
|
}
|
2011-09-05 00:32:22 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename A>
|
2014-11-28 16:09:29 +00:00
|
|
|
struct AbsImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfAbs<A>::Type;
|
|
|
|
|
2017-12-01 21:40:58 +00:00
|
|
|
static inline ResultType apply(A a)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-01 21:40:58 +00:00
|
|
|
if constexpr (std::is_integral<A>::value && std::is_signed<A>::value)
|
|
|
|
return a < 0 ? static_cast<ResultType>(~a) + 1 : a;
|
|
|
|
else if constexpr (std::is_integral<A>::value && std::is_unsigned<A>::value)
|
|
|
|
return static_cast<ResultType>(a);
|
|
|
|
else if constexpr (std::is_floating_point<A>::value)
|
|
|
|
return static_cast<ResultType>(std::abs(a));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-11-28 16:09:29 +00:00
|
|
|
};
|
2011-08-09 15:57:33 +00:00
|
|
|
|
2017-11-22 09:25:36 +00:00
|
|
|
template <typename A, typename B>
|
|
|
|
struct GCDImpl
|
|
|
|
{
|
2017-11-24 08:17:49 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
|
2017-11-22 09:25:36 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2017-11-23 08:40:06 +00:00
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
|
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));
|
2017-11-22 09:25:36 +00:00
|
|
|
return boost::math::gcd(
|
2017-11-23 08:40:06 +00:00
|
|
|
typename NumberTraits::ToInteger<Result>::Type(a),
|
|
|
|
typename NumberTraits::ToInteger<Result>::Type(b));
|
2017-11-22 09:25:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct LCMImpl
|
|
|
|
{
|
2017-11-22 09:50:23 +00:00
|
|
|
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
|
2017-11-22 09:25:36 +00:00
|
|
|
|
|
|
|
template <typename Result = ResultType>
|
|
|
|
static inline Result apply(A a, B b)
|
|
|
|
{
|
2017-11-23 08:40:06 +00:00
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
|
|
|
|
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<B>::Type(b), typename NumberTraits::ToInteger<A>::Type(a));
|
2017-11-22 09:25:36 +00:00
|
|
|
return boost::math::lcm(
|
2017-11-23 08:40:06 +00:00
|
|
|
typename NumberTraits::ToInteger<Result>::Type(a),
|
|
|
|
typename NumberTraits::ToInteger<Result>::Type(b));
|
2017-11-22 09:25:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 10:46:30 +00:00
|
|
|
/// this one is just for convenience
|
|
|
|
template <bool B, typename T1, typename T2> using If = typename std::conditional<B, T1, T2>::type;
|
|
|
|
/// these ones for better semantics
|
|
|
|
template <typename T> using Then = T;
|
|
|
|
template <typename T> using Else = T;
|
|
|
|
|
|
|
|
/// Used to indicate undefined operation
|
2014-09-08 12:22:36 +00:00
|
|
|
struct InvalidType;
|
2014-09-08 10:46:30 +00:00
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
template <typename T>
|
|
|
|
struct DataTypeFromFieldType
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Type = DataTypeNumber<T>;
|
2017-03-12 10:13:45 +00:00
|
|
|
};
|
|
|
|
|
2015-07-10 02:57:32 +00:00
|
|
|
template <>
|
|
|
|
struct DataTypeFromFieldType<NumberTraits::Error>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using Type = InvalidType;
|
2015-07-10 02:57:32 +00:00
|
|
|
};
|
|
|
|
|
2014-09-08 10:46:30 +00:00
|
|
|
template <typename DataType> struct IsIntegral { static constexpr auto value = false; };
|
|
|
|
template <> struct IsIntegral<DataTypeUInt8> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeUInt16> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeUInt32> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeUInt64> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeInt8> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeInt16> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeInt32> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsIntegral<DataTypeInt64> { static constexpr auto value = true; };
|
|
|
|
|
2014-09-17 20:18:52 +00:00
|
|
|
template <typename DataType> struct IsDateOrDateTime { static constexpr auto value = false; };
|
|
|
|
template <> struct IsDateOrDateTime<DataTypeDate> { static constexpr auto value = true; };
|
|
|
|
template <> struct IsDateOrDateTime<DataTypeDateTime> { static constexpr auto value = true; };
|
2014-09-08 10:46:30 +00:00
|
|
|
|
2015-07-10 02:57:32 +00:00
|
|
|
/** Returns appropriate result type for binary operator on dates (or datetimes):
|
2014-09-08 10:46:30 +00:00
|
|
|
* Date + Integral -> Date
|
|
|
|
* Integral + Date -> Date
|
2014-09-08 12:22:36 +00:00
|
|
|
* Date - Date -> Int32
|
2014-09-08 10:46:30 +00:00
|
|
|
* Date - Integral -> Date
|
2015-07-10 02:57:32 +00:00
|
|
|
* least(Date, Date) -> Date
|
|
|
|
* greatest(Date, Date) -> Date
|
2014-09-08 10:46:30 +00:00
|
|
|
* All other operations are not defined and return InvalidType, operations on
|
2017-07-23 08:40:43 +00:00
|
|
|
* distinct date types are also undefined (e.g. DataTypeDate - DataTypeDateTime)
|
|
|
|
*/
|
2014-09-08 10:46:30 +00:00
|
|
|
template <template <typename, typename> class Operation, typename LeftDataType, typename RightDataType>
|
|
|
|
struct DateBinaryOperationTraits
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using T0 = typename LeftDataType::FieldType;
|
|
|
|
using T1 = typename RightDataType::FieldType;
|
|
|
|
using Op = Operation<T0, T1>;
|
|
|
|
|
|
|
|
using ResultDataType =
|
|
|
|
If<std::is_same<Op, PlusImpl<T0, T1>>::value,
|
|
|
|
Then<
|
|
|
|
If<IsDateOrDateTime<LeftDataType>::value && IsIntegral<RightDataType>::value,
|
|
|
|
Then<LeftDataType>,
|
|
|
|
Else<
|
|
|
|
If<IsIntegral<LeftDataType>::value && IsDateOrDateTime<RightDataType>::value,
|
|
|
|
Then<RightDataType>,
|
2017-07-23 08:40:43 +00:00
|
|
|
Else<InvalidType>>>>>,
|
2017-04-01 07:20:54 +00:00
|
|
|
Else<
|
|
|
|
If<std::is_same<Op, MinusImpl<T0, T1>>::value,
|
|
|
|
Then<
|
|
|
|
If<IsDateOrDateTime<LeftDataType>::value,
|
|
|
|
Then<
|
|
|
|
If<std::is_same<LeftDataType, RightDataType>::value,
|
|
|
|
Then<DataTypeInt32>,
|
|
|
|
Else<
|
|
|
|
If<IsIntegral<RightDataType>::value,
|
|
|
|
Then<LeftDataType>,
|
2017-07-23 08:40:43 +00:00
|
|
|
Else<InvalidType>>>>>,
|
|
|
|
Else<InvalidType>>>,
|
2017-04-01 07:20:54 +00:00
|
|
|
Else<
|
|
|
|
If<std::is_same<T0, T1>::value
|
|
|
|
&& (std::is_same<Op, LeastImpl<T0, T1>>::value || std::is_same<Op, GreatestImpl<T0, T1>>::value),
|
|
|
|
Then<LeftDataType>,
|
2017-07-23 08:40:43 +00:00
|
|
|
Else<InvalidType>>>>>>;
|
2014-09-08 10:46:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Decides among date and numeric operations
|
|
|
|
template <template <typename, typename> class Operation, typename LeftDataType, typename RightDataType>
|
|
|
|
struct BinaryOperationTraits
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultDataType =
|
|
|
|
If<IsDateOrDateTime<LeftDataType>::value || IsDateOrDateTime<RightDataType>::value,
|
|
|
|
Then<
|
|
|
|
typename DateBinaryOperationTraits<
|
2017-07-23 08:40:43 +00:00
|
|
|
Operation, LeftDataType, RightDataType>::ResultDataType>,
|
2017-04-01 07:20:54 +00:00
|
|
|
Else<
|
|
|
|
typename DataTypeFromFieldType<
|
|
|
|
typename Operation<
|
|
|
|
typename LeftDataType::FieldType,
|
2017-07-23 08:40:43 +00:00
|
|
|
typename RightDataType::FieldType>::ResultType>::Type>>;
|
2014-09-08 10:46:30 +00:00
|
|
|
};
|
|
|
|
|
2012-12-20 20:01:18 +00:00
|
|
|
|
|
|
|
template <template <typename, typename> class Op, typename Name>
|
2011-08-09 15:57:33 +00:00
|
|
|
class FunctionBinaryArithmetic : public IFunction
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-10-30 05:26:31 +00:00
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionBinaryArithmetic>(context); }
|
|
|
|
|
|
|
|
FunctionBinaryArithmetic(const Context & context) : context(context) {}
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2011-08-12 20:39:42 +00:00
|
|
|
private:
|
2017-10-30 05:26:31 +00:00
|
|
|
const Context & context;
|
|
|
|
|
2017-12-01 21:40:58 +00:00
|
|
|
template <typename ResultDataType>
|
2017-04-01 07:20:54 +00:00
|
|
|
bool checkRightTypeImpl(DataTypePtr & type_res) const
|
|
|
|
{
|
2017-12-01 21:40:58 +00:00
|
|
|
/// Overload for InvalidType
|
|
|
|
if constexpr (std::is_same<ResultDataType, InvalidType>::value)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
type_res = std::make_shared<ResultDataType>();
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LeftDataType, typename RightDataType>
|
|
|
|
bool checkRightType(const DataTypes & arguments, DataTypePtr & type_res) const
|
|
|
|
{
|
|
|
|
using ResultDataType = typename BinaryOperationTraits<Op, LeftDataType, RightDataType>::ResultDataType;
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
if (typeid_cast<const RightDataType *>(arguments[1].get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
return checkRightTypeImpl<ResultDataType>(type_res);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T0>
|
|
|
|
bool checkLeftType(const DataTypes & arguments, DataTypePtr & type_res) const
|
|
|
|
{
|
2017-07-23 08:40:43 +00:00
|
|
|
if (typeid_cast<const T0 *>(arguments[0].get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-24 04:14:35 +00:00
|
|
|
if ( checkRightType<T0, DataTypeDate>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeDateTime>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeUInt8>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeUInt16>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeUInt32>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeUInt64>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeInt8>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeInt16>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeInt32>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeInt64>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeFloat32>(arguments, type_res)
|
|
|
|
|| checkRightType<T0, DataTypeFloat64>(arguments, type_res))
|
|
|
|
return true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload for date operations
|
|
|
|
template <typename LeftDataType, typename RightDataType, typename ColumnType>
|
|
|
|
bool executeRightType(Block & block, const ColumnNumbers & arguments, const size_t result, const ColumnType * col_left)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (!typeid_cast<const RightDataType *>(block.getByPosition(arguments[1]).type.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
using ResultDataType = typename BinaryOperationTraits<Op, LeftDataType, RightDataType>::ResultDataType;
|
|
|
|
|
|
|
|
return executeRightTypeDispatch<LeftDataType, RightDataType, ResultDataType>(
|
|
|
|
block, arguments, result, col_left);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload for InvalidType
|
2017-12-01 21:40:58 +00:00
|
|
|
template <typename LeftDataType, typename RightDataType, typename ResultDataType, typename ColumnType>
|
2017-12-02 03:33:15 +00:00
|
|
|
bool executeRightTypeDispatch(Block & block, const ColumnNumbers & arguments,
|
|
|
|
[[maybe_unused]] const size_t result, [[maybe_unused]] const ColumnType * col_left)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-01 21:40:58 +00:00
|
|
|
if constexpr (std::is_same<ResultDataType, InvalidType>::value)
|
|
|
|
throw Exception("Types " + String(TypeName<typename LeftDataType::FieldType>::get())
|
|
|
|
+ " and " + String(TypeName<typename LeftDataType::FieldType>::get())
|
|
|
|
+ " are incompatible for function " + getName() + " or not upscaleable to common type", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
using T0 = typename LeftDataType::FieldType;
|
|
|
|
using T1 = typename RightDataType::FieldType;
|
|
|
|
using ResultType = typename ResultDataType::FieldType;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-01 21:40:58 +00:00
|
|
|
return executeRightTypeImpl<T0, T1, ResultType>(block, arguments, result, col_left);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ColumnVector overload
|
|
|
|
template <typename T0, typename T1, typename ResultType = typename Op<T0, T1>::ResultType>
|
|
|
|
bool executeRightTypeImpl(Block & block, const ColumnNumbers & arguments, size_t result, const ColumnVector<T0> * col_left)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (auto col_right = checkAndGetColumn<ColumnVector<T1>>(block.getByPosition(arguments[1]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col_left->getData().size());
|
|
|
|
BinaryOperationImpl<T0, T1, Op<T0, T1>, ResultType>::vector_vector(col_left->getData(), col_right->getData(), vec_res);
|
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (auto col_right = checkAndGetColumnConst<ColumnVector<T1>>(block.getByPosition(arguments[1]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col_left->getData().size());
|
2017-07-21 06:35:58 +00:00
|
|
|
BinaryOperationImpl<T0, T1, Op<T0, T1>, ResultType>::vector_constant(col_left->getData(), col_right->template getValue<T1>(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception("Logical error: unexpected type of column", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ColumnConst overload
|
|
|
|
template <typename T0, typename T1, typename ResultType = typename Op<T0, T1>::ResultType>
|
2017-07-21 06:35:58 +00:00
|
|
|
bool executeRightTypeImpl(Block & block, const ColumnNumbers & arguments, size_t result, const ColumnConst * col_left)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (auto col_right = checkAndGetColumn<ColumnVector<T1>>(block.getByPosition(arguments[1]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col_left->size());
|
2017-07-21 06:35:58 +00:00
|
|
|
BinaryOperationImpl<T0, T1, Op<T0, T1>, ResultType>::constant_vector(col_left->template getValue<T0>(), col_right->getData(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (auto col_right = checkAndGetColumnConst<ColumnVector<T1>>(block.getByPosition(arguments[1]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
ResultType res = 0;
|
2017-07-21 06:35:58 +00:00
|
|
|
BinaryOperationImpl<T0, T1, Op<T0, T1>, ResultType>::constant_constant(col_left->template getValue<T0>(), col_right->template getValue<T1>(), res);
|
2017-12-10 22:44:04 +00:00
|
|
|
block.getByPosition(result).column = DataTypeNumber<ResultType>().createColumnConst(col_left->size(), toField(res));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LeftDataType>
|
|
|
|
bool executeLeftType(Block & block, const ColumnNumbers & arguments, const size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (!typeid_cast<const LeftDataType *>(block.getByPosition(arguments[0]).type.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
return executeLeftTypeImpl<LeftDataType>(block, arguments, result);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
template <typename LeftDataType>
|
2017-04-01 07:20:54 +00:00
|
|
|
bool executeLeftTypeImpl(Block & block, const ColumnNumbers & arguments, const size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (auto col_left = checkAndGetColumn<ColumnVector<typename LeftDataType::FieldType>>(block.getByPosition(arguments[0]).column.get()))
|
|
|
|
{
|
2017-07-24 04:14:35 +00:00
|
|
|
if ( executeRightType<LeftDataType, DataTypeDate>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeDateTime>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt8>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt16>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt64>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt8>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt16>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt64>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeFloat32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeFloat64>(block, arguments, result, col_left))
|
|
|
|
return true;
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
|
|
|
+ " of second argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
else if (auto col_left = checkAndGetColumnConst<ColumnVector<typename LeftDataType::FieldType>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-24 04:14:35 +00:00
|
|
|
if ( executeRightType<LeftDataType, DataTypeDate>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeDateTime>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt8>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt16>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeUInt64>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt8>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt16>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeInt64>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeFloat32>(block, arguments, result, col_left)
|
|
|
|
|| executeRightType<LeftDataType, DataTypeFloat64>(block, arguments, result, col_left))
|
|
|
|
return true;
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of second argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-08 10:46:30 +00:00
|
|
|
|
2017-10-30 05:26:31 +00:00
|
|
|
FunctionPtr getFunctionForIntervalArithmetic(const DataTypePtr & type0, const DataTypePtr & type1) const
|
|
|
|
{
|
|
|
|
/// Special case when the function is plus or minus, one of arguments is Date/DateTime and another is Interval.
|
|
|
|
/// We construct another function (example: addMonths) and call it.
|
|
|
|
|
|
|
|
bool function_is_plus = std::is_same<Op<UInt8, UInt8>, PlusImpl<UInt8, UInt8>>::value;
|
|
|
|
bool function_is_minus = std::is_same<Op<UInt8, UInt8>, MinusImpl<UInt8, UInt8>>::value;
|
|
|
|
|
|
|
|
if (!function_is_plus && !function_is_minus)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
int interval_arg = 1;
|
|
|
|
const DataTypeInterval * interval_data_type = checkAndGetDataType<DataTypeInterval>(type1.get());
|
|
|
|
if (!interval_data_type)
|
|
|
|
{
|
|
|
|
interval_arg = 0;
|
|
|
|
interval_data_type = checkAndGetDataType<DataTypeInterval>(type0.get());
|
|
|
|
}
|
|
|
|
if (!interval_data_type)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (interval_arg == 0 && function_is_minus)
|
|
|
|
throw Exception("Wrong order of arguments for function " + getName() + ": argument of type Interval cannot be first.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
const DataTypeDate * date_data_type = checkAndGetDataType<DataTypeDate>(interval_arg == 0 ? type1.get() : type0.get());
|
|
|
|
const DataTypeDateTime * date_time_data_type = nullptr;
|
|
|
|
if (!date_data_type)
|
|
|
|
{
|
|
|
|
date_time_data_type = checkAndGetDataType<DataTypeDateTime>(interval_arg == 0 ? type1.get() : type0.get());
|
|
|
|
if (!date_time_data_type)
|
|
|
|
throw Exception("Wrong argument types for function " + getName() + ": if one argument is Interval, then another must be Date or DateTime.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream function_name;
|
|
|
|
function_name << (function_is_plus ? "add" : "subtract") << interval_data_type->kindToString() << 's';
|
|
|
|
|
|
|
|
return FunctionFactory::instance().get(function_name.str(), context);
|
|
|
|
}
|
|
|
|
|
2011-08-09 15:57:33 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-10-30 05:26:31 +00:00
|
|
|
/// Special case when the function is plus or minus, one of arguments is Date/DateTime and another is Interval.
|
|
|
|
if (FunctionPtr function = getFunctionForIntervalArithmetic(arguments[0], arguments[1]))
|
|
|
|
{
|
2017-11-06 01:22:25 +00:00
|
|
|
ColumnsWithTypeAndName new_arguments(2);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 2; ++i)
|
|
|
|
new_arguments[i].type = arguments[i];
|
2017-10-30 05:26:31 +00:00
|
|
|
|
|
|
|
/// Interval argument must be second.
|
2017-11-06 01:22:25 +00:00
|
|
|
if (checkDataType<DataTypeInterval>(new_arguments[0].type.get()))
|
2017-10-30 05:26:31 +00:00
|
|
|
std::swap(new_arguments[0], new_arguments[1]);
|
|
|
|
|
|
|
|
/// Change interval argument to its representation
|
2017-11-06 01:22:25 +00:00
|
|
|
new_arguments[1].type = std::make_shared<DataTypeNumber<DataTypeInterval::FieldType>>();
|
2017-10-30 05:26:31 +00:00
|
|
|
|
2017-11-06 01:22:25 +00:00
|
|
|
DataTypePtr res;
|
|
|
|
std::vector<ExpressionAction> unused_prerequisites;
|
|
|
|
function->getReturnTypeAndPrerequisites(new_arguments, res, unused_prerequisites);
|
|
|
|
return res;
|
2017-10-30 05:26:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr type_res;
|
|
|
|
|
2017-07-24 04:14:35 +00:00
|
|
|
if (!( checkLeftType<DataTypeDate>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeDateTime>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeUInt8>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeUInt16>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeUInt32>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeUInt64>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeInt8>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeInt16>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeInt32>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeInt64>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeFloat32>(arguments, type_res)
|
|
|
|
|| checkLeftType<DataTypeFloat64>(arguments, type_res)))
|
2017-12-09 14:01:42 +00:00
|
|
|
throw Exception("Illegal types " + arguments[0]->getName() + " and " + arguments[1]->getName() + " of arguments of function " + getName(),
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return type_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
{
|
2017-10-30 05:26:31 +00:00
|
|
|
/// Special case when the function is plus or minus, one of arguments is Date/DateTime and another is Interval.
|
|
|
|
if (FunctionPtr function = getFunctionForIntervalArithmetic(block.getByPosition(arguments[0]).type, block.getByPosition(arguments[1]).type))
|
|
|
|
{
|
|
|
|
ColumnNumbers new_arguments = arguments;
|
|
|
|
|
|
|
|
/// Interval argument must be second.
|
|
|
|
if (checkDataType<DataTypeInterval>(block.getByPosition(arguments[0]).type.get()))
|
|
|
|
std::swap(new_arguments[0], new_arguments[1]);
|
|
|
|
|
|
|
|
/// Change interval argument type to its representation
|
|
|
|
Block new_block = block;
|
|
|
|
new_block.getByPosition(new_arguments[1]).type = std::make_shared<DataTypeNumber<DataTypeInterval::FieldType>>();
|
|
|
|
|
|
|
|
function->executeImpl(new_block, new_arguments, result);
|
|
|
|
block.getByPosition(result).column = new_block.getByPosition(result).column;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-24 04:14:35 +00:00
|
|
|
if (!( executeLeftType<DataTypeDate>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeDateTime>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeUInt8>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeUInt16>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeUInt32>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeUInt64>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeInt8>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeInt16>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeInt32>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeInt64>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeFloat32>(block, arguments, result)
|
|
|
|
|| executeLeftType<DataTypeFloat64>(block, arguments, result)))
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2011-08-09 15:57:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-29 08:06:29 +00:00
|
|
|
template <typename FunctionName>
|
|
|
|
struct FunctionUnaryArithmeticMonotonicity;
|
|
|
|
|
|
|
|
|
2017-01-05 16:34:05 +00:00
|
|
|
template <template <typename> class Op, typename Name, bool is_injective>
|
2011-09-05 00:32:22 +00:00
|
|
|
class FunctionUnaryArithmetic : public IFunction
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-12-01 21:40:58 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionUnaryArithmetic>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2011-09-05 00:32:22 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T0>
|
|
|
|
bool checkType(const DataTypes & arguments, DataTypePtr & result) const
|
|
|
|
{
|
2017-07-23 08:40:43 +00:00
|
|
|
if (typeid_cast<const T0 *>(arguments[0].get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
result = std::make_shared<DataTypeNumber<typename Op<typename T0::FieldType>::ResultType>>();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T0>
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnVector<T0> * col = checkAndGetColumn<ColumnVector<T0>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
using ResultType = typename Op<T0>::ResultType;
|
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<ResultType>::Container & vec_res = col_res->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
vec_res.resize(col->getData().size());
|
2017-07-21 06:35:58 +00:00
|
|
|
UnaryOperationImpl<T0, Op<T0>>::vector(col->getData(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-05 00:32:22 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool isInjective(const Block &) override { return is_injective; }
|
|
|
|
|
2017-07-24 04:14:35 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
DataTypePtr result;
|
|
|
|
|
2017-07-24 04:14:35 +00:00
|
|
|
if (!( checkType<DataTypeUInt8>(arguments, result)
|
|
|
|
|| checkType<DataTypeUInt16>(arguments, result)
|
|
|
|
|| checkType<DataTypeUInt32>(arguments, result)
|
|
|
|
|| checkType<DataTypeUInt64>(arguments, result)
|
|
|
|
|| checkType<DataTypeInt8>(arguments, result)
|
|
|
|
|| checkType<DataTypeInt16>(arguments, result)
|
|
|
|
|| checkType<DataTypeInt32>(arguments, result)
|
|
|
|
|| checkType<DataTypeInt64>(arguments, result)
|
|
|
|
|| checkType<DataTypeFloat32>(arguments, result)
|
|
|
|
|| checkType<DataTypeFloat64>(arguments, result)))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
{
|
2017-07-24 04:14:35 +00:00
|
|
|
if (!( executeType<UInt8>(block, arguments, result)
|
|
|
|
|| executeType<UInt16>(block, arguments, result)
|
|
|
|
|| executeType<UInt32>(block, arguments, result)
|
|
|
|
|| executeType<UInt64>(block, arguments, result)
|
|
|
|
|| executeType<Int8>(block, arguments, result)
|
|
|
|
|| executeType<Int16>(block, arguments, result)
|
|
|
|
|| executeType<Int32>(block, arguments, result)
|
|
|
|
|| executeType<Int64>(block, arguments, result)
|
|
|
|
|| executeType<Float32>(block, arguments, result)
|
|
|
|
|| executeType<Float64>(block, arguments, result)))
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasInformationAboutMonotonicity() const override
|
|
|
|
{
|
|
|
|
return FunctionUnaryArithmeticMonotonicity<Name>::has();
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:40:58 +00:00
|
|
|
Monotonicity getMonotonicityForRange(const IDataType &, const Field & left, const Field & right) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return FunctionUnaryArithmeticMonotonicity<Name>::get(left, right);
|
|
|
|
}
|
2011-09-05 00:32:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
struct NamePlus { static constexpr auto name = "plus"; };
|
|
|
|
struct NameMinus { static constexpr auto name = "minus"; };
|
|
|
|
struct NameMultiply { static constexpr auto name = "multiply"; };
|
|
|
|
struct NameDivideFloating { static constexpr auto name = "divide"; };
|
|
|
|
struct NameDivideIntegral { static constexpr auto name = "intDiv"; };
|
|
|
|
struct NameDivideIntegralOrZero { static constexpr auto name = "intDivOrZero"; };
|
|
|
|
struct NameModulo { static constexpr auto name = "modulo"; };
|
|
|
|
struct NameNegate { static constexpr auto name = "negate"; };
|
|
|
|
struct NameAbs { static constexpr auto name = "abs"; };
|
|
|
|
struct NameBitAnd { static constexpr auto name = "bitAnd"; };
|
|
|
|
struct NameBitOr { static constexpr auto name = "bitOr"; };
|
|
|
|
struct NameBitXor { static constexpr auto name = "bitXor"; };
|
|
|
|
struct NameBitNot { static constexpr auto name = "bitNot"; };
|
|
|
|
struct NameBitShiftLeft { static constexpr auto name = "bitShiftLeft"; };
|
|
|
|
struct NameBitShiftRight { static constexpr auto name = "bitShiftRight"; };
|
|
|
|
struct NameBitRotateLeft { static constexpr auto name = "bitRotateLeft"; };
|
|
|
|
struct NameBitRotateRight { static constexpr auto name = "bitRotateRight"; };
|
|
|
|
struct NameBitTest { static constexpr auto name = "bitTest"; };
|
|
|
|
struct NameBitTestAny { static constexpr auto name = "bitTestAny"; };
|
|
|
|
struct NameBitTestAll { static constexpr auto name = "bitTestAll"; };
|
|
|
|
struct NameLeast { static constexpr auto name = "least"; };
|
|
|
|
struct NameGreatest { static constexpr auto name = "greatest"; };
|
2017-11-22 09:25:36 +00:00
|
|
|
struct NameGCD { static constexpr auto name = "gcd"; };
|
|
|
|
struct NameLCM { static constexpr auto name = "lcm"; };
|
2013-12-17 13:49:21 +00:00
|
|
|
|
2017-01-05 16:34:05 +00:00
|
|
|
using FunctionPlus = FunctionBinaryArithmetic<PlusImpl, NamePlus>;
|
|
|
|
using FunctionMinus = FunctionBinaryArithmetic<MinusImpl, NameMinus>;
|
|
|
|
using FunctionMultiply = FunctionBinaryArithmetic<MultiplyImpl, NameMultiply>;
|
|
|
|
using FunctionDivideFloating = FunctionBinaryArithmetic<DivideFloatingImpl, NameDivideFloating>;
|
|
|
|
using FunctionDivideIntegral = FunctionBinaryArithmetic<DivideIntegralImpl, NameDivideIntegral>;
|
2016-05-28 10:35:44 +00:00
|
|
|
using FunctionDivideIntegralOrZero = FunctionBinaryArithmetic<DivideIntegralOrZeroImpl, NameDivideIntegralOrZero>;
|
2017-01-05 16:34:05 +00:00
|
|
|
using FunctionModulo = FunctionBinaryArithmetic<ModuloImpl, NameModulo>;
|
|
|
|
using FunctionNegate = FunctionUnaryArithmetic<NegateImpl, NameNegate, true>;
|
|
|
|
using FunctionAbs = FunctionUnaryArithmetic<AbsImpl, NameAbs, false>;
|
2017-07-21 06:35:58 +00:00
|
|
|
using FunctionBitAnd = FunctionBinaryArithmetic<BitAndImpl, NameBitAnd>;
|
2017-01-05 16:34:05 +00:00
|
|
|
using FunctionBitOr = FunctionBinaryArithmetic<BitOrImpl, NameBitOr>;
|
|
|
|
using FunctionBitXor = FunctionBinaryArithmetic<BitXorImpl, NameBitXor>;
|
|
|
|
using FunctionBitNot = FunctionUnaryArithmetic<BitNotImpl, NameBitNot, true>;
|
2017-07-21 06:35:58 +00:00
|
|
|
using FunctionBitShiftLeft = FunctionBinaryArithmetic<BitShiftLeftImpl, NameBitShiftLeft>;
|
2017-01-05 16:34:05 +00:00
|
|
|
using FunctionBitShiftRight = FunctionBinaryArithmetic<BitShiftRightImpl, NameBitShiftRight>;
|
2017-07-21 06:35:58 +00:00
|
|
|
using FunctionBitRotateLeft = FunctionBinaryArithmetic<BitRotateLeftImpl, NameBitRotateLeft>;
|
2017-03-20 19:16:02 +00:00
|
|
|
using FunctionBitRotateRight = FunctionBinaryArithmetic<BitRotateRightImpl, NameBitRotateRight>;
|
2017-07-21 06:35:58 +00:00
|
|
|
using FunctionBitTest = FunctionBinaryArithmetic<BitTestImpl, NameBitTest>;
|
2017-01-05 16:34:05 +00:00
|
|
|
using FunctionLeast = FunctionBinaryArithmetic<LeastImpl, NameLeast>;
|
|
|
|
using FunctionGreatest = FunctionBinaryArithmetic<GreatestImpl, NameGreatest>;
|
2017-11-22 09:25:36 +00:00
|
|
|
using FunctionGCD = FunctionBinaryArithmetic<GCDImpl, NameGCD>;
|
|
|
|
using FunctionLCM = FunctionBinaryArithmetic<LCMImpl, NameLCM>;
|
2011-08-09 15:57:33 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Monotonicity properties for some functions.
|
2015-11-29 08:06:29 +00:00
|
|
|
|
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameNegate>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static bool has() { return true; }
|
2017-12-01 21:40:58 +00:00
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return { true, false };
|
|
|
|
}
|
2015-11-29 08:06:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameAbs>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static bool has() { return true; }
|
|
|
|
static IFunction::Monotonicity get(const Field & left, const Field & right)
|
|
|
|
{
|
|
|
|
Float64 left_float = left.isNull() ? -std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), left);
|
|
|
|
Float64 right_float = right.isNull() ? std::numeric_limits<Float64>::infinity() : applyVisitor(FieldVisitorConvertToNumber<Float64>(), right);
|
2015-11-29 08:06:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if ((left_float < 0 && right_float > 0) || (left_float > 0 && right_float < 0))
|
|
|
|
return {};
|
2015-11-29 08:06:29 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return { true, (left_float > 0) };
|
|
|
|
}
|
2015-11-29 08:06:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct FunctionUnaryArithmeticMonotonicity<NameBitNot>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static bool has() { return false; }
|
2017-12-01 21:40:58 +00:00
|
|
|
static IFunction::Monotonicity get(const Field &, const Field &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2015-11-29 08:06:29 +00:00
|
|
|
};
|
|
|
|
|
2017-03-12 11:09:25 +00:00
|
|
|
}
|
2011-08-09 15:57:33 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Optimizations for integer division by a constant.
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-01-27 19:55:33 +00:00
|
|
|
#if __SSE2__
|
2017-04-01 07:20:54 +00:00
|
|
|
#define LIBDIVIDE_USE_SSE2 1
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-17 02:13:40 +00:00
|
|
|
#include <libdivide.h>
|
|
|
|
|
2017-03-12 11:09:25 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2014-08-17 02:13:40 +00:00
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct DivideIntegralByConstantImpl
|
2017-04-01 07:20:54 +00:00
|
|
|
: BinaryOperationImplBase<A, B, DivideIntegralImpl<A, B>>
|
2014-08-17 02:13:40 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename DivideIntegralImpl<A, B>::ResultType;
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static void vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c)
|
|
|
|
{
|
|
|
|
if (unlikely(b == 0))
|
|
|
|
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
|
2014-08-17 02:13:40 +00:00
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely(std::is_signed<B>::value && b == -1))
|
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = -c[i];
|
|
|
|
return;
|
|
|
|
}
|
2014-08-17 02:13:40 +00:00
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
libdivide::divider<A> divider(b);
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = a.size();
|
|
|
|
const A * a_pos = &a[0];
|
|
|
|
const A * a_end = a_pos + size;
|
|
|
|
ResultType * c_pos = &c[0];
|
2016-01-13 21:05:11 +00:00
|
|
|
|
2017-01-27 19:55:33 +00:00
|
|
|
#if __SSE2__
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr size_t values_per_sse_register = 16 / sizeof(A);
|
|
|
|
const A * a_end_sse = a_pos + size / values_per_sse_register * values_per_sse_register;
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (a_pos < a_end_sse)
|
|
|
|
{
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(c_pos),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(a_pos)) / divider);
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
a_pos += values_per_sse_register;
|
|
|
|
c_pos += values_per_sse_register;
|
|
|
|
}
|
2016-01-13 21:05:11 +00:00
|
|
|
#endif
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (a_pos < a_end)
|
|
|
|
{
|
|
|
|
*c_pos = *a_pos / divider;
|
|
|
|
++a_pos;
|
|
|
|
++c_pos;
|
|
|
|
}
|
|
|
|
}
|
2014-08-17 02:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename A, typename B>
|
|
|
|
struct ModuloByConstantImpl
|
2017-04-01 07:20:54 +00:00
|
|
|
: BinaryOperationImplBase<A, B, ModuloImpl<A, B>>
|
2014-08-17 02:13:40 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = typename ModuloImpl<A, B>::ResultType;
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static void vector_constant(const PaddedPODArray<A> & a, B b, PaddedPODArray<ResultType> & c)
|
|
|
|
{
|
|
|
|
if (unlikely(b == 0))
|
|
|
|
throw Exception("Division by zero", ErrorCodes::ILLEGAL_DIVISION);
|
2014-08-17 02:13:40 +00:00
|
|
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (unlikely((std::is_signed<B>::value && b == -1) || b == 1))
|
|
|
|
{
|
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
c[i] = 0;
|
|
|
|
return;
|
|
|
|
}
|
2014-08-17 02:13:40 +00:00
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
libdivide::divider<A> divider(b);
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/// Here we failed to make the SSE variant from libdivide give an advantage.
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = a.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2017-07-21 06:35:58 +00:00
|
|
|
c[i] = a[i] - (a[i] / divider) * b; /// NOTE: perhaps, the division semantics with the remainder of negative numbers is not preserved.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-08-17 02:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Specializations are specified for dividing numbers of the type UInt64 and UInt32 by the numbers of the same sign.
|
|
|
|
* Can be expanded to all possible combinations, but more code is needed.
|
2014-08-17 02:13:40 +00:00
|
|
|
*/
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
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> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt64, DivideIntegralImpl<UInt64, UInt64>> : DivideIntegralByConstantImpl<UInt64, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt8, DivideIntegralImpl<UInt32, UInt8>> : DivideIntegralByConstantImpl<UInt32, UInt8> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt16, DivideIntegralImpl<UInt32, UInt16>> : DivideIntegralByConstantImpl<UInt32, UInt16> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt32, DivideIntegralImpl<UInt32, UInt32>> : DivideIntegralByConstantImpl<UInt32, UInt32> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt64, DivideIntegralImpl<UInt32, UInt64>> : DivideIntegralByConstantImpl<UInt32, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int8, DivideIntegralImpl<Int64, Int8>> : DivideIntegralByConstantImpl<Int64, Int8> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int16, DivideIntegralImpl<Int64, Int16>> : DivideIntegralByConstantImpl<Int64, Int16> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int32, DivideIntegralImpl<Int64, Int32>> : DivideIntegralByConstantImpl<Int64, Int32> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int64, DivideIntegralImpl<Int64, Int64>> : DivideIntegralByConstantImpl<Int64, Int64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int8, DivideIntegralImpl<Int32, Int8>> : DivideIntegralByConstantImpl<Int32, Int8> {};
|
|
|
|
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> {};
|
|
|
|
|
|
|
|
|
|
|
|
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> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt64, UInt64, ModuloImpl<UInt64, UInt64>> : ModuloByConstantImpl<UInt64, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt8, ModuloImpl<UInt32, UInt8>> : ModuloByConstantImpl<UInt32, UInt8> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt16, ModuloImpl<UInt32, UInt16>> : ModuloByConstantImpl<UInt32, UInt16> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt32, ModuloImpl<UInt32, UInt32>> : ModuloByConstantImpl<UInt32, UInt32> {};
|
|
|
|
template <> struct BinaryOperationImpl<UInt32, UInt64, ModuloImpl<UInt32, UInt64>> : ModuloByConstantImpl<UInt32, UInt64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int8, ModuloImpl<Int64, Int8>> : ModuloByConstantImpl<Int64, Int8> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int16, ModuloImpl<Int64, Int16>> : ModuloByConstantImpl<Int64, Int16> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int32, ModuloImpl<Int64, Int32>> : ModuloByConstantImpl<Int64, Int32> {};
|
|
|
|
template <> struct BinaryOperationImpl<Int64, Int64, ModuloImpl<Int64, Int64>> : ModuloByConstantImpl<Int64, Int64> {};
|
|
|
|
|
|
|
|
template <> struct BinaryOperationImpl<Int32, Int8, ModuloImpl<Int32, Int8>> : ModuloByConstantImpl<Int32, Int8> {};
|
|
|
|
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> {};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Impl, typename Name>
|
|
|
|
struct FunctionBitTestMany : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionBitTestMany>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception{
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be at least 2.",
|
|
|
|
ErrorCodes::TOO_LESS_ARGUMENTS_FOR_FUNCTION};
|
|
|
|
|
|
|
|
const auto first_arg = arguments.front().get();
|
2017-07-23 08:40:43 +00:00
|
|
|
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!first_arg->isInteger())
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception{
|
|
|
|
"Illegal type " + first_arg->getName() + " of first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto i : ext::range(1, arguments.size()))
|
|
|
|
{
|
|
|
|
const auto pos_arg = arguments[i].get();
|
|
|
|
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!pos_arg->canBeUsedAsNonNegativeArrayIndex())
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception{
|
|
|
|
"Illegal type " + pos_arg->getName() + " of " + toString(i) + " argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
|
|
|
{
|
|
|
|
const auto value_col = block.getByPosition(arguments.front()).column.get();
|
|
|
|
|
2017-07-24 04:14:35 +00:00
|
|
|
if (!execute<UInt8>(block, arguments, result, value_col)
|
|
|
|
&& !execute<UInt16>(block, arguments, result, value_col)
|
|
|
|
&& !execute<UInt32>(block, arguments, result, value_col)
|
|
|
|
&& !execute<UInt64>(block, arguments, result, value_col)
|
|
|
|
&& !execute<Int8>(block, arguments, result, value_col)
|
|
|
|
&& !execute<Int16>(block, arguments, result, value_col)
|
|
|
|
&& !execute<Int32>(block, arguments, result, value_col)
|
|
|
|
&& !execute<Int64>(block, arguments, result, value_col))
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception{
|
|
|
|
"Illegal column " + value_col->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
|
|
|
bool execute(
|
|
|
|
Block & block, const ColumnNumbers & arguments, const size_t result,
|
|
|
|
const IColumn * const value_col_untyped)
|
|
|
|
{
|
|
|
|
if (const auto value_col = checkAndGetColumn<ColumnVector<T>>(value_col_untyped))
|
|
|
|
{
|
|
|
|
const auto size = value_col->size();
|
|
|
|
bool is_const;
|
2017-12-01 21:40:58 +00:00
|
|
|
const auto mask = createConstMask<T>(block, arguments, is_const);
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & val = value_col->getData();
|
|
|
|
|
2017-12-16 06:02:54 +00:00
|
|
|
auto out_col = ColumnVector<UInt8>::create(size);
|
2017-07-21 06:35:58 +00:00
|
|
|
auto & out = out_col->getData();
|
|
|
|
|
|
|
|
if (is_const)
|
|
|
|
{
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val[i], mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto mask = createMask<T>(size, block, arguments);
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val[i], mask[i]);
|
|
|
|
}
|
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(out_col);
|
2017-07-21 06:35:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (const auto value_col = checkAndGetColumnConst<ColumnVector<T>>(value_col_untyped))
|
|
|
|
{
|
|
|
|
const auto size = value_col->size();
|
|
|
|
bool is_const;
|
2017-12-01 21:40:58 +00:00
|
|
|
const auto mask = createConstMask<T>(block, arguments, is_const);
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto val = value_col->template getValue<T>();
|
|
|
|
|
|
|
|
if (is_const)
|
|
|
|
{
|
2017-12-10 22:44:04 +00:00
|
|
|
block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(size, toField(Impl::apply(val, mask)));
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto mask = createMask<T>(size, block, arguments);
|
2017-12-16 06:02:54 +00:00
|
|
|
auto out_col = ColumnVector<UInt8>::create(size);
|
2017-07-21 06:35:58 +00:00
|
|
|
|
|
|
|
auto & out = out_col->getData();
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val, mask[i]);
|
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(out_col);
|
2017-07-21 06:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType>
|
2017-12-01 21:40:58 +00:00
|
|
|
ValueType createConstMask(const Block & block, const ColumnNumbers & arguments, bool & is_const)
|
2017-07-21 06:35:58 +00:00
|
|
|
{
|
|
|
|
is_const = true;
|
|
|
|
ValueType mask = 0;
|
|
|
|
|
|
|
|
for (const auto i : ext::range(1, arguments.size()))
|
|
|
|
{
|
|
|
|
if (auto pos_col_const = checkAndGetColumnConst<ColumnVector<ValueType>>(block.getByPosition(arguments[i]).column.get()))
|
|
|
|
{
|
|
|
|
const auto pos = pos_col_const->template getValue<ValueType>();
|
|
|
|
mask = mask | (1 << pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_const = false;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType>
|
|
|
|
PaddedPODArray<ValueType> createMask(const size_t size, const Block & block, const ColumnNumbers & arguments)
|
|
|
|
{
|
|
|
|
PaddedPODArray<ValueType> mask(size, ValueType{});
|
|
|
|
|
|
|
|
for (const auto i : ext::range(1, arguments.size()))
|
|
|
|
{
|
|
|
|
const auto pos_col = block.getByPosition(arguments[i]).column.get();
|
|
|
|
|
|
|
|
if (!addToMaskImpl<UInt8>(mask, pos_col)
|
|
|
|
&& !addToMaskImpl<UInt16>(mask, pos_col)
|
|
|
|
&& !addToMaskImpl<UInt32>(mask, pos_col)
|
|
|
|
&& !addToMaskImpl<UInt64>(mask, pos_col))
|
|
|
|
throw Exception{
|
|
|
|
"Illegal column " + pos_col->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PosType, typename ValueType>
|
|
|
|
bool addToMaskImpl(PaddedPODArray<ValueType> & mask, const IColumn * const pos_col_untyped)
|
|
|
|
{
|
|
|
|
if (const auto pos_col = checkAndGetColumn<ColumnVector<PosType>>(pos_col_untyped))
|
|
|
|
{
|
|
|
|
const auto & pos = pos_col->getData();
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, mask.size()))
|
|
|
|
mask[i] = mask[i] | (1 << pos[i]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (const auto pos_col = checkAndGetColumnConst<ColumnVector<PosType>>(pos_col_untyped))
|
|
|
|
{
|
|
|
|
const auto & pos = pos_col->template getValue<PosType>();
|
|
|
|
const auto new_mask = 1 << pos;
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, mask.size()))
|
|
|
|
mask[i] = mask[i] | new_mask;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct BitTestAnyImpl
|
|
|
|
{
|
|
|
|
template <typename A, typename B>
|
|
|
|
static inline UInt8 apply(A a, B b) { return (a & b) != 0; };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BitTestAllImpl
|
|
|
|
{
|
|
|
|
template <typename A, typename B>
|
|
|
|
static inline UInt8 apply(A a, B b) { return (a & b) == b; };
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
using FunctionBitTestAny = FunctionBitTestMany<BitTestAnyImpl, NameBitTestAny>;
|
|
|
|
using FunctionBitTestAll = FunctionBitTestMany<BitTestAllImpl, NameBitTestAll>;
|
2014-08-17 02:13:40 +00:00
|
|
|
|
2011-08-09 15:57:33 +00:00
|
|
|
}
|