2017-08-30 18:52:14 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2017-08-30 18:52:14 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2017-08-30 18:52:14 +00:00
|
|
|
|
2021-10-27 23:10:39 +00:00
|
|
|
#include <Common/config.h>
|
2021-07-09 22:51:34 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
# include <llvm/IR/IRBuilder.h>
|
|
|
|
# include <DataTypes/Native.h>
|
|
|
|
#endif
|
2017-08-30 18:52:14 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
2017-08-30 18:52:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionGroupBitOrData
|
|
|
|
{
|
|
|
|
T value = 0;
|
|
|
|
static const char * name() { return "groupBitOr"; }
|
|
|
|
void update(T x) { value |= x; }
|
2021-07-09 22:51:34 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
static void compileCreate(llvm::IRBuilderBase & builder, llvm::Value * value_ptr)
|
|
|
|
{
|
|
|
|
auto type = toNativeType<T>(builder);
|
|
|
|
builder.CreateStore(llvm::Constant::getNullValue(type), value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value* compileUpdate(llvm::IRBuilderBase & builder, llvm::Value * lhs, llvm::Value * rhs)
|
|
|
|
{
|
|
|
|
return builder.CreateOr(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-08-30 18:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionGroupBitAndData
|
|
|
|
{
|
|
|
|
T value = -1; /// Two's complement arithmetic, sign extension.
|
|
|
|
static const char * name() { return "groupBitAnd"; }
|
|
|
|
void update(T x) { value &= x; }
|
2021-07-09 22:51:34 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
static void compileCreate(llvm::IRBuilderBase & builder, llvm::Value * value_ptr)
|
|
|
|
{
|
|
|
|
auto type = toNativeType<T>(builder);
|
|
|
|
builder.CreateStore(llvm::ConstantInt::get(type, -1), value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value* compileUpdate(llvm::IRBuilderBase & builder, llvm::Value * lhs, llvm::Value * rhs)
|
|
|
|
{
|
|
|
|
return builder.CreateAnd(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-08-30 18:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionGroupBitXorData
|
|
|
|
{
|
|
|
|
T value = 0;
|
|
|
|
static const char * name() { return "groupBitXor"; }
|
|
|
|
void update(T x) { value ^= x; }
|
2021-07-09 22:51:34 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
static void compileCreate(llvm::IRBuilderBase & builder, llvm::Value * value_ptr)
|
|
|
|
{
|
|
|
|
auto type = toNativeType<T>(builder);
|
|
|
|
builder.CreateStore(llvm::Constant::getNullValue(type), value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value* compileUpdate(llvm::IRBuilderBase & builder, llvm::Value * lhs, llvm::Value * rhs)
|
|
|
|
{
|
|
|
|
return builder.CreateXor(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-08-30 18:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Counts bitwise operation on numbers.
|
|
|
|
template <typename T, typename Data>
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionBitwise final : public IAggregateFunctionDataHelper<Data, AggregateFunctionBitwise<T, Data>>
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-07-09 22:51:34 +00:00
|
|
|
explicit AggregateFunctionBitwise(const DataTypePtr & type)
|
2019-02-11 19:26:32 +00:00
|
|
|
: IAggregateFunctionDataHelper<Data, AggregateFunctionBitwise<T, Data>>({type}, {}) {}
|
|
|
|
|
2017-08-30 18:52:14 +00:00
|
|
|
String getName() const override { return Data::name(); }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeNumber<T>>();
|
|
|
|
}
|
|
|
|
|
2021-04-09 17:12:06 +00:00
|
|
|
bool allocatesMemoryInArena() const override { return false; }
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void add(AggregateDataPtr __restrict place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
this->data(place).update(assert_cast<const ColumnVector<T> &>(*columns[0]).getData()[row_num]);
|
2017-08-30 18:52:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
|
|
|
this->data(place).update(this->data(rhs).value);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf) const override
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
|
|
|
writeBinary(this->data(place).value, buf);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, Arena *) const override
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
|
|
|
readBinary(this->data(place).value, buf);
|
|
|
|
}
|
|
|
|
|
2021-02-01 17:12:12 +00:00
|
|
|
void insertResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena *) const override
|
2017-08-30 18:52:14 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnVector<T> &>(to).getData().push_back(this->data(place).value);
|
2017-08-30 18:52:14 +00:00
|
|
|
}
|
2021-07-09 22:51:34 +00:00
|
|
|
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
|
|
|
|
bool isCompilable() const override
|
|
|
|
{
|
|
|
|
auto return_type = getReturnType();
|
|
|
|
return canBeNativeType(*return_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compileCreate(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_ptr) const override
|
|
|
|
{
|
|
|
|
llvm::IRBuilder<> & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
|
|
|
|
auto * return_type = toNativeType(b, getReturnType());
|
|
|
|
auto * value_ptr = b.CreatePointerCast(aggregate_data_ptr, return_type->getPointerTo());
|
|
|
|
Data::compileCreate(builder, value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compileAdd(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_ptr, const DataTypes &, const std::vector<llvm::Value *> & argument_values) const override
|
|
|
|
{
|
|
|
|
llvm::IRBuilder<> & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
|
|
|
|
auto * return_type = toNativeType(b, getReturnType());
|
|
|
|
|
|
|
|
auto * value_ptr = b.CreatePointerCast(aggregate_data_ptr, return_type->getPointerTo());
|
|
|
|
auto * value = b.CreateLoad(return_type, value_ptr);
|
|
|
|
|
|
|
|
const auto & argument_value = argument_values[0];
|
|
|
|
auto * result_value = Data::compileUpdate(builder, value, argument_value);
|
|
|
|
|
|
|
|
b.CreateStore(result_value, value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void compileMerge(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_dst_ptr, llvm::Value * aggregate_data_src_ptr) const override
|
|
|
|
{
|
|
|
|
llvm::IRBuilder<> & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
|
|
|
|
auto * return_type = toNativeType(b, getReturnType());
|
|
|
|
|
|
|
|
auto * value_dst_ptr = b.CreatePointerCast(aggregate_data_dst_ptr, return_type->getPointerTo());
|
|
|
|
auto * value_dst = b.CreateLoad(return_type, value_dst_ptr);
|
|
|
|
|
|
|
|
auto * value_src_ptr = b.CreatePointerCast(aggregate_data_src_ptr, return_type->getPointerTo());
|
|
|
|
auto * value_src = b.CreateLoad(return_type, value_src_ptr);
|
|
|
|
|
|
|
|
auto * result_value = Data::compileUpdate(builder, value_dst, value_src);
|
|
|
|
|
|
|
|
b.CreateStore(result_value, value_dst_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value * compileGetResult(llvm::IRBuilderBase & builder, llvm::Value * aggregate_data_ptr) const override
|
|
|
|
{
|
|
|
|
llvm::IRBuilder<> & b = static_cast<llvm::IRBuilder<> &>(builder);
|
|
|
|
|
|
|
|
auto * return_type = toNativeType(b, getReturnType());
|
|
|
|
auto * value_ptr = b.CreatePointerCast(aggregate_data_ptr, return_type->getPointerTo());
|
|
|
|
|
|
|
|
return b.CreateLoad(return_type, value_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-08-30 18:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|