ClickHouse/src/Functions/defaultValueOfTypeName.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

2020-08-19 06:44:11 +00:00
#include <Functions/IFunctionImpl.h>
#include <Functions/FunctionFactory.h>
#include <Core/Field.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypeFactory.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
2020-09-07 18:00:37 +00:00
namespace
{
2020-08-19 06:44:11 +00:00
/// Returns global default value for type name (example: 0 for numeric types, '' for String).
class FunctionDefaultValueOfTypeName : public IFunction
{
public:
static constexpr auto name = "defaultValueOfTypeName";
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionDefaultValueOfTypeName>();
}
String getName() const override
{
return name;
}
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
size_t getNumberOfArguments() const override
{
return 1;
}
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
2020-08-24 22:20:43 +00:00
const ColumnConst * col_type_const = typeid_cast<const ColumnConst *>(arguments.front().column.get());
2020-08-19 06:44:11 +00:00
if (!col_type_const || !isString(arguments.front().type))
throw Exception("The argument of function " + getName() + " must be a constant string describing type.",
ErrorCodes::ILLEGAL_COLUMN);
2020-08-19 06:44:11 +00:00
return DataTypeFactory::instance().get(col_type_const->getValue<String>());
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr & result_type, size_t input_rows_count) const override
2020-08-19 06:44:11 +00:00
{
2020-10-17 21:41:50 +00:00
const IDataType & type = *result_type;
return type.createColumnConst(input_rows_count, type.getDefault());
2020-08-19 06:44:11 +00:00
}
};
2020-09-07 18:00:37 +00:00
}
2020-08-19 06:44:11 +00:00
void registerFunctionDefaultValueOfTypeName(FunctionFactory & factory)
{
factory.registerFunction<FunctionDefaultValueOfTypeName>();
}
}