You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "mbutrovich (via GitHub)" <gi...@apache.org> on 2024/09/04 21:05:54 UTC

[PR] date_add expression [datafusion-comet]

mbutrovich opened a new pull request, #910:
URL: https://github.com/apache/datafusion-comet/pull/910

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Does not close an individual PR, but addresses an item in #858.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   New DateAdd expression in QueryPlanSerde. `date_add` has two arguments: a date and an int32 representing the number of days. DateFusion does not fully support `date_add` (see: https://github.com/apache/datafusion/issues/6876), but based on the constraints of the serialized Spark plan, this PR directly converts the second argument (Literal int32) to a DF interval literal and constructs a BinaryExpr to add the two values ([which DF does support](https://github.com/apache/datafusion/blob/91b1d2bfe8f603df94e846b91d8475a0af2e5240/datafusion/expr-common/src/type_coercion/binary.rs#L151))
   
   ## How are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   Added a test case for `date_add`.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add expression [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#issuecomment-2330373838

   I'll drive into this test failure tomorrow. My assumption of the second expression for date_add always being a Literal does not appear sound.
   ```
   Cause: org.apache.comet.CometNativeException: General execution error with reason: Not implemented: DateAdd { left: Some(Expr { expr_struct: Some(Literal(Literal { datatype: Some(DataType { type_id: Date, type_info: None }), is_null: false, value: Some(IntVal(0)) })) }), right: Some(Expr { expr_struct: Some(Cast(Cast { child: Some(Expr { expr_struct: Some(Remainder(Remainder { left: Some(Expr { expr_struct: Some(Bound(BoundReference { index: 0, datatype: Some(DataType { type_id: Int64, type_info: None }) })) }), right: Some(Expr { expr_struct: Some(If(IfExpr { if_expr: Some(Expr { expr_struct: Some(Eq(Equal { left: Some(Expr { expr_struct: Some(Literal(Literal { datatype: Some(DataType { type_id: Int64, type_info: None }), is_null: false, value: Some(LongVal(4)) })) }), right: Some(Expr { expr_struct: Some(Literal(Literal { datatype: Some(DataType { type_id: Int64, type_info: None }), is_null: false, value: Some(LongVal(0)) })) }) })) }), true_expr: Some(Expr { expr_struct: Some(L
 iteral(Literal { datatype: Some(DataType { type_id: Int64, type_info: None }), is_null: true, value: None })) }), false_expr: Some(Expr { expr_struct: Some(Literal(Literal { datatype: Some(DataType { type_id: Int64, type_info: None }), is_null: false, value: Some(LongVal(4)) })) }) })) }), fail_on_error: false, return_type: Some(DataType { type_id: Int64, type_info: None }) })) }), datatype: Some(DataType { type_id: Int32, type_info: None }), timezone: "America/Los_Angeles", eval_mode: Legacy, allow_incompat: false })) }) }.
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747670792


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,40 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` and `date_sub` expressions, which assumes days for the second
+/// argument, but we cannot directly add that to a Date32. We generate an IntervalDayTime from the
+/// second argument and use DataFusion's interface to apply Arrow's operators.
+fn spark_date_arithmetic(
+    args: &[ColumnarValue],
+    op: impl Fn(&dyn Datum, &dyn Datum) -> Result<ArrayRef, ArrowError>,
+) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {

Review Comment:
   Hmm, I think Spark `DateAdd`'s second parameter could be IntegerType, ShortType, ByteType, i.e., Int32, Int16, Int8 in arrow types.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747193482


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,32 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` expression, which assumes days for the second argument, but we
+/// cannot directly add that to a Date32. We generate an IntervalDayTime from the second argument,
+/// and use DataFusion's interface to apply Arrow's add operator.
+pub fn spark_date_add(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {
+        let interval = IntervalDayTime::new(*days, 0);
+        let interval_cv = ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(interval)));
+        let result = datum::apply(start, &interval_cv, add)?;
+        return Ok(result);
+    } else if let ColumnarValue::Array(days) = &args[1] {
+        let mut interval_builder = IntervalDayTimeBuilder::new();

Review Comment:
   Because we know the size upfront, we can use `with_capacity` here instead of `new`.
   
   ```suggestion
           let mut interval_builder = IntervalDayTimeBuilder::with_capacity(days.len());
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1752622844


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,76 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+macro_rules! scalar_date_arithmetic {
+    ($start:expr, $days:expr, $op:expr) => {{
+        let interval = IntervalDayTime::new(*$days as i32, 0);
+        let interval_cv = ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(interval)));
+        datum::apply($start, &interval_cv, $op)
+    }};
+}
+macro_rules! array_date_arithmetic {

Review Comment:
   nit: style
   
   ```suggestion
   }
   
   macro_rules! array_date_arithmetic {
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747184619


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,32 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` expression, which assumes days for the second argument, but we
+/// cannot directly add that to a Date32. We generate an IntervalDayTime from the second argument,
+/// and use DataFusion's interface to apply Arrow's add operator.
+pub fn spark_date_add(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {
+        let interval = IntervalDayTime::new(*days, 0);
+        let interval_cv = ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(interval)));
+        let result = datum::apply(start, &interval_cv, add)?;
+        return Ok(result);

Review Comment:
   No need to unwrap the result with `?` and then wrap again in `Ok`.
   
   ```suggestion
           return datum::apply(start, &interval_cv, add);
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747546861


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,40 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` and `date_sub` expressions, which assumes days for the second
+/// argument, but we cannot directly add that to a Date32. We generate an IntervalDayTime from the
+/// second argument and use DataFusion's interface to apply Arrow's operators.
+fn spark_date_arithmetic(
+    args: &[ColumnarValue],
+    op: impl Fn(&dyn Datum, &dyn Datum) -> Result<ArrayRef, ArrowError>,
+) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {
+        let interval = IntervalDayTime::new(*days, 0);
+        let interval_cv = ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(interval)));
+        return datum::apply(start, &interval_cv, op);
+    } else if let ColumnarValue::Array(days) = &args[1] {
+        let mut interval_builder = IntervalDayTimeBuilder::with_capacity(days.len());
+        for day in days.as_primitive::<Int32Type>().into_iter() {
+            if let Some(non_null_day) = day {
+                interval_builder.append_value(IntervalDayTime::new(non_null_day, 0));
+            } else {
+                interval_builder.append_null();
+            }
+        }
+        let interval_cv = ColumnarValue::Array(Arc::new(interval_builder.finish()));
+        return datum::apply(start, &interval_cv, op);
+    }
+    Err(DataFusionError::Internal(format!(
+        "Unsupported data type {:?} for function date_add",

Review Comment:
   nit: hard-coded reference to `date_add` in error message but this is now also used for `date_sub`.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747736596


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,40 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` and `date_sub` expressions, which assumes days for the second
+/// argument, but we cannot directly add that to a Date32. We generate an IntervalDayTime from the
+/// second argument and use DataFusion's interface to apply Arrow's operators.
+fn spark_date_arithmetic(
+    args: &[ColumnarValue],
+    op: impl Fn(&dyn Datum, &dyn Datum) -> Result<ArrayRef, ArrowError>,
+) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {

Review Comment:
   Good catch!



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747224565


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -146,6 +146,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("date_add with scalar") {
+    Seq(true, false).foreach { dictionaryEnabled =>
+      withTempDir { dir =>
+        val path = new Path(dir.toURI.toString, "test.parquet")
+        makeParquetFileAllTypes(path, dictionaryEnabled = dictionaryEnabled, 10000)
+        withParquetTable(path.toString, "tbl") {
+          checkSparkAnswerAndOperator(
+            "SELECT _2 FROM tbl WHERE _20 + 2 > CAST('1970-01-02' AS DATE)")
+        }
+      }
+    }
+  }
+

Review Comment:
   It would be good to add a test for overflow as well. Something like this:
   
   ```scala
     test("date_add with scalar overflow") {
       Seq(true, false).foreach { dictionaryEnabled =>
         withTempDir { dir =>
           val path = new Path(dir.toURI.toString, "test.parquet")
           makeParquetFileAllTypes(path, dictionaryEnabled = dictionaryEnabled, 10000)
           withParquetTable(path.toString, "tbl") {
             val (sparkErr, cometErr) = checkSparkMaybeThrows(
               sql(s"SELECT _20 + ${Int.MaxValue} FROM tbl"))
             assert(sparkErr.get.getMessage.contains("integer overflow"))
             assert(cometErr.get.getMessage.contains("`NaiveDate + TimeDelta` overflowed"))
           }
         }
       }
     }
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#issuecomment-2334860951

   I added support for Int8 and Int16. I don't love this control flow. @viirya suggested macros, but I'm open for other ways to approach this.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#issuecomment-2334414715

   q72 comparison (only SF 1 locally), last two rows of each (using Comet Exec) are relevant. main branch:
   ```
   TPCDS Snappy:                             Best Time(ms)   Avg Time(ms)   Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   ------------------------------------------------------------------------------------------------------------------------
   q72                                                7047           7848        1133          2.2         459.1       1.0X
   q72: Comet (Scan)                                  6824           7291         660          2.2         444.6       1.0X
   q72: Comet (Scan, Exec)                           10816          10896         112          1.4         704.7       0.7X
   q72: Comet (Exec)                                 10811          10959         209          1.4         704.4       0.7X
   ```
   This PR:
   ```
   TPCDS Snappy:                             Best Time(ms)   Avg Time(ms)   Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   ------------------------------------------------------------------------------------------------------------------------
   q72                                                6848           6992         204          2.2         446.2       1.0X
   q72: Comet (Scan)                                  6814           6819           7          2.3         444.0       1.0X
   q72: Comet (Scan, Exec)                           10376          10442          93          1.5         676.1       0.7X
   q72: Comet (Exec)                                 10472          10513          58          1.5         682.3       0.7X
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#issuecomment-2354196017

   Thanks @mbutrovich @andygrove @kazuyukitanimura 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add and date_sub functions [datafusion-comet]

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya merged PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747208959


##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -146,6 +146,32 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("date_add with scalar") {
+    Seq(true, false).foreach { dictionaryEnabled =>
+      withTempDir { dir =>
+        val path = new Path(dir.toURI.toString, "test.parquet")
+        makeParquetFileAllTypes(path, dictionaryEnabled = dictionaryEnabled, 10000)
+        withParquetTable(path.toString, "tbl") {
+          checkSparkAnswerAndOperator(
+            "SELECT _2 FROM tbl WHERE _20 + 2 > CAST('1970-01-02' AS DATE)")

Review Comment:
   A simpler approach for testing here may be to just use the expression in a projection. For example:
   
   ```sql
   SELECT _20 + 2 FROM tbl
   ```
   
   I also tried testing with subtracting instead of adding and the test failed because we don't support `date_sub` (I had assumed that `date_add` would accept negative values). We could add `date_sub` as a part of this PR or as a separate PR.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: date_add function [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #910:
URL: https://github.com/apache/datafusion-comet/pull/910#discussion_r1747185395


##########
native/spark-expr/src/scalar_funcs.rs:
##########
@@ -547,3 +551,32 @@ pub fn spark_isnan(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionEr
         },
     }
 }
+
+/// Spark-compatible `date_add` expression, which assumes days for the second argument, but we
+/// cannot directly add that to a Date32. We generate an IntervalDayTime from the second argument,
+/// and use DataFusion's interface to apply Arrow's add operator.
+pub fn spark_date_add(args: &[ColumnarValue]) -> Result<ColumnarValue, DataFusionError> {
+    let start = &args[0];
+    if let ColumnarValue::Scalar(ScalarValue::Int32(Some(days))) = &args[1] {
+        let interval = IntervalDayTime::new(*days, 0);
+        let interval_cv = ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(interval)));
+        let result = datum::apply(start, &interval_cv, add)?;
+        return Ok(result);
+    } else if let ColumnarValue::Array(days) = &args[1] {
+        let mut interval_builder = IntervalDayTimeBuilder::new();
+        for day in days.as_primitive::<Int32Type>().into_iter() {
+            if let Some(non_null_day) = day {
+                interval_builder.append_value(IntervalDayTime::new(non_null_day, 0));
+            } else {
+                interval_builder.append_null();
+            }
+        }
+        let interval_cv = ColumnarValue::Array(Arc::new(interval_builder.finish()));
+        let result = datum::apply(start, &interval_cv, add)?;
+        return Ok(result);

Review Comment:
   Same comment re unwrapping result:
   
   ```suggestion
           return datum::apply(start, &interval_cv, add);
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org