Jefffrey commented on code in PR #19711:
URL: https://github.com/apache/datafusion/pull/19711#discussion_r2676319850
##########
datafusion/sqllogictest/test_files/spark/datetime/add_months.slt:
##########
@@ -15,13 +15,38 @@
# specific language governing permissions and limitations
# under the License.
Review Comment:
Does spark have a behaviour for overflow/result date too large that we need
to consider?
##########
datafusion/sqllogictest/test_files/spark/datetime/date_add.slt:
##########
@@ -51,20 +51,15 @@ SELECT date_add('2016-07-30'::date, 2147483647::int)::int;
-2147466637
query I
-SELECT date_sub('1969-01-01'::date, 2147483647::int)::int;
+SELECT date_add('1969-01-01'::date, 2147483647::int)::int;
----
-2147483284
+2147483282
query D
SELECT date_add('2016-07-30'::date, 100000::int);
----
2290-05-15
-query D
-SELECT date_sub('2016-07-30'::date, 100000::int);
-----
-1742-10-15
Review Comment:
Maybe we can copy the date_sub ones into date_sub.slt?
##########
datafusion/spark/src/function/datetime/add_months.rs:
##########
@@ -0,0 +1,103 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::any::Any;
+use std::ops::Add;
+use std::sync::Arc;
+
+use arrow::datatypes::{DataType, Field, FieldRef, IntervalUnit};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, internal_err};
+use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext};
+use datafusion_expr::{
+ Cast, ColumnarValue, Expr, ReturnFieldArgs, ScalarFunctionArgs,
ScalarUDFImpl,
+ Signature, Volatility,
+};
+
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#add_months>
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkAddMonths {
+ signature: Signature,
+}
+
+impl Default for SparkAddMonths {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SparkAddMonths {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::exact(
+ vec![DataType::Date32, DataType::Int32],
+ Volatility::Immutable,
+ ),
+ }
+ }
+}
+
+impl ScalarUDFImpl for SparkAddMonths {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "add_months"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ internal_err!("return_field_from_args should be used instead")
+ }
+
+ fn return_field_from_args(&self, args: ReturnFieldArgs) ->
Result<FieldRef> {
+ let nullable = args.arg_fields.iter().any(|f| f.is_nullable())
+ || args
+ .scalar_arguments
+ .iter()
+ .any(|arg| matches!(arg, Some(sv) if sv.is_null()));
Review Comment:
```suggestion
let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
```
If an argument is a scalar null then its corresponding field should already
be nullable
--
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]