2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionsNull.h>
|
|
|
|
#include <Functions/FunctionsLogical.h>
|
|
|
|
#include <Functions/FunctionsComparison.h>
|
|
|
|
#include <Functions/FunctionsConditional.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeNull.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2016-07-06 09:47:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
void registerFunctionsNull(FunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction<FunctionIsNull>();
|
|
|
|
factory.registerFunction<FunctionIsNotNull>();
|
|
|
|
factory.registerFunction<FunctionCoalesce>();
|
|
|
|
factory.registerFunction<FunctionIfNull>();
|
|
|
|
factory.registerFunction<FunctionNullIf>();
|
|
|
|
factory.registerFunction<FunctionAssumeNotNull>();
|
|
|
|
factory.registerFunction<FunctionToNullable>();
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 23:32:18 +00:00
|
|
|
/// Implementation of isNull.
|
|
|
|
|
|
|
|
FunctionPtr FunctionIsNull::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionIsNull>();
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionIsNull::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionIsNull::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
ColumnWithTypeAndName & elem = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (elem.column->isNull())
|
|
|
|
{
|
|
|
|
/// Trivial case.
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = DataTypeUInt8().createConstColumn(elem.column->size(), UInt64(1));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (elem.column->isNullable())
|
|
|
|
{
|
|
|
|
/// Merely return the embedded null map.
|
|
|
|
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*elem.column);
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = nullable_col.getNullMapColumn();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Since no element is nullable, return a zero-constant column representing
|
|
|
|
/// a zero-filled null map.
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = DataTypeUInt8().createConstColumn(elem.column->size(), UInt64(0));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of isNotNull.
|
|
|
|
|
|
|
|
FunctionPtr FunctionIsNotNull::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionIsNotNull>();
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionIsNotNull::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionIsNotNull::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionIsNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Block temp_block
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(arguments[0]),
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
}
|
|
|
|
};
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionIsNull{}.executeImpl(temp_block, {0}, 1);
|
|
|
|
FunctionNot{}.executeImpl(temp_block, {1}, 2);
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = std::move(temp_block.getByPosition(2).column);
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of coalesce.
|
|
|
|
|
2017-03-09 20:39:20 +00:00
|
|
|
static const DataTypePtr getNestedDataType(const DataTypePtr & type)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (type->isNullable())
|
|
|
|
return static_cast<const DataTypeNullable &>(*type).getNestedType();
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return type;
|
2017-03-09 20:39:20 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 23:32:18 +00:00
|
|
|
FunctionPtr FunctionCoalesce::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionCoalesce>();
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionCoalesce::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionCoalesce::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Skip all NULL arguments. If any argument is non-Nullable, skip all next arguments.
|
|
|
|
DataTypes filtered_args;
|
|
|
|
filtered_args.reserve(arguments.size());
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
if (arg->isNull())
|
|
|
|
continue;
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
filtered_args.push_back(arg);
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!arg->isNullable())
|
|
|
|
break;
|
|
|
|
}
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypes new_args;
|
|
|
|
for (size_t i = 0; i < filtered_args.size(); ++i)
|
|
|
|
{
|
|
|
|
bool is_last = i + 1 == filtered_args.size();
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (is_last)
|
|
|
|
{
|
|
|
|
new_args.push_back(filtered_args[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_args.push_back(std::make_shared<DataTypeUInt8>());
|
|
|
|
new_args.push_back(getNestedDataType(filtered_args[i]));
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (new_args.empty())
|
|
|
|
return std::make_shared<DataTypeNull>();
|
|
|
|
if (new_args.size() == 1)
|
|
|
|
return new_args.front();
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto res = FunctionMultiIf{}.getReturnTypeImpl(new_args);
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// if last argument is not nullable, result should be also not nullable
|
|
|
|
if (!new_args.back()->isNullable() && res->isNullable())
|
|
|
|
res = getNestedDataType(res);
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// coalesce(arg0, arg1, ..., argN) is essentially
|
|
|
|
/// multiIf(isNotNull(arg0), assumeNotNull(arg0), isNotNull(arg1), assumeNotNull(arg1), ..., argN)
|
|
|
|
/// with constant NULL arguments removed.
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnNumbers filtered_args;
|
|
|
|
filtered_args.reserve(arguments.size());
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
const auto & column = block.getByPosition(arg).column;
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (column->isNull())
|
|
|
|
continue;
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
filtered_args.push_back(arg);
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!column->isNullable())
|
|
|
|
break;
|
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionIsNotNull is_not_null;
|
|
|
|
FunctionAssumeNotNull assume_not_null;
|
|
|
|
ColumnNumbers multi_if_args;
|
2016-08-05 09:53:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block temp_block = block;
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < filtered_args.size(); ++i)
|
|
|
|
{
|
|
|
|
size_t res_pos = temp_block.columns();
|
|
|
|
bool is_last = i + 1 == filtered_args.size();
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (is_last)
|
|
|
|
{
|
|
|
|
multi_if_args.push_back(filtered_args[i]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
|
|
|
|
is_not_null.executeImpl(temp_block, {filtered_args[i]}, res_pos);
|
|
|
|
temp_block.insert({nullptr, getNestedDataType(block.getByPosition(filtered_args[i]).type), ""});
|
|
|
|
assume_not_null.executeImpl(temp_block, {filtered_args[i]}, res_pos + 1);
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
multi_if_args.push_back(res_pos);
|
|
|
|
multi_if_args.push_back(res_pos + 1);
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// If all arguments appeared to be NULL.
|
|
|
|
if (multi_if_args.empty())
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = block.getByPosition(result).type->createConstColumn(block.rows(), Null());
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (multi_if_args.size() == 1)
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = block.getByPosition(multi_if_args.front()).column;
|
|
|
|
return;
|
|
|
|
}
|
2016-08-04 23:32:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionMultiIf{}.executeImpl(temp_block, multi_if_args, result);
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
auto res = std::move(temp_block.getByPosition(result).column);
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// if last argument is not nullable, result should be also not nullable
|
|
|
|
if (!block.getByPosition(multi_if_args.back()).column->isNullable() && res->isNullable())
|
|
|
|
res = static_cast<ColumnNullable &>(*res).getNestedColumn();
|
2017-03-09 20:39:20 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = res;
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of ifNull.
|
|
|
|
|
|
|
|
FunctionPtr FunctionIfNull::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionIfNull>();
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionIfNull::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments[0]->isNull())
|
|
|
|
return arguments[1];
|
2017-03-09 18:58:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!arguments[0]->isNullable())
|
|
|
|
return arguments[0];
|
2017-03-09 19:39:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return FunctionIf{}.getReturnTypeImpl({std::make_shared<DataTypeUInt8>(), getNestedDataType(arguments[0]), arguments[1]});
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Always null.
|
|
|
|
if (block.getByPosition(arguments[0]).column->isNull())
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = block.getByPosition(arguments[1]).column;
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 19:39:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Could not contain nulls, so nullIf makes no sense.
|
|
|
|
if (!block.getByPosition(arguments[0]).column->isNullable())
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = block.getByPosition(arguments[0]).column;
|
|
|
|
return;
|
|
|
|
}
|
2017-03-09 18:58:44 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// ifNull(col1, col2) == if(isNotNull(col1), assumeNotNull(col1), col2)
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block temp_block = block;
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t is_not_null_pos = temp_block.columns();
|
|
|
|
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
|
|
|
|
size_t assume_not_null_pos = temp_block.columns();
|
|
|
|
temp_block.insert({nullptr, getNestedDataType(block.getByPosition(arguments[0]).type), ""});
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionIsNotNull{}.executeImpl(temp_block, {arguments[0]}, is_not_null_pos);
|
|
|
|
FunctionAssumeNotNull{}.executeImpl(temp_block, {arguments[0]}, assume_not_null_pos);
|
|
|
|
FunctionIf{}.executeImpl(temp_block, {is_not_null_pos, assume_not_null_pos, arguments[1]}, result);
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column);
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of nullIf.
|
|
|
|
|
|
|
|
FunctionPtr FunctionNullIf::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionNullIf>();
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionNullIf::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionNullIf::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return FunctionIf{}.getReturnTypeImpl({std::make_shared<DataTypeUInt8>(), std::make_shared<DataTypeNull>(), arguments[0]});
|
2016-08-05 12:30:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionNullIf::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// nullIf(col1, col2) == multiIf(col1 == col2, NULL, col1)
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Block temp_block = block;
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t res_pos = temp_block.columns();
|
|
|
|
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionEquals{}.execute(temp_block, {arguments[0], arguments[1]}, res_pos);
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Argument corresponding to the NULL value.
|
|
|
|
size_t null_pos = temp_block.columns();
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Append a NULL column.
|
|
|
|
ColumnWithTypeAndName null_elem;
|
2017-07-21 06:35:58 +00:00
|
|
|
null_elem.column = DataTypeNull().createConstColumn(temp_block.rows(), Null());
|
2017-04-01 07:20:54 +00:00
|
|
|
null_elem.type = std::make_shared<DataTypeNull>();
|
|
|
|
null_elem.name = "NULL";
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
temp_block.insert(null_elem);
|
2016-08-05 14:00:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FunctionIf{}.executeImpl(temp_block, {res_pos, null_pos, arguments[0]}, result);
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column);
|
2016-08-04 23:32:18 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 12:52:09 +00:00
|
|
|
/// Implementation of assumeNotNull.
|
|
|
|
|
|
|
|
FunctionPtr FunctionAssumeNotNull::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionAssumeNotNull>();
|
2016-08-05 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionAssumeNotNull::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2016-08-05 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionAssumeNotNull::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments[0]->isNull())
|
|
|
|
throw Exception{"NULL is an invalid value for function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
return getNestedDataType(arguments[0]);
|
2016-08-05 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & col = block.getByPosition(arguments[0]).column;
|
|
|
|
ColumnPtr & res_col = block.getByPosition(result).column;
|
2016-08-05 12:52:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (col->isNull())
|
|
|
|
throw Exception{"NULL is an invalid value for function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
else if (col->isNullable())
|
|
|
|
{
|
|
|
|
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*col);
|
|
|
|
res_col = nullable_col.getNestedColumn();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res_col = col;
|
2016-08-05 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 19:31:16 +00:00
|
|
|
/// Implementation of toNullable.
|
|
|
|
|
|
|
|
FunctionPtr FunctionToNullable::create(const Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<FunctionToNullable>();
|
2017-03-09 19:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FunctionToNullable::getName() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return name;
|
2017-03-09 19:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionToNullable::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (arguments[0]->isNull() || arguments[0]->isNullable())
|
|
|
|
return arguments[0];
|
|
|
|
return std::make_shared<DataTypeNullable>(arguments[0]);
|
2017-03-09 19:31:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionToNullable::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & col = block.getByPosition(arguments[0]).column;
|
2017-03-09 19:31:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (col->isNull() || col->isNullable())
|
|
|
|
block.getByPosition(result).column = col;
|
|
|
|
else
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnNullable>(col,
|
2017-07-21 06:35:58 +00:00
|
|
|
std::make_shared<ColumnUInt8>(block.rows(), 0));
|
2017-03-09 19:31:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|