Skip to content

Commit

Permalink
Move overlay planning toExprPlanner (apache#11398)
Browse files Browse the repository at this point in the history
* move overlay to expr planner

* typo
  • Loading branch information
dharanad authored and findepi committed Jul 16, 2024
1 parent c77ac73 commit 4e8efec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
7 changes: 7 additions & 0 deletions datafusion/expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ pub trait ExprPlanner: Send + Sync {
) -> Result<PlannerResult<Vec<Expr>>> {
Ok(PlannerResult::Original(args))
}

/// Plans an overlay expression eg `overlay(str PLACING substr FROM pos [FOR count])`
///
/// Returns origin expression arguments if not possible
fn plan_overlay(&self, args: Vec<Expr>) -> Result<PlannerResult<Vec<Expr>>> {
Ok(PlannerResult::Original(args))
}
}

/// An operator with two arguments to plan
Expand Down
6 changes: 6 additions & 0 deletions datafusion/functions/src/core/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ impl ExprPlanner for CoreFunctionPlanner {
),
)))
}

fn plan_overlay(&self, args: Vec<Expr>) -> Result<PlannerResult<Vec<Expr>>> {
Ok(PlannerResult::Planned(Expr::ScalarFunction(
ScalarFunction::new_udf(crate::string::overlay(), args),
)))
}
}
1 change: 0 additions & 1 deletion datafusion/functions/src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
lower(),
ltrim(),
octet_length(),
overlay(),
repeat(),
replace(),
rtrim(),
Expand Down
28 changes: 14 additions & 14 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}

not_impl_err!("Extract not supported by UserDefinedExtensionPlanners: {extract_args:?}")
not_impl_err!("Extract not supported by ExprPlanner: {extract_args:?}")
}

SQLExpr::Array(arr) => self.sql_array_literal(arr.elem, schema),
Expand Down Expand Up @@ -292,7 +292,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}

not_impl_err!("GetFieldAccess not supported by UserDefinedExtensionPlanners: {field_access_expr:?}")
not_impl_err!(
"GetFieldAccess not supported by ExprPlanner: {field_access_expr:?}"
)
}

SQLExpr::CompoundIdentifier(ids) => {
Expand Down Expand Up @@ -657,7 +659,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
PlannerResult::Original(args) => create_struct_args = args,
}
}
not_impl_err!("Struct not supported by UserDefinedExtensionPlanners: {create_struct_args:?}")
not_impl_err!("Struct not supported by ExprPlanner: {create_struct_args:?}")
}

fn sql_position_to_expr(
Expand All @@ -680,9 +682,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
}
}

not_impl_err!(
"Position not supported by UserDefinedExtensionPlanners: {position_args:?}"
)
not_impl_err!("Position not supported by ExprPlanner: {position_args:?}")
}

fn try_plan_dictionary_literal(
Expand Down Expand Up @@ -914,26 +914,26 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let fun = self
.context_provider
.get_function_meta("overlay")
.ok_or_else(|| {
internal_datafusion_err!("Unable to find expected 'overlay' function")
})?;
let arg = self.sql_expr_to_logical_expr(expr, schema, planner_context)?;
let what_arg =
self.sql_expr_to_logical_expr(overlay_what, schema, planner_context)?;
let from_arg =
self.sql_expr_to_logical_expr(overlay_from, schema, planner_context)?;
let args = match overlay_for {
let mut overlay_args = match overlay_for {
Some(for_expr) => {
let for_expr =
self.sql_expr_to_logical_expr(*for_expr, schema, planner_context)?;
vec![arg, what_arg, from_arg, for_expr]
}
None => vec![arg, what_arg, from_arg],
};
Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fun, args)))
for planner in self.planners.iter() {
match planner.plan_overlay(overlay_args)? {
PlannerResult::Planned(expr) => return Ok(expr),
PlannerResult::Original(args) => overlay_args = args,
}
}
not_impl_err!("Overlay not supported by ExprPlanner: {overlay_args:?}")
}
}

Expand Down

0 comments on commit 4e8efec

Please sign in to comment.