kosiew commented on code in PR #20808: URL: https://github.com/apache/datafusion/pull/20808#discussion_r3020959389
########## datafusion/spark/src/function/datetime/quarter.rs: ########## @@ -0,0 +1,102 @@ +// 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 arrow::array::{Array, ArrayRef}; +use arrow::compute::{CastOptions, DatePart, cast_with_options, date_part}; +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::types::{logical_date, logical_string}; +use datafusion_common::utils::take_function_args; +use datafusion_common::{Result, internal_err}; +use datafusion_expr::{ + Coercion, ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, + Signature, TypeSignature, TypeSignatureClass, Volatility, +}; +use datafusion_functions::utils::make_scalar_function; +use std::sync::Arc; + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkQuarter { + signature: Signature, +} + +impl Default for SparkQuarter { + fn default() -> Self { + Self::new() + } +} + +impl SparkQuarter { + pub fn new() -> Self { + Self { + signature: Signature::one_of( + vec![ + TypeSignature::Coercible(vec![Coercion::new_exact( + TypeSignatureClass::Timestamp, + )]), + TypeSignature::Coercible(vec![Coercion::new_exact( + TypeSignatureClass::Native(logical_date()), + )]), + TypeSignature::Coercible(vec![Coercion::new_exact( + TypeSignatureClass::Native(logical_string()), + )]), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for SparkQuarter { + fn name(&self) -> &str { + "quarter" + } + + 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> { + Ok(Arc::new(Field::new(self.name(), DataType::Int32, true))) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + make_scalar_function(spark_quarter, vec![])(&args.args) + } +} + +fn spark_quarter(args: &[ArrayRef]) -> Result<ArrayRef> { + let [array] = take_function_args("quarter", args)?; + match array.data_type() { + DataType::Date32 | DataType::Timestamp(_, _) => { + let quarter = date_part(array, DatePart::Quarter)?; + Ok(quarter) + } + DataType::Utf8 | DataType::Utf8View | DataType::LargeUtf8 => { + let date_array = + cast_with_options(array, &DataType::Date32, &CastOptions::default())?; Review Comment: Thanks for addressing the earlier feedback. One thing that still stands out is how string inputs are handled here. Right now all strings are cast through Date32 before calling date_part. This means something like quarter('2020-09-08T12:00:12.12345678+00:00') goes through a narrower path compared to date_part('quarter', ...). The previous review suggested aligning quarter with the shared datetime coercion behavior. With the current approach, timestamp-shaped strings that DataFusion already accepts elsewhere may still be rejected or behave differently here. Could we reuse the same coercion path as date_part so the behavior stays consistent across functions? -- 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]
