From 4bbcec3d33cf2b5dcc9a54c7bac8e5c9294f5422 Mon Sep 17 00:00:00 2001 From: hcz Date: Wed, 19 Aug 2020 14:44:11 +0800 Subject: [PATCH] Add defaultValueOfTypeName --- src/Functions/defaultValueOfTypeName.cpp | 69 +++++++++++++++++++ .../registerFunctionsMiscellaneous.cpp | 2 + 2 files changed, 71 insertions(+) create mode 100644 src/Functions/defaultValueOfTypeName.cpp diff --git a/src/Functions/defaultValueOfTypeName.cpp b/src/Functions/defaultValueOfTypeName.cpp new file mode 100644 index 00000000000..5b57642aa0e --- /dev/null +++ b/src/Functions/defaultValueOfTypeName.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include + + +namespace DB +{ + +namespace ErrorCodes +{ + extern const int ILLEGAL_COLUMN; +} + +/// 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(); + } + + 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 + { + auto col_type_const = typeid_cast(arguments.front().column.get()); + if (!col_type_const || !isString(arguments.front().type)) + throw Exception("The argument of function " + getName() + " must be a constant string describing type. " + "Instead there is a column with the following structure: " + arguments.front().column->dumpStructure(), + ErrorCodes::ILLEGAL_COLUMN); + + return DataTypeFactory::instance().get(col_type_const->getValue()); + } + + void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override + { + const IDataType & type = *block.getByPosition(arguments[0]).type; + block.getByPosition(result).column = type.createColumnConst(input_rows_count, type.getDefault()); + } + + ColumnPtr getResultIfAlwaysReturnsConstantAndHasArguments(const Block & block, const ColumnNumbers & arguments) const override + { + const IDataType & type = *block.getByPosition(arguments[0]).type; + return type.createColumnConst(1, type.getDefault()); + } +}; + + +void registerFunctionDefaultValueOfTypeName(FunctionFactory & factory) +{ + factory.registerFunction(); +} + +} diff --git a/src/Functions/registerFunctionsMiscellaneous.cpp b/src/Functions/registerFunctionsMiscellaneous.cpp index 11d3e129c28..25e64eba999 100644 --- a/src/Functions/registerFunctionsMiscellaneous.cpp +++ b/src/Functions/registerFunctionsMiscellaneous.cpp @@ -18,6 +18,7 @@ void registerFunctionBlockSerializedSize(FunctionFactory &); void registerFunctionToColumnTypeName(FunctionFactory &); void registerFunctionDumpColumnStructure(FunctionFactory &); void registerFunctionDefaultValueOfArgumentType(FunctionFactory &); +void registerFunctionDefaultValueOfTypeName(FunctionFactory &); void registerFunctionBlockSize(FunctionFactory &); void registerFunctionBlockNumber(FunctionFactory &); void registerFunctionRowNumberInBlock(FunctionFactory &); @@ -79,6 +80,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory) registerFunctionToColumnTypeName(factory); registerFunctionDumpColumnStructure(factory); registerFunctionDefaultValueOfArgumentType(factory); + registerFunctionDefaultValueOfTypeName(factory); registerFunctionBlockSize(factory); registerFunctionBlockNumber(factory); registerFunctionRowNumberInBlock(factory);