2012-09-17 04:34:19 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
#include <DB/Columns/ColumnConst.h>
|
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
|
|
|
|
#include <DB/Functions/IFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Функции, разделяющие строки на массив строк или наоборот.
|
|
|
|
|
*
|
2012-09-23 05:28:59 +00:00
|
|
|
|
* splitByChar(sep, s)
|
|
|
|
|
* splitByString(sep, s)
|
|
|
|
|
* splitByRegexp(regexp, s)
|
|
|
|
|
*
|
|
|
|
|
* extractAll(regexp, s) - выделить из строки подпоследовательности, соответствующие регекспу.
|
|
|
|
|
*
|
2012-09-17 04:34:19 +00:00
|
|
|
|
* join(sep, arr)
|
|
|
|
|
* join(arr)
|
2012-09-23 05:28:59 +00:00
|
|
|
|
*
|
2012-09-22 07:30:40 +00:00
|
|
|
|
* alphaTokens(s) - выделить из строки подпоследовательности [a-zA-Z]+.
|
2012-09-17 04:34:19 +00:00
|
|
|
|
*
|
|
|
|
|
* Функции работы с URL расположены отдельно.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef const char * Pos;
|
|
|
|
|
|
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:
|
|
|
|
|
Pos pos;
|
|
|
|
|
Pos end;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/// Получить имя фукнции.
|
|
|
|
|
static String getName() { return "alphaTokens"; }
|
|
|
|
|
|
|
|
|
|
/// Проверить типы агрументов функции.
|
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2012-09-23 05:28:59 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
if (!dynamic_cast<const DataTypeString *>(&*arguments[0]))
|
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Инициализировать по аргументам функции.
|
|
|
|
|
void init(Block & block, const ColumnNumbers & arguments) {}
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
/// Вызывается для каждой следующей строки.
|
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
|
{
|
|
|
|
|
pos = pos_;
|
|
|
|
|
end = end_;
|
|
|
|
|
}
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
/// Получить следующий токен, если есть, или вернуть false.
|
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
|
{
|
|
|
|
|
/// Пропускаем мусор
|
|
|
|
|
while (pos < end && !((*pos >= 'a' && *pos <= 'z') || (*pos >= 'A' && *pos <= 'Z')))
|
|
|
|
|
++pos;
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
if (pos == end)
|
|
|
|
|
return false;
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
token_begin = pos;
|
|
|
|
|
|
|
|
|
|
while (pos < end && ((*pos >= 'a' && *pos <= 'z') || (*pos >= 'A' && *pos <= 'Z')))
|
|
|
|
|
++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:
|
|
|
|
|
Pos pos;
|
|
|
|
|
Pos end;
|
|
|
|
|
|
|
|
|
|
char sep;
|
|
|
|
|
|
2012-09-17 04:34:19 +00:00
|
|
|
|
public:
|
2012-09-23 05:28:59 +00:00
|
|
|
|
static String getName() { return "splitByChar"; }
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
2012-09-17 04:34:19 +00:00
|
|
|
|
{
|
2012-09-23 05:28:59 +00:00
|
|
|
|
if (arguments.size() != 2)
|
2012-09-17 04:34:19 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
2012-09-17 04:34:19 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (!dynamic_cast<const DataTypeString *>(&*arguments[0]))
|
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName() + ". Must be String.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
if (!dynamic_cast<const DataTypeString *>(&*arguments[1]))
|
|
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() + " of second argument of function " + getName() + ". Must be String.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init(Block & block, const ColumnNumbers & arguments)
|
|
|
|
|
{
|
|
|
|
|
const ColumnConstString * col = dynamic_cast<const ColumnConstString *>(&*block.getByPosition(arguments[0]).column);
|
|
|
|
|
|
|
|
|
|
if (!col)
|
|
|
|
|
throw Exception("Illegal column " + col->getName() + " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
const String & sep_str = col->getData();
|
|
|
|
|
|
|
|
|
|
if (sep_str.size() != 1)
|
|
|
|
|
throw Exception("Illegal separator for function " + getName() + ". Must be exactly one byte.");
|
|
|
|
|
|
|
|
|
|
sep = sep_str[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
pos = reinterpret_cast<Pos>(memchr(pos, sep, end - pos));
|
|
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
|
{
|
|
|
|
|
token_end = pos;
|
|
|
|
|
++pos;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
token_end = end;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SplitByStringImpl
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
Pos pos;
|
|
|
|
|
Pos end;
|
|
|
|
|
|
|
|
|
|
String sep;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static String getName() { return "splitByString"; }
|
|
|
|
|
|
|
|
|
|
static void checkArguments(const DataTypes & arguments)
|
|
|
|
|
{
|
|
|
|
|
SplitByCharImpl::checkArguments(arguments);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init(Block & block, const ColumnNumbers & arguments)
|
|
|
|
|
{
|
|
|
|
|
const ColumnConstString * col = dynamic_cast<const ColumnConstString *>(&*block.getByPosition(arguments[0]).column);
|
|
|
|
|
|
|
|
|
|
if (!col)
|
|
|
|
|
throw Exception("Illegal column " + col->getName() + " of first argument of function " + getName() + ". Must be constant string.",
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
sep = col->getData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Вызывается для каждой следующей строки.
|
|
|
|
|
void set(Pos pos_, Pos end_)
|
|
|
|
|
{
|
|
|
|
|
pos = pos_;
|
|
|
|
|
end = end_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить следующий токен, если есть, или вернуть false.
|
|
|
|
|
bool get(Pos & token_begin, Pos & token_end)
|
|
|
|
|
{
|
|
|
|
|
if (!pos)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
token_begin = pos;
|
|
|
|
|
pos = reinterpret_cast<Pos>(memmem(pos, end - pos, sep.data(), sep.size()));
|
|
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
|
{
|
|
|
|
|
token_end = pos;
|
|
|
|
|
pos += sep.size();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
token_end = end;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Функция, принимающая строку, и возвращающая массив подстрок, создаваемый некоторым генератором.
|
|
|
|
|
template <typename Generator>
|
|
|
|
|
class FunctionTokens : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
|
|
|
|
return Generator::getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const
|
|
|
|
|
{
|
2013-03-26 19:30:23 +00:00
|
|
|
|
Generator::checkArguments(arguments);
|
2012-09-23 05:28:59 +00:00
|
|
|
|
|
2012-09-17 04:34:19 +00:00
|
|
|
|
return new DataTypeArray(new DataTypeString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2013-03-26 19:30:23 +00:00
|
|
|
|
Generator generator;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
generator.init(block, arguments);
|
|
|
|
|
|
|
|
|
|
const ColumnString * col_str = dynamic_cast<const ColumnString *>(&*block.getByPosition(arguments.back()).column);
|
|
|
|
|
const ColumnConstString * col_const_str = dynamic_cast<const ColumnConstString *>(&*block.getByPosition(arguments.back()).column);
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
|
|
|
|
ColumnArray * col_res = new ColumnArray(new ColumnString);
|
|
|
|
|
ColumnString & res_strings = dynamic_cast<ColumnString &>(col_res->getData());
|
|
|
|
|
ColumnArray::Offsets_t & res_offsets = col_res->getOffsets();
|
2013-05-05 15:25:25 +00:00
|
|
|
|
ColumnString::Chars_t & res_strings_chars = res_strings.getChars();
|
2012-09-17 04:34:19 +00:00
|
|
|
|
ColumnString::Offsets_t & res_strings_offsets = res_strings.getOffsets();
|
|
|
|
|
|
|
|
|
|
if (col_str)
|
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
const ColumnString::Chars_t & src_chars = col_str->getChars();
|
2012-09-17 04:34:19 +00:00
|
|
|
|
const ColumnString::Offsets_t & src_offsets = col_str->getOffsets();
|
|
|
|
|
|
|
|
|
|
res_offsets.reserve(src_offsets.size());
|
|
|
|
|
res_strings_offsets.reserve(src_offsets.size() * 5); /// Константа 5 - наугад.
|
|
|
|
|
res_strings_chars.reserve(src_chars.size());
|
|
|
|
|
|
|
|
|
|
Pos token_begin = NULL;
|
|
|
|
|
Pos token_end = NULL;
|
|
|
|
|
|
|
|
|
|
size_t size = src_offsets.size();
|
|
|
|
|
ColumnString::Offset_t current_src_offset = 0;
|
|
|
|
|
ColumnArray::Offset_t current_dst_offset = 0;
|
|
|
|
|
ColumnString::Offset_t current_dst_strings_offset = 0;
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
Pos pos = reinterpret_cast<Pos>(&src_chars[current_src_offset]);
|
|
|
|
|
current_src_offset = src_offsets[i];
|
2012-09-23 05:28:59 +00:00
|
|
|
|
Pos end = reinterpret_cast<Pos>(&src_chars[current_src_offset]) - 1;
|
|
|
|
|
|
|
|
|
|
generator.set(pos, end);
|
2012-09-17 04:34:19 +00:00
|
|
|
|
|
|
|
|
|
size_t j = 0;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
while (generator.get(token_begin, token_end))
|
2012-09-17 04:34:19 +00:00
|
|
|
|
{
|
|
|
|
|
size_t token_size = token_end - token_begin;
|
|
|
|
|
|
|
|
|
|
res_strings_chars.resize(res_strings_chars.size() + token_size + 1);
|
|
|
|
|
memcpy(&res_strings_chars[current_dst_strings_offset], token_begin, token_size);
|
|
|
|
|
/// Нулевой байт после токена и так инициализирован std::vector-ом.
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = col_res;
|
|
|
|
|
}
|
|
|
|
|
else if (col_const_str)
|
|
|
|
|
{
|
|
|
|
|
String src = col_const_str->getData();
|
|
|
|
|
Array dst;
|
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
generator.set(src.data(), src.data() + src.size());
|
2012-09-17 04:34:19 +00:00
|
|
|
|
Pos token_begin = NULL;
|
|
|
|
|
Pos token_end = NULL;
|
|
|
|
|
|
2012-09-23 05:28:59 +00:00
|
|
|
|
while (generator.get(token_begin, token_end))
|
2012-09-17 04:34:19 +00:00
|
|
|
|
dst.push_back(String(token_begin, token_end - token_begin));
|
|
|
|
|
|
2013-03-27 10:07:23 +00:00
|
|
|
|
block.getByPosition(result).column = new ColumnConstArray(col_const_str->size(), dst, new DataTypeArray(new DataTypeString));
|
2012-09-17 04:34:19 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2012-09-23 05:28:59 +00:00
|
|
|
|
throw Exception("Illegal columns " + block.getByPosition(arguments.back()).column->getName()
|
|
|
|
|
+ ", " + block.getByPosition(arguments.back()).column->getName()
|
2012-09-17 04:34:19 +00:00
|
|
|
|
+ " of arguments of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-03-06 13:44:40 +00:00
|
|
|
|
typedef FunctionTokens<AlphaTokensImpl> FunctionAlphaTokens;
|
|
|
|
|
typedef FunctionTokens<SplitByCharImpl> FunctionSplitByChar;
|
2012-09-23 05:28:59 +00:00
|
|
|
|
typedef FunctionTokens<SplitByStringImpl> FunctionSplitByString;
|
|
|
|
|
|
2012-09-17 04:34:19 +00:00
|
|
|
|
}
|