From 3ee4e9eda821b192e7f56df7949e59f43b5faf4f Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 14 Jan 2021 18:29:42 +0300 Subject: [PATCH] Simplify tupleHammingDistance --- src/Functions/tupleHammingDistance.cpp | 35 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Functions/tupleHammingDistance.cpp b/src/Functions/tupleHammingDistance.cpp index 698b51d7cb4..6cb3883cd08 100644 --- a/src/Functions/tupleHammingDistance.cpp +++ b/src/Functions/tupleHammingDistance.cpp @@ -33,12 +33,29 @@ public: size_t getNumberOfArguments() const override { return 2; } + bool useDefaultImplementationForConstants() const override { return true; } + + Columns getTupleElements(const IColumn & column) const + { + if (const auto * const_column = typeid_cast(&column)) + return convertConstTupleToConstantElements(*const_column); + + if (const auto * column_tuple = typeid_cast(&column)) + { + Columns columns(column_tuple->tupleSize()); + for (size_t i = 0; i < columns.size(); ++i) + columns[i] = column_tuple->getColumnPtr(i); + return columns; + } + + throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument of function {} should be tuples, got {}", + getName(), column.getName()) + } + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override { const auto * left_tuple = checkAndGetDataType(arguments[0].type.get()); const auto * right_tuple = checkAndGetDataType(arguments[1].type.get()); - const auto * left_col = checkAndGetColumn(arguments[0].column.get()); - const auto * right_col = checkAndGetColumn(arguments[1].column.get()); if (!left_tuple) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument 0 of function {} should be tuples, got {}", @@ -48,8 +65,12 @@ public: throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Argument 1 of function {} should be tuples, got {}", getName(), arguments[1].type->getName()); - const auto & left_elements = left_tuple->getElements(); - const auto & right_elements = right_tuple->getElements(); + 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); if (left_elements.size() != right_elements.size()) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, @@ -95,10 +116,8 @@ public: { const auto * left_tuple = checkAndGetDataType(arguments[0].type.get()); const auto * right_tuple = checkAndGetDataType(arguments[1].type.get()); - const auto * left_col = checkAndGetColumn(arguments[0].column.get()); - const auto * right_col = checkAndGetColumn(arguments[1].column.get()); - const auto & left_elements = left_tuple->getElements(); - const auto & right_elements = right_tuple->getElements(); + auto left_elements = getTupleElements(*arguments[0].column); + auto right_elements = getTupleElements(*arguments[1].column); size_t tuple_size = left_elements.size(); if (tuple_size == 0)