2017-04-21 17:47:27 +00:00
|
|
|
#pragma once
|
2011-10-16 07:11:36 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionsArithmetic.h>
|
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2016-10-24 13:47:15 +00:00
|
|
|
|
|
|
|
|
2011-10-16 07:11:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2017-03-12 11:09:25 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** Search and replace functions in strings:
|
2011-10-16 07:11:36 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* position(haystack, needle) - the normal search for a substring in a string, returns the position (in bytes) of the found substring starting with 1, or 0 if no substring is found.
|
|
|
|
* positionUTF8(haystack, needle) - the same, but the position is calculated at code points, provided that the string is encoded in UTF-8.
|
2016-01-27 03:11:28 +00:00
|
|
|
* positionCaseInsensitive(haystack, needle)
|
|
|
|
* positionCaseInsensitiveUTF8(haystack, needle)
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* like(haystack, pattern) - search by the regular expression LIKE; Returns 0 or 1. Case-insensitive, but only for Latin.
|
2011-10-17 08:28:39 +00:00
|
|
|
* notLike(haystack, pattern)
|
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* match(haystack, pattern) - search by regular expression re2; Returns 0 or 1.
|
2012-07-21 03:45:48 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Applies regexp re2 and pulls:
|
|
|
|
* - the first subpattern, if the regexp has a subpattern;
|
|
|
|
* - the zero subpattern (the match part, otherwise);
|
|
|
|
* - if not match - an empty string.
|
2013-03-18 10:49:31 +00:00
|
|
|
* extract(haystack, pattern)
|
2011-10-17 08:28:39 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* replaceOne(haystack, pattern, replacement) - replacing the pattern with the specified rules, only the first occurrence.
|
|
|
|
* replaceAll(haystack, pattern, replacement) - replacing the pattern with the specified rules, all occurrences.
|
2011-10-16 07:11:36 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* replaceRegexpOne(haystack, pattern, replacement) - replaces the pattern with the specified regexp, only the first occurrence.
|
|
|
|
* replaceRegexpAll(haystack, pattern, replacement) - replaces the pattern with the specified type, all occurrences.
|
2014-01-27 16:01:53 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Warning! At this point, the arguments needle, pattern, n, replacement must be constants.
|
2011-10-16 07:11:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-01-27 13:49:06 +00:00
|
|
|
template <typename Impl, typename Name>
|
2017-03-10 17:52:36 +00:00
|
|
|
class FunctionsStringSearch : public IFunction
|
2014-01-27 13:49:06 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionsStringSearch>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[0]->isString())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[1]->isString())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeNumber<typename Impl::ResultType>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
{
|
|
|
|
using ResultType = typename Impl::ResultType;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column_haystack = block.getByPosition(arguments[0]).column;
|
|
|
|
const ColumnPtr & column_needle = block.getByPosition(arguments[1]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnConst * col_haystack_const = typeid_cast<const ColumnConst *>(&*column_haystack);
|
|
|
|
const ColumnConst * col_needle_const = typeid_cast<const ColumnConst *>(&*column_needle);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_haystack_const && col_needle_const)
|
|
|
|
{
|
|
|
|
ResultType res{};
|
2017-07-21 06:35:58 +00:00
|
|
|
Impl::constant_constant(col_haystack_const->getValue<String>(), col_needle_const->getValue<String>(), res);
|
2017-12-10 22:44:04 +00:00
|
|
|
block.getByPosition(result).column = block.getByPosition(result).type->createColumnConst(col_haystack_const->size(), toField(res));
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<ResultType>::Container & vec_res = col_res->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
vec_res.resize(column_haystack->size());
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnString * col_haystack_vector = checkAndGetColumn<ColumnString>(&*column_haystack);
|
|
|
|
const ColumnString * col_needle_vector = checkAndGetColumn<ColumnString>(&*column_needle);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_haystack_vector && col_needle_vector)
|
|
|
|
Impl::vector_vector(col_haystack_vector->getChars(),
|
|
|
|
col_haystack_vector->getOffsets(),
|
|
|
|
col_needle_vector->getChars(),
|
|
|
|
col_needle_vector->getOffsets(),
|
|
|
|
vec_res);
|
|
|
|
else if (col_haystack_vector && col_needle_const)
|
2017-07-21 06:35:58 +00:00
|
|
|
Impl::vector_constant(col_haystack_vector->getChars(), col_haystack_vector->getOffsets(), col_needle_const->getValue<String>(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
else if (col_haystack_const && col_needle_vector)
|
2017-07-21 06:35:58 +00:00
|
|
|
Impl::constant_vector(col_haystack_const->getValue<String>(), col_needle_vector->getChars(), col_needle_vector->getOffsets(), vec_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal columns " + block.getByPosition(arguments[0]).column->getName() + " and "
|
|
|
|
+ block.getByPosition(arguments[1]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of arguments of function "
|
|
|
|
+ getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2011-10-16 07:11:36 +00:00
|
|
|
};
|
|
|
|
|
2011-10-17 08:28:39 +00:00
|
|
|
|
2013-02-28 13:01:07 +00:00
|
|
|
template <typename Impl, typename Name>
|
|
|
|
class FunctionsStringSearchToString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionsStringSearchToString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[0]->isString())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[1]->isString())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr column = block.getByPosition(arguments[0]).column;
|
|
|
|
const ColumnPtr column_needle = block.getByPosition(arguments[1]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnConst * col_needle = typeid_cast<const ColumnConst *>(&*column_needle);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!col_needle)
|
|
|
|
throw Exception("Second argument of function " + getName() + " must be constant string.", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
ColumnString::Chars_t & vec_res = col_res->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_res = col_res->getOffsets();
|
2017-07-21 06:35:58 +00:00
|
|
|
Impl::vector(col->getChars(), col->getOffsets(), col_needle->getValue<String>(), vec_res, offsets_res);
|
2017-12-14 03:56:56 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception(
|
2017-07-21 06:35:58 +00:00
|
|
|
"Illegal column " + block.getByPosition(arguments[0]).column->getName() + " of argument of function " + getName(),
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2013-02-28 13:01:07 +00:00
|
|
|
};
|
2017-03-12 11:09:25 +00:00
|
|
|
|
2011-10-16 07:11:36 +00:00
|
|
|
}
|