2018-12-02 02:47:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-12-02 02:47:47 +00:00
|
|
|
#include <DataTypes/Native.h>
|
|
|
|
|
2022-09-28 08:45:15 +00:00
|
|
|
#include "config.h"
|
2018-12-02 02:47:47 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class FunctionIfBase : public IFunction
|
|
|
|
{
|
|
|
|
#if USE_EMBEDDED_COMPILER
|
|
|
|
public:
|
|
|
|
bool isCompilableImpl(const DataTypes & types) const override
|
|
|
|
{
|
2019-02-03 22:08:17 +00:00
|
|
|
/// It's difficult to compare Date and DateTime - cannot use JIT compilation.
|
|
|
|
bool has_date = false;
|
|
|
|
bool has_datetime = false;
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
for (const auto & type : types)
|
2019-02-03 22:08:17 +00:00
|
|
|
{
|
|
|
|
auto type_removed_nullable = removeNullable(type);
|
|
|
|
WhichDataType which(type_removed_nullable);
|
|
|
|
|
|
|
|
if (which.isDate())
|
|
|
|
has_date = true;
|
|
|
|
if (which.isDateTime())
|
|
|
|
has_datetime = true;
|
|
|
|
|
|
|
|
if (has_date && has_datetime)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!isCompilableType(type_removed_nullable))
|
2018-12-02 02:47:47 +00:00
|
|
|
return false;
|
2019-02-03 22:08:17 +00:00
|
|
|
}
|
2018-12-02 02:47:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-03 22:34:40 +00:00
|
|
|
llvm::Value * compileImpl(llvm::IRBuilderBase & builder, const DataTypes & types, Values values) const override
|
2018-12-02 02:47:47 +00:00
|
|
|
{
|
|
|
|
auto & b = static_cast<llvm::IRBuilder<> &>(builder);
|
2021-05-07 08:43:06 +00:00
|
|
|
auto return_type = getReturnTypeImpl(types);
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
auto * head = b.GetInsertBlock();
|
2021-05-07 08:43:06 +00:00
|
|
|
auto * join = llvm::BasicBlock::Create(head->getContext(), "join_block", head->getParent());
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
std::vector<std::pair<llvm::BasicBlock *, llvm::Value *>> returns;
|
|
|
|
for (size_t i = 0; i + 1 < types.size(); i += 2)
|
|
|
|
{
|
2021-05-07 08:43:06 +00:00
|
|
|
auto * then = llvm::BasicBlock::Create(head->getContext(), "then_" + std::to_string(i), head->getParent());
|
|
|
|
auto * next = llvm::BasicBlock::Create(head->getContext(), "next_" + std::to_string(i), head->getParent());
|
2021-05-03 22:34:40 +00:00
|
|
|
auto * cond = values[i];
|
2021-05-07 08:43:06 +00:00
|
|
|
|
|
|
|
b.CreateCondBr(nativeBoolCast(b, types[i], cond), then, next);
|
2018-12-02 02:47:47 +00:00
|
|
|
b.SetInsertPoint(then);
|
2021-05-07 08:43:06 +00:00
|
|
|
|
|
|
|
auto * value = nativeCast(b, types[i + 1], values[i + 1], return_type);
|
2018-12-02 02:47:47 +00:00
|
|
|
returns.emplace_back(b.GetInsertBlock(), value);
|
|
|
|
b.CreateBr(join);
|
|
|
|
b.SetInsertPoint(next);
|
|
|
|
}
|
2021-05-07 08:43:06 +00:00
|
|
|
|
|
|
|
auto * else_value = nativeCast(b, types.back(), values.back(), return_type);
|
|
|
|
returns.emplace_back(b.GetInsertBlock(), else_value);
|
2018-12-02 02:47:47 +00:00
|
|
|
b.CreateBr(join);
|
2021-05-07 08:43:06 +00:00
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
b.SetInsertPoint(join);
|
2021-05-07 08:43:06 +00:00
|
|
|
|
|
|
|
auto * phi = b.CreatePHI(toNativeType(b, return_type), returns.size());
|
|
|
|
for (const auto & [block, value] : returns)
|
|
|
|
phi->addIncoming(value, block);
|
|
|
|
|
2018-12-02 02:47:47 +00:00
|
|
|
return phi;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|