2011-08-22 01:01:01 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2017-12-25 06:33:51 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2018-09-03 04:57:01 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-07-23 08:40:43 +00:00
|
|
|
#include <type_traits>
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2018-09-03 04:57:01 +00:00
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
#if USE_EMBEDDED_COMPILER
|
2018-09-03 06:31:19 +00:00
|
|
|
#include <DataTypes/Native.h>
|
|
|
|
|
2018-05-06 09:29:57 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2018-06-21 15:23:20 +00:00
|
|
|
#include <llvm/IR/IRBuilder.h> // Y_IGNORE
|
2018-05-06 09:29:57 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2018-04-30 12:33:40 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-22 01:01:01 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-09-03 04:57:01 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-06-13 02:06:53 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2018-09-03 04:57:01 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-06-13 02:06:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 06:33:51 +00:00
|
|
|
/** Behaviour in presence of NULLs:
|
|
|
|
*
|
|
|
|
* Functions AND, XOR, NOT use default implementation for NULLs:
|
|
|
|
* - if one of arguments is Nullable, they return Nullable result where NULLs are returned when at least one argument was NULL.
|
|
|
|
*
|
|
|
|
* But function OR is different.
|
|
|
|
* It always return non-Nullable result and NULL are equivalent to 0 (false).
|
|
|
|
* For example, 1 OR NULL returns 1, not NULL.
|
2011-08-22 01:01:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct AndImpl
|
|
|
|
{
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturable()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturatedValue(bool a)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return !a;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool apply(bool a, bool b)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return a && b;
|
|
|
|
}
|
2017-12-25 06:33:51 +00:00
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool specialImplementationForNulls() { return false; }
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct OrImpl
|
|
|
|
{
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturable()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturatedValue(bool a)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool apply(bool a, bool b)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return a || b;
|
|
|
|
}
|
2017-12-25 06:33:51 +00:00
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool specialImplementationForNulls() { return true; }
|
2014-02-13 10:24:56 +00:00
|
|
|
};
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2014-02-13 10:24:56 +00:00
|
|
|
struct XorImpl
|
|
|
|
{
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturable()
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool isSaturatedValue(bool)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool apply(bool a, bool b)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
return a != b;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-12-25 06:33:51 +00:00
|
|
|
|
2018-04-30 12:33:40 +00:00
|
|
|
static inline constexpr bool specialImplementationForNulls() { return false; }
|
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static inline llvm::Value * apply(llvm::IRBuilder<> & builder, llvm::Value * a, llvm::Value * b)
|
|
|
|
{
|
|
|
|
return builder.CreateXor(a, b);
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-13 10:24:56 +00:00
|
|
|
};
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <typename A>
|
2014-02-13 10:24:56 +00:00
|
|
|
struct NotImpl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultType = UInt8;
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static inline UInt8 apply(A a)
|
|
|
|
{
|
|
|
|
return !a;
|
|
|
|
}
|
2018-04-30 12:33:40 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
static inline llvm::Value * apply(llvm::IRBuilder<> & builder, llvm::Value * a)
|
|
|
|
{
|
|
|
|
return builder.CreateNot(a);
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 10:24:56 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
using UInt8Container = ColumnUInt8::Container;
|
2016-05-28 10:35:44 +00:00
|
|
|
using UInt8ColumnPtrs = std::vector<const ColumnUInt8 *>;
|
2014-02-13 10:24:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename Op, size_t N>
|
|
|
|
struct AssociativeOperationImpl
|
2011-08-22 01:01:01 +00:00
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Erases the N last columns from `in` (if there are less, then all) and puts into `result` their combination.
|
2017-09-16 22:24:48 +00:00
|
|
|
static void NO_INLINE execute(UInt8ColumnPtrs & in, UInt8Container & result)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (N > in.size())
|
|
|
|
{
|
|
|
|
AssociativeOperationImpl<Op, N - 1>::execute(in, result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AssociativeOperationImpl<Op, N> operation(in);
|
|
|
|
in.erase(in.end() - N, in.end());
|
|
|
|
|
|
|
|
size_t n = result.size();
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
result[i] = operation.apply(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UInt8Container & vec;
|
|
|
|
AssociativeOperationImpl<Op, N - 1> continuation;
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Remembers the last N columns from `in`.
|
2017-04-01 07:20:54 +00:00
|
|
|
AssociativeOperationImpl(UInt8ColumnPtrs & in)
|
|
|
|
: vec(in[in.size() - N]->getData()), continuation(in) {}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Returns a combination of values in the i-th row of all columns stored in the constructor.
|
2017-04-01 07:20:54 +00:00
|
|
|
inline UInt8 apply(size_t i) const
|
|
|
|
{
|
|
|
|
if (Op::isSaturable())
|
|
|
|
{
|
|
|
|
UInt8 a = vec[i];
|
|
|
|
return Op::isSaturatedValue(a) ? a : continuation.apply(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Op::apply(vec[i], continuation.apply(i));
|
|
|
|
}
|
|
|
|
}
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 10:24:56 +00:00
|
|
|
template <typename Op>
|
|
|
|
struct AssociativeOperationImpl<Op, 1>
|
2011-08-22 01:01:01 +00:00
|
|
|
{
|
2017-12-02 02:47:12 +00:00
|
|
|
static void execute(UInt8ColumnPtrs &, UInt8Container &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
throw Exception("Logical error: AssociativeOperationImpl<Op, 1>::execute called", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const UInt8Container & vec;
|
2014-02-13 10:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AssociativeOperationImpl(UInt8ColumnPtrs & in)
|
|
|
|
: vec(in[in.size() - 1]->getData()) {}
|
2014-02-13 10:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
inline UInt8 apply(size_t i) const
|
|
|
|
{
|
|
|
|
return vec[i];
|
|
|
|
}
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-12-25 05:48:53 +00:00
|
|
|
template <typename Impl, typename Name>
|
2014-02-13 10:24:56 +00:00
|
|
|
class FunctionAnyArityLogical : public IFunction
|
2011-08-22 01:01:01 +00:00
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2018-06-03 20:39:06 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionAnyArityLogical>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2011-08-22 01:01:01 +00:00
|
|
|
private:
|
2017-12-16 02:34:02 +00:00
|
|
|
bool extractConstColumns(ColumnRawPtrs & in, UInt8 & res)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
bool has_res = false;
|
|
|
|
for (int i = static_cast<int>(in.size()) - 1; i >= 0; --i)
|
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
if (!in[i]->isColumnConst())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Field value = (*in[i])[0];
|
|
|
|
|
|
|
|
UInt8 x = !value.isNull() && applyVisitor(FieldVisitorConvertToNumber<bool>(), value);
|
|
|
|
if (has_res)
|
|
|
|
{
|
|
|
|
res = Impl::apply(res, x);
|
|
|
|
}
|
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 06:33:51 +00:00
|
|
|
res = x;
|
|
|
|
has_res = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-12-25 06:33:51 +00:00
|
|
|
|
|
|
|
in.erase(in.begin() + i);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
return has_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool convertTypeToUInt8(const IColumn * column, UInt8Container & res)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
auto col = checkAndGetColumn<ColumnVector<T>>(column);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!col)
|
|
|
|
return false;
|
2017-12-25 06:33:51 +00:00
|
|
|
const auto & vec = col->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = res.size();
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
res[i] = !!vec[i];
|
2017-12-16 04:29:34 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-25 06:33:51 +00:00
|
|
|
template <typename T>
|
|
|
|
bool convertNullableTypeToUInt8(const IColumn * column, UInt8Container & res)
|
|
|
|
{
|
|
|
|
auto col_nullable = checkAndGetColumn<ColumnNullable>(column);
|
|
|
|
|
|
|
|
auto col = checkAndGetColumn<ColumnVector<T>>(&col_nullable->getNestedColumn());
|
|
|
|
if (!col)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto & vec = col->getData();
|
|
|
|
const auto & null_map = col_nullable->getNullMapData();
|
|
|
|
|
|
|
|
size_t n = res.size();
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
res[i] = !!vec[i] && !null_map[i];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void convertToUInt8(const IColumn * column, UInt8Container & res)
|
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
if (!convertTypeToUInt8<Int8>(column, res) &&
|
|
|
|
!convertTypeToUInt8<Int16>(column, res) &&
|
|
|
|
!convertTypeToUInt8<Int32>(column, res) &&
|
|
|
|
!convertTypeToUInt8<Int64>(column, res) &&
|
|
|
|
!convertTypeToUInt8<UInt16>(column, res) &&
|
|
|
|
!convertTypeToUInt8<UInt32>(column, res) &&
|
|
|
|
!convertTypeToUInt8<UInt64>(column, res) &&
|
2017-07-24 04:14:35 +00:00
|
|
|
!convertTypeToUInt8<Float32>(column, res) &&
|
2017-12-25 06:33:51 +00:00
|
|
|
!convertTypeToUInt8<Float64>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Int8>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Int16>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Int32>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Int64>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<UInt8>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<UInt16>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<UInt32>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<UInt64>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Float32>(column, res) &&
|
|
|
|
!convertNullableTypeToUInt8<Float64>(column, res))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Unexpected type of column: " + column->getName(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
2011-08-22 01:01:01 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
2017-12-25 06:33:51 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return !Impl::specialImplementationForNulls(); }
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get result types by argument types. If the function does not apply to these arguments, throw an exception.
|
2017-04-01 07:20:54 +00:00
|
|
|
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::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!(isNumber(arguments[i])
|
|
|
|
|| (Impl::specialImplementationForNulls() && (arguments[i]->onlyNull() || isNumber(removeNullable(arguments[i]))))))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type ("
|
|
|
|
+ arguments[i]->getName()
|
|
|
|
+ ") of " + toString(i + 1) + " argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
size_t num_arguments = arguments.size();
|
|
|
|
ColumnRawPtrs in(num_arguments);
|
|
|
|
for (size_t i = 0; i < num_arguments; ++i)
|
2017-07-21 06:35:58 +00:00
|
|
|
in[i] = block.getByPosition(arguments[i]).column.get();
|
2017-12-16 04:29:34 +00:00
|
|
|
|
|
|
|
size_t rows = in[0]->size();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Combine all constant columns into a single value.
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt8 const_val = 0;
|
|
|
|
bool has_consts = extractConstColumns(in, const_val);
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
// If this value uniquely determines the result, return it.
|
2017-12-25 05:48:53 +00:00
|
|
|
if (has_consts && (in.empty() || Impl::apply(const_val, 0) == Impl::apply(const_val, 1)))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!in.empty())
|
2017-12-25 05:48:53 +00:00
|
|
|
const_val = Impl::apply(const_val, 0);
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = DataTypeUInt8().createColumnConst(rows, toField(const_val));
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// If this value is a neutral element, let's forget about it.
|
2017-12-25 05:48:53 +00:00
|
|
|
if (has_consts && Impl::apply(const_val, 0) == 0 && Impl::apply(const_val, 1) == 1)
|
2017-04-01 07:20:54 +00:00
|
|
|
has_consts = false;
|
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnUInt8::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt8Container & vec_res = col_res->getData();
|
|
|
|
|
|
|
|
if (has_consts)
|
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
vec_res.assign(rows, const_val);
|
2017-04-01 07:20:54 +00:00
|
|
|
in.push_back(col_res.get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-16 04:29:34 +00:00
|
|
|
vec_res.resize(rows);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 05:48:53 +00:00
|
|
|
/// Convert all columns to UInt8
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt8ColumnPtrs uint8_in;
|
2017-12-25 05:48:53 +00:00
|
|
|
Columns converted_columns;
|
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
for (const IColumn * column : in)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-25 05:48:53 +00:00
|
|
|
if (auto uint8_column = checkAndGetColumn<ColumnUInt8>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
uint8_in.push_back(uint8_column);
|
|
|
|
else
|
2017-12-25 05:48:53 +00:00
|
|
|
{
|
|
|
|
auto converted_column = ColumnUInt8::create(rows);
|
|
|
|
convertToUInt8(column, converted_column->getData());
|
|
|
|
uint8_in.push_back(converted_column.get());
|
|
|
|
converted_columns.emplace_back(std::move(converted_column));
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
/// Effeciently combine all the columns of the correct type.
|
2017-04-01 07:20:54 +00:00
|
|
|
while (uint8_in.size() > 1)
|
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// With a large block size, combining 6 columns per pass is the fastest.
|
|
|
|
/// When small - more, is faster.
|
2017-12-25 05:48:53 +00:00
|
|
|
AssociativeOperationImpl<Impl, 10>::execute(uint8_in, vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
uint8_in.push_back(col_res.get());
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// This is possible if there is exactly one non-constant among the arguments, and it is of type UInt8.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (uint8_in[0] != col_res.get())
|
|
|
|
vec_res.assign(uint8_in[0]->getData());
|
2017-12-16 04:29:34 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-04-30 12:33:40 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
bool isCompilableImpl(const DataTypes &) const override { return true; }
|
|
|
|
|
|
|
|
llvm::Value * compileImpl(llvm::IRBuilderBase & builder, const DataTypes & types, ValuePlaceholders values) const override
|
|
|
|
{
|
|
|
|
auto & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
if constexpr (!Impl::isSaturable())
|
|
|
|
{
|
2018-05-07 19:21:23 +00:00
|
|
|
auto * result = nativeBoolCast(b, types[0], values[0]());
|
2018-04-30 12:33:40 +00:00
|
|
|
for (size_t i = 1; i < types.size(); i++)
|
2018-05-07 19:21:23 +00:00
|
|
|
result = Impl::apply(b, result, nativeBoolCast(b, types[i], values[i]()));
|
2018-04-30 12:33:40 +00:00
|
|
|
return b.CreateSelect(result, b.getInt8(1), b.getInt8(0));
|
|
|
|
}
|
|
|
|
constexpr bool breakOnTrue = Impl::isSaturatedValue(true);
|
|
|
|
auto * next = b.GetInsertBlock();
|
|
|
|
auto * stop = llvm::BasicBlock::Create(next->getContext(), "", next->getParent());
|
|
|
|
b.SetInsertPoint(stop);
|
|
|
|
auto * phi = b.CreatePHI(b.getInt8Ty(), values.size());
|
|
|
|
for (size_t i = 0; i < types.size(); i++)
|
|
|
|
{
|
|
|
|
b.SetInsertPoint(next);
|
|
|
|
auto * value = values[i]();
|
2018-05-07 19:21:23 +00:00
|
|
|
auto * truth = nativeBoolCast(b, types[i], value);
|
2018-04-30 12:33:40 +00:00
|
|
|
if (!types[i]->equals(DataTypeUInt8{}))
|
|
|
|
value = b.CreateSelect(truth, b.getInt8(1), b.getInt8(0));
|
|
|
|
phi->addIncoming(value, b.GetInsertBlock());
|
|
|
|
if (i + 1 < types.size())
|
|
|
|
{
|
|
|
|
next = llvm::BasicBlock::Create(next->getContext(), "", next->getParent());
|
|
|
|
b.CreateCondBr(truth, breakOnTrue ? stop : next, breakOnTrue ? next : stop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.CreateBr(stop);
|
|
|
|
b.SetInsertPoint(stop);
|
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-09-03 04:57:01 +00:00
|
|
|
template <typename A, typename Op>
|
|
|
|
struct UnaryOperationImpl
|
|
|
|
{
|
|
|
|
using ResultType = typename Op::ResultType;
|
|
|
|
using ArrayA = typename ColumnVector<A>::Container;
|
|
|
|
using ArrayC = typename ColumnVector<ResultType>::Container;
|
|
|
|
|
|
|
|
static void NO_INLINE vector(const ArrayA & a, ArrayC & c)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-22 01:01:01 +00:00
|
|
|
template <template <typename> class Impl, typename Name>
|
|
|
|
class FunctionUnaryLogical : 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;
|
2018-06-03 20:39:06 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionUnaryLogical>(); }
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (auto col = checkAndGetColumn<ColumnVector<T>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnUInt8::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnUInt8::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<T, Impl<T>>::vector(col->getData(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-16 04:29:34 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-22 01:01:01 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isNumber(arguments[0]))
|
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 std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-11-24 01:48:06 +00:00
|
|
|
if (!(executeType<UInt8>(block, arguments, result)
|
2017-07-24 04:14:35 +00:00
|
|
|
|| 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);
|
|
|
|
}
|
2018-04-30 12:33:40 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
bool isCompilableImpl(const DataTypes &) const override { return true; }
|
|
|
|
|
|
|
|
llvm::Value * compileImpl(llvm::IRBuilderBase & builder, const DataTypes & types, ValuePlaceholders values) const override
|
|
|
|
{
|
|
|
|
auto & b = static_cast<llvm::IRBuilder<> &>(builder);
|
2018-05-07 19:21:23 +00:00
|
|
|
return b.CreateSelect(Impl<UInt8>::apply(b, nativeBoolCast(b, types[0], values[0]())), b.getInt8(1), b.getInt8(0));
|
2018-04-30 12:33:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-08-22 01:01:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
struct NameAnd { static constexpr auto name = "and"; };
|
|
|
|
struct NameOr { static constexpr auto name = "or"; };
|
|
|
|
struct NameXor { static constexpr auto name = "xor"; };
|
|
|
|
struct NameNot { static constexpr auto name = "not"; };
|
2011-08-22 01:01:01 +00:00
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
using FunctionAnd = FunctionAnyArityLogical<AndImpl, NameAnd>;
|
|
|
|
using FunctionOr = FunctionAnyArityLogical<OrImpl, NameOr>;
|
|
|
|
using FunctionXor = FunctionAnyArityLogical<XorImpl, NameXor>;
|
|
|
|
using FunctionNot = FunctionUnaryLogical<NotImpl, NameNot>;
|
2011-08-22 01:01:01 +00:00
|
|
|
|
|
|
|
}
|