davidlghellin commented on code in PR #17424:
URL: https://github.com/apache/datafusion/pull/17424#discussion_r2352983828


##########
datafusion/functions/src/datetime/make_interval.rs:
##########
@@ -0,0 +1,618 @@
+// 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::sync::Arc;
+
+use crate::utils::make_scalar_function;
+use arrow::array::{
+    Array, ArrayRef, IntervalMonthDayNanoBuilder, PrimitiveArray, 
PrimitiveBuilder,
+};
+use arrow::datatypes::DataType::Interval;
+use arrow::datatypes::IntervalUnit::MonthDayNano;
+use arrow::datatypes::{DataType, IntervalMonthDayNano, 
IntervalMonthDayNanoType};
+use datafusion_common::{exec_err, plan_datafusion_err, DataFusionError, 
Result};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    Volatility,
+};
+use datafusion_macros::user_doc;
+
+#[user_doc(
+    doc_section(label = "Time and Date Functions"),
+    description = "Construct an INTERVAL (MonthDayNano) from component parts. 
Missing arguments default to 0; if any provided argument is NULL on a row, the 
result is NULL.",
+    syntax_example = "make_interval([years[, months[, weeks[, days[, hours[, 
mins[, secs]]]]]])",
+    sql_example = r#"```sql
+-- Inline example without creating a table
+> SELECT
+      y, m, w, d, h, mi, s,
+      make_interval(y, m, w, d, h, mi, s) AS interval
+    FROM VALUES
+      (1,   1,   1,   1,   1,   1,   1.0)
+    AS v(y, m, w, d, h, mi, s);
++---+---+---+---+---+---+---+---------------------------------------------------+
+|y  |m  |w  |d  |h  |mi |s  |interval                                          
 |
++---+---+---+---+---+---+---+---------------------------------------------------+
+|1  |1  |1  |1  |1  |1  |1.0|1 years 1 months 8 days 1 hours 1 minutes 1 
seconds|
++---+---+---+---+---+---+---+---------------------------------------------------+
+```"#,
+    argument(
+        name = "years",
+        description = "Years to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "months",
+        description = "Months to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "weeks",
+        description = "Weeks to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "days",
+        description = "Days to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "hours",
+        description = "Hours to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "mins",
+        description = "Minutes to use when making the interval. Optional; 
defaults to 0. Can be a constant, column or function, and any combination of 
arithmetic operators."
+    ),
+    argument(
+        name = "secs",
+        description = "Seconds to use when making the interval (may be 
fractional). Optional; defaults to 0. Must be finite (not NaN/±Inf). Can be a 
constant, column or function, and any combination of arithmetic operators."
+    )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct MakeIntervalFunc {
+    signature: Signature,
+}
+
+impl Default for MakeIntervalFunc {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl MakeIntervalFunc {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for MakeIntervalFunc {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "make_interval"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        Ok(Interval(MonthDayNano))
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        if args.args.is_empty() {
+            let n: usize = std::cmp::max(args.number_rows, 1);
+            let mut b: PrimitiveBuilder<IntervalMonthDayNanoType> =
+                IntervalMonthDayNanoBuilder::with_capacity(n);
+            for _ in 0..n {
+                b.append_value(IntervalMonthDayNano::new(0, 0, 0));
+            }
+            return Ok(ColumnarValue::Array(Arc::new(b.finish())));
+        }
+        make_scalar_function(make_interval_kernel, vec![])(&args.args)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        let length = arg_types.len();
+        match length {
+            x if x > 7 => {
+                exec_err!(
+                    "make_interval expects between 1 and 7, got {}",
+                    arg_types.len()
+                )
+            }
+            _ => Ok((0..arg_types.len())
+                .map(|i| {
+                    if i == 6 {
+                        DataType::Float64
+                    } else {
+                        DataType::Int32
+                    }
+                })
+                .collect()),
+        }
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+fn make_interval_kernel(args: &[ArrayRef]) -> Result<ArrayRef, 
DataFusionError> {
+    use arrow::array::AsArray;
+    use arrow::datatypes::{Float64Type, Int32Type};
+
+    // 0 args is in invoke_with_args
+    if args.is_empty() || args.len() > 7 {
+        return exec_err!("make_interval expects between 0 and 7, got {}", 
args.len());
+    }
+
+    let n_rows = args[0].len();
+    for (i, a) in args.iter().enumerate().skip(1) {
+        if a.len() != n_rows {
+            return exec_err!(
+                "make_dt_interval: argument {i} has length {}, expected 
{n_rows}",
+                a.len()
+            );
+        }
+    }

Review Comment:
   ok, It is not necessary.



-- 
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-unsubscr...@datafusion.apache.org

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


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

Reply via email to