-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Convert Binary Operator StringConcat to Function for array_concat, array_append and array_prepend
#8636
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
Convert Binary Operator StringConcat to Function for array_concat, array_append and array_prepend
#8636
Changes from 4 commits
bffd825
e7cfcce
0c18f78
6201176
69df2b6
77386b8
a905394
e4c83ab
1ef0e50
111299b
6a0d782
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Optimizer rule for expression rewrite | ||
|
|
||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::tree_node::TreeNode; | ||
| use datafusion_common::tree_node::TreeNodeRewriter; | ||
| use datafusion_common::Result; | ||
| use datafusion_expr::expr::ScalarFunction; | ||
| use datafusion_expr::BuiltinScalarFunction; | ||
| use datafusion_expr::Operator; | ||
| use datafusion_expr::Projection; | ||
| use datafusion_expr::ScalarFunctionDefinition; | ||
| use datafusion_expr::{BinaryExpr, Expr, LogicalPlan}; | ||
|
|
||
| use super::AnalyzerRule; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct OperatorToFunction {} | ||
|
|
||
| impl OperatorToFunction { | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl AnalyzerRule for OperatorToFunction { | ||
| fn name(&self) -> &str { | ||
| "operator_to_function" | ||
| } | ||
|
|
||
| fn analyze(&self, plan: LogicalPlan, _: &ConfigOptions) -> Result<LogicalPlan> { | ||
| analyze_internal(plan) | ||
| } | ||
| } | ||
|
|
||
| fn analyze_internal(plan: LogicalPlan) -> Result<LogicalPlan> { | ||
| // OperatorToFunction is only applied to Projection | ||
| match plan { | ||
| LogicalPlan::Projection(_) => {} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on test, I found they are all project plan with inputs.len() 1. Not sure is this assumption correct or not
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will not catch uses of operators in all places (like I think you can follow the model of https://github.com/apache/arrow-datafusion/blob/f4233a92761e9144b8747e66b95bf0b3f82464b8/datafusion/optimizer/src/analyzer/type_coercion.rs#L74-L122 and call let new_expr = plan
.expressions()
.into_iter()
.map(|expr| {
// ensure aggregate names don't change:
// https://github.com/apache/arrow-datafusion/issues/3555
rewrite_preserving_name(expr, &mut expr_rewrite)
})
.collect::<Result<Vec<_>>>()?;
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did follow the code in type coercion at the first place, but I'm not sure whether we need these for operatorToFunction since I did find any test cases
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems column wise cases cover it. |
||
| _ => { | ||
| return Ok(plan); | ||
| } | ||
| } | ||
|
|
||
| let mut expr_rewriter = OperatorToFunctionRewriter {}; | ||
|
|
||
| let new_expr = plan | ||
| .expressions() | ||
| .into_iter() | ||
| .map(|expr| expr.rewrite(&mut expr_rewriter)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
|
|
||
| // Not found cases that inputs more than one | ||
| assert_eq!(plan.inputs().len(), 1); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
| let input = plan.inputs()[0]; | ||
|
|
||
| Ok(LogicalPlan::Projection(Projection::try_new( | ||
| new_expr, | ||
| input.to_owned().into(), | ||
| )?)) | ||
| } | ||
|
|
||
| pub(crate) struct OperatorToFunctionRewriter {} | ||
|
|
||
| impl TreeNodeRewriter for OperatorToFunctionRewriter { | ||
| type N = Expr; | ||
|
|
||
| fn mutate(&mut self, expr: Expr) -> Result<Expr> { | ||
| match expr { | ||
| Expr::BinaryExpr(BinaryExpr { | ||
| ref left, | ||
| op, | ||
| ref right, | ||
| }) => { | ||
| if let Some(fun) = rewrite_array_concat_operator_to_func( | ||
| left.as_ref(), | ||
| op, | ||
| right.as_ref(), | ||
| ) { | ||
| // Convert &Box<Expr> -> Expr | ||
| let left = (**left).clone(); | ||
| let right = (**right).clone(); | ||
| return Ok(Expr::ScalarFunction(ScalarFunction { | ||
| func_def: ScalarFunctionDefinition::BuiltIn(fun), | ||
| args: vec![left, right], | ||
| })); | ||
| } | ||
| Ok(expr) | ||
| } | ||
| _ => Ok(expr), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Summary of the logic below: | ||
| /// | ||
| /// array || array -> array concat | ||
| /// | ||
| /// array || scalar -> array append | ||
| /// | ||
| /// scalar || array -> array prepend | ||
| /// | ||
| /// (arry concat, array append, array prepend) || array -> array concat | ||
| /// | ||
| /// (arry concat, array append, array prepend) || scalar -> array append | ||
| fn rewrite_array_concat_operator_to_func( | ||
| left: &Expr, | ||
| op: Operator, | ||
| right: &Expr, | ||
| ) -> Option<BuiltinScalarFunction> { | ||
| // Convert `Array StringConcat Array` to ScalarFunction::ArrayConcat | ||
|
|
||
| if op != Operator::StringConcat { | ||
| return None; | ||
| } | ||
|
|
||
| match (left, right) { | ||
| // Chain concat operator (a || b) || array, | ||
| // (arry concat, array append, array prepend) || array -> array concat | ||
| ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayConcat), | ||
| args: _left_args, | ||
| }), | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _right_args, | ||
| }), | ||
| ) | ||
| | ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayAppend), | ||
| args: _left_args, | ||
| }), | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _right_args, | ||
| }), | ||
| ) | ||
| | ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayPrepend), | ||
| args: _left_args, | ||
| }), | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _right_args, | ||
| }), | ||
| ) => Some(BuiltinScalarFunction::ArrayConcat), | ||
| // Chain concat operator (a || b) || scalar, | ||
| // (arry concat, array append, array prepend) || scalar -> array append | ||
| ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayConcat), | ||
| args: _left_args, | ||
| }), | ||
| _scalar, | ||
| ) | ||
| | ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayAppend), | ||
| args: _left_args, | ||
| }), | ||
| _scalar, | ||
| ) | ||
| | ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::ArrayPrepend), | ||
| args: _left_args, | ||
| }), | ||
| _scalar, | ||
| ) => Some(BuiltinScalarFunction::ArrayAppend), | ||
| // array || array -> array concat | ||
| ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _left_args, | ||
| }), | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _right_args, | ||
| }), | ||
| ) => Some(BuiltinScalarFunction::ArrayConcat), | ||
| // array || scalar -> array append | ||
| ( | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _left_args, | ||
| }), | ||
| _right_scalar, | ||
| ) => Some(BuiltinScalarFunction::ArrayAppend), | ||
| // scalar || array -> array prepend | ||
| ( | ||
| _left_scalar, | ||
| Expr::ScalarFunction(ScalarFunction { | ||
| func_def: | ||
| ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::MakeArray), | ||
| args: _right_args, | ||
| }), | ||
| ) => Some(BuiltinScalarFunction::ArrayPrepend), | ||
|
|
||
| _ => None, | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.