ClickHouse/src/Functions/tupleHammingDistance.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
5.8 KiB
C++
Raw Normal View History

2019-11-06 10:35:23 +00:00
#include <Columns/ColumnTuple.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-09-09 12:41:22 +00:00
#include <Functions/ITupleFunction.h>
2019-11-06 10:35:23 +00:00
#include <Functions/castTypeToEither.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
2021-01-14 14:29:51 +00:00
/// tupleHammingDistance function: (Tuple(...), Tuple(...))-> N
/// Return the number of non-equal tuple elements
2021-09-09 12:41:22 +00:00
class FunctionTupleHammingDistance : public ITupleFunction
2019-11-06 10:35:23 +00:00
{
public:
static constexpr auto name = "tupleHammingDistance";
2021-01-14 14:29:51 +00:00
2021-09-09 12:41:22 +00:00
explicit FunctionTupleHammingDistance(ContextPtr context_) : ITupleFunction(context_) {}
2021-08-25 14:02:04 +00:00
static FunctionPtr create(ContextPtr context_) { return std::make_shared<FunctionTupleHammingDistance>(context_); }
2019-11-06 10:35:23 +00:00
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 2; }
2021-01-14 15:29:42 +00:00
bool useDefaultImplementationForConstants() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
2021-01-14 14:29:51 +00:00
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
2019-11-06 10:35:23 +00:00
{
2021-01-14 14:29:51 +00:00
const auto * left_tuple = checkAndGetDataType<DataTypeTuple>(arguments[0].type.get());
const auto * right_tuple = checkAndGetDataType<DataTypeTuple>(arguments[1].type.get());
if (!left_tuple)
2021-09-09 12:41:22 +00:00
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument 0 of function {} should be tuple, got {}",
2021-01-14 14:29:51 +00:00
getName(), arguments[0].type->getName());
if (!right_tuple)
2021-09-09 12:41:22 +00:00
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument 1 of function {} should be tuple, got {}",
2021-01-14 14:29:51 +00:00
getName(), arguments[1].type->getName());
2021-01-14 15:34:44 +00:00
const auto & left_types = left_tuple->getElements();
const auto & right_types = right_tuple->getElements();
2021-01-14 15:29:42 +00:00
Columns left_elements;
Columns right_elements;
if (arguments[0].column)
left_elements = getTupleElements(*arguments[0].column);
if (arguments[1].column)
right_elements = getTupleElements(*arguments[1].column);
2021-01-14 14:29:51 +00:00
2021-01-14 16:02:17 +00:00
if (left_types.size() != right_types.size())
2021-01-14 14:29:51 +00:00
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Expected tuples of the same size as arguments of function {}. Got {} and {}",
getName(), arguments[0].type->getName(), arguments[1].type->getName());
2021-01-14 16:02:17 +00:00
size_t tuple_size = left_types.size();
2021-01-14 14:29:51 +00:00
if (tuple_size == 0)
return std::make_shared<DataTypeUInt8>();
2021-01-14 16:02:17 +00:00
auto compare = FunctionFactory::instance().get("notEquals", context);
2021-01-14 14:29:51 +00:00
auto plus = FunctionFactory::instance().get("plus", context);
DataTypePtr res_type;
2021-01-14 14:29:51 +00:00
for (size_t i = 0; i < tuple_size; ++i)
{
try
{
2021-01-14 15:34:44 +00:00
ColumnWithTypeAndName left{left_elements.empty() ? nullptr : left_elements[i], left_types[i], {}};
ColumnWithTypeAndName right{right_elements.empty() ? nullptr : right_elements[i], right_types[i], {}};
2021-01-14 14:29:51 +00:00
auto elem_compare = compare->build(ColumnsWithTypeAndName{left, right});
if (i == 0)
{
res_type = elem_compare->getResultType();
continue;
}
ColumnWithTypeAndName left_type{res_type, {}};
ColumnWithTypeAndName right_type{elem_compare->getResultType(), {}};
auto plus_elem = plus->build({left_type, right_type});
res_type = plus_elem->getResultType();
2021-01-14 14:29:51 +00:00
}
2023-06-25 00:37:10 +00:00
catch (Exception & e)
2021-01-14 14:29:51 +00:00
{
e.addMessage("While executing function {} for tuple element {}", getName(), i);
throw;
}
}
return res_type;
2019-11-06 10:35:23 +00:00
}
2020-12-17 19:14:01 +00:00
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
2019-11-06 10:35:23 +00:00
{
2021-01-14 14:29:51 +00:00
const auto * left_tuple = checkAndGetDataType<DataTypeTuple>(arguments[0].type.get());
const auto * right_tuple = checkAndGetDataType<DataTypeTuple>(arguments[1].type.get());
2021-01-14 15:34:44 +00:00
const auto & left_types = left_tuple->getElements();
const auto & right_types = right_tuple->getElements();
2021-01-14 15:29:42 +00:00
auto left_elements = getTupleElements(*arguments[0].column);
auto right_elements = getTupleElements(*arguments[1].column);
2021-01-14 14:29:51 +00:00
size_t tuple_size = left_elements.size();
if (tuple_size == 0)
return DataTypeUInt8().createColumnConstWithDefaultValue(input_rows_count);
2021-01-14 16:02:17 +00:00
auto compare = FunctionFactory::instance().get("notEquals", context);
2021-01-14 14:29:51 +00:00
auto plus = FunctionFactory::instance().get("plus", context);
ColumnWithTypeAndName res;
2021-01-14 14:29:51 +00:00
for (size_t i = 0; i < tuple_size; ++i)
2020-08-16 07:42:35 +00:00
{
2021-01-14 15:34:44 +00:00
ColumnWithTypeAndName left{left_elements[i], left_types[i], {}};
ColumnWithTypeAndName right{right_elements[i], right_types[i], {}};
2021-01-14 14:29:51 +00:00
auto elem_compare = compare->build(ColumnsWithTypeAndName{left, right});
ColumnWithTypeAndName column;
column.type = elem_compare->getResultType();
column.column = elem_compare->execute({left, right}, column.type, input_rows_count);
if (i == 0)
{
res = std::move(column);
}
else
{
auto plus_elem = plus->build({res, column});
auto res_type = plus_elem->getResultType();
res.column = plus_elem->execute({res, column}, res_type, input_rows_count);
res.type = res_type;
}
2021-01-14 14:29:51 +00:00
}
2019-11-06 10:35:23 +00:00
2021-01-14 14:29:51 +00:00
return res.column;
2019-11-06 10:35:23 +00:00
}
};
REGISTER_FUNCTION(TupleHammingDistance)
2019-11-06 10:35:23 +00:00
{
factory.registerFunction<FunctionTupleHammingDistance>();
}
}