Skip to content
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

[Enhancement](agg-func) Make covar functions get greater precisions #43786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions be/src/vec/aggregate_functions/aggregate_function_covar.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,26 @@ struct BaseData {
count = 0;
}

static double inf_to_nan(double val) {
// This function performs squaring operations, and due to differences in computation order,
// it might produce different values such as inf and nan.
// For performance reasons, we are uniformly changing it to nan
if (std::isinf(val)) [[unlikely]] {
return std::nan("");
}
return val;
};

// Cov(X, Y) = E(XY) - E(X)E(Y)
double get_pop_result() const {
if (count == 1) {
return 0.0;
}
return sum_xy / count - sum_x * sum_y / (count * count);
return inf_to_nan((double)(sum_xy / count - sum_x / count * sum_y / count));
}

double get_samp_result() const {
return sum_xy / (count - 1) - sum_x * sum_y / (count * (count - 1));
return inf_to_nan((double)(sum_xy / (count - 1) - sum_x / count * sum_y / (count - 1)));
}

void merge(const BaseData& rhs) {
Expand All @@ -122,10 +132,10 @@ struct BaseData {
count += 1;
}

double sum_x;
double sum_y;
double sum_xy;
int64_t count;
long double sum_x {};
long double sum_y {};
long double sum_xy {};
int64_t count {};
};

template <typename T, typename Data>
Expand Down
16 changes: 8 additions & 8 deletions be/src/vec/aggregate_functions/aggregate_function_stddev.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct BaseData {
// it might produce different values such as inf and nan.
// In MySQL, this will directly result in an error due to exceeding the double range.
// For performance reasons, we are uniformly changing it to nan
if (std::isinf(val)) {
if (std::isinf(val)) [[unlikely]] {
return std::nan("");
}
return val;
Expand Down Expand Up @@ -107,8 +107,8 @@ struct BaseData {
if (rhs.count == 0) {
return;
}
double delta = mean - rhs.mean;
double sum_count = count + rhs.count;
long double delta = mean - rhs.mean;
long double sum_count = count + rhs.count;
mean = rhs.mean + delta * count / sum_count;
m2 = rhs.m2 + m2 + (delta * delta) * rhs.count * count / sum_count;
count = int64_t(sum_count);
Expand All @@ -119,16 +119,16 @@ struct BaseData {
assert_cast<const ColumnVector<T>&, TypeCheckOnRelease::DISABLE>(*column);
double source_data = sources.get_data()[row_num];

double delta = source_data - mean;
double r = delta / (1 + count);
long double delta = source_data - mean;
long double r = delta / (1 + count);
mean += r;
m2 += count * delta * r;
count += 1;
}

double mean;
double m2;
int64_t count;
long double mean {};
long double m2 {};
int64_t count {};
};

template <typename T, typename Data>
Expand Down
Loading