2019-05-20 21:06:43 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2018-09-10 00:51:08 +00:00
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2019-05-20 21:06:43 +00:00
|
|
|
#include <Functions/GatherUtils/Algorithms.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <Functions/GatherUtils/GatherUtils.h>
|
2018-09-09 23:47:56 +00:00
|
|
|
#include <Functions/GatherUtils/Sinks.h>
|
|
|
|
#include <Functions/GatherUtils/Slices.h>
|
2019-05-20 21:06:43 +00:00
|
|
|
#include <Functions/GatherUtils/Sources.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2018-09-10 02:36:33 +00:00
|
|
|
#include <ext/map.h>
|
2019-05-20 21:06:43 +00:00
|
|
|
#include <ext/range.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
#include "formatString.h"
|
2018-09-09 23:36:06 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2019-05-20 21:06:43 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using namespace GatherUtils;
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Name, bool is_injective>
|
|
|
|
class ConcatImpl : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
2020-03-18 03:27:32 +00:00
|
|
|
explicit ConcatImpl(const Context & context_) : context(context_) {}
|
2019-05-20 21:06:43 +00:00
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<ConcatImpl>(context); }
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
String getName() const override { return name; }
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
bool isInjective(const Block &) override { return is_injective; }
|
2018-09-09 23:36:06 +00:00
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
2019-05-20 21:06:43 +00:00
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be at least 2.",
|
2018-09-09 23:36:06 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-05-20 21:12:25 +00:00
|
|
|
if (arguments.size() > FormatImpl::argument_threshold)
|
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be at most " + std::to_string(FormatImpl::argument_threshold),
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2018-09-09 23:36:06 +00:00
|
|
|
for (const auto arg_idx : ext::range(0, arguments.size()))
|
|
|
|
{
|
|
|
|
const auto arg = arguments[arg_idx].get();
|
|
|
|
if (!isStringOrFixedString(arg))
|
2019-05-20 21:06:43 +00:00
|
|
|
throw Exception{"Illegal type " + arg->getName() + " of argument " + std::to_string(arg_idx + 1) + " of function "
|
|
|
|
+ getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
|
|
|
|
{
|
2019-05-20 21:06:43 +00:00
|
|
|
/// Format function is not proven to be faster for two arguments.
|
|
|
|
/// Actually there is overhead of 2 to 5 extra instructions for each string for checking empty strings in FormatImpl.
|
|
|
|
/// Though, benchmarks are really close, for most examples we saw executeBinary is slightly faster (0-3%).
|
|
|
|
/// For 3 and more arguments FormatImpl is much faster (up to 50-60%).
|
2018-09-09 23:36:06 +00:00
|
|
|
if (arguments.size() == 2)
|
|
|
|
executeBinary(block, arguments, result, input_rows_count);
|
|
|
|
else
|
2019-05-20 21:06:43 +00:00
|
|
|
executeFormatImpl(block, arguments, result, input_rows_count);
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Context & context;
|
|
|
|
|
|
|
|
void executeBinary(Block & block, const ColumnNumbers & arguments, const size_t result, size_t input_rows_count)
|
|
|
|
{
|
|
|
|
const IColumn * c0 = block.getByPosition(arguments[0]).column.get();
|
|
|
|
const IColumn * c1 = block.getByPosition(arguments[1]).column.get();
|
|
|
|
|
|
|
|
const ColumnString * c0_string = checkAndGetColumn<ColumnString>(c0);
|
|
|
|
const ColumnString * c1_string = checkAndGetColumn<ColumnString>(c1);
|
|
|
|
const ColumnConst * c0_const_string = checkAndGetColumnConst<ColumnString>(c0);
|
|
|
|
const ColumnConst * c1_const_string = checkAndGetColumnConst<ColumnString>(c1);
|
|
|
|
|
|
|
|
auto c_res = ColumnString::create();
|
|
|
|
|
|
|
|
if (c0_string && c1_string)
|
|
|
|
concat(StringSource(*c0_string), StringSource(*c1_string), StringSink(*c_res, c0->size()));
|
|
|
|
else if (c0_string && c1_const_string)
|
|
|
|
concat(StringSource(*c0_string), ConstSource<StringSource>(*c1_const_string), StringSink(*c_res, c0->size()));
|
|
|
|
else if (c0_const_string && c1_string)
|
|
|
|
concat(ConstSource<StringSource>(*c0_const_string), StringSource(*c1_string), StringSink(*c_res, c0->size()));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Fallback: use generic implementation for not very important cases.
|
2019-05-20 21:06:43 +00:00
|
|
|
executeFormatImpl(block, arguments, result, input_rows_count);
|
2018-09-09 23:36:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(c_res);
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
void executeFormatImpl(Block & block, const ColumnNumbers & arguments, const size_t result, size_t input_rows_count)
|
2018-09-09 23:36:06 +00:00
|
|
|
{
|
2019-05-20 21:06:43 +00:00
|
|
|
auto c_res = ColumnString::create();
|
|
|
|
std::vector<const ColumnString::Chars *> data(arguments.size());
|
|
|
|
std::vector<const ColumnString::Offsets *> offsets(arguments.size());
|
|
|
|
std::vector<size_t> fixed_string_N(arguments.size());
|
|
|
|
std::vector<String> constant_strings(arguments.size());
|
|
|
|
bool has_column_string = false;
|
|
|
|
bool has_column_fixed_string = false;
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
const ColumnPtr & column = block.getByPosition(arguments[i]).column;
|
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
|
|
|
|
{
|
|
|
|
has_column_string = true;
|
|
|
|
data[i] = &col->getChars();
|
|
|
|
offsets[i] = &col->getOffsets();
|
|
|
|
}
|
|
|
|
else if (const ColumnFixedString * fixed_col = checkAndGetColumn<ColumnFixedString>(column.get()))
|
|
|
|
{
|
|
|
|
has_column_fixed_string = true;
|
|
|
|
data[i] = &fixed_col->getChars();
|
|
|
|
fixed_string_N[i] = fixed_col->getN();
|
|
|
|
}
|
|
|
|
else if (const ColumnConst * const_col = checkAndGetColumnConstStringOrFixedString(column.get()))
|
|
|
|
{
|
|
|
|
constant_strings[i] = const_col->getValue<String>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
|
|
|
"Illegal column " + column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-05-20 21:06:43 +00:00
|
|
|
String pattern;
|
|
|
|
pattern.reserve(2 * arguments.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
|
|
|
pattern += "{}";
|
|
|
|
|
|
|
|
FormatImpl::formatExecute(
|
|
|
|
has_column_string,
|
|
|
|
has_column_fixed_string,
|
|
|
|
std::move(pattern),
|
|
|
|
data,
|
|
|
|
offsets,
|
|
|
|
fixed_string_N,
|
|
|
|
constant_strings,
|
|
|
|
c_res->getChars(),
|
|
|
|
c_res->getOffsets(),
|
|
|
|
input_rows_count);
|
2018-09-09 23:36:06 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(c_res);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-10 00:51:08 +00:00
|
|
|
|
2018-09-09 23:36:06 +00:00
|
|
|
struct NameConcat
|
|
|
|
{
|
|
|
|
static constexpr auto name = "concat";
|
|
|
|
};
|
|
|
|
struct NameConcatAssumeInjective
|
|
|
|
{
|
|
|
|
static constexpr auto name = "concatAssumeInjective";
|
|
|
|
};
|
|
|
|
|
|
|
|
using FunctionConcat = ConcatImpl<NameConcat, false>;
|
|
|
|
using FunctionConcatAssumeInjective = ConcatImpl<NameConcatAssumeInjective, true>;
|
|
|
|
|
2018-09-10 00:51:08 +00:00
|
|
|
|
|
|
|
/// Also works with arrays.
|
2019-12-12 14:16:59 +00:00
|
|
|
class ConcatOverloadResolver : public IFunctionOverloadResolverImpl
|
2018-09-10 00:51:08 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "concat";
|
2019-12-12 14:16:59 +00:00
|
|
|
static FunctionOverloadResolverImplPtr create(const Context & context) { return std::make_unique<ConcatOverloadResolver>(context); }
|
2018-09-10 00:51:08 +00:00
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
explicit ConcatOverloadResolver(const Context & context_) : context(context_) {}
|
2018-09-10 00:51:08 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
2018-09-10 01:19:14 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2018-09-10 01:39:06 +00:00
|
|
|
bool isVariadic() const override { return true; }
|
2018-09-10 00:51:08 +00:00
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
FunctionBaseImplPtr build(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type) const override
|
2018-09-10 00:51:08 +00:00
|
|
|
{
|
|
|
|
if (isArray(arguments.at(0).type))
|
2019-12-10 13:21:26 +00:00
|
|
|
{
|
|
|
|
return FunctionOverloadResolverAdaptor(FunctionFactory::instance().getImpl("arrayConcat", context)).buildImpl(arguments);
|
|
|
|
}
|
2018-09-10 00:51:08 +00:00
|
|
|
else
|
2019-12-09 14:41:55 +00:00
|
|
|
return std::make_unique<DefaultFunction>(
|
2019-05-20 21:06:43 +00:00
|
|
|
FunctionConcat::create(context), ext::map<DataTypes>(arguments, [](const auto & elem) { return elem.type; }), return_type);
|
2018-09-10 00:51:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2018-09-10 00:51:08 +00:00
|
|
|
{
|
2018-09-10 02:36:33 +00:00
|
|
|
if (arguments.size() < 2)
|
2019-05-20 21:06:43 +00:00
|
|
|
throw Exception(
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
|
|
|
|
+ ", should be at least 2.",
|
2018-09-10 02:36:33 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2018-09-10 00:51:08 +00:00
|
|
|
|
2019-11-05 13:23:09 +00:00
|
|
|
/// We always return Strings from concat, even if arguments were fixed strings.
|
|
|
|
return std::make_shared<DataTypeString>();
|
2018-09-10 00:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Context & context;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-09-09 23:36:06 +00:00
|
|
|
void registerFunctionsConcat(FunctionFactory & factory)
|
|
|
|
{
|
2019-12-12 14:16:59 +00:00
|
|
|
factory.registerFunction<ConcatOverloadResolver>(FunctionFactory::CaseInsensitive);
|
2018-09-09 23:36:06 +00:00
|
|
|
factory.registerFunction<FunctionConcatAssumeInjective>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|