sunchao opened a new issue, #7748:
URL: https://github.com/apache/datafusion-comet/issues/7748

   ### Describe the bug
   
   With `spark.sql.legacy.allowNegativeScaleOfDecimal=true`, Spark accepts 
`DecimalType(10, -256)`. Comet accepts the type and writes the original scale 
into an `int32` protobuf field. In Rust, `to_arrow_datatype` builds 
`Decimal128` using `info.scale as i8`. This turns `-256` into `0` and `-129` 
into `127`. [Spark scale 
check](https://github.com/apache/spark/blob/v4.1.3/sql/api/src/main/scala/org/apache/spark/sql/types/DecimalType.scala#L160-L164)
 · [Comet type 
check](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala#L605-L609)
 · [Scale 
serialization](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala#L684-L690)
 · [Protobuf 
field](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/proto/src/proto/types.proto#L79-L82)
 · [Arrow type 
 
conversion](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/core/src/execution/serde.rs#L89-L93)
   
   I traced two paths on `main` at `f141d9010e89`. In both, the expression 
already has a decimal type when Comet serializes it, so the cast-target check 
in #7716 cannot catch the scale. [Review requesting this 
follow-up](https://github.com/apache/datafusion-comet/pull/7716#pullrequestreview-6000937129)
   
   - **Folded decimal literal.** `CometCast.convert` evaluates a cast with a 
literal child in Spark and serializes the resulting literal. `CometLiteral` 
writes the unscaled integer and its declared type. Rust pairs the same integer 
with the narrowed scale in `ScalarValue::Decimal128`. For an unscaled integer 
of `1`, the Spark decimal represents `10^256` at scale `-256`. The Rust scalar 
represents `1` at scale `0`. 
[Folding](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/expressions/CometCast.scala#L148-L155)
 · [Literal 
serialization](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/serde/literals.scala#L109-L137)
 · [Type 
decoding](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/core/src/execution/planner/literal.rs#L31-L35)
 · [Scalar construction](https://github.com/apache/dataf
 
usion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/core/src/execution/planner/literal.rs#L104-L115)
   - **Existing decimal column.** `CometSink.convert` checks the output 
attributes of the input operator and serializes their types into `Scan.fields`. 
The Rust planner maps those fields through `to_arrow_datatype`, so the planned 
type of a `DecimalType(10, -256)` column becomes `Decimal128(10, 0)` before the 
planner imports the input stream. [Scan field 
serialization](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/serde/operator/CometSink.scala#L53-L84)
 · [Planned field 
types](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/core/src/execution/planner.rs#L2029-L2030)
 · [Stream 
import](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/native/core/src/execution/planner.rs#L2051-L2063)
   
   These findings are from source inspection. The end-to-end query outcomes 
still need runtime verification. In particular, Arrow can reject the original 
column scale during stream import because it parses the scale as an `i8`. 
[Arrow schema 
import](https://github.com/apache/arrow-rs/blob/59.3.2/arrow-schema/src/ffi.rs#L526-L548)
   
   ### Expected behavior
   
   When a literal or input field has a scale below `-128`, Comet should fall 
back to Spark during planning with a reason that identifies the scale. The 
result should preserve Spark's declared type and value. Scale `-128` fits in a 
signed byte and should remain eligible wherever the operation supports it.
   
   ### Suggested regression coverage
   
   With negative scales enabled, project `lit("1e256").cast(DecimalType(10, 
-256))` alongside a column from a nonempty input to exercise literal folding. 
For the column case, use an RDD-backed DataFrame with an explicit 
`DecimalType(10, -256)` field containing `new java.math.BigDecimal("1E+256")` 
and enable `spark.comet.convert.rdd.enabled`. Check the fallback reason and 
compare the schema and value with Spark. Include `-129` and `-128` to exercise 
the boundary. [RDD conversion 
setting](https://github.com/apache/datafusion-comet/blob/f141d9010e89423f5e0387d2e9e1990e3c4b684b/spark/src/main/scala/org/apache/comet/CometConf.scala#L310-L316)
   


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