Skip to content

Commit 755be5a

Browse files
Omega359ccciudatu
authored andcommitted
Move coalesce to datafusion-functions and remove BuiltInScalarFunction (apache#10098)
1 parent 59a5ac0 commit 755be5a

File tree

27 files changed

+241
-631
lines changed

27 files changed

+241
-631
lines changed

datafusion/core/src/datasource/listing/helpers.rs

-10
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ pub fn expr_applicable_for_cols(col_names: &[String], expr: &Expr) -> bool {
9090

9191
Expr::ScalarFunction(scalar_function) => {
9292
match &scalar_function.func_def {
93-
ScalarFunctionDefinition::BuiltIn(fun) => {
94-
match fun.volatility() {
95-
Volatility::Immutable => Ok(TreeNodeRecursion::Continue),
96-
// TODO: Stable functions could be `applicable`, but that would require access to the context
97-
Volatility::Stable | Volatility::Volatile => {
98-
is_applicable = false;
99-
Ok(TreeNodeRecursion::Stop)
100-
}
101-
}
102-
}
10393
ScalarFunctionDefinition::UDF(fun) => {
10494
match fun.signature().volatility {
10595
Volatility::Immutable => Ok(TreeNodeRecursion::Continue),

datafusion/expr/src/built_in_function.rs

-207
This file was deleted.

datafusion/expr/src/expr.rs

+3-19
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::logical_plan::Subquery;
2828
use crate::utils::expr_to_columns;
2929
use crate::window_frame;
3030
use crate::{
31-
aggregate_function, built_in_function, built_in_window_function, udaf,
32-
BuiltinScalarFunction, ExprSchemable, Operator, Signature,
31+
aggregate_function, built_in_window_function, udaf, ExprSchemable, Operator,
32+
Signature,
3333
};
3434

3535
use arrow::datatypes::DataType;
@@ -362,10 +362,6 @@ impl Between {
362362
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
363363
/// Defines which implementation of a function for DataFusion to call.
364364
pub enum ScalarFunctionDefinition {
365-
/// Resolved to a `BuiltinScalarFunction`
366-
/// There is plan to migrate `BuiltinScalarFunction` to UDF-based implementation (issue#8045)
367-
/// This variant is planned to be removed in long term
368-
BuiltIn(BuiltinScalarFunction),
369365
/// Resolved to a user defined function
370366
UDF(Arc<crate::ScalarUDF>),
371367
/// A scalar function constructed with name. This variant can not be executed directly
@@ -393,7 +389,6 @@ impl ScalarFunctionDefinition {
393389
/// Function's name for display
394390
pub fn name(&self) -> &str {
395391
match self {
396-
ScalarFunctionDefinition::BuiltIn(fun) => fun.name(),
397392
ScalarFunctionDefinition::UDF(udf) => udf.name(),
398393
ScalarFunctionDefinition::Name(func_name) => func_name.as_ref(),
399394
}
@@ -403,9 +398,6 @@ impl ScalarFunctionDefinition {
403398
/// when evaluated multiple times with the same input.
404399
pub fn is_volatile(&self) -> Result<bool> {
405400
match self {
406-
ScalarFunctionDefinition::BuiltIn(fun) => {
407-
Ok(fun.volatility() == crate::Volatility::Volatile)
408-
}
409401
ScalarFunctionDefinition::UDF(udf) => {
410402
Ok(udf.signature().volatility == crate::Volatility::Volatile)
411403
}
@@ -419,14 +411,6 @@ impl ScalarFunctionDefinition {
419411
}
420412

421413
impl ScalarFunction {
422-
/// Create a new ScalarFunction expression
423-
pub fn new(fun: built_in_function::BuiltinScalarFunction, args: Vec<Expr>) -> Self {
424-
Self {
425-
func_def: ScalarFunctionDefinition::BuiltIn(fun),
426-
args,
427-
}
428-
}
429-
430414
/// Create a new ScalarFunction expression with a user-defined function (UDF)
431415
pub fn new_udf(udf: Arc<crate::ScalarUDF>, args: Vec<Expr>) -> Self {
432416
Self {
@@ -1282,7 +1266,7 @@ impl Expr {
12821266
pub fn short_circuits(&self) -> bool {
12831267
match self {
12841268
Expr::ScalarFunction(ScalarFunction { func_def, .. }) => {
1285-
matches!(func_def, ScalarFunctionDefinition::BuiltIn(fun) if *fun == BuiltinScalarFunction::Coalesce)
1269+
matches!(func_def, ScalarFunctionDefinition::UDF(fun) if fun.name().eq("coalesce"))
12861270
}
12871271
Expr::BinaryExpr(BinaryExpr { op, .. }) => {
12881272
matches!(op, Operator::And | Operator::Or)

datafusion/expr/src/expr_fn.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
2020
use crate::expr::{
2121
AggregateFunction, BinaryExpr, Cast, Exists, GroupingSet, InList, InSubquery,
22-
Placeholder, ScalarFunction, TryCast,
22+
Placeholder, TryCast,
2323
};
2424
use crate::function::{
2525
AccumulatorArgs, AccumulatorFactoryFunction, PartitionEvaluatorFactory,
2626
};
2727
use crate::{
28-
aggregate_function, built_in_function, conditional_expressions::CaseBuilder,
29-
logical_plan::Subquery, AggregateUDF, Expr, LogicalPlan, Operator,
30-
ScalarFunctionImplementation, ScalarUDF, Signature, Volatility,
28+
aggregate_function, conditional_expressions::CaseBuilder, logical_plan::Subquery,
29+
AggregateUDF, Expr, LogicalPlan, Operator, ScalarFunctionImplementation, ScalarUDF,
30+
Signature, Volatility,
3131
};
3232
use crate::{AggregateUDFImpl, ColumnarValue, ScalarUDFImpl, WindowUDF, WindowUDFImpl};
3333
use arrow::datatypes::{DataType, Field};
@@ -478,23 +478,6 @@ pub fn is_not_unknown(expr: Expr) -> Expr {
478478
Expr::IsNotUnknown(Box::new(expr))
479479
}
480480

481-
macro_rules! nary_scalar_expr {
482-
($ENUM:ident, $FUNC:ident, $DOC:expr) => {
483-
#[doc = $DOC ]
484-
pub fn $FUNC(args: Vec<Expr>) -> Expr {
485-
Expr::ScalarFunction(ScalarFunction::new(
486-
built_in_function::BuiltinScalarFunction::$ENUM,
487-
args,
488-
))
489-
}
490-
};
491-
}
492-
493-
// generate methods for creating the supported unary/binary expressions
494-
495-
// math functions
496-
nary_scalar_expr!(Coalesce, coalesce, "returns `coalesce(args...)`, which evaluates to the value of the first [Expr] which is not NULL");
497-
498481
/// Create a CASE WHEN statement with literal WHEN expressions for comparison to the base expression.
499482
pub fn case(expr: Expr) -> CaseBuilder {
500483
CaseBuilder::new(Some(Box::new(expr)), vec![], vec![], None)

datafusion/expr/src/expr_schema.rs

-17
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,6 @@ impl ExprSchemable for Expr {
139139
.map(|e| e.get_type(schema))
140140
.collect::<Result<Vec<_>>>()?;
141141
match func_def {
142-
ScalarFunctionDefinition::BuiltIn(fun) => {
143-
// verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
144-
data_types(&arg_data_types, &fun.signature()).map_err(|_| {
145-
plan_datafusion_err!(
146-
"{}",
147-
utils::generate_signature_error_msg(
148-
&format!("{fun}"),
149-
fun.signature(),
150-
&arg_data_types,
151-
)
152-
)
153-
})?;
154-
155-
// perform additional function arguments validation (due to limited
156-
// expressiveness of `TypeSignature`), then infer return type
157-
fun.return_type(&arg_data_types)
158-
}
159142
ScalarFunctionDefinition::UDF(fun) => {
160143
// verify that function is invoked with correct number and type of arguments as defined in `TypeSignature`
161144
data_types(&arg_data_types, fun.signature()).map_err(|_| {

datafusion/expr/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
//! The [expr_fn] module contains functions for creating expressions.
2727
2828
mod accumulator;
29-
mod built_in_function;
3029
mod built_in_window_function;
3130
mod columnar_value;
3231
mod literal;
@@ -60,7 +59,6 @@ pub mod window_state;
6059

6160
pub use accumulator::Accumulator;
6261
pub use aggregate_function::AggregateFunction;
63-
pub use built_in_function::BuiltinScalarFunction;
6462
pub use built_in_window_function::BuiltInWindowFunction;
6563
pub use columnar_value::ColumnarValue;
6664
pub use expr::{

datafusion/expr/src/tree_node.rs

-3
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,6 @@ impl TreeNode for Expr {
283283
.update_data(|be| Expr::Sort(Sort::new(be, asc, nulls_first))),
284284
Expr::ScalarFunction(ScalarFunction { func_def, args }) => {
285285
transform_vec(args, &mut f)?.map_data(|new_args| match func_def {
286-
ScalarFunctionDefinition::BuiltIn(fun) => {
287-
Ok(Expr::ScalarFunction(ScalarFunction::new(fun, new_args)))
288-
}
289286
ScalarFunctionDefinition::UDF(fun) => {
290287
Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fun, new_args)))
291288
}

0 commit comments

Comments
 (0)