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

Simplify math expression code (use unary kernel) #309

Merged
merged 1 commit into from
May 10, 2021
Merged
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
32 changes: 8 additions & 24 deletions datafusion/src/physical_plan/math_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,21 @@

//! Math expressions

use arrow::array::{make_array, Array, ArrayData, Float32Array, Float64Array};
use arrow::buffer::Buffer;
use arrow::datatypes::{DataType, ToByteSlice};

use super::{ColumnarValue, ScalarValue};
use crate::error::{DataFusionError, Result};

macro_rules! compute_op {
($ARRAY:expr, $FUNC:ident, $TYPE:ident) => {{
let len = $ARRAY.len();
let result = (0..len)
.map(|i| $ARRAY.value(i).$FUNC() as f64)
.collect::<Vec<f64>>();
let data = ArrayData::new(
DataType::Float64,
len,
Some($ARRAY.null_count()),
$ARRAY.data().null_buffer().cloned(),
0,
vec![Buffer::from(result.to_byte_slice())],
vec![],
);
Ok(make_array(data))
}};
}
use arrow::array::{Float32Array, Float64Array};
use arrow::datatypes::DataType;
use std::sync::Arc;

macro_rules! downcast_compute_op {
($ARRAY:expr, $NAME:expr, $FUNC:ident, $TYPE:ident) => {{
let n = $ARRAY.as_any().downcast_ref::<$TYPE>();
match n {
Some(array) => compute_op!(array, $FUNC, $TYPE),
Some(array) => {
let res: $TYPE =
arrow::compute::kernels::arity::unary(array, |x| x.$FUNC());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids creating intermediate Vec and uses the (efficient) unary kernel

Ok(Arc::new(res))
}
_ => Err(DataFusionError::Internal(format!(
"Invalid data type for {}",
$NAME
Expand Down
2 changes: 1 addition & 1 deletion datafusion/tests/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ async fn sqrt_f32_vs_f64() -> Result<()> {
// sqrt(f32)'s plan passes
let sql = "SELECT avg(sqrt(c11)) FROM aggregate_test_100";
let actual = execute(&mut ctx, sql).await;
let expected = vec![vec!["0.6584408485889435"]];
let expected = vec![vec!["0.6584407806396484"]];
Copy link
Contributor Author

@Dandandan Dandandan May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small diff, since we avoid as f64 now.


assert_eq!(actual, expected);
let sql = "SELECT avg(sqrt(CAST(c11 AS double))) FROM aggregate_test_100";
Expand Down