mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 08:32:02 +00:00
Added function ifNotFinite
This commit is contained in:
parent
3f9f262734
commit
27b5f47282
70
dbms/src/Functions/ifNotFinite.cpp
Normal file
70
dbms/src/Functions/ifNotFinite.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
#include <Functions/IFunctionImpl.h>
|
||||
#include <Functions/FunctionHelpers.h>
|
||||
#include <Functions/FunctionFactory.h>
|
||||
#include <DataTypes/DataTypesNumber.h>
|
||||
#include <DataTypes/getLeastSupertype.h>
|
||||
#include <Core/ColumnNumbers.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
/// ifNotFinite(x, y) is equivalent to isFinite(x) ? x : y.
|
||||
class FunctionIfNotFinite : public IFunction
|
||||
{
|
||||
public:
|
||||
static constexpr auto name = "ifNotFinite";
|
||||
|
||||
FunctionIfNotFinite(const Context & context_) : context(context_) {}
|
||||
|
||||
static FunctionPtr create(const Context & context)
|
||||
{
|
||||
return std::make_shared<FunctionIfNotFinite>(context);
|
||||
}
|
||||
|
||||
std::string getName() const override
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
size_t getNumberOfArguments() const override { return 2; }
|
||||
bool useDefaultImplementationForNulls() const override { return false; }
|
||||
bool useDefaultImplementationForConstants() const override { return true; }
|
||||
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
|
||||
|
||||
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
||||
{
|
||||
return getLeastSupertype({arguments[0], arguments[1]});
|
||||
}
|
||||
|
||||
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
|
||||
{
|
||||
Block temp_block = block;
|
||||
|
||||
size_t is_finite_pos = temp_block.columns();
|
||||
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
|
||||
|
||||
auto is_finite = FunctionFactory::instance().get("isFinite", context)->build(
|
||||
{temp_block.getByPosition(arguments[0])});
|
||||
|
||||
auto func_if = FunctionFactory::instance().get("if", context)->build(
|
||||
{temp_block.getByPosition(is_finite_pos), temp_block.getByPosition(arguments[0]), temp_block.getByPosition(arguments[1])});
|
||||
|
||||
is_finite->execute(temp_block, {arguments[0]}, is_finite_pos, input_rows_count);
|
||||
func_if->execute(temp_block, {is_finite_pos, arguments[0], arguments[1]}, result, input_rows_count);
|
||||
|
||||
block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column);
|
||||
}
|
||||
|
||||
private:
|
||||
const Context & context;
|
||||
};
|
||||
|
||||
|
||||
void registerFunctionIfNotFinite(FunctionFactory & factory)
|
||||
{
|
||||
factory.registerFunction<FunctionIfNotFinite>();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ void registerFunctionHasColumnInTable(FunctionFactory &);
|
||||
void registerFunctionIsFinite(FunctionFactory &);
|
||||
void registerFunctionIsInfinite(FunctionFactory &);
|
||||
void registerFunctionIsNaN(FunctionFactory &);
|
||||
void registerFunctionIfNotFinite(FunctionFactory &);
|
||||
void registerFunctionThrowIf(FunctionFactory &);
|
||||
void registerFunctionVersion(FunctionFactory &);
|
||||
void registerFunctionUptime(FunctionFactory &);
|
||||
@ -93,6 +94,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory)
|
||||
registerFunctionIsFinite(factory);
|
||||
registerFunctionIsInfinite(factory);
|
||||
registerFunctionIsNaN(factory);
|
||||
registerFunctionIfNotFinite(factory);
|
||||
registerFunctionThrowIf(factory);
|
||||
registerFunctionVersion(factory);
|
||||
registerFunctionUptime(factory);
|
||||
|
16
dbms/tests/queries/0_stateless/01065_if_not_finite.reference
Normal file
16
dbms/tests/queries/0_stateless/01065_if_not_finite.reference
Normal file
@ -0,0 +1,16 @@
|
||||
111
|
||||
1
|
||||
0.5
|
||||
0.33
|
||||
0.25
|
||||
0.2
|
||||
0.17
|
||||
0.14
|
||||
0.12
|
||||
0.11
|
||||
1
|
||||
-1
|
||||
2
|
||||
2
|
||||
\N
|
||||
-42
|
9
dbms/tests/queries/0_stateless/01065_if_not_finite.sql
Normal file
9
dbms/tests/queries/0_stateless/01065_if_not_finite.sql
Normal file
@ -0,0 +1,9 @@
|
||||
SELECT ifNotFinite(round(1 / number, 2), 111) FROM numbers(10);
|
||||
|
||||
SELECT ifNotFinite(1, 2);
|
||||
SELECT ifNotFinite(-1.0, 2);
|
||||
SELECT ifNotFinite(nan, 2);
|
||||
SELECT ifNotFinite(-1 / 0, 2);
|
||||
SELECT ifNotFinite(log(0), NULL);
|
||||
SELECT ifNotFinite(sqrt(-1), -42);
|
||||
SELECT ifNotFinite(1234567890123456789, -1234567890123456789); -- { serverError 386 }
|
Loading…
Reference in New Issue
Block a user