Skip to content

Commit

Permalink
[fix](function) avoid calculating sqrt of negative in agg function CO…
Browse files Browse the repository at this point in the history
…RR (apache#39324)

Issue Number: close #xxx

before:
```sql
mysql [sqlfunctest]>SELECT CORR(data, 89.999999) FROM DOUBLEDATA_NOT_EMPTY_NULLABLE;
+---------------------------------------+
| corr(data, cast(89.999999 as DOUBLE)) |
+---------------------------------------+
|                                  NULL |
+---------------------------------------+
1 row in set (0.53 sec)
```
--- actually here's a silent nan by float exception. now sure in all
platform.

after:
```sql
mysql [sqlfunctest]>SELECT CORR(data, 89.999999) FROM DOUBLEDATA_NOT_EMPTY_NULLABLE;
+---------------------------------------+
| corr(data, cast(89.999999 as DOUBLE)) |
+---------------------------------------+
|                                     0 |
+---------------------------------------+
1 row in set (0.13 sec)
```
--- is stable
  • Loading branch information
zclllyybb committed Aug 23, 2024
1 parent 92b1941 commit 1df5a9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion be/src/vec/aggregate_functions/aggregate_function_corr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ struct CorrMoment {
}

T get() const {
if ((m0 * x2 - x1 * x1) * (m0 * y2 - y1 * y1) == 0) [[unlikely]] {
// avoid float error(silent nan) when x or y is constant
if (m0 * x2 <= x1 * x1 || m0 * y2 <= y1 * y1) [[unlikely]] {
return 0;
}
return (m0 * xy - x1 * y1) / sqrt((m0 * x2 - x1 * x1) * (m0 * y2 - y1 * y1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,22 @@
0.0
0.0
0.0

-- !sql_const1 --
0.0

-- !sql_const2 --
0.0

-- !sql_const3 --
0.0

-- !sql_const4 --
0.0

-- !sql_const5 --
0.0

-- !sql_const6 --
0.0

Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ suite("test_corr") {
qt_sql1 "select corr(non_nullable(x), non_nullable(y)) ans from test_corr group by id order by ans"
qt_sql2 "select corr(x, non_nullable(y)) ans from test_corr group by id order by ans"
qt_sql3 "select corr(non_nullable(x), y) ans from test_corr group by id order by ans"

qt_sql_const1 "select corr(x,1) from test_corr"
qt_sql_const2 "select corr(x,1e100) from test_corr"
qt_sql_const3 "select corr(x,1e-100) from test_corr"
qt_sql_const4 "select corr(1,y) from test_corr"
qt_sql_const5 "select corr(1e100,y) from test_corr"
qt_sql_const6 "select corr(1e-100,y) from test_corr"
}

0 comments on commit 1df5a9a

Please sign in to comment.