alamb commented on code in PR #9583:
URL: https://github.com/apache/arrow-datafusion/pull/9583#discussion_r1521763317


##########
datafusion/optimizer/src/analyzer/function_rewrite.rs:
##########
@@ -0,0 +1,123 @@
+// Licensed to the Apache Software Foundation (ASF) under one

Review Comment:
   Github renders this as a new file, but it is the old 
datafusion/optimizer/src/analyzer/rewrite_expr.rs with the array specific logic 
removed and instead invokes each `FunctionRewrite` individually



##########
datafusion/functions-array/src/rewrite.rs:
##########
@@ -0,0 +1,211 @@
+// 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.
+
+//! Rewrites for using Array Functions
+
+use crate::concat::{array_append, array_concat};
+use crate::expr_fn::{array_has_all, array_prepend};
+use datafusion_common::config::ConfigOptions;
+use datafusion_common::tree_node::Transformed;
+use datafusion_common::utils::list_ndims;
+use datafusion_common::{Column, DFSchema};
+use datafusion_expr::expr::ScalarFunction;
+use datafusion_expr::expr_rewriter::FunctionRewrite;
+use datafusion_expr::{
+    BinaryExpr, BuiltinScalarFunction, Expr, GetFieldAccess, GetIndexedField, 
Operator,
+};
+
+/// Rewrites expressions into function calls to array functions
+pub(crate) struct ArrayFunctionRewriter {}
+
+impl FunctionRewrite for ArrayFunctionRewriter {
+    fn name(&self) -> &str {
+        "FunctionRewrite"
+    }
+
+    fn rewrite(
+        &self,
+        expr: Expr,
+        schema: &DFSchema,
+        _config: &ConfigOptions,
+    ) -> datafusion_common::Result<Transformed<Expr>> {
+        let transformed = match expr {

Review Comment:
   The whole point of this PR is to move these rules out of 
`datafusion/optimizer/src/analyzer/rewrite_expr.rs`  in the optimizer crate and 
into the functions-array crate 



##########
datafusion/optimizer/src/analyzer/mod.rs:
##########
@@ -15,33 +15,33 @@
 // specific language governing permissions and limitations
 // under the License.
 
-pub mod count_wildcard_rule;
-pub mod inline_table_scan;
-pub mod rewrite_expr;
-pub mod subquery;
-pub mod type_coercion;
+use std::sync::Arc;

Review Comment:
   I don't know why the imports were moved -- I think my editor did it 



##########
datafusion/expr/src/expr_rewriter/mod.rs:
##########
@@ -33,6 +34,30 @@ use datafusion_common::{Column, DFSchema, Result};
 mod order_by;
 pub use order_by::rewrite_sort_cols_by_aggs;
 
+/// Trait for rewriting [`Expr`]s into function calls.
+///
+/// This trait is used with `FunctionRegistry::register_function_rewrite` to
+/// to evaluating `Expr`s using functions that may not be built in to 
DataFusion
+///
+/// For example, concatenating arrays `a || b` is represented as
+/// `Operator::ArrowAt`, but can be implemented by calling a function
+/// `array_concat` from the `functions-array` crate.
+pub trait FunctionRewrite {

Review Comment:
   This is the new trait used to rewrite `Expr`s to function calls



##########
datafusion/functions-array/src/rewrite.rs:
##########
@@ -0,0 +1,211 @@
+// 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.
+
+//! Rewrites for using Array Functions
+
+use crate::concat::{array_append, array_concat};
+use crate::expr_fn::{array_has_all, array_prepend};
+use datafusion_common::config::ConfigOptions;
+use datafusion_common::tree_node::Transformed;
+use datafusion_common::utils::list_ndims;
+use datafusion_common::{Column, DFSchema};
+use datafusion_expr::expr::ScalarFunction;
+use datafusion_expr::expr_rewriter::FunctionRewrite;
+use datafusion_expr::{
+    BinaryExpr, BuiltinScalarFunction, Expr, GetFieldAccess, GetIndexedField, 
Operator,
+};
+
+/// Rewrites expressions into function calls to array functions
+pub(crate) struct ArrayFunctionRewriter {}
+
+impl FunctionRewrite for ArrayFunctionRewriter {
+    fn name(&self) -> &str {
+        "FunctionRewrite"
+    }
+
+    fn rewrite(
+        &self,
+        expr: Expr,
+        schema: &DFSchema,
+        _config: &ConfigOptions,
+    ) -> datafusion_common::Result<Transformed<Expr>> {
+        let transformed = match expr {
+            // array1 @> array2 -> array_has_all(array1, array2)
+            Expr::BinaryExpr(BinaryExpr { left, op, right })
+                if op == Operator::AtArrow
+                    && is_func(&left, "make_array")
+                    && is_func(&right, "make_array") =>
+            {
+                Transformed::yes(array_has_all(*left, *right))

Review Comment:
   I also rewrote these rules to avoid `clone`ing (as they all get an owned 
`Expr`)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to