cool9850311 commented on code in PR #27474:
URL: https://github.com/apache/datafusion/pull/27474#discussion_r4658153644


##########
datafusion/expr-common/src/type_coercion/binary.rs:
##########
@@ -1269,25 +1227,39 @@ fn get_common_decimal_type(
     other_type: &DataType,
 ) -> Option<DataType> {
     use arrow::datatypes::DataType::*;
-    match decimal_type {
-        Decimal32(_, _) => {
-            let other_decimal_type = 
coerce_numeric_type_to_decimal32(other_type)?;
-            get_wider_decimal_type(decimal_type, &other_decimal_type)
-        }
-        Decimal64(_, _) => {
-            let other_decimal_type = 
coerce_numeric_type_to_decimal64(other_type)?;
-            get_wider_decimal_type(decimal_type, &other_decimal_type)
-        }
-        Decimal128(_, _) => {
-            let other_decimal_type = 
coerce_numeric_type_to_decimal128(other_type)?;
-            get_wider_decimal_type(decimal_type, &other_decimal_type)
-        }
-        Decimal256(_, _) => {
-            let other_decimal_type = 
coerce_numeric_type_to_decimal256(other_type)?;
-            get_wider_decimal_type(decimal_type, &other_decimal_type)
-        }
-        _ => None,
+
+    // How many digits an integer needs is known exactly, so a common type with
+    // no room for them is one no such value casts into.
+    if let Some((integer_precision, integer_scale)) =
+        integer_decimal_precision(other_type)
+    {
+        let (precision, scale) = decimal_precision_and_scale(decimal_type)?;
+        let common_precision = required_decimal_precision(
+            precision,
+            scale,
+            integer_precision,
+            integer_scale,
+        );
+        let common_scale = scale.max(integer_scale);
+        if let Some(variant) = narrowest_decimal_variant(decimal_type, 
common_precision) {

Review Comment:
   Thank you -- this reproduces here, and your diff is the right place for it. 
My numbers, on
   10M rows of `DECIMAL(38, 24)` unioned four times (40M), `target_partitions = 
1`,
   `release-nonlto`, with the same binary for both rows so the plan is the only 
difference:
   
   | query | plan | `FilterExec` `elapsed_compute` |
   | --- | --- | --- |
   | `d <> 5` | `CAST(d@0 AS Decimal256(44, 24)) != 5.0…` | 112.95 ms |
   | `d <> CAST(5 AS DECIMAL(38,24))` | `d@0 != 5.0…` | 31.89 ms |
   
   On the pushed commit `d <> 5` plans as `d@0 != 5.0…` and takes 24-27 ms, 
against 23-25 ms
   for that same plan on the base.
   
   An `IN` list is the same rewrite and comes back with it: `d IN (4, 5)` kept 
the cast as
   `CAST(d@0 AS Decimal256(44, 24)) IN (SET) (…)` and is an unwrapped `OR` 
chain again.
   
   I took the diff as is, with one addition: the comment above the `Decimal256` 
rejection in
   `test_decimal_scale_up_preimages` said the scale-only rule rejects 
`Decimal256`, which
   after this holds only for a `Decimal256` target, so it says that.
   
   Nothing pinned this, so your three cases are now
   `test_cast_predicate_preimage_decimal256_literal`, plus a value beyond 
`i128` for the
   `to_i128` guard and the integer side of the same comparison, which is a 
different arm of
   the target match. `simplify_expr.slt` gets a `decimal(38, 24)` column in its
   decimal-unwrap table whose `<> 5` and `IN (4, 5)` plans must carry no cast. 
Without the
   `casts.rs` change the unit test fails alone (1 of 251) and both explains 
fail with the
   `Decimal256` cast in the plan.
   
   This is the second commit, `8d4db4cac`, so the change since your review is 
those three
   files. The branch is rebased onto main, so it also sits on top of #27148, 
which landed in
   `casts.rs` meanwhile.
   



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