2018-11-26 16:20:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-11-26 16:20:40 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <ext/range.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-12-07 03:20:27 +00:00
|
|
|
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 "
|
2018-12-07 03:20:27 +00:00
|
|
|
+ toString(arguments.size()) + ", should be at least 2.", ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION};
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
const auto & first_arg = arguments.front();
|
|
|
|
|
|
|
|
if (!isInteger(first_arg))
|
|
|
|
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];
|
|
|
|
|
|
|
|
if (!isUnsignedInteger(pos_arg))
|
|
|
|
throw Exception{"Illegal type " + pos_arg->getName() + " of " + toString(i) + " argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnPtr executeImpl(ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto * value_col = arguments.front().column.get();
|
|
|
|
|
|
|
|
ColumnPtr res;
|
2020-10-20 13:11:57 +00:00
|
|
|
if (!((res = execute<UInt8>(arguments, result_type, value_col))
|
2020-10-17 16:48:53 +00:00
|
|
|
|| (res = execute<UInt16>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<UInt32>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<UInt64>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<Int8>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<Int16>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<Int32>(arguments, result_type, value_col))
|
|
|
|
|| (res = execute<Int64>(arguments, result_type, value_col))))
|
2018-11-26 16:20:40 +00:00
|
|
|
throw Exception{"Illegal column " + value_col->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
2020-10-17 16:48:53 +00:00
|
|
|
|
|
|
|
return res;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
2020-10-17 16:48:53 +00:00
|
|
|
ColumnPtr execute(
|
|
|
|
ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type,
|
2020-10-14 13:09:11 +00:00
|
|
|
const IColumn * const value_col_untyped) const
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
if (const auto value_col = checkAndGetColumn<ColumnVector<T>>(value_col_untyped))
|
|
|
|
{
|
|
|
|
const auto size = value_col->size();
|
|
|
|
bool is_const;
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto const_mask = createConstMaskIfConst<T>(arguments, is_const);
|
2018-11-26 16:20:40 +00:00
|
|
|
const auto & val = value_col->getData();
|
|
|
|
|
|
|
|
auto out_col = ColumnVector<UInt8>::create(size);
|
|
|
|
auto & out = out_col->getData();
|
|
|
|
|
|
|
|
if (is_const)
|
|
|
|
{
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val[i], const_mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto mask = createMask<T>(size, arguments);
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val[i], mask[i]);
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
return out_col;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
else if (const auto value_col_const = checkAndGetColumnConst<ColumnVector<T>>(value_col_untyped))
|
|
|
|
{
|
|
|
|
const auto size = value_col_const->size();
|
|
|
|
bool is_const;
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto const_mask = createConstMaskIfConst<T>(arguments, is_const);
|
2018-11-26 16:20:40 +00:00
|
|
|
const auto val = value_col_const->template getValue<T>();
|
|
|
|
|
|
|
|
if (is_const)
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
return result_type->createColumnConst(size, toField(Impl::apply(val, const_mask)));
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto mask = createMask<T>(size, arguments);
|
2018-11-26 16:20:40 +00:00
|
|
|
auto out_col = ColumnVector<UInt8>::create(size);
|
|
|
|
|
|
|
|
auto & out = out_col->getData();
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = Impl::apply(val, mask[i]);
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
return out_col;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:48:53 +00:00
|
|
|
return nullptr;
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType>
|
2020-10-17 16:48:53 +00:00
|
|
|
ValueType createConstMaskIfConst(const ColumnsWithTypeAndName & arguments, bool & out_is_const) const
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
out_is_const = true;
|
|
|
|
ValueType mask = 0;
|
|
|
|
|
|
|
|
for (const auto i : ext::range(1, arguments.size()))
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
if (auto pos_col_const = checkAndGetColumnConst<ColumnVector<ValueType>>(arguments[i].column.get()))
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
2020-02-17 06:54:24 +00:00
|
|
|
const auto pos = pos_col_const->getUInt(0);
|
|
|
|
if (pos < 8 * sizeof(ValueType))
|
|
|
|
mask = mask | (ValueType(1) << pos);
|
2018-11-26 16:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out_is_const = false;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueType>
|
2020-10-17 16:48:53 +00:00
|
|
|
PaddedPODArray<ValueType> createMask(const size_t size, const ColumnsWithTypeAndName & arguments) const
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
PaddedPODArray<ValueType> mask(size, ValueType{});
|
|
|
|
|
|
|
|
for (const auto i : ext::range(1, arguments.size()))
|
|
|
|
{
|
2020-10-17 16:48:53 +00:00
|
|
|
const auto * pos_col = arguments[i].column.get();
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
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>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool NO_SANITIZE_UNDEFINED addToMaskImpl(PaddedPODArray<ValueType> & mask, const IColumn * const pos_col_untyped) const
|
2018-11-26 16:20:40 +00:00
|
|
|
{
|
|
|
|
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()))
|
2020-02-17 06:54:24 +00:00
|
|
|
if (pos[i] < 8 * sizeof(ValueType))
|
|
|
|
mask[i] = mask[i] | (ValueType(1) << pos[i]);
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (const auto pos_col_const = checkAndGetColumnConst<ColumnVector<PosType>>(pos_col_untyped))
|
|
|
|
{
|
|
|
|
const auto & pos = pos_col_const->template getValue<PosType>();
|
2020-02-17 06:54:24 +00:00
|
|
|
const auto new_mask = pos < 8 * sizeof(ValueType) ? ValueType(1) << pos : 0;
|
2018-11-26 16:20:40 +00:00
|
|
|
|
|
|
|
for (const auto i : ext::range(0, mask.size()))
|
|
|
|
mask[i] = mask[i] | new_mask;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|