2022-01-18 11:52:27 +00:00
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionsConversion.h>
|
|
|
|
#include <Functions/CastOverloadResolver.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class FunctionToBool : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toBool";
|
|
|
|
|
|
|
|
static FunctionPtr create(ContextPtr)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionToBool>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2022-05-16 19:15:18 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
2022-01-18 11:52:27 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2022-01-18 11:52:27 +00:00
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
auto bool_type = DataTypeFactory::instance().get("Bool");
|
|
|
|
return arguments[0]->isNullable() ? makeNullable(bool_type) : bool_type;
|
2022-01-18 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t) const override
|
2022-01-18 11:52:27 +00:00
|
|
|
{
|
|
|
|
ColumnsWithTypeAndName cast_args
|
|
|
|
{
|
|
|
|
arguments[0],
|
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
DataTypeString().createColumnConst(arguments[0].column->size(), arguments[0].type->isNullable() ? "Nullable(Bool)" : "Bool"),
|
2022-01-18 11:52:27 +00:00
|
|
|
std::make_shared<DataTypeString>(),
|
|
|
|
""
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionOverloadResolverPtr func_builder_cast = CastInternalOverloadResolver<CastType::nonAccurate>::createImpl();
|
|
|
|
auto func_cast = func_builder_cast->build(cast_args);
|
2022-05-16 19:15:18 +00:00
|
|
|
return func_cast->execute(cast_args, result_type, arguments[0].column->size());
|
2022-01-18 11:52:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(ToBool)
|
2022-01-18 11:52:27 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionToBool>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|