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: Make it easier to work with Expr::ScalarFunction #8350

Merged
merged 1 commit into from
Nov 29, 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
6 changes: 3 additions & 3 deletions datafusion/core/src/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {

Ok(name)
}
Expr::ScalarFunction(expr::ScalarFunction { func_def, args }) => {
Expr::ScalarFunction(fun) => {
// function should be resolved during `AnalyzerRule`s
if let ScalarFunctionDefinition::Name(_) = func_def {
if let ScalarFunctionDefinition::Name(_) = fun.func_def {
return internal_err!("Function `Expr` with name should be resolved.");
}

create_function_physical_name(func_def.name(), false, args)
create_function_physical_name(fun.name(), false, &fun.args)
}
Expr::WindowFunction(WindowFunction { fun, args, .. }) => {
create_function_physical_name(&fun.to_string(), false, args)
Expand Down
15 changes: 10 additions & 5 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ pub struct ScalarFunction {
pub args: Vec<Expr>,
}

impl ScalarFunction {
// return the Function's name
pub fn name(&self) -> &str {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point of the PR is to add this

self.func_def.name()
}
}

impl ScalarFunctionDefinition {
/// Function's name for display
pub fn name(&self) -> &str {
Expand Down Expand Up @@ -1219,8 +1226,8 @@ impl fmt::Display for Expr {
write!(f, " NULLS LAST")
}
}
Expr::ScalarFunction(ScalarFunction { func_def, args }) => {
fmt_function(f, func_def.name(), false, args, true)
Expr::ScalarFunction(fun) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing so I think makes code like this easier / less inceptionish (Expr::ScalarFunction vs expr::ScalarFunction 🤯 )

fmt_function(f, fun.name(), false, &fun.args, true)
}
Expr::WindowFunction(WindowFunction {
fun,
Expand Down Expand Up @@ -1552,9 +1559,7 @@ fn create_name(e: &Expr) -> Result<String> {
}
}
}
Expr::ScalarFunction(ScalarFunction { func_def, args }) => {
create_function_name(func_def.name(), false, args)
}
Expr::ScalarFunction(fun) => create_function_name(fun.name(), false, &fun.args),
Expr::WindowFunction(WindowFunction {
fun,
args,
Expand Down
12 changes: 6 additions & 6 deletions datafusion/substrait/src/logical_plan/producer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use datafusion::common::{exec_err, internal_err, not_impl_err};
#[allow(unused_imports)]
use datafusion::logical_expr::aggregate_function;
use datafusion::logical_expr::expr::{
Alias, BinaryExpr, Case, Cast, GroupingSet, InList,
ScalarFunction as DFScalarFunction, ScalarFunctionDefinition, Sort, WindowFunction,
Alias, BinaryExpr, Case, Cast, GroupingSet, InList, ScalarFunctionDefinition, Sort,
WindowFunction,
};
use datafusion::logical_expr::{expr, Between, JoinConstraint, LogicalPlan, Operator};
use datafusion::prelude::Expr;
Expand Down Expand Up @@ -822,9 +822,9 @@ pub fn to_substrait_rex(
Ok(substrait_or_list)
}
}
Expr::ScalarFunction(DFScalarFunction { func_def, args }) => {
Expr::ScalarFunction(fun) => {
let mut arguments: Vec<FunctionArgument> = vec![];
for arg in args {
for arg in &fun.args {
arguments.push(FunctionArgument {
arg_type: Some(ArgType::Value(to_substrait_rex(
arg,
Expand All @@ -836,12 +836,12 @@ pub fn to_substrait_rex(
}

// function should be resolved during `AnalyzerRule`
if let ScalarFunctionDefinition::Name(_) = func_def {
if let ScalarFunctionDefinition::Name(_) = fun.func_def {
return internal_err!("Function `Expr` with name should be resolved.");
}

let function_anchor =
_register_function(func_def.name().to_string(), extension_info);
_register_function(fun.name().to_string(), extension_info);
Ok(Expression {
rex_type: Some(RexType::ScalarFunction(ScalarFunction {
function_reference: function_anchor,
Expand Down