mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Add function "isConstant"
This commit is contained in:
parent
dec3e0f986
commit
754967bde6
52
src/Functions/isConstant.cpp
Normal file
52
src/Functions/isConstant.cpp
Normal 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>();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -56,6 +56,7 @@ void registerFunctionBasename(FunctionFactory &);
|
||||
void registerFunctionTransform(FunctionFactory &);
|
||||
void registerFunctionGetMacro(FunctionFactory &);
|
||||
void registerFunctionGetScalar(FunctionFactory &);
|
||||
void registerFunctionIsConstant(FunctionFactory &);
|
||||
|
||||
#if USE_ICU
|
||||
void registerFunctionConvertCharset(FunctionFactory &);
|
||||
@ -114,6 +115,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
|
||||
registerFunctionTransform(factory);
|
||||
registerFunctionGetMacro(factory);
|
||||
registerFunctionGetScalar(factory);
|
||||
registerFunctionIsConstant(factory);
|
||||
|
||||
#if USE_ICU
|
||||
registerFunctionConvertCharset(factory);
|
||||
|
9
tests/queries/0_stateless/01118_is_constant.reference
Normal file
9
tests/queries/0_stateless/01118_is_constant.reference
Normal file
@ -0,0 +1,9 @@
|
||||
1
|
||||
1
|
||||
0
|
||||
1
|
||||
1
|
||||
---
|
||||
0
|
||||
0
|
||||
---
|
10
tests/queries/0_stateless/01118_is_constant.sql
Normal file
10
tests/queries/0_stateless/01118_is_constant.sql
Normal 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 }
|
Loading…
Reference in New Issue
Block a user