You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/10 16:00:58 UTC

[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1981: Remove some more unecessary cloning in sql_expr_to_logical_expr

alamb commented on a change in pull request #1981:
URL: https://github.com/apache/arrow-datafusion/pull/1981#discussion_r823873888



##########
File path: datafusion/src/sql/planner.rs
##########
@@ -1209,7 +1208,7 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
     }
 
     /// Generate a relational expression from a SQL expression
-    pub fn sql_to_rex(&self, sql: &SQLExpr, schema: &DFSchema) -> Result<Expr> {
+    pub fn sql_to_rex(&self, sql: SQLExpr, schema: &DFSchema) -> Result<Expr> {

Review comment:
       This is the real change in this PR: take `sql` by ownership rather than reference

##########
File path: datafusion/src/sql/planner.rs
##########
@@ -1495,121 +1493,134 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
             }
 
             SQLExpr::Cast {
-                ref expr,
-                ref data_type,
+                expr,
+                data_type,
             } => Ok(Expr::Cast {
-                expr: Box::new(self.sql_expr_to_logical_expr(expr, schema)?),
-                data_type: convert_data_type(data_type)?,
+                expr: Box::new(self.sql_expr_to_logical_expr(*expr, schema)?),
+                data_type: convert_data_type(&data_type)?,
             }),
 
             SQLExpr::TryCast {
-                ref expr,
-                ref data_type,
+                expr,
+                data_type,
             } => Ok(Expr::TryCast {
-                expr: Box::new(self.sql_expr_to_logical_expr(expr, schema)?),
-                data_type: convert_data_type(data_type)?,
+                expr: Box::new(self.sql_expr_to_logical_expr(*expr, schema)?),
+                data_type: convert_data_type(&data_type)?,
             }),
 
             SQLExpr::TypedString {
                 ref data_type,
                 ref value,
             } => Ok(Expr::Cast {
                 expr: Box::new(lit(&**value)),
-                data_type: convert_data_type(data_type)?,
+                data_type: convert_data_type(&data_type)?,
             }),
 
-            SQLExpr::IsNull(ref expr) => Ok(Expr::IsNull(Box::new(
-                self.sql_expr_to_logical_expr(expr, schema)?,
+            SQLExpr::IsNull(expr) => Ok(Expr::IsNull(Box::new(
+                self.sql_expr_to_logical_expr(*expr, schema)?,
             ))),
 
-            SQLExpr::IsNotNull(ref expr) => Ok(Expr::IsNotNull(Box::new(
-                self.sql_expr_to_logical_expr(expr, schema)?,
+            SQLExpr::IsNotNull(expr) => Ok(Expr::IsNotNull(Box::new(
+                self.sql_expr_to_logical_expr(*expr, schema)?,
             ))),
 
             SQLExpr::IsDistinctFrom(left, right) => Ok(Expr::BinaryExpr {
-                left: Box::new(self.sql_expr_to_logical_expr(left, schema)?),
+                left: Box::new(self.sql_expr_to_logical_expr(*left, schema)?),
                 op: Operator::IsDistinctFrom,
-                right: Box::new(self.sql_expr_to_logical_expr(right, schema)?),
+                right: Box::new(self.sql_expr_to_logical_expr(*right, schema)?),
             }),
 
             SQLExpr::IsNotDistinctFrom(left, right) => Ok(Expr::BinaryExpr {
-                left: Box::new(self.sql_expr_to_logical_expr(left, schema)?),
+                left: Box::new(self.sql_expr_to_logical_expr(*left, schema)?),
                 op: Operator::IsNotDistinctFrom,
-                right: Box::new(self.sql_expr_to_logical_expr(right, schema)?),
+                right: Box::new(self.sql_expr_to_logical_expr(*right, schema)?),
             }),
 
-            SQLExpr::UnaryOp { ref op, ref expr } => {
-                self.parse_sql_unary_op(op, expr, schema)
+            SQLExpr::UnaryOp { op, expr } => {
+                self.parse_sql_unary_op(op, *expr, schema)
             }
 
             SQLExpr::Between {
-                ref expr,
-                ref negated,
-                ref low,
-                ref high,
+                expr,
+                negated,
+                low,
+                high,
             } => Ok(Expr::Between {
-                expr: Box::new(self.sql_expr_to_logical_expr(expr, schema)?),
-                negated: *negated,
-                low: Box::new(self.sql_expr_to_logical_expr(low, schema)?),
-                high: Box::new(self.sql_expr_to_logical_expr(high, schema)?),
+                expr: Box::new(self.sql_expr_to_logical_expr(*expr, schema)?),
+                negated,
+                low: Box::new(self.sql_expr_to_logical_expr(*low, schema)?),
+                high: Box::new(self.sql_expr_to_logical_expr(*high, schema)?),
             }),
 
             SQLExpr::InList {
-                ref expr,
-                ref list,
-                ref negated,
+                expr,
+                list,
+                negated,
             } => {
                 let list_expr = list
-                    .iter()
+                    .into_iter()
                     .map(|e| self.sql_expr_to_logical_expr(e, schema))
                     .collect::<Result<Vec<_>>>()?;
 
                 Ok(Expr::InList {
-                    expr: Box::new(self.sql_expr_to_logical_expr(expr, schema)?),
+                    expr: Box::new(self.sql_expr_to_logical_expr(*expr, schema)?),
                     list: list_expr,
-                    negated: *negated,
+                    negated,
                 })
             }
 
             SQLExpr::BinaryOp {
-                ref left,
-                ref op,
-                ref right,
-            } => self.parse_sql_binary_op(left, op, right, schema),
+                left,
+                op,
+                right,
+            } => self.parse_sql_binary_op(*left, op, *right, schema),
 
             #[cfg(feature = "unicode_expressions")]
             SQLExpr::Substring {
                 expr,
                 substring_from,
                 substring_for,
             } => {
-                let arg = self.sql_expr_to_logical_expr(expr, schema)?;
+                if substring_from.is_none() && substring_for.is_none() {

Review comment:
       I moved the error up here because it consumes the original `expr` to make a nice message.

##########
File path: datafusion/src/sql/planner.rs
##########
@@ -1246,34 +1245,37 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
 
     fn sql_fn_arg_to_logical_expr(
         &self,
-        sql: &FunctionArg,
+        sql: FunctionArg,
         schema: &DFSchema,
     ) -> Result<Expr> {
-        let arg: &FunctionArgExpr = match sql {
-            FunctionArg::Named { name: _, arg } => arg,
-            FunctionArg::Unnamed(arg) => arg,
-        };
-
-        match arg {
-            FunctionArgExpr::Expr(arg) => self.sql_expr_to_logical_expr(arg, schema),
-            FunctionArgExpr::Wildcard => Ok(Expr::Wildcard),
-            FunctionArgExpr::QualifiedWildcard(_) => {
-                Err(DataFusionError::NotImplemented(format!(
-                    "Unsupported qualified wildcard argument: {:?}",
-                    sql
-                )))
+        match sql {

Review comment:
       I had to expand out the 4 cases here to avoid cloning - it isn't as nice but I think it is still pretty straightforward




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org