-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Minor: Cleanup BuiltinScalarFunction's phys-expr creation #8114
Merged
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
|
@@ -34,14 +34,12 @@ use crate::execution_props::ExecutionProps; | |
use crate::sort_properties::SortProperties; | ||
use crate::{ | ||
array_expressions, conditional_expressions, datetime_expressions, | ||
expressions::{cast_column, nullif_func}, | ||
math_expressions, string_expressions, struct_expressions, PhysicalExpr, | ||
ScalarFunctionExpr, | ||
expressions::nullif_func, math_expressions, string_expressions, struct_expressions, | ||
PhysicalExpr, ScalarFunctionExpr, | ||
}; | ||
use arrow::{ | ||
array::ArrayRef, | ||
compute::kernels::length::{bit_length, length}, | ||
datatypes::TimeUnit, | ||
datatypes::{DataType, Int32Type, Int64Type, Schema}, | ||
}; | ||
use datafusion_common::{internal_err, DataFusionError, Result, ScalarValue}; | ||
|
@@ -71,143 +69,8 @@ pub fn create_physical_expr( | |
|
||
let data_type = fun.return_type(&input_expr_types)?; | ||
|
||
let fun_expr: ScalarFunctionImplementation = match fun { | ||
// These functions need args and input schema to pick an implementation | ||
// Unlike the string functions, which actually figure out the function to use with each array, | ||
// here we return either a cast fn or string timestamp translation based on the expression data type | ||
// so we don't have to pay a per-array/batch cost. | ||
BuiltinScalarFunction::ToTimestamp => { | ||
Arc::new(match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) => |col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Second, None), | ||
None, | ||
) | ||
}, | ||
Ok(DataType::Timestamp(_, None)) => |col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Nanosecond, None), | ||
None, | ||
) | ||
}, | ||
Ok(DataType::Utf8) => datetime_expressions::to_timestamp, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function to_timestamp" | ||
); | ||
} | ||
}) | ||
} | ||
BuiltinScalarFunction::ToTimestampMillis => { | ||
Arc::new(match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) | Ok(DataType::Timestamp(_, None)) => { | ||
|col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Millisecond, None), | ||
None, | ||
) | ||
} | ||
} | ||
Ok(DataType::Utf8) => datetime_expressions::to_timestamp_millis, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function to_timestamp_millis" | ||
); | ||
} | ||
}) | ||
} | ||
BuiltinScalarFunction::ToTimestampMicros => { | ||
Arc::new(match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) | Ok(DataType::Timestamp(_, None)) => { | ||
|col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Microsecond, None), | ||
None, | ||
) | ||
} | ||
} | ||
Ok(DataType::Utf8) => datetime_expressions::to_timestamp_micros, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function to_timestamp_micros" | ||
); | ||
} | ||
}) | ||
} | ||
BuiltinScalarFunction::ToTimestampNanos => { | ||
Arc::new(match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) | Ok(DataType::Timestamp(_, None)) => { | ||
|col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Nanosecond, None), | ||
None, | ||
) | ||
} | ||
} | ||
Ok(DataType::Utf8) => datetime_expressions::to_timestamp_nanos, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function to_timestamp_nanos" | ||
); | ||
} | ||
}) | ||
} | ||
BuiltinScalarFunction::ToTimestampSeconds => Arc::new({ | ||
match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) | Ok(DataType::Timestamp(_, None)) => { | ||
|col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Second, None), | ||
None, | ||
) | ||
} | ||
} | ||
Ok(DataType::Utf8) => datetime_expressions::to_timestamp_seconds, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function to_timestamp_seconds" | ||
); | ||
} | ||
} | ||
}), | ||
BuiltinScalarFunction::FromUnixtime => Arc::new({ | ||
match input_phy_exprs[0].data_type(input_schema) { | ||
Ok(DataType::Int64) => |col_values: &[ColumnarValue]| { | ||
cast_column( | ||
&col_values[0], | ||
&DataType::Timestamp(TimeUnit::Second, None), | ||
None, | ||
) | ||
}, | ||
other => { | ||
return internal_err!( | ||
"Unsupported data type {other:?} for function from_unixtime" | ||
); | ||
} | ||
} | ||
}), | ||
BuiltinScalarFunction::ArrowTypeof => { | ||
let input_data_type = input_phy_exprs[0].data_type(input_schema)?; | ||
Arc::new(move |_| { | ||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(format!( | ||
"{input_data_type}" | ||
))))) | ||
}) | ||
} | ||
BuiltinScalarFunction::Abs => { | ||
let input_data_type = input_phy_exprs[0].data_type(input_schema)?; | ||
let abs_fun = math_expressions::create_abs_function(&input_data_type)?; | ||
Arc::new(move |args| make_scalar_function(abs_fun)(args)) | ||
} | ||
// These don't need args and input schema | ||
_ => create_physical_fun(fun, execution_props)?, | ||
}; | ||
let fun_expr: ScalarFunctionImplementation = | ||
create_physical_fun(fun, execution_props)?; | ||
|
||
let monotonicity = fun.monotonicity(); | ||
|
||
|
@@ -397,6 +260,9 @@ pub fn create_physical_fun( | |
) -> Result<ScalarFunctionImplementation> { | ||
Ok(match fun { | ||
// math functions | ||
BuiltinScalarFunction::Abs => { | ||
Arc::new(|args| make_scalar_function(math_expressions::abs_invoke)(args)) | ||
} | ||
BuiltinScalarFunction::Acos => Arc::new(math_expressions::acos), | ||
BuiltinScalarFunction::Asin => Arc::new(math_expressions::asin), | ||
BuiltinScalarFunction::Atan => Arc::new(math_expressions::atan), | ||
|
@@ -625,6 +491,24 @@ pub fn create_physical_fun( | |
execution_props.query_execution_start_time, | ||
)) | ||
} | ||
BuiltinScalarFunction::ToTimestamp => { | ||
Arc::new(datetime_expressions::to_timestamp_invoke) | ||
} | ||
BuiltinScalarFunction::ToTimestampMillis => { | ||
Arc::new(datetime_expressions::to_timestamp_millis_invoke) | ||
} | ||
BuiltinScalarFunction::ToTimestampMicros => { | ||
Arc::new(datetime_expressions::to_timestamp_micros_invoke) | ||
} | ||
BuiltinScalarFunction::ToTimestampNanos => { | ||
Arc::new(datetime_expressions::to_timestamp_nanos_invoke) | ||
} | ||
BuiltinScalarFunction::ToTimestampSeconds => { | ||
Arc::new(datetime_expressions::to_timestamp_seconds_invoke) | ||
} | ||
BuiltinScalarFunction::FromUnixtime => { | ||
Arc::new(datetime_expressions::from_unixtime_invoke) | ||
} | ||
BuiltinScalarFunction::InitCap => Arc::new(|args| match args[0].data_type() { | ||
DataType::Utf8 => { | ||
make_scalar_function(string_expressions::initcap::<i32>)(args) | ||
|
@@ -927,11 +811,19 @@ pub fn create_physical_fun( | |
}), | ||
BuiltinScalarFunction::Upper => Arc::new(string_expressions::upper), | ||
BuiltinScalarFunction::Uuid => Arc::new(string_expressions::uuid), | ||
_ => { | ||
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. 👍 for removing the catchall |
||
return internal_err!( | ||
"create_physical_fun: Unsupported scalar function {fun:?}" | ||
); | ||
} | ||
BuiltinScalarFunction::ArrowTypeof => Arc::new(move |args| { | ||
if args.len() != 1 { | ||
return internal_err!( | ||
"arrow_typeof function requires 1 arguments, got {}", | ||
args.len() | ||
); | ||
} | ||
|
||
let input_data_type = args[0].data_type(); | ||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some(format!( | ||
"{input_data_type}" | ||
))))) | ||
}), | ||
}) | ||
} | ||
|
||
|
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.
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.
It seems that this
use
may not be required 🤔.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.
🤔 but clippy did not flag it