2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 01:24:09 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <Core/ColumnNumbers.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2022-05-19 10:13:44 +00:00
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
/// Implements the function assumeNotNull which takes 1 argument and works as follows:
|
|
|
|
/// - if the argument is a nullable column, return its embedded column;
|
|
|
|
/// - otherwise return the original argument.
|
|
|
|
/// NOTE: assumeNotNull may not be called with the NULL value.
|
|
|
|
class FunctionAssumeNotNull : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "assumeNotNull";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2018-09-08 01:24:09 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionAssumeNotNull>();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2018-09-08 01:24:09 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2019-10-02 17:51:00 +00:00
|
|
|
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
|
2018-09-08 01:24:09 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
return removeNullable(arguments[0]);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const override
|
2018-09-08 01:24:09 +00:00
|
|
|
{
|
2020-10-17 15:22:42 +00:00
|
|
|
const ColumnPtr & col = arguments[0].column;
|
2018-09-08 01:24:09 +00:00
|
|
|
|
2022-05-19 10:13:44 +00:00
|
|
|
if (arguments[0].type->onlyNull() && !col->empty())
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot create non-empty column with type Nothing");
|
|
|
|
|
2020-04-22 08:31:10 +00:00
|
|
|
if (const auto * nullable_col = checkAndGetColumn<ColumnNullable>(*col))
|
2020-10-17 15:22:42 +00:00
|
|
|
return nullable_col->getNestedColumnPtr();
|
2018-09-08 01:24:09 +00:00
|
|
|
else
|
2020-10-17 15:22:42 +00:00
|
|
|
return col;
|
2018-09-08 01:24:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 01:24:09 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(AssumeNotNull)
|
2018-09-08 01:24:09 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionAssumeNotNull>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|