Add function "isConstant"

This commit is contained in:
Alexey Milovidov 2020-04-12 04:24:00 +03:00
parent dec3e0f986
commit 754967bde6
4 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,52 @@
#include <Functions/IFunctionImpl.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnsNumber.h>
namespace DB
{
/// Returns 1 if and only if the argument is constant expression.
/// This function is exists for development, debugging and demonstration purposes.
class FunctionIsConstant : public IFunction
{
public:
static constexpr auto name = "isConstant";
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionIsConstant>();
}
String getName() const override
{
return name;
}
bool useDefaultImplementationForNulls() const override { return false; }
size_t getNumberOfArguments() const override
{
return 1;
}
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
{
return std::make_shared<DataTypeUInt8>();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
const auto & elem = block.getByPosition(arguments[0]);
block.getByPosition(result).column = ColumnUInt8::create(input_rows_count, isColumnConst(*elem.column));
}
};
void registerFunctionIsConstant(FunctionFactory & factory)
{
factory.registerFunction<FunctionIsConstant>();
}
}

View File

@ -56,6 +56,7 @@ void registerFunctionBasename(FunctionFactory &);
void registerFunctionTransform(FunctionFactory &); void registerFunctionTransform(FunctionFactory &);
void registerFunctionGetMacro(FunctionFactory &); void registerFunctionGetMacro(FunctionFactory &);
void registerFunctionGetScalar(FunctionFactory &); void registerFunctionGetScalar(FunctionFactory &);
void registerFunctionIsConstant(FunctionFactory &);
#if USE_ICU #if USE_ICU
void registerFunctionConvertCharset(FunctionFactory &); void registerFunctionConvertCharset(FunctionFactory &);
@ -114,6 +115,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
registerFunctionTransform(factory); registerFunctionTransform(factory);
registerFunctionGetMacro(factory); registerFunctionGetMacro(factory);
registerFunctionGetScalar(factory); registerFunctionGetScalar(factory);
registerFunctionIsConstant(factory);
#if USE_ICU #if USE_ICU
registerFunctionConvertCharset(factory); registerFunctionConvertCharset(factory);

View File

@ -0,0 +1,9 @@
1
1
0
1
1
---
0
0
---

View File

@ -0,0 +1,10 @@
select isConstant(1);
select isConstant([1]);
select isConstant(arrayJoin([1]));
SELECT isConstant((SELECT 1));
SELECT isConstant(x) FROM (SELECT 1 x);
SELECT '---';
SELECT isConstant(x) FROM (SELECT 1 x UNION ALL SELECT 2);
SELECT '---';
select isConstant(); -- { serverError 42 }
select isConstant(1, 2); -- { serverError 42 }