jayzhan211 commented on code in PR #27474:
URL: https://github.com/apache/datafusion/pull/27474#discussion_r4656167005
##########
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:
A `DECIMAL(p, s)` column with `s >= 19` compared with an integer literal now
goes through `Decimal256`, and the cast stays on the column: `WHERE d <> 5` on
a `DECIMAL(38, 24)` column runs `CAST(d AS Decimal256(44, 24)) != 5.0…`, and
its `FilterExec` takes 80–99 ms on 40M rows against 17–20 ms on main, where the
plan is `d != 5.0…`. The literal fits the column's own type, so letting
unwrap-cast accept a `Decimal256` literal brings back the old plan and time
(17–19 ms), while integer columns still get the wider type. With this change
the `expr-common` and optimizer unit tests and the full sqllogictest suite pass:
```diff
--- a/datafusion/expr-common/src/casts.rs
+++ b/datafusion/expr-common/src/casts.rs
@@ fn is_decimal_type(dt: &DataType) -> bool {
matches!(
dt,
- DataType::Decimal128(_, _)
+ DataType::Decimal256(_, _)
+ | DataType::Decimal128(_, _)
| DataType::Decimal64(_, _)
| DataType::Decimal32(_, _)
)
@@ fn decimal_precision_scale(dt: &DataType) -> (u8, i8) {
match dt {
- DataType::Decimal128(p, s)
+ DataType::Decimal256(p, s)
+ | DataType::Decimal128(p, s)
| DataType::Decimal64(p, s)
| DataType::Decimal32(p, s) => (*p, *s),
@@ fn is_supported_numeric_type(data_type: &DataType) -> bool {
| DataType::Decimal128(_, _)
+ | DataType::Decimal256(_, _)
| DataType::Timestamp(_, _)
@@ fn try_cast_numeric_literal(
+ ScalarValue::Decimal256(Some(v), _, scale) => {
+ let v = v.to_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
+ }
+ }
ScalarValue::Decimal128(Some(v), _, scale) => {
```
<details>
<summary>Unit test for casts.rs, and the queries behind the numbers</summary>
```rs
#[test]
fn test_cast_predicate_preimage_decimal256_literal() {
use arrow::datatypes::i256;
// `DECIMAL(38, 20) > 5` coerces both sides to `Decimal256(40, 20)`
let five = 5 * 10_i128.pow(20);
assert_preimage_exact(
&DataType::Decimal128(38, 20),
&DataType::Decimal256(40, 20),
Operator::Gt,
&ScalarValue::Decimal256(Some(i256::from_i128(five)), 40, 20),
ScalarValue::Decimal128(Some(five), 38, 20),
);
// 5 has no room in `Decimal128(38, 38)`, so the cast stays
let five =
i256::from_i128(5).wrapping_mul(i256::from_i128(10).wrapping_pow(38));
assert_preimage_none(
&DataType::Decimal128(38, 38),
&DataType::Decimal256(58, 38),
Operator::Lt,
&ScalarValue::Decimal256(Some(five), 58, 38),
);
// 10^70 does not fit in i128, so the cast stays instead of overflowing
assert_preimage_none(
&DataType::Decimal128(38, 20),
&DataType::Decimal256(76, 70),
Operator::Gt,
&ScalarValue::Decimal256(Some(i256::ZERO), 76, 70),
);
}
```
```sql
-- release-nonlto datafusion-cli
SET datafusion.execution.parquet.max_row_group_size = 1000000;
COPY (SELECT CAST(value AS DECIMAL(38, 20)) / 1000 AS d FROM
generate_series(1, 10000000) ORDER BY value)
TO 'wide.parquet' STORED AS PARQUET;
SET datafusion.execution.target_partitions = 1;
CREATE EXTERNAL TABLE w STORED AS PARQUET LOCATION 'wide.parquet';
CREATE TABLE m AS SELECT d FROM w UNION ALL SELECT d FROM w UNION ALL SELECT
d FROM w UNION ALL SELECT d FROM w;
EXPLAIN ANALYZE SELECT count(*) FROM m WHERE d <> 5;
-- FilterExec 80-99 ms
EXPLAIN ANALYZE SELECT count(*) FROM m WHERE d <> CAST(5 AS DECIMAL(38,
24)); -- 17-20 ms, main's plan
```
</details>
--
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]