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

rewrite nvl function #9413

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 0 additions & 3 deletions datafusion/functions/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
//! "core" DataFusion functions

mod nullif;
mod nvl;
mod nvl2;

// create UDFs
make_udf_function!(nullif::NullIfFunc, NULLIF, nullif);
make_udf_function!(nvl::NVLFunc, NVL, nvl);
make_udf_function!(nvl2::NVL2Func, NVL2, nvl2);

// Export the functions out of this package, both as expr_fn as well as a list of functions
export_functions!(
(nullif, arg_1 arg_2, "returns NULL if value1 equals value2; otherwise it returns value1. This can be used to perform the inverse operation of the COALESCE expression."),
(nvl, arg_1 arg_2, "returns value2 if value1 is NULL; otherwise it returns value1"),
(nvl2, arg_1 arg_2 arg_3, "Returns value2 if value1 is not NULL; otherwise, it returns value3.")
);
279 changes: 0 additions & 279 deletions datafusion/functions/src/core/nvl.rs

This file was deleted.

36 changes: 36 additions & 0 deletions datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
crate::utils::normalize_ident(name.0[0].clone())
};

if Self::all_suger_function().contains(&name.as_str()) {
if let Some(expr) =
self.try_rewrite_suger_function(&name, &args, schema, planner_context)?
{
return Ok(expr);
}
}
// user-defined function (UDF) should have precedence in case it has the same name as a scalar built-in function
if let Some(fm) = self.context_provider.get_function_meta(&name) {
let args = self.function_args_to_expr(args, schema, planner_context)?;
Expand Down Expand Up @@ -311,4 +318,33 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}
}

fn all_suger_function() -> &'static [&'static str] {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be a const

const NVL_ALIAS: &'static [&'static str] = ...

&["nvl", "ifnull"]
}

fn try_rewrite_suger_function(
&self,
name: &str,
args: &[FunctionArg],
schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Option<Expr>> {
match (name, args) {
// rewirte nvl function to nvl2 function
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// rewirte nvl function to nvl2 function
// rewrite nvl function to nvl2 function

("nvl" | "ifnull", [left, right]) => {
if let Some(fm) = self.context_provider.get_function_meta("nvl2") {
let new_args = vec![left.clone(), left.clone(), right.clone()];
let args =
self.function_args_to_expr(new_args, schema, planner_context)?;
Ok(Some(Expr::ScalarFunction(ScalarFunction::new_udf(
fm, args,
))))
} else {
Ok(None)
}
}
(_, _) => Ok(None),
}
}
}
Loading