-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: add Spark date_from_unix_date function to datafusion-spark #23852
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
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,181 @@ | ||
| // 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 std::sync::Arc; | ||
|
|
||
| use arrow::array::{Array, Date32Array, Int32Array}; | ||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, ScalarValue, exec_err, internal_datafusion_err}; | ||
| use datafusion_expr::simplify::{ExprSimplifyResult, SimplifyContext}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, Expr, ExprSchemable, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
| Volatility, | ||
| }; | ||
|
|
||
| /// Spark-compatible `date_from_unix_date` function. | ||
| /// Creates a date from the number of days since epoch (1970-01-01). | ||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#date_from_unix_date> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkDateFromUnixDate { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for SparkDateFromUnixDate { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkDateFromUnixDate { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::exact(vec![DataType::Int32], Volatility::Immutable), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkDateFromUnixDate { | ||
| fn name(&self) -> &str { | ||
| "date_from_unix_date" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::Date32) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let [days] = take_function_args(self.name(), args.args)?; | ||
| match days { | ||
| ColumnarValue::Array(array) => { | ||
| let days = | ||
| array.as_any().downcast_ref::<Int32Array>().ok_or_else(|| { | ||
| internal_datafusion_err!( | ||
| "date_from_unix_date expected an Int32 array" | ||
| ) | ||
| })?; | ||
| // Date32 and Int32 both count days since the Unix epoch | ||
| // (1970-01-01), so the i32 values transfer directly. | ||
| let dates = | ||
| Date32Array::new(days.values().clone(), days.nulls().cloned()); | ||
| Ok(ColumnarValue::Array(Arc::new(dates))) | ||
| } | ||
| ColumnarValue::Scalar(ScalarValue::Int32(days)) => { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Date32(days))) | ||
| } | ||
| ColumnarValue::Scalar(other) => { | ||
| exec_err!("date_from_unix_date expected an Int32 argument, got {other:?}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn simplify( | ||
| &self, | ||
| args: Vec<Expr>, | ||
| info: &SimplifyContext, | ||
| ) -> Result<ExprSimplifyResult> { | ||
| let [days] = take_function_args(self.name(), args)?; | ||
| Ok(ExprSimplifyResult::Simplified( | ||
| days.cast_to(&DataType::Date32, info.schema())?, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use arrow::datatypes::Field; | ||
|
|
||
| /// Exercises the physical `invoke_with_args` path directly, as consumers | ||
| /// that build physical `ScalarFunctionExpr` nodes (e.g. DataFusion Comet) | ||
| /// call it without ever running the logical `SimplifyExpressions` pass. | ||
| #[test] | ||
| fn test_date_from_unix_date_array() -> Result<()> { | ||
| let func = SparkDateFromUnixDate::new(); | ||
|
|
||
| let input = Int32Array::from(vec![Some(0), Some(1), Some(-1), None, Some(18628)]); | ||
| let result = func.invoke_with_args(ScalarFunctionArgs { | ||
| args: vec![ColumnarValue::Array(Arc::new(input))], | ||
| arg_fields: vec![Arc::new(Field::new("arg", DataType::Int32, true))], | ||
| number_rows: 5, | ||
| return_field: Arc::new(Field::new( | ||
| "date_from_unix_date", | ||
| DataType::Date32, | ||
| true, | ||
| )), | ||
| config_options: Arc::new(Default::default()), | ||
| })?; | ||
| match result { | ||
| ColumnarValue::Array(arr) => { | ||
| let expected = Date32Array::from(vec![ | ||
| Some(0), | ||
| Some(1), | ||
| Some(-1), | ||
| None, | ||
| Some(18628), | ||
| ]); | ||
| assert_eq!(arr.as_ref(), &expected as &dyn Array); | ||
| } | ||
| other => panic!("unexpected result: {other:?}"), | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_date_from_unix_date_scalar() -> Result<()> { | ||
| let func = SparkDateFromUnixDate::new(); | ||
|
|
||
| let result = func.invoke_with_args(ScalarFunctionArgs { | ||
| args: vec![ColumnarValue::Scalar(ScalarValue::Int32(Some(0)))], | ||
| arg_fields: vec![Arc::new(Field::new("arg", DataType::Int32, true))], | ||
| number_rows: 1, | ||
| return_field: Arc::new(Field::new( | ||
| "date_from_unix_date", | ||
| DataType::Date32, | ||
| true, | ||
| )), | ||
| config_options: Arc::new(Default::default()), | ||
| })?; | ||
| match result { | ||
| ColumnarValue::Scalar(ScalarValue::Date32(Some(v))) => assert_eq!(v, 0), | ||
| other => panic!("unexpected result: {other:?}"), | ||
| } | ||
|
|
||
| let result = func.invoke_with_args(ScalarFunctionArgs { | ||
| args: vec![ColumnarValue::Scalar(ScalarValue::Int32(None))], | ||
| arg_fields: vec![Arc::new(Field::new("arg", DataType::Int32, true))], | ||
| number_rows: 1, | ||
| return_field: Arc::new(Field::new( | ||
| "date_from_unix_date", | ||
| DataType::Date32, | ||
| true, | ||
| )), | ||
| config_options: Arc::new(Default::default()), | ||
| })?; | ||
| match result { | ||
| ColumnarValue::Scalar(ScalarValue::Date32(None)) => {} | ||
| other => panic!("unexpected result: {other:?}"), | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # 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. | ||
|
|
||
| # date_from_unix_date tests | ||
| # | ||
| # The function lowers to an Int32 -> Date32 cast in `simplify()`, so the logical | ||
| # optimizer replaces it before physical planning and `invoke_with_args` is never | ||
| # reached. Consumers that build physical expressions directly (for example | ||
| # DataFusion Comet) do not run `SimplifyExpressions` and call `invoke_with_args` | ||
| # instead. Both paths are covered below: the second section disables the logical | ||
| # optimizer so that the function survives into the physical plan. | ||
|
|
||
| ########## | ||
| # simplify() path (default) | ||
| ########## | ||
|
|
||
| # The plan below confirms this section exercises the rewritten cast. | ||
| query TT | ||
| EXPLAIN SELECT date_from_unix_date(a) FROM (VALUES (0::INT), (1::INT), (18628::INT)) AS t(a); | ||
| ---- | ||
| logical_plan | ||
| 01)Projection: CAST(t.a AS Date32) AS date_from_unix_date(t.a) | ||
| 02)--SubqueryAlias: t | ||
| 03)----Projection: column1 AS a | ||
| 04)------Values: (Int32(0) AS Int64(0)), (Int32(1) AS Int64(1)), (Int32(18628) AS Int64(18628)) | ||
| physical_plan | ||
| 01)ProjectionExec: expr=[CAST(column1@0 AS Date32) as date_from_unix_date(t.a)] | ||
| 02)--DataSourceExec: partitions=1, partition_sizes=[1] | ||
|
|
||
| # Scalar cases | ||
| query D | ||
| SELECT date_from_unix_date(0::INT); | ||
| ---- | ||
| 1970-01-01 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date(1::INT); | ||
| ---- | ||
| 1970-01-02 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date((-1)::INT); | ||
| ---- | ||
| 1969-12-31 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date(18628::INT); | ||
| ---- | ||
| 2021-01-01 | ||
|
|
||
| # NULL input | ||
| query D | ||
| SELECT date_from_unix_date(NULL::INT); | ||
| ---- | ||
| NULL | ||
|
|
||
| # Array input | ||
| query D | ||
| SELECT date_from_unix_date(a) FROM (VALUES (0::INT), (1::INT), (18628::INT)) AS t(a); | ||
| ---- | ||
| 1970-01-01 | ||
| 1970-01-02 | ||
| 2021-01-01 | ||
|
|
||
| ########## | ||
| # invoke_with_args() path | ||
| ########## | ||
|
|
||
| statement ok | ||
| set datafusion.optimizer.max_passes = 0; | ||
|
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. we prob need to document that we don't want
Member
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. Maybe I should just remove the simplify path from this PR, but my concern is that someone will later implement it and remove invoke_with_args and then Comet will no longer be able to leverage this work |
||
|
|
||
| # The plan below confirms this section exercises the function itself. | ||
| query TT | ||
| EXPLAIN SELECT date_from_unix_date(a) FROM (VALUES (0::INT), (1::INT), (18628::INT)) AS t(a); | ||
| ---- | ||
| logical_plan | ||
| 01)Projection: date_from_unix_date(t.a) | ||
| 02)--SubqueryAlias: t | ||
| 03)----Projection: column1 AS a | ||
| 04)------Values: (CAST(Int64(0) AS Int32)), (CAST(Int64(1) AS Int32)), (CAST(Int64(18628) AS Int32)) | ||
| physical_plan | ||
| 01)ProjectionExec: expr=[date_from_unix_date(column1@0) as date_from_unix_date(t.a)] | ||
| 02)--DataSourceExec: partitions=1, partition_sizes=[1] | ||
|
|
||
| # Scalar cases | ||
| query D | ||
| SELECT date_from_unix_date(0::INT); | ||
| ---- | ||
| 1970-01-01 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date(1::INT); | ||
| ---- | ||
| 1970-01-02 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date((-1)::INT); | ||
| ---- | ||
| 1969-12-31 | ||
|
|
||
| query D | ||
| SELECT date_from_unix_date(18628::INT); | ||
| ---- | ||
| 2021-01-01 | ||
|
|
||
| # NULL input | ||
| query D | ||
| SELECT date_from_unix_date(NULL::INT); | ||
| ---- | ||
| NULL | ||
|
|
||
| # Array input | ||
| query D | ||
| SELECT date_from_unix_date(a) FROM (VALUES (0::INT), (1::INT), (18628::INT)) AS t(a); | ||
| ---- | ||
| 1970-01-01 | ||
| 1970-01-02 | ||
| 2021-01-01 | ||
|
|
||
| statement ok | ||
| reset datafusion.optimizer.max_passes; | ||
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.
the tests might be not needed as it should have already been covere by slt, however nice test would be to test
simplifyoutput andinvoke_with_args_outputare the same