2016-07-06 09:47:55 +00:00
|
|
|
#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>
|
2016-07-06 09:47:55 +00:00
|
|
|
#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>
|
2016-07-06 09:47:55 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
void registerFunctionsNull(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionIsNull>();
|
|
|
|
factory.registerFunction<FunctionIsNotNull>();
|
2016-07-25 18:26:45 +00:00
|
|
|
factory.registerFunction<FunctionCoalesce>();
|
2016-08-05 12:30:56 +00:00
|
|
|
factory.registerFunction<FunctionIfNull>();
|
|
|
|
factory.registerFunction<FunctionNullIf>();
|
2016-08-05 12:52:09 +00:00
|
|
|
factory.registerFunction<FunctionAssumeNotNull>();
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
2016-08-12 14:57:39 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
2016-08-04 23:32:18 +00:00
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2016-08-12 14:57:39 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2016-08-04 23:32:18 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionIsNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
|
|
|
ColumnWithTypeAndName & elem = block.getByPosition(arguments[0]);
|
|
|
|
if (elem.column->isNull())
|
|
|
|
{
|
|
|
|
/// Trivial case.
|
2016-08-12 14:57:39 +00:00
|
|
|
block.getByPosition(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.getByPosition(result).column = nullable_col.getNullValuesByteMap();
|
|
|
|
}
|
|
|
|
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.getByPosition(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
|
|
|
|
{
|
|
|
|
if (arguments.size() != 1)
|
2016-08-12 14:57:39 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
2016-08-04 23:32:18 +00:00
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2016-08-12 14:57:39 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2016-08-04 23:32:18 +00:00
|
|
|
|
|
|
|
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.getByPosition(arguments[0]),
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nullptr,
|
|
|
|
std::make_shared<DataTypeUInt8>(),
|
|
|
|
""
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
FunctionIsNull{}.executeImpl(temp_block, {0}, 1);
|
|
|
|
FunctionNot{}.executeImpl(temp_block, {1}, 2);
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(temp_block.getByPosition(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;
|
2016-08-05 14:00:56 +00:00
|
|
|
elem.column = std::make_shared<ColumnNull>(temp_block.rowsInFirstColumn(), 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.getByPosition(result).column = std::move(temp_block.getByPosition(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionIfNull::hasSpecialSupportForNulls() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
|
|
|
if (arguments.size() != 2)
|
2016-08-12 14:57:39 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
2016-08-05 12:30:56 +00:00
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
2016-08-12 14:57:39 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2016-08-05 12:30:56 +00:00
|
|
|
|
|
|
|
return FunctionMultiIf{}.getReturnTypeImpl({std::make_shared<DataTypeUInt8>(), arguments[0], arguments[1]});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
|
|
|
/// ifNull(col1, col2) == multiIf(isNotNull(col1), col1, col2)
|
|
|
|
|
2016-08-05 14:00:56 +00:00
|
|
|
Block temp_block = block;
|
|
|
|
|
|
|
|
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
|
|
|
FunctionIsNotNull{}.executeImpl(temp_block, {arguments[0]}, res_pos);
|
|
|
|
FunctionMultiIf{}.executeImpl(temp_block, {res_pos, arguments[0], arguments[1]}, result);
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2016-08-05 14:00:56 +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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
|
|
|
if (arguments.size() != 2)
|
2016-08-12 14:57:39 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
2016-08-05 12:30:56 +00:00
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
2016-08-12 14:57:39 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2016-08-05 12:30:56 +00:00
|
|
|
|
|
|
|
return FunctionMultiIf{}.getReturnTypeImpl({std::make_shared<DataTypeUInt8>(), std::make_shared<DataTypeNull>(), arguments[0]});
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-08-05 14:00:56 +00:00
|
|
|
null_elem.column = std::make_shared<ColumnNull>(temp_block.rowsInFirstColumn(), 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);
|
|
|
|
|
|
|
|
FunctionMultiIf{}.executeImpl(temp_block, {res_pos, null_pos, arguments[0]}, result);
|
2016-08-05 12:30:56 +00:00
|
|
|
|
2016-08-05 14:00:56 +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)
|
|
|
|
{
|
|
|
|
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.size() != 1)
|
2016-08-12 14:57:39 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed "
|
2016-08-05 12:52:09 +00:00
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2016-08-12 14:57:39 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2016-08-05 12:52:09 +00:00
|
|
|
|
2016-08-15 11:14:29 +00:00
|
|
|
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};
|
2016-08-15 11:14:29 +00:00
|
|
|
else if (arguments[0]->isNullable())
|
2016-08-05 12:52:09 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
const DataTypeNullable & nullable_type = static_cast<const DataTypeNullable &>(*arguments[0]);
|
2016-08-05 12:52:09 +00:00
|
|
|
return nullable_type.getNestedType();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return arguments[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionAssumeNotNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
|
|
|
const ColumnPtr & col = block.getByPosition(arguments[0]).column;
|
|
|
|
ColumnPtr & res_col = block.getByPosition(result).column;
|
|
|
|
|
2016-08-15 11:14:29 +00:00
|
|
|
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};
|
2016-08-15 11:14:29 +00:00
|
|
|
else if (col->isNullable())
|
2016-08-05 12:52:09 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*col);
|
2016-08-05 12:52:09 +00:00
|
|
|
res_col = nullable_col.getNestedColumn();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res_col = col;
|
|
|
|
}
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|