Skip to content

Commit

Permalink
Merge pull request ClickHouse#60150 from Algunenano/cosineDistance
Browse files Browse the repository at this point in the history
Fix cosineDistance crash with Nullable
  • Loading branch information
Algunenano authored Feb 20, 2024
2 parents e09113f + 8c984c5 commit f243e01
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/en/sql-reference/functions/distance-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ Result:

## cosineDistance

Calculates the cosine distance between two vectors (the values of the tuples are the coordinates). The less the returned value is, the more similar are the vectors.
Calculates the cosine distance between two vectors (the values of the tuples are the coordinates). The smaller the returned value is, the more similar are the vectors.

**Syntax**

Expand Down
12 changes: 6 additions & 6 deletions src/Functions/vectorFunctions.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <Columns/ColumnTuple.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeInterval.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeNothing.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/ITupleFunction.h>
Expand Down Expand Up @@ -1364,11 +1364,11 @@ class FunctionCosineDistance : public ITupleFunction

ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
if (getReturnTypeImpl(arguments)->isNullable())
{
return DataTypeNullable(std::make_shared<DataTypeNothing>())
.createColumnConstWithDefaultValue(input_rows_count);
}
/// TODO: cosineDistance does not support nullable arguments
/// https://github.com/ClickHouse/ClickHouse/pull/27933#issuecomment-916670286
auto return_type = getReturnTypeImpl(arguments);
if (return_type->isNullable())
return return_type->createColumnConstWithDefaultValue(input_rows_count);

FunctionDotProduct dot(context);
ColumnWithTypeAndName dot_result{dot.executeImpl(arguments, DataTypePtr(), input_rows_count),
Expand Down
11 changes: 11 additions & 0 deletions tests/queries/0_stateless/02994_cosineDistanceNullable.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
\N
3 changes: 3 additions & 0 deletions tests/queries/0_stateless/02994_cosineDistanceNullable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- https://github.com/ClickHouse/ClickHouse/issues/59596
SELECT cosineDistance((1, 1), (toNullable(0.5), 0.1));
SELECT cosineDistance((1, 1), (toNullable(0.5), 0.1)) from numbers(10);

0 comments on commit f243e01

Please sign in to comment.