-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
ad1576e
fd47524
d11e1ff
43eef61
41fa4d1
fd6588e
bbd95c5
9ac943a
55a9c28
dcea4d0
c1917c1
97a47a6
6fb1455
8a43624
7cb2b9f
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,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, TimeUnit}; | ||
| use datafusion::logical_expr::{ColumnarValue, Signature, TypeSignature, Volatility}; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, internal_err}; | ||
| use datafusion_expr::{ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl}; | ||
| 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::Exact(vec![DataType::Utf8]), | ||
| TypeSignature::Exact(vec![DataType::Utf8View]), | ||
| TypeSignature::Exact(vec![DataType::LargeUtf8]), | ||
| TypeSignature::Exact(vec![DataType::Date32]), | ||
| TypeSignature::Exact(vec![DataType::Timestamp( | ||
| TimeUnit::Millisecond, | ||
| None, | ||
| )]), | ||
| ], | ||
| 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, | ||
| args.arg_fields[0].is_nullable(), | ||
| ))) | ||
| } | ||
|
|
||
| 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())?; | ||
| let quarter = date_part(&date_array, DatePart::Quarter)?; | ||
| Ok(quarter) | ||
| } | ||
| data_type => { | ||
| internal_err!("quarter does not support: {data_type}") | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,57 @@ | |
| # 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 | ||
| 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 | ||
|
|
||
| query I | ||
| SELECT quarter('2009-01-12 10:00:00'::timestamp); | ||
| ---- | ||
| 1 | ||
|
|
||
| query I | ||
| SELECT quarter('2009-01-12'::string); | ||
|
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. Nice to see the string coverage added here. I think we still need the specific regression case from Spark's documented uncasted form. Right now this file checks Since preserving that call shape was the reason for broadening the signature, could we add that case back as well? |
||
| ---- | ||
| 1 | ||
|
|
||
| query I | ||
| SELECT quarter('abc'::string); | ||
| ---- | ||
| NULL | ||
|
|
||
| query I | ||
| SELECT quarter(''::string); | ||
| ---- | ||
| 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 there is still one important gap here.
quarteris still declared with an exactTimestamp(Millisecond, None)signature, while Spark'sdate_partwrapper already uses the broader coercible timestamp path.Because of that, timestamp inputs with other units or timezones can still get rejected during planning, even though the implementation below handles
DataType::Timestamp(_, _)once execution starts.Could we align this with the existing Spark datetime coercion model so
quarterbehaves consistently with the rest of that path?