2016-07-06 09:47:55 +00:00
|
|
|
#include <DB/Functions/IFunction.h>
|
2016-08-04 15:06:27 +00:00
|
|
|
#include <DB/Columns/ColumnConst.h>
|
2016-07-06 09:47:55 +00:00
|
|
|
#include <DB/Columns/ColumnNullable.h>
|
|
|
|
#include <DB/DataTypes/DataTypeNull.h>
|
|
|
|
#include <DB/DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-08-11 16:47:28 +00:00
|
|
|
/// Suppose a function which has no special support for nullable arguments
|
|
|
|
/// has been called with arguments, one or more of them being nullable.
|
|
|
|
/// Then the method below endows the result, which is nullable, with a null
|
|
|
|
/// byte map that is determined by OR-ing the null byte maps of the nullable
|
|
|
|
/// arguments.
|
2016-08-11 00:17:30 +00:00
|
|
|
void createNullValuesByteMap(Block & block, const ColumnNumbers & args, size_t result)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
ColumnNullable & res_col = static_cast<ColumnNullable &>(*block.unsafeGetByPosition(result).column);
|
2016-07-06 09:47:55 +00:00
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
for (const auto & arg : args)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-11 00:17:30 +00:00
|
|
|
if (arg == result)
|
2016-07-06 09:47:55 +00:00
|
|
|
continue;
|
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
const ColumnWithTypeAndName & elem = block.unsafeGetByPosition(arg);
|
2016-08-12 15:22:28 +00:00
|
|
|
if (elem.column && elem.column->isNullable())
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-11 00:17:30 +00:00
|
|
|
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*elem.column);
|
|
|
|
res_col.updateNullValuesByteMap(nullable_col);
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if a block contains at least one null column.
|
2016-08-05 07:57:02 +00:00
|
|
|
bool hasNullColumns(const Block & block, const ColumnNumbers & arguments)
|
2016-08-05 07:49:56 +00:00
|
|
|
{
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
const auto & elem = block.unsafeGetByPosition(arg);
|
2016-08-12 15:22:28 +00:00
|
|
|
if (elem.column && elem.column->isNull())
|
2016-08-05 07:49:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if at least one column is null.
|
2016-07-06 09:47:55 +00:00
|
|
|
bool hasNullColumns(const ColumnsWithTypeAndName & args)
|
|
|
|
{
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.type.get()->isNull())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if at least one argument is null.
|
|
|
|
bool hasNullArguments(const DataTypes & args)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.get()->isNull())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if a block contains at least one nullable column.
|
2016-08-05 07:57:02 +00:00
|
|
|
bool hasNullableColumns(const Block & block, const ColumnNumbers & arguments)
|
2016-08-05 07:49:56 +00:00
|
|
|
{
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
const auto & elem = block.unsafeGetByPosition(arg);
|
2016-08-12 15:22:28 +00:00
|
|
|
if (elem.column && elem.column->isNullable())
|
2016-08-05 07:49:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if at least one column is nullable.
|
2016-07-06 09:47:55 +00:00
|
|
|
bool hasNullableColumns(const ColumnsWithTypeAndName & args)
|
|
|
|
{
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.type.get()->isNullable())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Check if at least one argument is nullable.
|
|
|
|
bool hasNullableArguments(const DataTypes & args)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.get()->isNullable())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Turn the specified set of columns into a set of non-nullable columns.
|
2016-07-06 09:47:55 +00:00
|
|
|
ColumnsWithTypeAndName toNonNullableColumns(const ColumnsWithTypeAndName & args)
|
|
|
|
{
|
|
|
|
ColumnsWithTypeAndName new_args;
|
|
|
|
new_args.reserve(args.size());
|
|
|
|
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.type.get()->isNullable())
|
|
|
|
{
|
|
|
|
auto nullable_col = static_cast<const ColumnNullable *>(arg.column.get());
|
|
|
|
ColumnPtr nested_col = (nullable_col != nullptr) ? nullable_col->getNestedColumn() : nullptr;
|
|
|
|
auto nullable_type = static_cast<const DataTypeNullable *>(arg.type.get());
|
|
|
|
DataTypePtr nested_type = nullable_type->getNestedType();
|
|
|
|
|
|
|
|
new_args.emplace_back(nested_col, nested_type, arg.name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
new_args.emplace_back(arg.column, arg.type, arg.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_args;
|
|
|
|
}
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
/// Turn the specified set of data types into a set of non-nullable data types.
|
|
|
|
DataTypes toNonNullableArguments(const DataTypes & args)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
|
|
|
DataTypes new_args;
|
|
|
|
new_args.reserve(args.size());
|
|
|
|
|
|
|
|
for (const auto & arg : args)
|
|
|
|
{
|
|
|
|
if (arg.get()->isNullable())
|
|
|
|
{
|
|
|
|
auto nullable_type = static_cast<const DataTypeNullable *>(arg.get());
|
|
|
|
DataTypePtr nested_type = nullable_type->getNestedType();
|
|
|
|
new_args.push_back(nested_type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
new_args.push_back(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr IFunction::getReturnType(const DataTypes & arguments) const
|
|
|
|
{
|
2016-08-05 09:19:32 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullArguments(arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
return std::make_shared<DataTypeNull>();
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullableArguments(arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-05 09:19:32 +00:00
|
|
|
const DataTypes new_args = toNonNullableArguments(arguments);
|
2016-07-06 09:47:55 +00:00
|
|
|
return getReturnTypeImpl(new_args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return getReturnTypeImpl(arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IFunction::getReturnTypeAndPrerequisites(
|
|
|
|
const ColumnsWithTypeAndName & arguments,
|
|
|
|
DataTypePtr & out_return_type,
|
|
|
|
std::vector<ExpressionAction> & out_prerequisites)
|
|
|
|
{
|
|
|
|
if (!hasSpecialSupportForNulls() && hasNullColumns(arguments))
|
|
|
|
{
|
|
|
|
out_return_type = std::make_shared<DataTypeNull>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasSpecialSupportForNulls() && hasNullableColumns(arguments))
|
|
|
|
{
|
|
|
|
const ColumnsWithTypeAndName new_args = toNonNullableColumns(arguments);
|
|
|
|
getReturnTypeAndPrerequisitesImpl(new_args, out_return_type, out_prerequisites);
|
|
|
|
out_return_type = std::make_shared<DataTypeNullable>(out_return_type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
getReturnTypeAndPrerequisitesImpl(arguments, out_return_type, out_prerequisites);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IFunction::getLambdaArgumentTypes(DataTypes & arguments) const
|
|
|
|
{
|
2016-08-05 09:19:32 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullArguments(arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
return;
|
|
|
|
|
2016-08-05 09:19:32 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullableArguments(arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-05 09:19:32 +00:00
|
|
|
DataTypes new_args = toNonNullableArguments(arguments);
|
2016-07-06 09:47:55 +00:00
|
|
|
getLambdaArgumentTypesImpl(new_args);
|
|
|
|
arguments = std::move(new_args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
getLambdaArgumentTypesImpl(arguments);
|
|
|
|
}
|
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
/// Return a copy of a given block in which the specified columns are replaced by
|
|
|
|
/// their respective nested columns if they are nullable.
|
2016-08-11 00:39:12 +00:00
|
|
|
Block IFunction::extractNonNullableBlock(const Block & block, ColumnNumbers args)
|
2016-08-10 19:12:29 +00:00
|
|
|
{
|
2016-08-11 00:17:30 +00:00
|
|
|
std::sort(args.begin(), args.end());
|
2016-08-10 19:12:29 +00:00
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
Block non_nullable_block;
|
2016-08-10 19:12:29 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < block.columns(); ++i)
|
|
|
|
{
|
|
|
|
const auto & col = block.unsafeGetByPosition(i);
|
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
bool found = std::binary_search(args.begin(), args.end(), i) && col.column && col.type;
|
2016-08-10 19:12:29 +00:00
|
|
|
|
2016-08-12 15:22:28 +00:00
|
|
|
if (found && col.column->isNullable())
|
2016-08-10 19:12:29 +00:00
|
|
|
{
|
|
|
|
auto nullable_col = static_cast<const ColumnNullable *>(col.column.get());
|
|
|
|
ColumnPtr nested_col = nullable_col->getNestedColumn();
|
|
|
|
|
|
|
|
auto nullable_type = static_cast<const DataTypeNullable *>(col.type.get());
|
|
|
|
DataTypePtr nested_type = nullable_type->getNestedType();
|
|
|
|
|
2016-08-11 00:17:30 +00:00
|
|
|
non_nullable_block.insert(i, {nested_col, nested_type, col.name});
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
|
|
|
else
|
2016-08-11 00:17:30 +00:00
|
|
|
non_nullable_block.insert(i, col);
|
2016-08-10 19:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return non_nullable_block;
|
|
|
|
}
|
|
|
|
|
2016-08-05 11:31:55 +00:00
|
|
|
template <typename Fun>
|
|
|
|
void IFunction::perform(Block & block, const ColumnNumbers & arguments, size_t result, const Fun & performer)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-05 07:49:56 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullColumns(block, arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-12 15:22:28 +00:00
|
|
|
/// We have found at least one NULL argument. Therefore we return NULL.
|
2016-07-06 09:47:55 +00:00
|
|
|
ColumnWithTypeAndName & dest_col = block.getByPosition(result);
|
2016-08-04 15:06:27 +00:00
|
|
|
dest_col.column = std::make_shared<ColumnNull>(block.rowsInFirstColumn(), Null());
|
2016-07-06 09:47:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:49:56 +00:00
|
|
|
if (!hasSpecialSupportForNulls() && hasNullableColumns(block, arguments))
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
Block non_nullable_block = extractNonNullableBlock(block, arguments);
|
2016-08-05 11:31:55 +00:00
|
|
|
performer(non_nullable_block, arguments, result);
|
2016-08-12 15:22:28 +00:00
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
const ColumnWithTypeAndName & source_col = non_nullable_block.getByPosition(result);
|
|
|
|
ColumnWithTypeAndName & dest_col = block.getByPosition(result);
|
|
|
|
dest_col.column = std::make_shared<ColumnNullable>(source_col.column);
|
2016-08-12 15:22:28 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
ColumnNullable & nullable_col = static_cast<ColumnNullable &>(*dest_col.column);
|
2016-08-11 00:17:30 +00:00
|
|
|
nullable_col.getNullValuesByteMap() = std::make_shared<ColumnUInt8>(dest_col.column->size(), 0);
|
|
|
|
createNullValuesByteMap(block, arguments, result);
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
else
|
2016-08-05 11:31:55 +00:00
|
|
|
performer(block, arguments, result);
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 11:31:55 +00:00
|
|
|
void IFunction::execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-05 11:31:55 +00:00
|
|
|
auto performer = [&](Block & block, const ColumnNumbers & arguments, size_t result)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
2016-08-05 11:31:55 +00:00
|
|
|
executeImpl(block, arguments, result);
|
|
|
|
};
|
2016-07-06 09:47:55 +00:00
|
|
|
|
2016-08-05 11:31:55 +00:00
|
|
|
perform(block, arguments, result, performer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IFunction::execute(Block & block, const ColumnNumbers & arguments, const ColumnNumbers & prerequisites, size_t result)
|
|
|
|
{
|
|
|
|
auto performer = [&](Block & block, const ColumnNumbers & arguments, size_t result)
|
2016-07-06 09:47:55 +00:00
|
|
|
{
|
|
|
|
executeImpl(block, arguments, prerequisites, result);
|
2016-08-05 11:31:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
perform(block, arguments, result, performer);
|
2016-07-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|