2019-02-21 05:08:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2019-02-21 05:08:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/** Calculate similarity metrics:
|
|
|
|
*
|
2019-03-29 01:02:05 +00:00
|
|
|
* ngramDistance(haystack, needle) - calculate n-gram distance between haystack and needle.
|
2019-02-22 03:02:10 +00:00
|
|
|
* Returns float number from 0 to 1 - the closer to zero, the more strings are similar to each other.
|
2019-03-05 22:42:28 +00:00
|
|
|
* Also support CaseInsensitive and UTF8 formats.
|
2019-03-29 01:02:05 +00:00
|
|
|
* ngramDistanceCaseInsensitive(haystack, needle)
|
|
|
|
* ngramDistanceUTF8(haystack, needle)
|
|
|
|
* ngramDistanceCaseInsensitiveUTF8(haystack, needle)
|
2019-02-21 05:08:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
2019-02-22 03:02:10 +00:00
|
|
|
extern const int TOO_LARGE_STRING_SIZE;
|
2019-02-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 17:43:52 +00:00
|
|
|
template <typename T>
|
|
|
|
concept has_max_string_size = requires { T::max_string_size; };
|
|
|
|
|
2019-02-21 05:08:37 +00:00
|
|
|
template <typename Impl, typename Name>
|
|
|
|
class FunctionsStringSimilarity : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionsStringSimilarity>(); }
|
2019-02-21 05:08:37 +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
|
|
|
|
2019-02-21 05:08:37 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
|
|
|
|
arguments[0]->getName(), getName());
|
2019-02-21 05:08:37 +00:00
|
|
|
|
|
|
|
if (!isString(arguments[1]))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
|
|
|
|
arguments[1]->getName(), getName());
|
2019-02-21 05:08:37 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeNumber<typename Impl::ResultType>>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2019-02-21 05:08:37 +00:00
|
|
|
{
|
|
|
|
using ResultType = typename Impl::ResultType;
|
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
const ColumnPtr & column_haystack = arguments[0].column;
|
|
|
|
const ColumnPtr & column_needle = arguments[1].column;
|
2019-02-21 05:08:37 +00:00
|
|
|
|
|
|
|
const ColumnConst * col_haystack_const = typeid_cast<const ColumnConst *>(&*column_haystack);
|
|
|
|
const ColumnConst * col_needle_const = typeid_cast<const ColumnConst *>(&*column_needle);
|
|
|
|
|
2019-05-05 20:48:46 +00:00
|
|
|
if (col_haystack_const && col_needle_const)
|
2019-02-21 05:08:37 +00:00
|
|
|
{
|
|
|
|
ResultType res{};
|
2019-02-22 03:02:10 +00:00
|
|
|
const String & needle = col_needle_const->getValue<String>();
|
2023-09-22 17:43:52 +00:00
|
|
|
if constexpr (has_max_string_size<Impl>)
|
2019-02-22 03:02:10 +00:00
|
|
|
{
|
2023-09-22 17:43:52 +00:00
|
|
|
if (needle.size() > Impl::max_string_size)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::TOO_LARGE_STRING_SIZE,
|
|
|
|
"String size of needle is too big for function {}. "
|
|
|
|
"Should be at most {}",
|
|
|
|
getName(),
|
|
|
|
Impl::max_string_size);
|
|
|
|
}
|
2019-02-22 03:02:10 +00:00
|
|
|
}
|
2020-03-23 02:12:31 +00:00
|
|
|
Impl::constantConstant(col_haystack_const->getValue<String>(), needle, res);
|
2020-10-18 19:00:13 +00:00
|
|
|
return result_type->createColumnConst(col_haystack_const->size(), toField(res));
|
2019-02-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto col_res = ColumnVector<ResultType>::create();
|
|
|
|
|
|
|
|
typename ColumnVector<ResultType>::Container & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(column_haystack->size());
|
|
|
|
|
|
|
|
const ColumnString * col_haystack_vector = checkAndGetColumn<ColumnString>(&*column_haystack);
|
2019-05-05 20:48:46 +00:00
|
|
|
const ColumnString * col_needle_vector = checkAndGetColumn<ColumnString>(&*column_needle);
|
2019-02-21 05:08:37 +00:00
|
|
|
|
2019-05-05 20:48:46 +00:00
|
|
|
if (col_haystack_vector && col_needle_const)
|
2019-02-22 03:02:10 +00:00
|
|
|
{
|
|
|
|
const String & needle = col_needle_const->getValue<String>();
|
2023-09-22 17:43:52 +00:00
|
|
|
if constexpr (has_max_string_size<Impl>)
|
2019-02-22 03:02:10 +00:00
|
|
|
{
|
2023-09-22 17:43:52 +00:00
|
|
|
if (needle.size() > Impl::max_string_size)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::TOO_LARGE_STRING_SIZE,
|
|
|
|
"String size of needle is too big for function {}. "
|
|
|
|
"Should be at most {}",
|
|
|
|
getName(),
|
|
|
|
Impl::max_string_size);
|
|
|
|
}
|
2019-02-22 03:02:10 +00:00
|
|
|
}
|
2020-03-23 02:12:31 +00:00
|
|
|
Impl::vectorConstant(col_haystack_vector->getChars(), col_haystack_vector->getOffsets(), needle, vec_res);
|
2019-02-22 03:02:10 +00:00
|
|
|
}
|
2019-05-05 20:48:46 +00:00
|
|
|
else if (col_haystack_vector && col_needle_vector)
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
Impl::vectorVector(
|
2019-05-05 20:48:46 +00:00
|
|
|
col_haystack_vector->getChars(),
|
|
|
|
col_haystack_vector->getOffsets(),
|
|
|
|
col_needle_vector->getChars(),
|
|
|
|
col_needle_vector->getOffsets(),
|
|
|
|
vec_res);
|
|
|
|
}
|
|
|
|
else if (col_haystack_const && col_needle_vector)
|
|
|
|
{
|
2019-05-25 18:47:26 +00:00
|
|
|
const String & haystack = col_haystack_const->getValue<String>();
|
2023-09-22 17:43:52 +00:00
|
|
|
if constexpr (has_max_string_size<Impl>)
|
2019-05-05 20:48:46 +00:00
|
|
|
{
|
2023-09-22 17:43:52 +00:00
|
|
|
if (haystack.size() > Impl::max_string_size)
|
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::TOO_LARGE_STRING_SIZE,
|
|
|
|
"String size of haystack is too big for function {}. "
|
|
|
|
"Should be at most {}",
|
|
|
|
getName(),
|
|
|
|
Impl::max_string_size);
|
|
|
|
}
|
2019-05-05 20:48:46 +00:00
|
|
|
}
|
2020-03-23 02:12:31 +00:00
|
|
|
Impl::constantVector(haystack, col_needle_vector->getChars(), col_needle_vector->getOffsets(), vec_res);
|
2019-05-05 20:48:46 +00:00
|
|
|
}
|
2019-02-21 05:08:37 +00:00
|
|
|
else
|
2019-02-22 03:02:10 +00:00
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal columns {} and {} of arguments of function {}",
|
|
|
|
arguments[0].column->getName(), arguments[1].column->getName(), getName());
|
2019-02-22 03:02:10 +00:00
|
|
|
}
|
2019-02-21 05:08:37 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return col_res;
|
2019-02-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|