-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Spark quarter function implementation #20808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
ad1576e
fd47524
d11e1ff
43eef61
41fa4d1
fd6588e
bbd95c5
9ac943a
55a9c28
dcea4d0
c1917c1
97a47a6
6fb1455
8a43624
7cb2b9f
a6517fe
67e7093
528c6fc
4830962
231e662
88a8503
167d747
91738da
90dcdfa
b53949d
13bf0c7
e26b85f
9e4d137
b52d6d4
b2ec222
79cfd94
1f6045a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // 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::{ArrayRef, AsArray, Int32Array}; | ||
| use arrow::datatypes::{DataType, Date32Type, Field, FieldRef}; | ||
| use chrono::Datelike; | ||
| use datafusion::logical_expr::{ColumnarValue, Signature, Volatility}; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, ScalarValue, internal_err}; | ||
| use datafusion_expr::{ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl}; | ||
| use std::any::Any; | ||
| 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::exact(vec![DataType::Date32], Volatility::Immutable), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkQuarter { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| 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, | ||
| args.arg_fields[0].is_nullable(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the return-field nullability needs to be loosened here. Right now That looks like a schema contract bug. It also differs from existing Spark helpers like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. fixed |
||
| ))) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let [arg] = take_function_args("quarter", args.args)?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small suggestion here: this seems to repeat the same scalar/array Would it make sense to delegate to that existing implementation instead? It feels like that would help keep coercion rules, null handling, and any future date-part behavior aligned in one place. |
||
| match arg { | ||
| ColumnarValue::Scalar(ScalarValue::Date32(days)) => { | ||
| if let Some(days) = days { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int32(Some( | ||
| spark_quarter(days)?, | ||
| )))) | ||
| } else { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Int32(None))) | ||
| } | ||
| } | ||
| ColumnarValue::Array(array) => { | ||
| let result = match array.data_type() { | ||
| DataType::Date32 => { | ||
| let result: Int32Array = array | ||
| .as_primitive::<Date32Type>() | ||
| .try_unary(spark_quarter)? | ||
| .with_data_type(DataType::Int32); | ||
| Ok(Arc::new(result) as ArrayRef) | ||
| } | ||
| other => { | ||
| internal_err!( | ||
| "Unsupported data type {other:?} for Spark function `quarter`" | ||
| ) | ||
| } | ||
| }?; | ||
| Ok(ColumnarValue::Array(result)) | ||
| } | ||
| other => { | ||
| internal_err!("Unsupported arg {other:?} for Spark function `quarter") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn spark_quarter(days: i32) -> Result<i32> { | ||
| let quarter = Date32Type::to_naive_date_opt(days).unwrap().quarter(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing that made me a little nervous here is the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to rework it |
||
| Ok(quarter as i32) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,37 @@ | |
| # 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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: 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. |
||
| SELECT quarter('2009-01-12'::DATE); | ||
| ---- | ||
| 1 | ||
|
|
||
| query I | ||
| SELECT quarter('1970-01-01'::DATE); | ||
| ---- | ||
| 1 | ||
|
|
||
| query I | ||
| SELECT quarter('1870-01-01'::DATE); | ||
| ---- | ||
| 1 | ||
|
|
||
| query I | ||
| SELECT quarter('2011-04-21'::DATE); | ||
| ---- | ||
| 2 | ||
|
|
||
| query I | ||
| SELECT quarter('2024-08-14'::DATE); | ||
| ---- | ||
| 3 | ||
|
|
||
| query I | ||
| SELECT quarter('2016-12-12'::DATE); | ||
| ---- | ||
| 4 | ||
|
|
||
| query I | ||
| SELECT quarter(NULL::DATE); | ||
| ---- | ||
| NULL | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the main thing we should fix before merging. Right now the UDF is registered with an exact
Date32signature, which means we no longer preserve Spark’s documented call shape forquarter.Spark’s SQL docs show
SELECT quarter('2016-08-31');returning3, and this SLT file used to carry that example before it was replaced with explicit::DATEcasts. With the current signature, we only validate the casted form and could end up rejecting the plain string-literal case that Spark accepts.Could we switch this to a coercible signature, or possibly just route through the existing
date_part('quarter', ...)behavior, and add coverage for the uncasted query?