Skip to content

Commit 4ca9592

Browse files
committed
switch float math udfs from std to libm
1 parent 70e7e62 commit 4ca9592

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

datafusion/functions/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ regex = { workspace = true, optional = true }
8686
sha2 = { version = "^0.10.1", optional = true }
8787
unicode-segmentation = { version = "^1.7.1", optional = true }
8888
uuid = { version = "1.7", features = ["v4"], optional = true }
89+
libm = "0.2.11"
8990

9091
[dev-dependencies]
9192
arrow = { workspace = true, features = ["test_utils"] }

datafusion/functions/src/macros.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ macro_rules! make_math_unary_udf {
157157
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
158158
};
159159

160+
#[allow(unused_imports)]
161+
use $crate::macros::StdFloat as _;
162+
160163
#[derive(Debug)]
161164
pub struct $UDF {
162165
signature: Signature,
@@ -218,12 +221,16 @@ macro_rules! make_math_unary_udf {
218221
DataType::Float64 => Arc::new(
219222
args[0]
220223
.as_primitive::<Float64Type>()
221-
.unary::<_, Float64Type>(|x: f64| f64::$UNARY_FUNC(x)),
224+
.unary::<_, Float64Type>(|x: f64| {
225+
libm::Libm::<f64>::$UNARY_FUNC(x)
226+
}),
222227
) as ArrayRef,
223228
DataType::Float32 => Arc::new(
224229
args[0]
225230
.as_primitive::<Float32Type>()
226-
.unary::<_, Float32Type>(|x: f32| f32::$UNARY_FUNC(x)),
231+
.unary::<_, Float32Type>(|x: f32| {
232+
libm::Libm::<f32>::$UNARY_FUNC(x)
233+
}),
227234
) as ArrayRef,
228235
other => {
229236
return exec_err!(
@@ -333,7 +340,7 @@ macro_rules! make_math_binary_udf {
333340
let result = arrow::compute::binary::<_, _, _, Float64Type>(
334341
y,
335342
x,
336-
|y, x| f64::$BINARY_FUNC(y, x),
343+
|y, x| libm::Libm::<f64>::$BINARY_FUNC(y, x),
337344
)?;
338345
Arc::new(result) as _
339346
}
@@ -343,7 +350,7 @@ macro_rules! make_math_binary_udf {
343350
let result = arrow::compute::binary::<_, _, _, Float32Type>(
344351
y,
345352
x,
346-
|y, x| f32::$BINARY_FUNC(y, x),
353+
|y, x| libm::Libm::<f32>::$BINARY_FUNC(y, x),
347354
)?;
348355
Arc::new(result) as _
349356
}
@@ -365,3 +372,30 @@ macro_rules! make_math_binary_udf {
365372
}
366373
};
367374
}
375+
376+
/// Adds methods that exist in std but are missing from libm,
377+
/// because they are not platform intrinsics but just conveniences.
378+
pub trait StdFloat<T> {
379+
fn to_radians(x: T) -> T;
380+
fn to_degrees(x: T) -> T;
381+
}
382+
383+
impl StdFloat<f64> for libm::Libm<f64> {
384+
fn to_radians(x: f64) -> f64 {
385+
x.to_radians()
386+
}
387+
388+
fn to_degrees(x: f64) -> f64 {
389+
x.to_degrees()
390+
}
391+
}
392+
393+
impl StdFloat<f32> for libm::Libm<f32> {
394+
fn to_radians(x: f32) -> f32 {
395+
x.to_radians()
396+
}
397+
398+
fn to_degrees(x: f32) -> f32 {
399+
x.to_degrees()
400+
}
401+
}

datafusion/functions/src/math/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ make_math_unary_udf!(
177177
LnFunc,
178178
LN,
179179
ln,
180-
ln,
180+
log,
181181
super::ln_order,
182182
super::bounds::unbounded_bounds,
183183
super::get_ln_doc

datafusion/sqllogictest/test_files/scalar.slt

+7-7
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ select abs(a), abs(b), abs(c) from signed_integers;
9393
query RRR rowsort
9494
select acos(0), acos(0.5), acos(1);
9595
----
96-
1.5707963267948966 1.0471975511965976 0
96+
1.5707963267948966 1.0471975511965979 0
9797

9898
# acos scalar nulls
9999
query R rowsort
@@ -140,7 +140,7 @@ NaN 9.90349 NaN
140140
query RRR rowsort
141141
select asin(0), asin(0.5), asin(1);
142142
----
143-
0 0.5235987755982988 1.5707963267948966
143+
0 0.5235987755982989 1.5707963267948966
144144

145145
# asin scalar nulls
146146
query R rowsort
@@ -362,7 +362,7 @@ select round(cos(a), 5), round(cos(b), 5), round(cos(c), 5) from signed_integers
362362
query RRR rowsort
363363
select cosh(1), cosh(2), cosh(3);
364364
----
365-
1.5430806348152437 3.7621956910836314 10.067661995777765
365+
1.543080634815244 3.7621956910836314 10.067661995777765
366366

367367
# cosh scalar nulls
368368
query R rowsort
@@ -385,7 +385,7 @@ select round(cosh(a), 5), round(cosh(b), 5), round(cosh(c), 5) from small_floats
385385
query RRR rowsort
386386
select exp(0), exp(1), exp(2);
387387
----
388-
1 2.718281828459045 7.38905609893065
388+
1 2.7182818284590455 7.38905609893065
389389

390390
# exp scalar nulls
391391
query R rowsort
@@ -454,7 +454,7 @@ select floor(a), floor(b), floor(c) from signed_integers;
454454
query RRR rowsort
455455
select ln(1), ln(exp(1)), ln(3);
456456
----
457-
0 1 1.0986122886681098
457+
0 1 1.0986122886681096
458458

459459
# ln scalar nulls
460460
query R rowsort
@@ -872,7 +872,7 @@ select round(sin(a), 5), round(sin(b), 5), round(sin(c), 5) from small_floats;
872872
query RRR rowsort
873873
select sinh(1), sinh(2), sinh(3);
874874
----
875-
1.1752011936438014 3.6268604078470186 10.017874927409903
875+
1.1752011936438014 3.626860407847019 10.017874927409903
876876

877877
# sinh scalar nulls
878878
query R rowsort
@@ -937,7 +937,7 @@ NaN
937937
query RRR rowsort
938938
select tan(0), tan(pi() / 6), tan(pi() / 4);
939939
----
940-
0 0.5773502691896256 0.9999999999999999
940+
0 0.5773502691896257 0.9999999999999999
941941

942942
# tan scalar nulls
943943
query R rowsort

0 commit comments

Comments
 (0)