This is an automated email from the ASF dual-hosted git repository.

agrove pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git


The following commit(s) were added to refs/heads/main by this push:
     new c2a6b8ae7 feat: Cast date to Numeric (No Op) (#3544)
c2a6b8ae7 is described below

commit c2a6b8ae75eb1b4bbba913e88c8e76b9c6d2e4f9
Author: B Vadlamani <[email protected]>
AuthorDate: Tue Feb 17 20:50:16 2026 -0800

    feat: Cast date to Numeric (No Op) (#3544)
---
 .../org/apache/comet/expressions/CometCast.scala   | 47 ++++++++++++++++-----
 .../scala/org/apache/comet/CometCastSuite.scala    | 48 +++++++++++-----------
 2 files changed, 60 insertions(+), 35 deletions(-)

diff --git a/spark/src/main/scala/org/apache/comet/expressions/CometCast.scala 
b/spark/src/main/scala/org/apache/comet/expressions/CometCast.scala
index 000cc5fd4..8cbe76a19 100644
--- a/spark/src/main/scala/org/apache/comet/expressions/CometCast.scala
+++ b/spark/src/main/scala/org/apache/comet/expressions/CometCast.scala
@@ -67,16 +67,36 @@ object CometCast extends CometExpressionSerde[Cast] with 
CometExprShim {
       case _: Literal =>
         exprToProtoInternal(Literal.create(cast.eval(), cast.dataType), 
inputs, binding)
       case _ =>
-        val childExpr = exprToProtoInternal(cast.child, inputs, binding)
-        if (childExpr.isDefined) {
-          castToProto(cast, cast.timeZoneId, cast.dataType, childExpr.get, 
evalMode(cast))
+        if (isAlwaysCastToNull(cast.child.dataType, cast.dataType, 
evalMode(cast))) {
+          exprToProtoInternal(Literal.create(null, cast.dataType), inputs, 
binding)
         } else {
-          withInfo(cast, cast.child)
-          None
+          val childExpr = exprToProtoInternal(cast.child, inputs, binding)
+          if (childExpr.isDefined) {
+            castToProto(cast, cast.timeZoneId, cast.dataType, childExpr.get, 
evalMode(cast))
+          } else {
+            withInfo(cast, cast.child)
+            None
+          }
         }
     }
   }
 
+//  Some casts like date -> int/byte / long are always null. Terminate early 
in planning
+  private def isAlwaysCastToNull(
+      fromType: DataType,
+      toType: DataType,
+      evalMode: CometEvalMode.Value): Boolean = {
+    (fromType, toType) match {
+      case (
+            DataTypes.DateType,
+            DataTypes.BooleanType | DataTypes.ByteType | DataTypes.ShortType |
+            DataTypes.IntegerType | DataTypes.LongType | DataTypes.FloatType |
+            DataTypes.DoubleType | _: DecimalType) if evalMode == 
CometEvalMode.LEGACY =>
+        true
+      case _ => false
+    }
+  }
+
   /**
    * Wrap an already serialized expression in a cast.
    */
@@ -168,7 +188,7 @@ object CometCast extends CometExpressionSerde[Cast] with 
CometExprShim {
           }
         }
         Compatible()
-      case (DataTypes.DateType, toType) => canCastFromDate(toType)
+      case (DataTypes.DateType, toType) => canCastFromDate(toType, evalMode)
       case _ => unsupported(fromType, toType)
     }
   }
@@ -355,11 +375,16 @@ object CometCast extends CometExpressionSerde[Cast] with 
CometExprShim {
     case _ => Unsupported(Some(s"Cast from DecimalType to $toType is not 
supported"))
   }
 
-  private def canCastFromDate(toType: DataType): SupportLevel = toType match {
-    case DataTypes.TimestampType =>
-      Compatible()
-    case _ => Unsupported(Some(s"Cast from DateType to $toType is not 
supported"))
-  }
+  private def canCastFromDate(toType: DataType, evalMode: 
CometEvalMode.Value): SupportLevel =
+    toType match {
+      case DataTypes.TimestampType =>
+        Compatible()
+      case DataTypes.BooleanType | DataTypes.ByteType | DataTypes.ShortType |
+          DataTypes.IntegerType | DataTypes.LongType | DataTypes.FloatType |
+          DataTypes.DoubleType | _: DecimalType if evalMode == 
CometEvalMode.LEGACY =>
+        Compatible()
+      case _ => Unsupported(Some(s"Cast from DateType to $toType is not 
supported"))
+    }
 
   private def unsupported(fromType: DataType, toType: DataType): Unsupported = 
{
     Unsupported(Some(s"Cast from $fromType to $toType is not supported"))
diff --git a/spark/src/test/scala/org/apache/comet/CometCastSuite.scala 
b/spark/src/test/scala/org/apache/comet/CometCastSuite.scala
index 9fc9a1657..326904d56 100644
--- a/spark/src/test/scala/org/apache/comet/CometCastSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometCastSuite.scala
@@ -960,44 +960,44 @@ class CometCastSuite extends CometTestBase with 
AdaptiveSparkPlanHelper {
 
   // CAST from DateType
 
-  ignore("cast DateType to BooleanType") {
-    // Arrow error: Cast error: Casting from Date32 to Boolean not supported
-    castTest(generateDates(), DataTypes.BooleanType)
+  // Date to Boolean/Byte/Short/Int/Long/Float/Double/Decimal casts always 
return NULL
+  // in LEGACY mode. In ANSI and TRY mode, Spark throws AnalysisException at
+  // query parsing time. Hence, ANSI and Try mode are disabled in tests
+
+  test("cast DateType to BooleanType") {
+    castTest(generateDates(), DataTypes.BooleanType, testAnsi = false, testTry 
= false)
   }
 
-  ignore("cast DateType to ByteType") {
-    // Arrow error: Cast error: Casting from Date32 to Int8 not supported
-    castTest(generateDates(), DataTypes.ByteType)
+  test("cast DateType to ByteType") {
+    castTest(generateDates(), DataTypes.ByteType, testAnsi = false, testTry = 
false)
   }
 
-  ignore("cast DateType to ShortType") {
-    // Arrow error: Cast error: Casting from Date32 to Int16 not supported
-    castTest(generateDates(), DataTypes.ShortType)
+  test("cast DateType to ShortType") {
+    castTest(generateDates(), DataTypes.ShortType, testAnsi = false, testTry = 
false)
   }
 
-  ignore("cast DateType to IntegerType") {
-    // input: 2345-01-01, expected: null, actual: 3789391
-    castTest(generateDates(), DataTypes.IntegerType)
+  test("cast DateType to IntegerType") {
+    castTest(generateDates(), DataTypes.IntegerType, testAnsi = false, testTry 
= false)
   }
 
-  ignore("cast DateType to LongType") {
-    // input: 2024-01-01, expected: null, actual: 19723
-    castTest(generateDates(), DataTypes.LongType)
+  test("cast DateType to LongType") {
+    castTest(generateDates(), DataTypes.LongType, testAnsi = false, testTry = 
false)
   }
 
-  ignore("cast DateType to FloatType") {
-    // Arrow error: Cast error: Casting from Date32 to Float32 not supported
-    castTest(generateDates(), DataTypes.FloatType)
+  test("cast DateType to FloatType") {
+    castTest(generateDates(), DataTypes.FloatType, testAnsi = false, testTry = 
false)
   }
 
-  ignore("cast DateType to DoubleType") {
-    // Arrow error: Cast error: Casting from Date32 to Float64 not supported
-    castTest(generateDates(), DataTypes.DoubleType)
+  test("cast DateType to DoubleType") {
+    castTest(generateDates(), DataTypes.DoubleType, testAnsi = false, testTry 
= false)
   }
 
-  ignore("cast DateType to DecimalType(10,2)") {
-    // Arrow error: Cast error: Casting from Date32 to Decimal128(10, 2) not 
supported
-    castTest(generateDates(), DataTypes.createDecimalType(10, 2))
+  test("cast DateType to DecimalType(10,2)") {
+    castTest(
+      generateDates(),
+      DataTypes.createDecimalType(10, 2),
+      testAnsi = false,
+      testTry = false)
   }
 
   test("cast DateType to StringType") {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to