alamb commented on code in PR #15110:
URL: https://github.com/apache/datafusion/pull/15110#discussion_r1987055078


##########
datafusion/optimizer/src/simplify_expressions/unwrap_cast.rs:
##########
@@ -468,6 +510,10 @@ mod tests {
         // the 99999999999 is not within the range of MAX(int32) and 
MIN(int32), we don't cast the lit(99999999999) to int32 type
         let expr_lt = cast(col("c1"), DataType::Int64).lt(lit(99999999999i64));
         assert_eq!(optimize_test(expr_lt.clone(), &schema), expr_lt);
+
+        // cast(c1, UTF8) < 123, only eq/not_eq should be optimized

Review Comment:
   👍 



##########
datafusion/optimizer/src/simplify_expressions/unwrap_cast.rs:
##########
@@ -177,6 +192,33 @@ pub(super) fn 
is_cast_expr_and_support_unwrap_cast_in_comparison_for_inlist<
     true
 }
 
+fn cast_literal_to_type_with_op(
+    lit_value: &ScalarValue,
+    target_type: &DataType,
+    op: Operator,
+) -> Option<ScalarValue> {
+    dbg!(lit_value, target_type, op);
+    match (op, lit_value) {
+        (
+            Operator::Eq | Operator::NotEq,
+            ScalarValue::Utf8(Some(ref str))
+            | ScalarValue::Utf8View(Some(ref str))
+            | ScalarValue::LargeUtf8(Some(ref str)),
+        ) => match target_type {
+            DataType::Int8 => str.parse::<i8>().ok().map(ScalarValue::from),

Review Comment:
   by calling `ok()` here I think this converts a predicate like `cast(col, 
utf8) = 'foo'` into `col = 'null'`
   
   Which is not quite the same thing. 
   
   Also, `str.parse()` likely has subltely different semantics than the arrow 
cast kernels. 
   
   This, can can you please make this function:
   1. Return `None` if the conversion fails (following the convention of other 
functions in this module like `try_cast_dictionary`)
   2. Use the cast kernel to parse the values?
   
   
   
   The `cast` kernel can be called by:
   1. Converting ScalarValue to an array with 
[`ScalarValue::to_array`](https://docs.rs/datafusion/latest/datafusion/common/enum.ScalarValue.html#method.to_array)
   2. Call `cast`: https://docs.rs/arrow/latest/arrow/array/cast/index.html
   3. Calling 
[`ScalarValue::try_from_array`](https://docs.rs/datafusion/latest/datafusion/common/enum.ScalarValue.html#method.try_from_array)



-- 
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-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to