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


##########
datafusion/optimizer/src/pre_cast_lit_in_comparison.rs:
##########
@@ -0,0 +1,311 @@
+// 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.
+
+//! Pre-cast literal binary comparison rule can be only used to the binary 
comparison expr.
+//! It can reduce adding the `Expr::Cast` to the expr instead of adding the 
`Expr::Cast` to literal expr.
+use crate::{OptimizerConfig, OptimizerRule};
+use arrow::datatypes::DataType;
+use datafusion_common::{DFSchemaRef, DataFusionError, Result, ScalarValue};
+use datafusion_expr::utils::from_plan;
+use datafusion_expr::{binary_expr, lit, Expr, ExprSchemable, LogicalPlan, 
Operator};
+
+/// The rule can be only used to the numeric binary comparison with literal 
expr, like below pattern:
+/// `left_expr comparison_op literal_expr` or `literal_expr comparison_op 
right_expr`.
+/// The data type of two sides must be signed numeric type now, and will 
support more data type later.
+///
+/// If the binary comparison expr match above rules, the optimizer will check 
if the value of `literal`
+/// is in within range(min,max) which is the range(min,max) of the data type 
for `left_expr` or `right_expr`.
+///
+/// If this true, the literal expr will be casted to the data type of expr on 
the other side, and the result of
+/// binary comparison will be `left_expr comparison_op cast(literal_expr, 
left_data_type)` or
+/// `cast(literal_expr, right_data_type) comparison_op right_expr`. For better 
optimization,
+/// the expr of `cast(literal_expr, target_type)` will be precomputed and 
converted to the new expr `new_literal_expr`
+/// which data type is `target_type`.
+/// If this false, do nothing.
+///
+/// This is inspired by the optimizer rule `UnwrapCastInBinaryComparison` of 
Spark.

Review Comment:
   It really helps me review code when there are comments that explain the 
rationale 



-- 
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