Skip to content

Commit

Permalink
Move functionality into BuildInScalarFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Jun 9, 2023
1 parent 1a1efcf commit 5bfd5ef
Show file tree
Hide file tree
Showing 9 changed files with 944 additions and 913 deletions.
850 changes: 849 additions & 1 deletion datafusion/expr/src/built_in_function.rs

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use crate::expr::{
use crate::field_util::get_indexed_field;
use crate::type_coercion::binary::get_result_type;
use crate::type_coercion::other::get_coerce_type_for_case_expression;
use crate::{
aggregate_function, function, window_function, LogicalPlan, Projection, Subquery,
};
use crate::{aggregate_function, window_function, LogicalPlan, Projection, Subquery};
use arrow::compute::can_cast_types;
use arrow::datatypes::DataType;
use datafusion_common::{Column, DFField, DFSchema, DataFusionError, ExprSchema, Result};
Expand Down Expand Up @@ -107,7 +105,7 @@ impl ExprSchemable for Expr {
.iter()
.map(|e| e.get_type(schema))
.collect::<Result<Vec<_>>>()?;
function::return_type(fun, &data_types)
fun.return_type(&data_types)
}
Expr::WindowFunction(WindowFunction { fun, args, .. }) => {
let data_types = args
Expand Down
819 changes: 44 additions & 775 deletions datafusion/expr/src/function.rs

Large diffs are not rendered by default.

125 changes: 0 additions & 125 deletions datafusion/expr/src/function_err.rs

This file was deleted.

1 change: 0 additions & 1 deletion datafusion/expr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod expr_rewriter;
pub mod expr_schema;
pub mod field_util;
pub mod function;
pub mod function_err;
mod literal;
pub mod logical_plan;
mod nullif;
Expand Down
42 changes: 42 additions & 0 deletions datafusion/expr/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,48 @@ pub enum TypeSignature {
OneOf(Vec<TypeSignature>),
}

impl TypeSignature {
pub(crate) fn to_string_repr(&self) -> Vec<String> {
match self {
TypeSignature::Variadic(types) => {
vec![format!("{}, ..", Self::join_types(types, "/"))]
}
TypeSignature::Uniform(arg_count, valid_types) => {
vec![std::iter::repeat(Self::join_types(valid_types, "/"))
.take(*arg_count)
.collect::<Vec<String>>()
.join(", ")]
}
TypeSignature::Exact(types) => {
vec![Self::join_types(types, ", ")]
}
TypeSignature::Any(arg_count) => {
vec![std::iter::repeat("Any")
.take(*arg_count)
.collect::<Vec<&str>>()
.join(", ")]
}
TypeSignature::VariadicEqual => vec!["T, .., T".to_string()],
TypeSignature::VariadicAny => vec!["Any, .., Any".to_string()],
TypeSignature::OneOf(sigs) => {
sigs.iter().flat_map(|s| s.to_string_repr()).collect()
}
}
}

/// Helper function to join types with specified delimiter.
pub(crate) fn join_types<T: std::fmt::Display>(
types: &[T],
delimiter: &str,
) -> String {
types
.iter()
.map(|t| t.to_string())
.collect::<Vec<String>>()
.join(delimiter)
}
}

/// The signature of a function defines the supported argument types
/// and its volatility.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down
6 changes: 3 additions & 3 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ use datafusion_expr::type_coercion::other::{
use datafusion_expr::type_coercion::{is_datetime, is_numeric, is_utf8_or_large_utf8};
use datafusion_expr::utils::from_plan;
use datafusion_expr::{
aggregate_function, function, is_false, is_not_false, is_not_true, is_not_unknown,
is_true, is_unknown, type_coercion, AggregateFunction, Expr, LogicalPlan, Operator,
aggregate_function, is_false, is_not_false, is_not_true, is_not_unknown, is_true,
is_unknown, type_coercion, AggregateFunction, Expr, LogicalPlan, Operator,
WindowFrame, WindowFrameBound, WindowFrameUnits,
};
use datafusion_expr::{ExprSchemable, Signature};
Expand Down Expand Up @@ -383,7 +383,7 @@ impl TreeNodeRewriter for TypeCoercionRewriter {
let nex_expr = coerce_arguments_for_signature(
args.as_slice(),
&self.schema,
&function::signature(&fun),
&fun.signature(),
)?;
let expr = Expr::ScalarFunction(ScalarFunction::new(fun, nex_expr));
Ok(expr)
Expand Down
6 changes: 3 additions & 3 deletions datafusion/physical-expr/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use arrow::{
};
use datafusion_common::{DataFusionError, Result, ScalarValue};
use datafusion_expr::{
function, BuiltinScalarFunction, ColumnarValue, ScalarFunctionImplementation,
BuiltinScalarFunction, ColumnarValue, ScalarFunctionImplementation,
};
use std::sync::Arc;

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

let data_type = function::return_type(fun, &input_expr_types)?;
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
Expand Down Expand Up @@ -2922,7 +2922,7 @@ mod tests {
execution_props: &ExecutionProps,
) -> Result<Arc<dyn PhysicalExpr>> {
let type_coerced_phy_exprs =
coerce(input_phy_exprs, input_schema, &function::signature(fun)).unwrap();
coerce(input_phy_exprs, input_schema, &fun.signature()).unwrap();
create_physical_expr(fun, &type_coerced_phy_exprs, input_schema, execution_props)
}

Expand Down
2 changes: 1 addition & 1 deletion datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{DFSchema, DataFusionError, Result};
use datafusion_expr::expr::{ScalarFunction, ScalarUDF};
use datafusion_expr::function_err::suggest_valid_function;
use datafusion_expr::function::suggest_valid_function;
use datafusion_expr::utils::COUNT_STAR_EXPANSION;
use datafusion_expr::window_frame::regularize;
use datafusion_expr::{
Expand Down

0 comments on commit 5bfd5ef

Please sign in to comment.