comphead commented on code in PR #7750:
URL: https://github.com/apache/datafusion-comet/pull/7750#discussion_r4743953204


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -604,9 +605,10 @@ object QueryPlanSerde extends Logging with CometExprShim 
with CometTypeShim {
       allowDuplicateStructFieldNames: Boolean = true): Boolean = {
     def supported(dt: DataType): Boolean = dt match {
       case _: ByteType | _: ShortType | _: IntegerType | _: LongType | _: 
FloatType |
-          _: DoubleType | _: BinaryType | _: TimestampType | _: 
TimestampNTZType |
-          _: DecimalType | _: DateType | _: BooleanType | _: NullType =>
+          _: DoubleType | _: BinaryType | _: TimestampType | _: 
TimestampNTZType | _: DateType |
+          _: BooleanType | _: NullType =>
         true
+      case decimal: DecimalType => 
DataTypeSupport.isArrowDecimalScaleSupported(decimal)

Review Comment:
   The rule now sits in `supportedDataType`, in 
`DataTypeSupport.isTypeSupported` and in the array arm of 
`CometLiteral.getSupportLevel`, but `serializeDataType` below still accepts the 
type. `CometCast.castToProto` (`CometCast.scala` line 198) gates only on 
`serializeDataType(dt)`, and so do `CometAttributeReference.getSupportLevel` 
and the native scan checks (`CometNativeScan.scala` lines 221 and 296). All of 
those already treat `None` as unsupported. Returning `None` from 
`serializeDataType` for a `DecimalType` whose scale is not a valid byte would 
cover them in one place, nested types included, and make the other additions 
unnecessary. https://github.com/apache/datafusion-comet/pull/7716 adds the same 
rule to `CometCast.isSupported` and to the codegen `isSupportedDataType`, so it 
would shrink that PR too. I did not run this. Callers that use `.get` after 
their own gate would need a look.



##########
spark/src/test/scala/org/apache/comet/CometNativeCastSuite.scala:
##########
@@ -2552,6 +2553,35 @@ class CometNativeCastSuite
     }
   }
 
+  test("decimal scales outside Arrow's signed-byte range fall back to Spark") {
+    withSQLConf(
+      "spark.sql.legacy.allowNegativeScaleOfDecimal" -> "true",
+      CometConf.COMET_CONVERT_FROM_RDD_ENABLED.key -> "true") {
+      val decimalType = DataTypes.createDecimalType(10, -256)
+      val schema = StructType(Seq(StructField("d", decimalType)))
+      val row = Row(new java.math.BigDecimal("1E+256"))
+      val input = 
spark.createDataFrame(spark.sparkContext.parallelize(Seq(row), 1), schema)
+
+      val fallbackReasons = ListBuffer.empty[String]
+      assert(!CometSparkToColumnarExec.isSchemaSupported(schema, 
fallbackReasons))
+      assert(
+        fallbackReasons.exists(_.contains("scale is outside Arrow's supported 
range")),
+        fallbackReasons.toString)
+      assert(input.collect().toSeq == Seq(row))
+
+      val literal = Literal.create(new java.math.BigDecimal("1E+256"), 
decimalType)
+      assert(CometLiteral.getSupportLevel(literal).isInstanceOf[Unsupported])
+
+      Seq(-129, -128, 0, 38).foreach { scale =>
+        val dataType = DataTypes.createDecimalType(38, scale)
+        assert(
+          QueryPlanSerde.supportedDataType(dataType) ==
+            (scale >= Byte.MinValue && scale <= Byte.MaxValue),
+          s"Unexpected support decision for $dataType")
+      }
+    }
+  }

Review Comment:
   This test is in the cast suite but does not exercise a cast. Its 
`CometSparkToColumnarExec.isSchemaSupported` assertion repeats the new 
`CometNativeShuffleSuite` test, which already covers both sinks. The `Seq(-129, 
-128, 0, 38)` loop computes its expected value with the same expression as 
`isArrowDecimalScaleSupported`, so it cannot fail independently. 
`input.collect()` runs a bare RDD scan with no Comet operator on top (I read it 
and did not run it), so it does not show the sink conversion falling back. 
Suggest keeping the shuffle suite test, moving the literal assertion next to 
the year-month array test in `CometLiteralSuite`, and writing the boundary 
cases as explicit expected values (`-129` false, `-128` true).



##########
spark/src/main/scala/org/apache/comet/serde/literals.scala:
##########
@@ -49,7 +49,9 @@ object CometLiteral extends CometExpressionSerde[Literal] 
with CometTypeShim wit
           expr.dataType,
           allowComplex = true,
           allowDuplicateStructFieldNames = false)
-      case ArrayType(elementType, _) => 
listLiteralElementSupported(elementType)
+      case arrayType: ArrayType =>
+        listLiteralElementSupported(arrayType.elementType) &&
+        supportedDataType(arrayType, allowComplex = true, allowIntervals = 
true)

Review Comment:
   This arm needs the `allowIntervals = true` workaround only because 
`supportedDataType` is applied to the whole array type. Putting the check in 
`listLiteralElementSupported` (`case d: DecimalType => 
DataTypeSupport.isArrowDecimalScaleSupported(d)`) gives the same result for 
nested arrays, leaves the `ArrayType` arm as it was, and avoids a second gate 
with different type rules than the element check next to it.



##########
spark/src/main/scala/org/apache/comet/DataTypeSupport.scala:
##########
@@ -84,6 +89,10 @@ object DataTypeSupport {
   val MAP_KEY = "map key"
   val MAP_VALUE = "map value"
 
+  /** Arrow's Decimal128 scale is a signed 8-bit integer. */
+  def isArrowDecimalScaleSupported(dataType: DecimalType): Boolean =
+    dataType.scale >= Byte.MinValue && dataType.scale <= Byte.MaxValue

Review Comment:
   Spark's `DecimalType` constructor requires `scale <= precision <= 38` 
(checked in `DecimalType.scala` at v3.5.8), so `scale <= Byte.MaxValue` is 
always true and only the lower bound can fail. `dataType.scale.isValidByte` 
states the rule directly and is what 
https://github.com/apache/datafusion-comet/pull/7716 uses.



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