TechWizard9999 opened a new issue, #430:
URL: https://github.com/apache/datafusion-spark/issues/430
### Describe the bug
For a DECIMAL input, `bround` declares one result type and returns another
when the scale is a constant that is not a literal yet, such as `CAST(1 AS
INT)`, `1::int` or `1 + 0`. With the default optimizer, an expression around it
then gives a wrong result, fails, or panics. `bround(CAST('2.25' AS
DECIMAL(3,2)), CAST(1 AS INT))` on its own is right.
`return_field_from_args` cannot read such a scale, so it falls back to the
input scale
([bround.rs#L143-L149](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/spark/src/function/math/bround.rs#L143-L149))
and declares `Decimal128(4, 2)`. Type coercion of the outer expression uses
that type. Then `simplify_expressions` folds the cast, and the same call
returns `Decimal128(3, 1)`. Nothing coerces the outer expression again.
### To Reproduce
```sql
SELECT greatest(bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)),
CAST('2.25' AS DECIMAL(3,2))) AS cast_scale,
greatest(bround(CAST('2.25' AS DECIMAL(3,2)), 1), CAST('2.25' AS
DECIMAL(3,2))) AS literal_scale;
SELECT c FROM (VALUES (CAST('2.25' AS DECIMAL(3,2))), (CAST('2.35' AS
DECIMAL(3,2)))) AS t(c)
WHERE bround(c, CAST(1 AS INT)) IN (CAST('0.22' AS DECIMAL(4,2)),
CAST('9.99' AS DECIMAL(4,2)));
SELECT bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)) = CAST('2.2' AS
DECIMAL(3,1));
SELECT array(bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)),
CAST('1.23' AS DECIMAL(4,2)));
```
datafusion-spark returns:
```
2.3 2.25
2.25 (one row, but bround gives 2.2 and 2.4, so no row should match)
Arrow error: Invalid argument error: Invalid comparison operation:
Decimal128(3, 1) == Decimal128(4, 2)
panicked at arrow-data-60.1.0\src\transform\mod.rs:464:13:
assertion `left == right` failed: Arrays with inconsistent types passed to
MutableArrayData
left: Decimal128(3, 1)
right: Decimal128(4, 2)
```
<details>
<summary>sqllogictest file with Spark's results (fails today)</summary>
Save it as `crates/sqllogictest/test_files/math/bround_cast_scale.slt` and
run `cargo test -p datafusion-spark-sqllogictest -- bround_cast_scale`. Today
simplify mode fails at the first record and evaluate mode passes.
```
query RR
SELECT greatest(bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)),
CAST('2.25' AS DECIMAL(3,2))) AS cast_scale, greatest(bround(CAST('2.25' AS
DECIMAL(3,2)), 1), CAST('2.25' AS DECIMAL(3,2))) AS literal_scale;
----
2.25 2.25
query R
SELECT c FROM (VALUES (CAST('2.25' AS DECIMAL(3,2))), (CAST('2.35' AS
DECIMAL(3,2)))) AS t(c) WHERE bround(c, CAST(1 AS INT)) IN (CAST('0.22' AS
DECIMAL(4,2)), CAST('9.99' AS DECIMAL(4,2)));
----
query B
SELECT bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)) = CAST('2.2' AS
DECIMAL(3,1)) AS eq;
----
true
query ?
SELECT array(bround(CAST('2.25' AS DECIMAL(3,2)), CAST(1 AS INT)),
CAST('1.23' AS DECIMAL(4,2))) AS arr;
----
[2.20, 1.23]
```
</details>
### Expected behavior
As in Spark:
```
2.25 2.25
(no rows)
true
[2.20,1.23]
```
### Spark versions
Spark 4.2.0, from the source; not run on a live Spark. `RoundBase` requires
a foldable scale
([mathExpressions.scala#L1588-L1603](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala#L1588-L1603)),
evaluates it once
([#L1608-L1609](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala#L1608-L1609))
and computes `dataType` from that value
([#L1567-L1584](https://github.com/apache/spark/blob/v4.2.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala#L1567-L1584)).
So `bround` of a `DECIMAL(3,2)` with scale `CAST(1 AS INT)` is a
`DECIMAL(3,1)` from analysis on. 3.5.8, 4.0.4 and 4.1.3 have the same logic.
### datafusion-spark version
main at ab9d308
### Additional context
Reproduced on main ab9d308 with the sqllogictest runner in simplify mode,
and with `datafusion-spark-cli` with ANSI mode on and off. In evaluate mode,
without `simplify_expressions`, all four statements return Spark's values,
because there the call keeps `Decimal128(4, 2)` (the limitation that
[bround.slt#L153-L158](https://github.com/apache/datafusion-spark/blob/ab9d308e4ee504dbd3a9731abfc3519f937c86d1/crates/sqllogictest/test_files/math/bround.slt#L153-L158)
documents).
The panic comes from an `assert_eq!` in arrow-data, so it is not limited to
debug builds.
#384 describes this fallback as a missing foldable check. This issue shows
that it also gives wrong results. A test written with explicit casts, as the
sqllogictest README asks, can hit it.
cc @andygrove
--
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]