ClickHouse/dbms/src/Functions/FunctionsNull.cpp

326 lines
8.7 KiB
C++
Raw Normal View History

#include <DB/Functions/FunctionsNull.h>
2016-08-04 23:32:18 +00:00
#include <DB/Functions/FunctionsLogical.h>
2016-08-05 12:30:56 +00:00
#include <DB/Functions/FunctionsComparison.h>
2016-08-04 23:32:18 +00:00
#include <DB/Functions/FunctionsConditional.h>
#include <DB/Functions/FunctionFactory.h>
2016-08-04 23:32:18 +00:00
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/DataTypes/DataTypeNull.h>
#include <DB/DataTypes/DataTypeNullable.h>
#include <DB/Columns/ColumnConst.h>
#include <DB/Columns/ColumnNullable.h>
namespace DB
{
void registerFunctionsNull(FunctionFactory & factory)
{
factory.registerFunction<FunctionIsNull>();
factory.registerFunction<FunctionIsNotNull>();
factory.registerFunction<FunctionCoalesce>();
2016-08-05 12:30:56 +00:00
factory.registerFunction<FunctionIfNull>();
factory.registerFunction<FunctionNullIf>();
factory.registerFunction<FunctionAssumeNotNull>();
}
2016-08-04 23:32:18 +00:00
/// Implementation of isNull.
FunctionPtr FunctionIsNull::create(const Context & context)
{
return std::make_shared<FunctionIsNull>();
}
std::string FunctionIsNull::getName() const
{
return name;
}
bool FunctionIsNull::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionIsNull::getReturnTypeImpl(const DataTypes & arguments) const
{
return std::make_shared<DataTypeUInt8>();
}
void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
{
ColumnWithTypeAndName & elem = block.safeGetByPosition(arguments[0]);
2016-08-04 23:32:18 +00:00
if (elem.column->isNull())
{
/// Trivial case.
block.safeGetByPosition(result).column = std::make_shared<ColumnConstUInt8>(elem.column->size(), 1);
2016-08-04 23:32:18 +00:00
}
else if (elem.column->isNullable())
{
/// Merely return the embedded null map.
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*elem.column);
block.safeGetByPosition(result).column = nullable_col.getNullMapColumn();
2016-08-04 23:32:18 +00:00
}
else
{
2016-08-12 14:57:39 +00:00
/// Since no element is nullable, return a zero-constant column representing
/// a zero-filled null map.
block.safeGetByPosition(result).column = std::make_shared<ColumnConstUInt8>(elem.column->size(), 0);
2016-08-04 23:32:18 +00:00
}
}
/// Implementation of isNotNull.
FunctionPtr FunctionIsNotNull::create(const Context & context)
{
return std::make_shared<FunctionIsNotNull>();
}
std::string FunctionIsNotNull::getName() const
{
return name;
}
bool FunctionIsNotNull::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionIsNotNull::getReturnTypeImpl(const DataTypes & arguments) const
{
return std::make_shared<DataTypeUInt8>();
}
void FunctionIsNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
{
2016-08-05 14:00:56 +00:00
Block temp_block
{
block.safeGetByPosition(arguments[0]),
2016-08-05 14:00:56 +00:00
{
nullptr,
std::make_shared<DataTypeUInt8>(),
""
},
{
nullptr,
std::make_shared<DataTypeUInt8>(),
""
}
};
FunctionIsNull{}.executeImpl(temp_block, {0}, 1);
FunctionNot{}.executeImpl(temp_block, {1}, 2);
block.safeGetByPosition(result).column = std::move(temp_block.safeGetByPosition(2).column);
2016-08-04 23:32:18 +00:00
}
/// Implementation of coalesce.
FunctionPtr FunctionCoalesce::create(const Context & context)
{
return std::make_shared<FunctionCoalesce>();
}
std::string FunctionCoalesce::getName() const
{
return name;
}
bool FunctionCoalesce::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionCoalesce::getReturnTypeImpl(const DataTypes & arguments) const
{
DataTypes new_args;
for (size_t i = 0; i < arguments.size(); ++i)
{
new_args.push_back(std::make_shared<DataTypeUInt8>());
new_args.push_back(arguments[i]);
}
new_args.push_back(std::make_shared<DataTypeNull>());
2016-08-05 12:30:56 +00:00
return FunctionMultiIf{}.getReturnTypeImpl(new_args);
2016-08-04 23:32:18 +00:00
}
void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
{
/// coalesce(arg0, arg1, ..., argN) is essentially
/// multiIf(isNotNull(arg0), arg0, isNotNull(arg1), arg1, ..., isNotNull(argN), argN, NULL)
2016-08-05 09:53:14 +00:00
FunctionIsNotNull is_not_null;
2016-08-05 10:56:39 +00:00
ColumnNumbers multi_if_args;
2016-08-05 09:53:14 +00:00
2016-08-05 14:00:56 +00:00
Block temp_block = block;
2016-08-04 23:32:18 +00:00
for (size_t i = 0; i < arguments.size(); ++i)
{
2016-08-05 14:00:56 +00:00
size_t res_pos = temp_block.columns();
temp_block.insert({nullptr, std::make_shared<DataTypeUInt8>(), ""});
2016-08-04 23:32:18 +00:00
2016-08-05 14:00:56 +00:00
is_not_null.executeImpl(temp_block, {arguments[i]}, res_pos);
2016-08-04 23:32:18 +00:00
2016-08-05 10:56:39 +00:00
multi_if_args.push_back(res_pos);
multi_if_args.push_back(arguments[i]);
2016-08-04 23:32:18 +00:00
}
2016-08-05 09:35:37 +00:00
/// Argument corresponding to the fallback NULL value.
2016-08-05 14:00:56 +00:00
multi_if_args.push_back(temp_block.columns());
2016-08-04 23:32:18 +00:00
2016-08-05 09:35:37 +00:00
/// Append a fallback NULL column.
2016-08-04 23:32:18 +00:00
ColumnWithTypeAndName elem;
elem.column = std::make_shared<ColumnNull>(temp_block.rows(), Null());
2016-08-04 23:32:18 +00:00
elem.type = std::make_shared<DataTypeNull>();
elem.name = "NULL";
2016-08-05 14:00:56 +00:00
temp_block.insert(elem);
2016-08-04 23:32:18 +00:00
2016-08-05 14:00:56 +00:00
FunctionMultiIf{}.executeImpl(temp_block, multi_if_args, result);
block.safeGetByPosition(result).column = std::move(temp_block.safeGetByPosition(result).column);
2016-08-05 12:30:56 +00:00
}
/// Implementation of ifNull.
FunctionPtr FunctionIfNull::create(const Context & context)
{
return std::make_shared<FunctionIfNull>();
}
std::string FunctionIfNull::getName() const
{
return name;
}
static const DataTypePtr getNestedDataType(const DataTypePtr & type)
{
if (type->isNullable())
return static_cast<const DataTypeNullable &>(*type).getNestedType();
return type;
}
2016-08-05 12:30:56 +00:00
bool FunctionIfNull::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const
{
if (arguments[0]->isNull())
return arguments[1];
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)
{
if (block.getByPosition(arguments[0]).column->isNull())
block.getByPosition(result).column = block.getByPosition(arguments[1]).column;
/// ifNull(col1, col2) == if(isNotNull(col1), assumeNotNull(col1), col2)
2016-08-05 12:30:56 +00:00
2016-08-05 14:00:56 +00:00
Block temp_block = block;
size_t is_not_null_pos = temp_block.columns();
2016-08-05 14:00:56 +00:00
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
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
block.safeGetByPosition(result).column = std::move(temp_block.safeGetByPosition(result).column);
2016-08-05 12:30:56 +00:00
}
/// Implementation of nullIf.
FunctionPtr FunctionNullIf::create(const Context & context)
{
return std::make_shared<FunctionNullIf>();
}
std::string FunctionNullIf::getName() const
{
return name;
}
bool FunctionNullIf::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionNullIf::getReturnTypeImpl(const DataTypes & arguments) const
{
2017-03-07 01:35:36 +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)
{
/// nullIf(col1, col2) == multiIf(col1 == col2, NULL, col1)
2016-08-05 14:00:56 +00:00
Block temp_block = block;
2016-08-05 12:30:56 +00:00
2016-08-05 14:00:56 +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
2016-08-05 14:00:56 +00:00
FunctionEquals{}.execute(temp_block, {arguments[0], arguments[1]}, res_pos);
2016-08-05 12:30:56 +00:00
/// Argument corresponding to the NULL value.
2016-08-05 14:00:56 +00:00
size_t null_pos = temp_block.columns();
2016-08-05 12:30:56 +00:00
/// Append a NULL column.
ColumnWithTypeAndName null_elem;
null_elem.column = std::make_shared<ColumnNull>(temp_block.rows(), Null());
2016-08-05 12:30:56 +00:00
null_elem.type = std::make_shared<DataTypeNull>();
null_elem.name = "NULL";
2016-08-05 14:00:56 +00:00
temp_block.insert(null_elem);
2017-03-07 01:35:36 +00:00
FunctionIf{}.executeImpl(temp_block, {res_pos, null_pos, arguments[0]}, result);
2016-08-05 12:30:56 +00:00
block.safeGetByPosition(result).column = std::move(temp_block.safeGetByPosition(result).column);
2016-08-04 23:32:18 +00:00
}
/// Implementation of assumeNotNull.
FunctionPtr FunctionAssumeNotNull::create(const Context & context)
{
return std::make_shared<FunctionAssumeNotNull>();
}
std::string FunctionAssumeNotNull::getName() const
{
return name;
}
bool FunctionAssumeNotNull::hasSpecialSupportForNulls() const
{
return true;
}
DataTypePtr FunctionAssumeNotNull::getReturnTypeImpl(const DataTypes & arguments) const
{
if (arguments[0]->isNull())
2016-08-12 14:57:39 +00:00
throw Exception{"NULL is an invalid value for function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
return getNestedDataType(arguments[0]);
}
void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
{
const ColumnPtr & col = block.safeGetByPosition(arguments[0]).column;
ColumnPtr & res_col = block.safeGetByPosition(result).column;
if (col->isNull())
2016-08-12 14:57:39 +00:00
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;
}
}