-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[improve](function)Refactor distance function return types to float #55184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
uchenily
wants to merge
4
commits into
apache:master
Choose a base branch
from
uchenily:improve-update-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−131
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
|
||
#pragma once | ||
|
||
#include <gen_cpp/Types_types.h> | ||
|
||
#include "vec/columns/column.h" | ||
#include "vec/columns/column_array.h" | ||
#include "vec/common/assert_cast.h" | ||
|
@@ -35,73 +37,66 @@ class L1Distance { | |
public: | ||
static constexpr auto name = "l1_distance"; | ||
struct State { | ||
double sum = 0; | ||
float sum = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { state.sum += fabs(x - y); } | ||
static double finalize(const State& state) { return state.sum; } | ||
static void accumulate(State& state, float x, float y) { state.sum += fabs(x - y); } | ||
static float finalize(const State& state) { return state.sum; } | ||
}; | ||
|
||
class L2Distance { | ||
public: | ||
static constexpr auto name = "l2_distance"; | ||
struct State { | ||
double sum = 0; | ||
float sum = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { state.sum += (x - y) * (x - y); } | ||
static double finalize(const State& state) { return sqrt(state.sum); } | ||
static void accumulate(State& state, float x, float y) { state.sum += (x - y) * (x - y); } | ||
static float finalize(const State& state) { return sqrt(state.sum); } | ||
}; | ||
|
||
class InnerProduct { | ||
public: | ||
static constexpr auto name = "inner_product"; | ||
struct State { | ||
double sum = 0; | ||
float sum = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { state.sum += x * y; } | ||
static double finalize(const State& state) { return state.sum; } | ||
static void accumulate(State& state, float x, float y) { state.sum += x * y; } | ||
static float finalize(const State& state) { return state.sum; } | ||
}; | ||
|
||
class CosineDistance { | ||
public: | ||
static constexpr auto name = "cosine_distance"; | ||
struct State { | ||
double dot_prod = 0; | ||
double squared_x = 0; | ||
double squared_y = 0; | ||
float dot_prod = 0; | ||
float squared_x = 0; | ||
float squared_y = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { | ||
static void accumulate(State& state, float x, float y) { | ||
state.dot_prod += x * y; | ||
state.squared_x += x * x; | ||
state.squared_y += y * y; | ||
} | ||
static double finalize(const State& state) { | ||
static float finalize(const State& state) { | ||
return 1 - state.dot_prod / sqrt(state.squared_x * state.squared_y); | ||
} | ||
}; | ||
|
||
class L2DistanceApproximate { | ||
class L2DistanceApproximate : public L2Distance { | ||
public: | ||
static constexpr auto name = "l2_distance_approximate"; | ||
struct State { | ||
double sum = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { state.sum += (x - y) * (x - y); } | ||
static double finalize(const State& state) { return sqrt(state.sum); } | ||
}; | ||
|
||
class InnerProductApproximate { | ||
class InnerProductApproximate : public InnerProduct { | ||
public: | ||
static constexpr auto name = "inner_product_approximate"; | ||
struct State { | ||
double sum = 0; | ||
}; | ||
static void accumulate(State& state, double x, double y) { state.sum += x * y; } | ||
static double finalize(const State& state) { return state.sum; } | ||
}; | ||
|
||
template <typename DistanceImpl> | ||
class FunctionArrayDistance : public IFunction { | ||
public: | ||
using DataType = PrimitiveTypeTraits<TYPE_FLOAT>::DataType; | ||
using ColumnType = PrimitiveTypeTraits<TYPE_FLOAT>::ColumnType; | ||
|
||
static constexpr auto name = DistanceImpl::name; | ||
String get_name() const override { return name; } | ||
static FunctionPtr create() { return std::make_shared<FunctionArrayDistance<DistanceImpl>>(); } | ||
|
@@ -110,7 +105,7 @@ class FunctionArrayDistance : public IFunction { | |
bool use_default_implementation_for_nulls() const override { return false; } | ||
|
||
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { | ||
return make_nullable(std::make_shared<DataTypeFloat64>()); | ||
return std::make_shared<DataType>(); | ||
} | ||
|
||
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, | ||
|
@@ -140,27 +135,14 @@ class FunctionArrayDistance : public IFunction { | |
} | ||
|
||
// prepare return data | ||
auto dst = ColumnFloat64::create(input_rows_count); | ||
auto dst = ColumnType::create(input_rows_count); | ||
auto& dst_data = dst->get_data(); | ||
auto dst_null_column = ColumnUInt8::create(input_rows_count, 0); | ||
auto& dst_null_data = dst_null_column->get_data(); | ||
|
||
const auto& offsets1 = *arr1.offsets_ptr; | ||
const auto& offsets2 = *arr2.offsets_ptr; | ||
const auto& nested_col1 = assert_cast<const ColumnFloat64*>(arr1.nested_col.get()); | ||
const auto& nested_col2 = assert_cast<const ColumnFloat64*>(arr2.nested_col.get()); | ||
const auto& nested_col1 = assert_cast<const ColumnType*>(arr1.nested_col.get()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if input column is nullable |
||
const auto& nested_col2 = assert_cast<const ColumnType*>(arr2.nested_col.get()); | ||
for (ssize_t row = 0; row < offsets1.size(); ++row) { | ||
if (arr1.array_nullmap_data && arr1.array_nullmap_data[row]) { | ||
dst_null_data[row] = true; | ||
continue; | ||
} | ||
if (arr2.array_nullmap_data && arr2.array_nullmap_data[row]) { | ||
dst_null_data[row] = true; | ||
continue; | ||
} | ||
|
||
dst_null_data[row] = false; | ||
|
||
// Calculate actual array sizes for current row. | ||
// For nullable arrays, we cannot compare absolute offset values directly because: | ||
// 1. When a row is null, its offset might equal the previous offset (no elements added) | ||
|
@@ -179,25 +161,14 @@ class FunctionArrayDistance : public IFunction { | |
for (ssize_t pos = offsets1[row - 1]; pos < offsets1[row]; ++pos) { | ||
// Calculate corresponding position in the second array | ||
ssize_t pos2 = offsets2[row - 1] + (pos - offsets1[row - 1]); | ||
if (arr1.nested_nullmap_data && arr1.nested_nullmap_data[pos]) { | ||
dst_null_data[row] = true; | ||
break; | ||
} | ||
if (arr2.nested_nullmap_data && arr2.nested_nullmap_data[pos2]) { | ||
dst_null_data[row] = true; | ||
break; | ||
} | ||
DistanceImpl::accumulate(st, nested_col1->get_element(pos), | ||
nested_col2->get_element(pos2)); | ||
} | ||
if (!dst_null_data[row]) { | ||
dst_data[row] = DistanceImpl::finalize(st); | ||
dst_null_data[row] = std::isnan(dst_data[row]); | ||
} | ||
|
||
dst_data[row] = DistanceImpl::finalize(st); | ||
} | ||
|
||
block.replace_by_position( | ||
result, ColumnNullable::create(std::move(dst), std::move(dst_null_column))); | ||
block.replace_by_position(result, std::move(dst)); | ||
return Status::OK(); | ||
} | ||
|
||
|
@@ -209,7 +180,7 @@ class FunctionArrayDistance : public IFunction { | |
} | ||
auto nested_type = | ||
remove_nullable(assert_cast<const DataTypeArray&>(*array_type).get_nested_type()); | ||
return nested_type->get_primitive_type() == TYPE_DOUBLE; | ||
return nested_type->get_primitive_type() == TYPE_FLOAT; | ||
} | ||
}; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why cast_to_array is removed?