Skip to content

Commit f11a589

Browse files
committed
Move functionality into BuildInScalarFunction
1 parent d689daf commit f11a589

File tree

9 files changed

+937
-906
lines changed

9 files changed

+937
-906
lines changed

datafusion/expr/src/built_in_function.rs

Lines changed: 842 additions & 1 deletion
Large diffs are not rendered by default.

datafusion/expr/src/expr_schema.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ use crate::expr::{
2323
use crate::field_util::get_indexed_field;
2424
use crate::type_coercion::binary::get_result_type;
2525
use crate::type_coercion::other::get_coerce_type_for_case_expression;
26-
use crate::{
27-
aggregate_function, function, window_function, LogicalPlan, Projection, Subquery,
28-
};
26+
use crate::{aggregate_function, window_function, LogicalPlan, Projection, Subquery};
2927
use arrow::compute::can_cast_types;
3028
use arrow::datatypes::DataType;
3129
use datafusion_common::{Column, DFField, DFSchema, DataFusionError, ExprSchema, Result};
@@ -107,7 +105,7 @@ impl ExprSchemable for Expr {
107105
.iter()
108106
.map(|e| e.get_type(schema))
109107
.collect::<Result<Vec<_>>>()?;
110-
function::return_type(fun, &data_types)
108+
fun.return_type(&data_types)
111109
}
112110
Expr::WindowFunction(WindowFunction { fun, args, .. }) => {
113111
let data_types = args

datafusion/expr/src/function.rs

Lines changed: 44 additions & 768 deletions
Large diffs are not rendered by default.

datafusion/expr/src/function_err.rs

Lines changed: 0 additions & 125 deletions
This file was deleted.

datafusion/expr/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub mod expr_rewriter;
3737
pub mod expr_schema;
3838
pub mod field_util;
3939
pub mod function;
40-
pub mod function_err;
4140
mod literal;
4241
pub mod logical_plan;
4342
mod nullif;

datafusion/expr/src/signature.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ pub enum TypeSignature {
6060
OneOf(Vec<TypeSignature>),
6161
}
6262

63+
impl TypeSignature {
64+
pub(crate) fn to_string_repr(&self) -> Vec<String> {
65+
match self {
66+
TypeSignature::Variadic(types) => {
67+
vec![format!("{}, ..", Self::join_types(types, "/"))]
68+
}
69+
TypeSignature::Uniform(arg_count, valid_types) => {
70+
vec![std::iter::repeat(Self::join_types(valid_types, "/"))
71+
.take(*arg_count)
72+
.collect::<Vec<String>>()
73+
.join(", ")]
74+
}
75+
TypeSignature::Exact(types) => {
76+
vec![Self::join_types(types, ", ")]
77+
}
78+
TypeSignature::Any(arg_count) => {
79+
vec![std::iter::repeat("Any")
80+
.take(*arg_count)
81+
.collect::<Vec<&str>>()
82+
.join(", ")]
83+
}
84+
TypeSignature::VariadicEqual => vec!["T, .., T".to_string()],
85+
TypeSignature::VariadicAny => vec!["Any, .., Any".to_string()],
86+
TypeSignature::OneOf(sigs) => {
87+
sigs.iter().flat_map(|s| s.to_string_repr()).collect()
88+
}
89+
}
90+
}
91+
92+
/// Helper function to join types with specified delimiter.
93+
pub(crate) fn join_types<T: std::fmt::Display>(
94+
types: &[T],
95+
delimiter: &str,
96+
) -> String {
97+
types
98+
.iter()
99+
.map(|t| t.to_string())
100+
.collect::<Vec<String>>()
101+
.join(delimiter)
102+
}
103+
}
104+
63105
/// The signature of a function defines the supported argument types
64106
/// and its volatility.
65107
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ use datafusion_expr::type_coercion::other::{
4040
use datafusion_expr::type_coercion::{is_datetime, is_numeric, is_utf8_or_large_utf8};
4141
use datafusion_expr::utils::from_plan;
4242
use datafusion_expr::{
43-
aggregate_function, function, is_false, is_not_false, is_not_true, is_not_unknown,
44-
is_true, is_unknown, type_coercion, AggregateFunction, Expr, LogicalPlan, Operator,
43+
aggregate_function, is_false, is_not_false, is_not_true, is_not_unknown, is_true,
44+
is_unknown, type_coercion, AggregateFunction, Expr, LogicalPlan, Operator,
4545
WindowFrame, WindowFrameBound, WindowFrameUnits,
4646
};
4747
use datafusion_expr::{ExprSchemable, Signature};
@@ -383,7 +383,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
383383
let nex_expr = coerce_arguments_for_signature(
384384
args.as_slice(),
385385
&self.schema,
386-
&function::signature(&fun),
386+
&fun.signature(),
387387
)?;
388388
let expr = Expr::ScalarFunction(ScalarFunction::new(fun, nex_expr));
389389
Ok(expr)

datafusion/physical-expr/src/functions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use arrow::{
4545
};
4646
use datafusion_common::{DataFusionError, Result, ScalarValue};
4747
use datafusion_expr::{
48-
function, BuiltinScalarFunction, ColumnarValue, ScalarFunctionImplementation,
48+
BuiltinScalarFunction, ColumnarValue, ScalarFunctionImplementation,
4949
};
5050
use std::sync::Arc;
5151

@@ -62,7 +62,7 @@ pub fn create_physical_expr(
6262
.map(|e| e.data_type(input_schema))
6363
.collect::<Result<Vec<_>>>()?;
6464

65-
let data_type = function::return_type(fun, &input_expr_types)?;
65+
let data_type = fun.return_type(&input_expr_types)?;
6666

6767
let fun_expr: ScalarFunctionImplementation = match fun {
6868
// These functions need args and input schema to pick an implementation
@@ -2922,7 +2922,7 @@ mod tests {
29222922
execution_props: &ExecutionProps,
29232923
) -> Result<Arc<dyn PhysicalExpr>> {
29242924
let type_coerced_phy_exprs =
2925-
coerce(input_phy_exprs, input_schema, &function::signature(fun)).unwrap();
2925+
coerce(input_phy_exprs, input_schema, &fun.signature()).unwrap();
29262926
create_physical_expr(fun, &type_coerced_phy_exprs, input_schema, execution_props)
29272927
}
29282928

datafusion/sql/src/expr/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
1919
use datafusion_common::{DFSchema, DataFusionError, Result};
2020
use datafusion_expr::expr::{ScalarFunction, ScalarUDF};
21-
use datafusion_expr::function_err::suggest_valid_function;
21+
use datafusion_expr::function::suggest_valid_function;
2222
use datafusion_expr::utils::COUNT_STAR_EXPANSION;
2323
use datafusion_expr::window_frame::regularize;
2424
use datafusion_expr::{

0 commit comments

Comments
 (0)