kosiew commented on code in PR #20808: URL: https://github.com/apache/datafusion/pull/20808#discussion_r3091866547
########## datafusion/spark/src/function/datetime/quarter.rs: ########## @@ -0,0 +1,95 @@ +// 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::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::types::logical_date; +use datafusion_common::utils::take_function_args; +use datafusion_common::{Result, ScalarValue, internal_err}; +use datafusion_expr::expr::ScalarFunction; +use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; +use datafusion_expr::{ + Coercion, ColumnarValue, Expr, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, + Signature, TypeSignature, TypeSignatureClass, Volatility, +}; +use datafusion_functions::datetime::date_part; +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( Review Comment: Nice progress here, the simplification to `date_part` cleans things up a lot 👍 One thing that still looks unresolved is the string literal coercion path. Right now `quarter` only advertises timestamp and date signatures, and since DataFusion validates arguments before `simplify()` runs, string literals get rejected early. That means cases like `quarter('2016-08-31')` or timestamp-shaped strings still dont make it far enough to benefit from the rewrite to `date_part('quarter', ...)`, which *does* support implicit string coercion. So in practice, the Spark-compatible behavior for string inputs is still missing. We probably need to either broaden the signature or otherwise allow these inputs through so the rewrite can happen. ########## datafusion/sqllogictest/test_files/spark/datetime/quarter.slt: ########## @@ -15,13 +15,52 @@ # specific language governing permissions and limitations # under the License. -# This file was originally created by a porting script from: -# https://github.com/lakehq/sail/tree/43b6ed8221de5c4c4adbedbb267ae1351158b43c/crates/sail-spark-connect/tests/gold_data/function -# This file is part of the implementation of the datafusion-spark function library. -# For more information, please see: -# https://github.com/apache/datafusion/issues/15914 - -## Original Query: SELECT quarter('2016-08-31'); -## PySpark 3.5.5 Result: {'quarter(2016-08-31)': 3, 'typeof(quarter(2016-08-31))': 'int', 'typeof(2016-08-31)': 'string'} -#query -#SELECT quarter('2016-08-31'::string); +query I Review Comment: The added coverage for DATE and TIMESTAMP inputs looks good 👍 That said, we’re still missing the specific Spark regression case that was called out earlier: `SELECT quarter('2016-08-31');` Since the implementation still doesn’t accept plain string literals, not having this exact case in the SLT means the mismatch isn’t being caught. It would be great to add this test back in so we lock in the expected Spark behavior and prevent regressions once the coercion issue is fixed. -- 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]
