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

Minor: Move functionality into BuildInScalarFunction #6612

Merged
merged 2 commits into from
Jun 11, 2023
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
712 changes: 711 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 @@ -22,9 +22,7 @@ use crate::expr::{
};
use crate::field_util::get_indexed_field;
use crate::type_coercion::binary::get_result_type;
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 @@ -87,7 +85,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)
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 is a pretty good example of the point of this PR -- the functionality is now part of fun rather than a free function that takes &self as its first argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(I hope to apply the same pattern to window_function::return_type and aggregate_function::return_type

Copy link
Member

Choose a reason for hiding this comment

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

(I hope to apply the same pattern to window_function::return_type and aggregate_function::return_type

🚀agree with it

}
Expr::WindowFunction(WindowFunction { fun, args, .. }) => {
let data_types = args
Expand Down
685 changes: 44 additions & 641 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,
Projection, WindowFrame, WindowFrameBound, WindowFrameUnits,
};
use datafusion_expr::{ExprSchemable, Signature};
Expand Down Expand Up @@ -390,7 +390,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 @@ -2921,7 +2921,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