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


##########
datafusion/sqllogictest/test_files/simplify_expr.slt:
##########
@@ -377,6 +378,41 @@ physical_plan
 01)FilterExec: price36@0 = 4.0000
 02)--DataSourceExec: partitions=1, partition_sizes=[1]
 
+# An integer literal is an `Int64`, so a scale of 19 or more needs a
+# `Decimal256` common type, whose literal must still unwrap to the column.
+query R
+select price38_24 from decimal_prices where price38_24 <> 5 order by 
price38_24;
+----
+-1.000000000000000000000000
+0.000000000000000000000000
+4.000000000000000000000000
+
+query TT
+explain select price38_24 from decimal_prices where price38_24 <> 5;

Review Comment:
   @cool9850311 thanks for the quick turnaround; plain columns unwrap again. 
The same cast still stays on a dictionary-encoded column, though: with `d` as 
`Dictionary(Int32, Decimal128(38, 24))`, `d = 5` plans as `CAST(d AS 
Decimal256(44, 24)) = 5.0…`, where main plans `d = Dictionary(Int32, 
Decimal128(5.0…, 38, 24))`, and the same goes for `IN (4, 5)`. The cause is 
that `try_cast_dictionary` only wraps a literal whose type equals the 
dictionary's value type. This arm fixes it, and the full sqllogictest suite 
plus the expr-common, optimizer and physical-expr tests pass with it:
   
   ```diff
            (_, DataType::Dictionary(index_type, inner_type))
                if **inner_type == lit_value_type =>
            {
                ScalarValue::Dictionary(index_type.clone(), 
Box::new(lit_value.clone()))
            }
   +        (_, DataType::Dictionary(index_type, inner_type)) => 
ScalarValue::Dictionary(
   +            index_type.clone(),
   +            Box::new(try_cast_numeric_literal(lit_value, inner_type)?),
   +        ),
   ```
   
   It changes what `try_cast_literal_to_type` documents for dictionaries of 
every value type, so it's fine to handle in a follow-up PR with its own tests.
   
   <details>
   <summary>Repro</summary>
   
   ```sql
   create table td as select arrow_cast(column1, 'Dictionary(Int32, 
Decimal128(38, 24))') as d
   from (values (cast(4 as decimal(38,24))), (cast(5 as decimal(38,24))), 
(NULL));
   explain select d from td where d = 5;
   explain select d from td where d in (4, 5);
   ```
   </details>



##########
datafusion/expr-common/src/casts.rs:
##########
@@ -821,6 +824,19 @@ fn try_cast_numeric_literal(
                 None
             }
         }
+        ScalarValue::Decimal256(Some(v), _, scale) => {

Review Comment:
   Optional cleanup: this arm is now the fourth copy of the rescale body. With 
one helper, each decimal arm becomes a single line (47 fewer lines). Using 
`checked_pow` in the helper also means a bad scale returns `None` in the older 
arms instead of overflowing:
   
   ```diff
   +/// Rescales the unscaled decimal `v` at `scale` to the scale whose 
multiplier is
   +/// `mul`, or `None` if that drops digits or overflows `i128`.
   +fn rescale_decimal_literal(v: i128, scale: i8, mul: i128) -> Option<i128> {
   +    let lit_scale_mul = 10_i128.checked_pow(u32::try_from(scale).ok()?)?;
   +    if mul >= lit_scale_mul {
   +        v.checked_mul(mul / lit_scale_mul)
   +    } else if v % (lit_scale_mul / mul) == 0 {
   +        Some(v / (lit_scale_mul / mul))
   +    } else {
   +        None
   +    }
   +}
   ```
   ```rs
           ScalarValue::Decimal32(Some(v), _, scale) => {
               rescale_decimal_literal(*v as i128, *scale, mul)
           }
           ScalarValue::Decimal64(Some(v), _, scale) => {
               rescale_decimal_literal(*v as i128, *scale, mul)
           }
           ScalarValue::Decimal128(Some(v), _, scale) => {
               rescale_decimal_literal(*v, *scale, mul)
           }
           ScalarValue::Decimal256(Some(v), _, scale) => {
               rescale_decimal_literal(v.to_i128()?, *scale, mul)
           }
   ```



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