clickhouse: added bitwise and and or functions [#CONV-6318].

This commit is contained in:
Michael Kolupaev 2012-12-20 13:41:55 +00:00
parent 57fccc9387
commit 1383bfd005
3 changed files with 111 additions and 3 deletions

View File

@ -240,6 +240,90 @@ struct ModuloImpl
}
};
template<typename A, typename B>
struct BitwiseAndImpl
{
typedef typename NumberTraits::ResultOfBitwise<A, B>::Type ResultType;
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
{
size_t size = a.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a[i])
& static_cast<ResultType>(b[i]);
}
}
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
{
size_t size = a.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a[i])
& static_cast<ResultType>(b);
}
}
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
{
size_t size = b.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a)
& static_cast<ResultType>(b[i]);
}
}
static void constant_constant(A a, B b, ResultType & c)
{
c = static_cast<ResultType>(a)
& static_cast<ResultType>(b);
}
};
template<typename A, typename B>
struct BitwiseOrImpl
{
typedef typename NumberTraits::ResultOfBitwise<A, B>::Type ResultType;
static void vector_vector(const std::vector<A> & a, const std::vector<B> & b, std::vector<ResultType> & c)
{
size_t size = a.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a[i])
| static_cast<ResultType>(b[i]);
}
}
static void vector_constant(const std::vector<A> & a, B b, std::vector<ResultType> & c)
{
size_t size = a.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a[i])
| static_cast<ResultType>(b);
}
}
static void constant_vector(A a, const std::vector<B> & b, std::vector<ResultType> & c)
{
size_t size = b.size();
for (size_t i = 0; i < size; ++i)
{
c[i] = static_cast<ResultType>(a)
| static_cast<ResultType>(b[i]);
}
}
static void constant_constant(A a, B b, ResultType & c)
{
c = static_cast<ResultType>(a)
| static_cast<ResultType>(b);
}
};
template<typename A>
struct NegateImpl
{
@ -567,14 +651,18 @@ struct NameMultiply { static const char * get() { return "multiply"; } };
struct NameDivideFloating { static const char * get() { return "divide"; } };
struct NameDivideIntegral { static const char * get() { return "intDiv"; } };
struct NameModulo { static const char * get() { return "modulo"; } };
struct NameBitwiseAnd { static const char * get() { return "bitwiseAnd"; } };
struct NameBitwiseOr { static const char * get() { return "bitwiseOr"; } };
struct NameNegate { static const char * get() { return "negate"; } };
typedef FunctionBinaryArithmetic<PlusImpl, NamePlus> FunctionPlus;
typedef FunctionBinaryArithmetic<PlusImpl, NamePlus> FunctionPlus;
typedef FunctionBinaryArithmetic<MinusImpl, NameMinus> FunctionMinus;
typedef FunctionBinaryArithmetic<MultiplyImpl, NameMultiply> FunctionMultiply;
typedef FunctionBinaryArithmetic<MultiplyImpl, NameMultiply> FunctionMultiply;
typedef FunctionBinaryArithmetic<DivideFloatingImpl, NameDivideFloating> FunctionDivideFloating;
typedef FunctionBinaryArithmetic<DivideIntegralImpl, NameDivideIntegral> FunctionDivideIntegral;
typedef FunctionBinaryArithmetic<ModuloImpl, NameModulo> FunctionModulo;
typedef FunctionBinaryArithmetic<BitwiseAndImpl, NameBitwiseAnd> FunctionBitwiseAnd;
typedef FunctionBinaryArithmetic<BitwiseOrImpl, NameBitwiseOr> FunctionBitwiseOr;
typedef FunctionUnaryArithmetic<NegateImpl, NameNegate> FunctionNegate;

View File

@ -146,7 +146,25 @@ namespace NumberTraits
typename Traits<A>::Bits>::Type Type;
};
/** Перед применением оператора %, операнды приводятся к целым числам. */
/** При побитовых операциях получается целое число, битность которого равна максимальной из битностей аргументов.
*/
template <typename A, typename B> struct ResultOfBitwise
{
typedef typename Construct<
typename boost::mpl::or_<typename Traits<A>::Sign, typename Traits<B>::Sign>::type,
Integer,
typename boost::mpl::max<
typename boost::mpl::if_<
typename Traits<A>::Floatness,
Bits64,
typename Traits<A>::Bits>::type,
typename boost::mpl::if_<
typename Traits<B>::Floatness,
Bits64,
typename Traits<B>::Bits>::type>::type>::Type Type;
};
/** Перед применением оператора % и побитовых операций, операнды приводятся к целым числам. */
template <typename A> struct ToInteger
{
typedef typename Construct<

View File

@ -34,6 +34,8 @@ FunctionPtr FunctionFactory::get(
else if (name == "divide") return new FunctionDivideFloating;
else if (name == "intDiv") return new FunctionDivideIntegral;
else if (name == "modulo") return new FunctionModulo;
else if (name == "bitwiseAnd") return new FunctionBitwiseAnd;
else if (name == "bitwiseOr") return new FunctionBitwiseOr;
else if (name == "negate") return new FunctionNegate;
else if (name == "equals") return new FunctionEquals;