AdamGS commented on code in PR #23631:
URL: https://github.com/apache/datafusion/pull/23631#discussion_r3603509817


##########
datafusion/optimizer/src/simplify_expressions/utils.rs:
##########
@@ -139,54 +97,26 @@ pub fn delete_xor_in_complex_expr(expr: &Expr, needle: 
&Expr, is_left: bool) ->
 }
 
 pub fn is_zero(s: &Expr) -> bool {
-    match s {
-        Expr::Literal(ScalarValue::Int8(Some(0)), _)
-        | Expr::Literal(ScalarValue::Int16(Some(0)), _)
-        | Expr::Literal(ScalarValue::Int32(Some(0)), _)
-        | Expr::Literal(ScalarValue::Int64(Some(0)), _)
-        | Expr::Literal(ScalarValue::UInt8(Some(0)), _)
-        | Expr::Literal(ScalarValue::UInt16(Some(0)), _)
-        | Expr::Literal(ScalarValue::UInt32(Some(0)), _)
-        | Expr::Literal(ScalarValue::UInt64(Some(0)), _) => true,
-        Expr::Literal(ScalarValue::Float32(Some(v)), _) if *v == 0. => true,
-        Expr::Literal(ScalarValue::Float64(Some(v)), _) if *v == 0. => true,
-        Expr::Literal(ScalarValue::Decimal128(Some(v), _p, _s), _) if *v == 0 
=> true,
-        Expr::Literal(ScalarValue::Decimal256(Some(v), _p, _s), _)
-            if *v == i256::ZERO =>
-        {
-            true
-        }
-        _ => false,
+    if let Expr::Literal(sv, _) = s
+        && sv.data_type().is_numeric()
+    {
+        // unwrap safe since numeric types always have a 0 value
+        sv == &ScalarValue::new_zero(&sv.data_type()).unwrap()
+    } else {
+        false
     }
 }
 
 pub fn is_one(s: &Expr) -> bool {
-    match s {
-        Expr::Literal(ScalarValue::Int8(Some(1)), _)
-        | Expr::Literal(ScalarValue::Int16(Some(1)), _)
-        | Expr::Literal(ScalarValue::Int32(Some(1)), _)
-        | Expr::Literal(ScalarValue::Int64(Some(1)), _)
-        | Expr::Literal(ScalarValue::UInt8(Some(1)), _)
-        | Expr::Literal(ScalarValue::UInt16(Some(1)), _)
-        | Expr::Literal(ScalarValue::UInt32(Some(1)), _)
-        | Expr::Literal(ScalarValue::UInt64(Some(1)), _) => true,
-        Expr::Literal(ScalarValue::Float32(Some(v)), _) if *v == 1. => true,
-        Expr::Literal(ScalarValue::Float64(Some(v)), _) if *v == 1. => true,
-        Expr::Literal(ScalarValue::Decimal128(Some(v), _p, s), _) => {
-            *s >= 0
-                && POWS_OF_TEN
-                    .get(*s as usize)
-                    .map(|x| x == v)
-                    .unwrap_or_default()
-        }
-        Expr::Literal(ScalarValue::Decimal256(Some(v), _p, s), _) => {
-            *s >= 0
-                && match i256::from(10).checked_pow(*s as u32) {
-                    Some(res) => res == *v,
-                    None => false,
-                }
-        }
-        _ => false,
+    if let Expr::Literal(sv, _) = s
+        && sv.data_type().is_numeric()
+        // there are edge cases like negative scale decimals not being able to
+        // create a one value so this can fail
+        && let Ok(one) = ScalarValue::new_one(&sv.data_type())

Review Comment:
   I went back and found the PRs I remembered:
   - #20768
   - #21219
   
   Seems like the issue was more about aggregating multiple `ScalarValue` 
instances, which used to fallback to `arrow` based aggregations that required 
heap allocation, so I think this is perfectly fine.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to