2012-09-17 04:34:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-12 21:23:40 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
2021-10-19 17:54:27 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
2021-10-12 21:23:40 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2021-10-12 21:23:40 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-10-12 21:23:40 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-10-22 20:19:32 +00:00
|
|
|
#include <Functions/FunctionsConversion.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/Regexps.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2021-10-12 21:23:40 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-08-16 14:21:24 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2022-01-30 01:10:27 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-06-13 02:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** Functions that split strings into an array of strings or vice versa.
|
2012-09-17 04:34:19 +00:00
|
|
|
*
|
2012-09-23 05:28:59 +00:00
|
|
|
* splitByChar(sep, s)
|
|
|
|
* splitByString(sep, s)
|
|
|
|
* splitByRegexp(regexp, s)
|
2021-06-19 18:52:09 +00:00
|
|
|
*
|
2021-06-19 12:33:36 +00:00
|
|
|
* splitByWhitespace(s) - split the string by whitespace characters
|
|
|
|
* splitByNonAlpha(s) - split the string by whitespace and punctuation characters
|
2012-09-23 05:28:59 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* extractAll(s, regexp) - select from the string the subsequences corresponding to the regexp.
|
|
|
|
* - first subpattern, if regexp has subpattern;
|
|
|
|
* - zero subpattern (the match part, otherwise);
|
|
|
|
* - otherwise, an empty array
|
2013-08-02 16:31:23 +00:00
|
|
|
*
|
2015-10-19 21:07:24 +00:00
|
|
|
* arrayStringConcat(arr)
|
|
|
|
* arrayStringConcat(arr, delimiter)
|
2017-05-27 15:45:25 +00:00
|
|
|
* - join an array of strings into one string via a separator.
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* alphaTokens(s) - select from the string subsequence `[a-zA-Z]+`.
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* URL functions are located separately.
|
2012-09-17 04:34:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using Pos = const char *;
|
2012-09-17 04:34:19 +00:00
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Substring generators. All of them have a common interface.
|
2012-09-23 05:28:59 +00:00
|
|
|
|
|
|
|
class AlphaTokensImpl
|
2012-09-17 04:34:19 +00:00
|
|
|
{
|
2012-09-23 05:28:59 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
|
|
|
public:
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get the name of the function.
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "alphaTokens";
|
|
|
|
static String getName() { return name; }
|
|
|
|
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return false; }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static size_t getNumberOfArguments() { return 1; }
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Check the type of the function's arguments.
|
2017-04-01 07:20:54 +00:00
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Initialize by the function arguments.
|
2020-11-17 13:24:45 +00:00
|
|
|
void init(const ColumnsWithTypeAndName & /*arguments*/) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Called for each next string.
|
2017-04-01 07:20:54 +00:00
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Returns the position of the argument, that is the column of strings
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get the next token, if any, or return false.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
|
|
|
/// Skip garbage
|
|
|
|
while (pos < end && !isAlphaASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
|
|
|
while (pos < end && isAlphaASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
token_end = pos;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-23 05:28:59 +00:00
|
|
|
};
|
2012-09-17 04:34:19 +00:00
|
|
|
|
2021-06-19 12:33:36 +00:00
|
|
|
class SplitByNonAlphaImpl
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Get the name of the function.
|
|
|
|
static constexpr auto name = "splitByNonAlpha";
|
|
|
|
static String getName() { return name; }
|
|
|
|
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return false; }
|
2021-06-19 12:33:36 +00:00
|
|
|
static size_t getNumberOfArguments() { return 1; }
|
|
|
|
|
|
|
|
/// Check the type of the function's arguments.
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize by the function arguments.
|
|
|
|
void init(const ColumnsWithTypeAndName & /*arguments*/) {}
|
|
|
|
|
|
|
|
/// Called for each next string.
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the position of the argument, that is the column of strings
|
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the next token, if any, or return false.
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
|
|
|
/// Skip garbage
|
|
|
|
while (pos < end && (isWhitespaceASCII(*pos) || isPunctuationASCII(*pos)))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
|
|
|
while (pos < end && !(isWhitespaceASCII(*pos) || isPunctuationASCII(*pos)))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
token_end = pos;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SplitByWhitespaceImpl
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Get the name of the function.
|
|
|
|
static constexpr auto name = "splitByWhitespace";
|
|
|
|
static String getName() { return name; }
|
|
|
|
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return false; }
|
2021-06-19 12:33:36 +00:00
|
|
|
static size_t getNumberOfArguments() { return 1; }
|
|
|
|
|
|
|
|
/// Check the type of the function's arguments.
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize by the function arguments.
|
|
|
|
void init(const ColumnsWithTypeAndName & /*arguments*/) {}
|
|
|
|
|
|
|
|
/// Called for each next string.
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the position of the argument, that is the column of strings
|
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the next token, if any, or return false.
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
|
|
|
/// Skip garbage
|
|
|
|
while (pos < end && isWhitespaceASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
|
|
|
while (pos < end && !isWhitespaceASCII(*pos))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
token_end = pos;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2012-09-17 04:34:19 +00:00
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
class SplitByCharImpl
|
2012-09-17 04:34:19 +00:00
|
|
|
{
|
2012-09-23 05:28:59 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char sep;
|
2022-01-29 15:42:04 +00:00
|
|
|
Int64 max_split = -1;
|
|
|
|
Int64 curr_split = 0;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2012-09-17 04:34:19 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "splitByChar";
|
|
|
|
static String getName() { return name; }
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return true; }
|
|
|
|
static size_t getNumberOfArguments() { return 0; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
2022-01-30 01:10:27 +00:00
|
|
|
if (arguments.size() < 2 || arguments.size() > 3)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Function '{}' needs at least 2 arguments, at most 3 arguments; passed {}.",
|
|
|
|
arguments.size());
|
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[1]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() + " of second argument of function " + getName() + ". Must be String.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2022-01-29 15:42:04 +00:00
|
|
|
|
2022-01-30 01:10:27 +00:00
|
|
|
if (arguments.size() == 3 && !isNativeInteger(arguments[2]))
|
2022-01-29 15:42:04 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Third argument for function '{}' must be integer, got '{}' instead",
|
|
|
|
getName(),
|
|
|
|
arguments[2]->getName());
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
void init(const ColumnsWithTypeAndName & arguments)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnConst * col = checkAndGetColumnConstStringOrFixedString(arguments[0].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!col)
|
2020-10-18 19:00:13 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
String sep_str = col->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (sep_str.size() != 1)
|
2018-11-22 21:19:58 +00:00
|
|
|
throw Exception("Illegal separator for function " + getName() + ". Must be exactly one byte.", ErrorCodes::BAD_ARGUMENTS);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
sep = sep_str[0];
|
2022-01-29 15:42:04 +00:00
|
|
|
|
|
|
|
if (arguments.size() > 2)
|
|
|
|
{
|
|
|
|
std::optional<Int64> max_split_opt = std::nullopt;
|
|
|
|
if (!((max_split_opt = getMaxSplit<UInt8>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<Int8>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<UInt16>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<Int16>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<UInt32>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<Int32>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<UInt64>(arguments[2]))
|
|
|
|
|| (max_split_opt = getMaxSplit<Int64>(arguments[2]))))
|
|
|
|
{
|
|
|
|
throw Exception{
|
|
|
|
"Illegal column " + arguments[2].column->getName() + " of third argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
|
|
|
max_split = *max_split_opt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataType>
|
|
|
|
std::optional<Int64> getMaxSplit(const ColumnWithTypeAndName & argument)
|
|
|
|
{
|
|
|
|
const auto * col = checkAndGetColumnConst<ColumnVector<DataType>>(argument.column.get());
|
|
|
|
if (!col)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
Int64 result= static_cast<Int64>(col->template getValue<DataType>());
|
|
|
|
if (result < 0 && result != -1)
|
|
|
|
throw Exception("Illegal column " + argument.column->getName()
|
|
|
|
+ " of third argument of function " + getName() + ". Must be non-negative number or -1",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
return result;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Returns the position of the argument, that is the column of strings
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
|
|
|
if (!pos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
2022-01-29 15:42:04 +00:00
|
|
|
if (unlikely(max_split >= 0 && curr_split >= max_split))
|
|
|
|
{
|
|
|
|
token_end = end;
|
|
|
|
pos = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
pos = reinterpret_cast<Pos>(memchr(pos, sep, end - pos));
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
token_end = pos;
|
|
|
|
++pos;
|
2022-01-29 15:42:04 +00:00
|
|
|
++curr_split;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
token_end = end;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-23 05:28:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class SplitByStringImpl
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String sep;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "splitByString";
|
|
|
|
static String getName() { return name; }
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
static size_t getNumberOfArguments() { return 2; }
|
|
|
|
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
|
|
|
SplitByCharImpl::checkArguments(arguments);
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
void init(const ColumnsWithTypeAndName & arguments)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnConst * col = checkAndGetColumnConstStringOrFixedString(arguments[0].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!col)
|
2020-10-18 19:00:13 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
sep = col->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Returns the position of the argument that is the column of strings
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Called for each next string.
|
2017-04-01 07:20:54 +00:00
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get the next token, if any, or return false.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
2020-03-19 02:26:40 +00:00
|
|
|
if (sep.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-03-20 05:37:25 +00:00
|
|
|
if (pos == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
2020-03-19 02:26:40 +00:00
|
|
|
pos += 1;
|
2017-04-01 07:20:54 +00:00
|
|
|
token_end = pos;
|
|
|
|
}
|
|
|
|
else
|
2020-03-19 02:26:40 +00:00
|
|
|
{
|
2020-03-20 05:37:25 +00:00
|
|
|
if (!pos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
2020-03-19 02:26:40 +00:00
|
|
|
pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size()));
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
token_end = pos;
|
|
|
|
pos += sep.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
token_end = end;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-23 05:28:59 +00:00
|
|
|
};
|
|
|
|
|
2021-05-13 02:37:09 +00:00
|
|
|
class SplitByRegexpImpl
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Regexps::Pool::Pointer re;
|
|
|
|
OptimizedRegularExpression::MatchVec matches;
|
|
|
|
|
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "splitByRegexp";
|
|
|
|
static String getName() { return name; }
|
2022-01-30 01:10:27 +00:00
|
|
|
|
|
|
|
static bool isVariadic() { return false; }
|
2021-05-13 02:37:09 +00:00
|
|
|
static size_t getNumberOfArguments() { return 2; }
|
|
|
|
|
|
|
|
/// Check the type of function arguments.
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
{
|
|
|
|
SplitByStringImpl::checkArguments(arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize by the function arguments.
|
|
|
|
void init(const ColumnsWithTypeAndName & arguments)
|
|
|
|
{
|
|
|
|
const ColumnConst * col = checkAndGetColumnConstStringOrFixedString(arguments[0].column.get());
|
|
|
|
|
|
|
|
if (!col)
|
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
|
|
|
+ " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2021-05-13 03:15:38 +00:00
|
|
|
if (!col->getValue<String>().empty())
|
|
|
|
re = Regexps::get<false, false>(col->getValue<String>());
|
2021-05-13 02:37:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the position of the argument that is the column of strings
|
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called for each next string.
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the next token, if any, or return false.
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
2021-05-13 03:15:38 +00:00
|
|
|
if (!re)
|
2021-05-13 02:37:09 +00:00
|
|
|
{
|
2021-05-13 03:15:38 +00:00
|
|
|
if (pos == end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
pos += 1;
|
|
|
|
token_end = pos;
|
2021-05-13 02:37:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-13 03:15:38 +00:00
|
|
|
if (!pos || pos > end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
|
|
|
if (!re->match(pos, end - pos, matches) || !matches[0].length)
|
|
|
|
{
|
|
|
|
token_end = end;
|
|
|
|
pos = end + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token_end = pos + matches[0].offset;
|
|
|
|
pos = token_end + matches[0].length;
|
|
|
|
}
|
2021-05-13 02:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-05 08:40:56 +00:00
|
|
|
class ExtractAllImpl
|
2013-08-02 13:09:42 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
Regexps::Pool::Pointer re;
|
|
|
|
OptimizedRegularExpression::MatchVec matches;
|
|
|
|
size_t capture;
|
2013-08-02 13:09:42 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Pos pos;
|
|
|
|
Pos end;
|
2013-08-02 13:09:42 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "extractAll";
|
|
|
|
static String getName() { return name; }
|
2022-01-30 01:10:27 +00:00
|
|
|
static bool isVariadic() { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
static size_t getNumberOfArguments() { return 2; }
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Check the type of function arguments.
|
2018-11-24 01:48:06 +00:00
|
|
|
static void checkArguments(const DataTypes & arguments)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
SplitByStringImpl::checkArguments(arguments);
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Initialize by the function arguments.
|
2020-11-17 13:24:45 +00:00
|
|
|
void init(const ColumnsWithTypeAndName & arguments)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnConst * col = checkAndGetColumnConstStringOrFixedString(arguments[1].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!col)
|
2020-10-18 19:00:13 +00:00
|
|
|
throw Exception("Illegal column " + arguments[1].column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
re = Regexps::get<false, false>(col->getValue<String>());
|
2017-04-01 07:20:54 +00:00
|
|
|
capture = re->getNumberOfSubpatterns() > 0 ? 1 : 0;
|
|
|
|
|
|
|
|
matches.resize(capture + 1);
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Returns the position of the argument that is the column of strings
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getStringsArgumentPosition()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Called for each next string.
|
2017-04-01 07:20:54 +00:00
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
{
|
|
|
|
pos = pos_;
|
|
|
|
end = end_;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Get the next token, if any, or return false.
|
2017-04-01 07:20:54 +00:00
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
{
|
|
|
|
if (!pos || pos > end)
|
|
|
|
return false;
|
|
|
|
|
2017-11-18 02:48:11 +00:00
|
|
|
if (!re->match(pos, end - pos, matches) || !matches[0].length)
|
2017-04-01 07:20:54 +00:00
|
|
|
return false;
|
|
|
|
|
2018-12-27 01:51:22 +00:00
|
|
|
if (matches[capture].offset == std::string::npos)
|
|
|
|
{
|
|
|
|
/// Empty match.
|
|
|
|
token_begin = pos;
|
|
|
|
token_end = pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
token_begin = pos + matches[capture].offset;
|
|
|
|
token_end = token_begin + matches[capture].length;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-18 02:48:11 +00:00
|
|
|
pos += matches[0].offset + matches[0].length;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-02 13:09:42 +00:00
|
|
|
};
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// A function that takes a string, and returns an array of substrings created by some generator.
|
2012-09-23 05:28:59 +00:00
|
|
|
template <typename Generator>
|
|
|
|
class FunctionTokens : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Generator::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionTokens>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2022-01-30 01:10:27 +00:00
|
|
|
bool isVariadic() const override { return Generator::isVariadic(); }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getNumberOfArguments() const override { return Generator::getNumberOfArguments(); }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
Generator::checkArguments(arguments);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
Generator generator;
|
2020-10-18 19:00:13 +00:00
|
|
|
generator.init(arguments);
|
|
|
|
const auto & array_argument = arguments[generator.getStringsArgumentPosition()];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnString * col_str = checkAndGetColumn<ColumnString>(array_argument.column.get());
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnConst * col_const_str =
|
2020-10-18 19:00:13 +00:00
|
|
|
checkAndGetColumnConstStringOrFixedString(array_argument.column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnArray::create(ColumnString::create());
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnString & res_strings = typeid_cast<ColumnString &>(col_res->getData());
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnArray::Offsets & res_offsets = col_res->getOffsets();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & res_strings_chars = res_strings.getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & res_strings_offsets = res_strings.getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_str)
|
|
|
|
{
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & src_chars = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & src_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
res_offsets.reserve(src_offsets.size());
|
2017-05-27 15:45:25 +00:00
|
|
|
res_strings_offsets.reserve(src_offsets.size() * 5); /// Constant 5 - at random.
|
2017-04-01 07:20:54 +00:00
|
|
|
res_strings_chars.reserve(src_chars.size());
|
|
|
|
|
|
|
|
Pos token_begin = nullptr;
|
|
|
|
Pos token_end = nullptr;
|
|
|
|
|
|
|
|
size_t size = src_offsets.size();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offset current_src_offset = 0;
|
|
|
|
ColumnArray::Offset current_dst_offset = 0;
|
|
|
|
ColumnString::Offset current_dst_strings_offset = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
Pos pos = reinterpret_cast<Pos>(&src_chars[current_src_offset]);
|
|
|
|
current_src_offset = src_offsets[i];
|
|
|
|
Pos end = reinterpret_cast<Pos>(&src_chars[current_src_offset]) - 1;
|
|
|
|
|
|
|
|
generator.set(pos, end);
|
|
|
|
|
|
|
|
size_t j = 0;
|
|
|
|
while (generator.get(token_begin, token_end))
|
|
|
|
{
|
|
|
|
size_t token_size = token_end - token_begin;
|
|
|
|
|
|
|
|
res_strings_chars.resize(res_strings_chars.size() + token_size + 1);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(&res_strings_chars[current_dst_strings_offset], token_begin, token_size);
|
|
|
|
res_strings_chars[current_dst_strings_offset + token_size] = 0;
|
|
|
|
|
|
|
|
current_dst_strings_offset += token_size + 1;
|
|
|
|
res_strings_offsets.push_back(current_dst_strings_offset);
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_dst_offset += j;
|
|
|
|
res_offsets.push_back(current_dst_offset);
|
|
|
|
}
|
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return col_res;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else if (col_const_str)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
String src = col_const_str->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
Array dst;
|
|
|
|
|
|
|
|
generator.set(src.data(), src.data() + src.size());
|
|
|
|
Pos token_begin = nullptr;
|
|
|
|
Pos token_end = nullptr;
|
|
|
|
|
|
|
|
while (generator.get(token_begin, token_end))
|
|
|
|
dst.push_back(String(token_begin, token_end - token_begin));
|
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return result_type->createColumnConst(col_const_str->size(), dst);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 19:00:13 +00:00
|
|
|
throw Exception("Illegal columns " + array_argument.column->getName()
|
|
|
|
+ ", " + array_argument.column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of arguments of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2012-09-17 04:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-10-22 20:19:32 +00:00
|
|
|
/// Joins an array of type serializable to string into one string via a separator.
|
2015-10-19 18:49:52 +00:00
|
|
|
class FunctionArrayStringConcat : public IFunction
|
|
|
|
{
|
|
|
|
private:
|
2021-10-19 17:54:27 +00:00
|
|
|
static void executeInternal(
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & src_chars,
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & src_string_offsets,
|
|
|
|
const ColumnArray::Offsets & src_array_offsets,
|
2021-10-19 17:54:27 +00:00
|
|
|
const char * delimiter,
|
|
|
|
const size_t delimiter_size,
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & dst_chars,
|
2021-10-19 17:54:27 +00:00
|
|
|
ColumnString::Offsets & dst_string_offsets,
|
|
|
|
const char8_t * null_map)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
size_t size = src_array_offsets.size();
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// With a small margin - as if the separator goes after the last string of the array.
|
2017-04-01 07:20:54 +00:00
|
|
|
dst_chars.resize(
|
|
|
|
src_chars.size()
|
2017-05-27 15:45:25 +00:00
|
|
|
+ delimiter_size * src_string_offsets.size() /// Separators after each string...
|
|
|
|
+ src_array_offsets.size() /// Zero byte after each joined string
|
|
|
|
- src_string_offsets.size()); /// The former zero byte after each string of the array
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// There will be as many strings as there were arrays.
|
2017-04-01 07:20:54 +00:00
|
|
|
dst_string_offsets.resize(src_array_offsets.size());
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnArray::Offset current_src_array_offset = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offset current_dst_string_offset = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Loop through the array of strings.
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2021-10-19 17:54:27 +00:00
|
|
|
bool first_non_null = true;
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Loop through the rows within the array. /// NOTE You can do everything in one copy, if the separator has a size of 1.
|
2017-04-01 07:20:54 +00:00
|
|
|
for (auto next_src_array_offset = src_array_offsets[i]; current_src_array_offset < next_src_array_offset; ++current_src_array_offset)
|
|
|
|
{
|
2021-10-19 17:54:27 +00:00
|
|
|
if (unlikely(null_map && null_map[current_src_array_offset]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!first_non_null)
|
|
|
|
{
|
|
|
|
memcpy(&dst_chars[current_dst_string_offset], delimiter, delimiter_size);
|
|
|
|
current_dst_string_offset += delimiter_size;
|
|
|
|
}
|
|
|
|
first_non_null = false;
|
|
|
|
|
|
|
|
const auto current_src_string_offset = current_src_array_offset ? src_string_offsets[current_src_array_offset - 1] : 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t bytes_to_copy = src_string_offsets[current_src_array_offset] - current_src_string_offset - 1;
|
|
|
|
|
|
|
|
memcpySmallAllowReadWriteOverflow15(
|
|
|
|
&dst_chars[current_dst_string_offset], &src_chars[current_src_string_offset], bytes_to_copy);
|
|
|
|
|
|
|
|
current_dst_string_offset += bytes_to_copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_chars[current_dst_string_offset] = 0;
|
|
|
|
++current_dst_string_offset;
|
|
|
|
|
|
|
|
dst_string_offsets[i] = current_dst_string_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_chars.resize(dst_string_offsets.back());
|
|
|
|
}
|
2015-10-19 18:49:52 +00:00
|
|
|
|
2021-10-19 17:54:27 +00:00
|
|
|
static void executeInternal(
|
|
|
|
const ColumnString & col_string,
|
|
|
|
const ColumnArray & col_arr,
|
|
|
|
const String & delimiter,
|
|
|
|
ColumnString & col_res,
|
|
|
|
const char8_t * null_map = nullptr)
|
|
|
|
{
|
|
|
|
executeInternal(
|
|
|
|
col_string.getChars(),
|
|
|
|
col_string.getOffsets(),
|
|
|
|
col_arr.getOffsets(),
|
|
|
|
delimiter.data(),
|
|
|
|
delimiter.size(),
|
|
|
|
col_res.getChars(),
|
|
|
|
col_res.getOffsets(),
|
|
|
|
null_map);
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:19:32 +00:00
|
|
|
static ColumnPtr serializeNestedColumn(const ColumnArray & col_arr, const DataTypePtr & nested_type)
|
|
|
|
{
|
|
|
|
if (isString(nested_type))
|
|
|
|
{
|
|
|
|
return col_arr.getDataPtr();
|
|
|
|
}
|
|
|
|
else if (const ColumnNullable * col_nullable = checkAndGetColumn<ColumnNullable>(col_arr.getData());
|
|
|
|
col_nullable && isString(col_nullable->getNestedColumn().getDataType()))
|
|
|
|
{
|
|
|
|
return col_nullable->getNestedColumnPtr();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ColumnsWithTypeAndName cols;
|
|
|
|
cols.emplace_back(col_arr.getDataPtr(), nested_type, "tmp");
|
2021-12-15 17:40:36 +00:00
|
|
|
return ConvertImplGenericToString<ColumnString>::execute(cols, std::make_shared<DataTypeString>(), col_arr.size());
|
2021-10-22 20:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 18:49:52 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "arrayStringConcat";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionArrayStringConcat>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
2021-10-12 21:23:40 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
String delimiter;
|
|
|
|
if (arguments.size() == 2)
|
|
|
|
{
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnConst * col_delim = checkAndGetColumnConstStringOrFixedString(arguments[1].column.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!col_delim)
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be constant string.", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
delimiter = col_delim->getValue<String>();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:19:32 +00:00
|
|
|
const auto & nested_type = assert_cast<const DataTypeArray &>(*arguments[0].type).getNestedType();
|
|
|
|
if (const ColumnConst * col_const_arr = checkAndGetColumnConst<ColumnArray>(arguments[0].column.get());
|
|
|
|
col_const_arr && isString(nested_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
Array src_arr = col_const_arr->getValue<Array>();
|
|
|
|
String dst_str;
|
2021-10-12 21:23:40 +00:00
|
|
|
bool first_non_null = true;
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0, size = src_arr.size(); i < size; ++i)
|
|
|
|
{
|
2021-10-12 21:23:40 +00:00
|
|
|
if (src_arr[i].isNull())
|
|
|
|
continue;
|
|
|
|
if (!first_non_null)
|
2017-04-01 07:20:54 +00:00
|
|
|
dst_str += delimiter;
|
2021-10-12 21:23:40 +00:00
|
|
|
first_non_null = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
dst_str += src_arr[i].get<const String &>();
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return result_type->createColumnConst(col_const_arr->size(), dst_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2021-10-22 20:19:32 +00:00
|
|
|
|
|
|
|
ColumnPtr src_column = arguments[0].column->convertToFullColumnIfConst();
|
|
|
|
const ColumnArray & col_arr = assert_cast<const ColumnArray &>(*src_column.get());
|
|
|
|
|
|
|
|
ColumnPtr str_subcolumn = serializeNestedColumn(col_arr, nested_type);
|
|
|
|
const ColumnString & col_string = assert_cast<const ColumnString &>(*str_subcolumn.get());
|
|
|
|
|
|
|
|
auto col_res = ColumnString::create();
|
|
|
|
if (const ColumnNullable * col_nullable = checkAndGetColumn<ColumnNullable>(col_arr.getData()))
|
|
|
|
executeInternal(col_string, col_arr, delimiter, *col_res, col_nullable->getNullMapData().data());
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2021-10-22 20:19:32 +00:00
|
|
|
executeInternal(col_string, col_arr, delimiter, *col_res);
|
|
|
|
return col_res;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-10-19 18:49:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
using FunctionAlphaTokens = FunctionTokens<AlphaTokensImpl>;
|
2021-06-19 12:33:36 +00:00
|
|
|
using FunctionSplitByNonAlpha = FunctionTokens<SplitByNonAlphaImpl>;
|
|
|
|
using FunctionSplitByWhitespace = FunctionTokens<SplitByWhitespaceImpl>;
|
2017-06-13 02:06:53 +00:00
|
|
|
using FunctionSplitByChar = FunctionTokens<SplitByCharImpl>;
|
2016-05-28 10:35:44 +00:00
|
|
|
using FunctionSplitByString = FunctionTokens<SplitByStringImpl>;
|
2021-05-13 02:37:09 +00:00
|
|
|
using FunctionSplitByRegexp = FunctionTokens<SplitByRegexpImpl>;
|
2017-06-13 02:06:53 +00:00
|
|
|
using FunctionExtractAll = FunctionTokens<ExtractAllImpl>;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
2012-09-17 04:34:19 +00:00
|
|
|
}
|