comphead commented on code in PR #75:
URL: https://github.com/apache/datafusion-spark/pull/75#discussion_r4566785655


##########
crates/spark/src/function/datetime/dayname.rs:
##########
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+use std::sync::Arc;
+
+use arrow::array::{AsArray, StringArray};
+use arrow::compute::{DatePart, date_part};
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::types::{NativeType, logical_date};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, ScalarValue, internal_err};
+use datafusion_expr::{
+    Coercion, ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, 
ScalarUDFImpl,
+    Signature, TypeSignatureClass, Volatility,
+};
+
+const DAY_NAMES: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
+
+/// `day` is 0 for Monday through 6 for Sunday.
+fn day_number_to_name(day: i32) -> Option<&'static str> {
+    DAY_NAMES.get(day as usize).copied()
+}
+
+/// Spark-compatible `dayname` expression (Spark 4.0+).
+/// Returns the three-letter abbreviated English day name (`Mon` to `Sun`) of 
a date.
+/// Timestamps are cast to dates first, like Spark's implicit cast.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#dayname>
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkDayName {
+    signature: Signature,
+}
+
+impl Default for SparkDayName {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkDayName {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::coercible(
+                vec![Coercion::new_implicit(
+                    TypeSignatureClass::Native(logical_date()),
+                    vec![TypeSignatureClass::Timestamp],
+                    NativeType::Date,
+                )],
+                Volatility::Immutable,
+            ),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkDayName {
+    fn name(&self) -> &str {
+        "dayname"
+    }
+
+    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());
+        Ok(Arc::new(Field::new(self.name(), DataType::Utf8, nullable)))
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {

Review Comment:
   Apart from `DatePart::DayOfWeekMonday0` and the name table, this file is 
`monthname.rs` line for line: the signature, `return_field_from_args`, and both 
`invoke_with_args` branches. Could both share one helper in `datetime`, for 
example `fn date_part_name(arg: ColumnarValue, part: DatePart, name: fn(i32) -> 
Option<&'static str>) -> Result<ColumnarValue>`, plus a shared date signature? 
That keeps the two in sync when one gets a fix, such as presizing the output or 
changing the time zone handling.



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