-
Notifications
You must be signed in to change notification settings - Fork 5
Snowflake time_from_parts udf #298
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
52f75a7
Inital version
osipovartem ff71924
snowflake timestamp_from_parts udf
osipovartem c573998
snowflake time_from_parts udf
osipovartem 7cef529
snowflake time_from_parts udf
osipovartem ebf236a
Fix clippy
osipovartem c25e4c4
Fix clippy
osipovartem cf233d7
Fix example typo
osipovartem c188f48
Support negative values
osipovartem 080a652
Merge
osipovartem 758b54a
Merge
osipovartem df4614a
Merge
osipovartem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
crates/runtime/src/datafusion/functions/time_from_parts.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| // 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::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::datafusion::functions::timestamp_from_parts::{ | ||
| make_time, take_function_args, to_primitive_array, | ||
| }; | ||
| use arrow::array::builder::PrimitiveBuilder; | ||
| use arrow::array::{Array, PrimitiveArray}; | ||
| use arrow::datatypes::{DataType, Int64Type, Time64NanosecondType}; | ||
| use arrow_schema::DataType::{Int64, Time64}; | ||
| use arrow_schema::TimeUnit; | ||
| use datafusion::logical_expr::TypeSignature::Coercible; | ||
| use datafusion::logical_expr::TypeSignatureClass; | ||
| use datafusion_common::types::logical_int64; | ||
| use datafusion_common::{internal_err, Result, ScalarValue}; | ||
| use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||
| use datafusion_macros::user_doc; | ||
|
|
||
| #[user_doc( | ||
| doc_section(label = "Time and Date Functions"), | ||
| description = "Creates a timestamp from individual numeric components.", | ||
| syntax_example = "time_from_parts(<hour>, <minute>, <second> [, <nanosecond> ])", | ||
| sql_example = "```sql | ||
| > select time_from_parts(12, 34, 56, 987654321); | ||
| +-----------------------------------------------------------------+ | ||
| | time_from_parts(Int64(12),Int64(34),Int64(56),Int64(987654321)) | | ||
| +-----------------------------------------------------------------+ | ||
| | 1740398450.0 | | ||
| +-----------------------------------------------------------------+ | ||
| ```", | ||
| argument( | ||
| name = "hour", | ||
| description = "An integer expression to use as an hour for building a timestamp, usually in the 0-23 range." | ||
| ), | ||
| argument( | ||
| name = "minute", | ||
| description = "An integer expression to use as a minute for building a timestamp, usually in the 0-59 range." | ||
| ), | ||
| argument( | ||
| name = "second", | ||
| description = "An integer expression to use as a second for building a timestamp, usually in the 0-59 range." | ||
| ), | ||
| argument( | ||
| name = "nanoseconds", | ||
| description = "Optional integer expression to use as a nanosecond for building a timestamp, | ||
| usually in the 0-999999999 range." | ||
| ) | ||
| )] | ||
| #[derive(Debug)] | ||
| pub struct TimeFromPartsFunc { | ||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
| } | ||
|
|
||
| impl Default for TimeFromPartsFunc { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl TimeFromPartsFunc { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::one_of( | ||
| vec![ | ||
| Coercible(vec![TypeSignatureClass::Native(logical_int64()); 4]), | ||
| Coercible(vec![TypeSignatureClass::Native(logical_int64()); 3]), | ||
| ], | ||
| Volatility::Immutable, | ||
| ), | ||
| aliases: vec![String::from("timefromparts")], | ||
| } | ||
| } | ||
| } | ||
| impl ScalarUDFImpl for TimeFromPartsFunc { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &'static str { | ||
| "time_from_parts" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(Time64(TimeUnit::Nanosecond)) | ||
| } | ||
|
|
||
| fn invoke_batch(&self, args: &[ColumnarValue], _number_rows: usize) -> Result<ColumnarValue> { | ||
| // first, identify if any of the arguments is an Array. If yes, store its `len`, | ||
| // as any scalar will need to be converted to an array of len `len`. | ||
| let array_size = args | ||
| .iter() | ||
| .find_map(|arg| match arg { | ||
| ColumnarValue::Array(a) => Some(a.len()), | ||
| ColumnarValue::Scalar(_) => None, | ||
| }) | ||
| .unwrap_or(1); | ||
| let is_scalar = array_size == 1; | ||
|
|
||
| let result = time_from_components(args, array_size)?; | ||
| if is_scalar { | ||
| // If all inputs are scalar, keeps output as scalar | ||
| let result = ScalarValue::try_from_array(&result, 0)?; | ||
| Ok(ColumnarValue::Scalar(result)) | ||
| } else { | ||
| Ok(ColumnarValue::Array(Arc::new(result))) | ||
| } | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
| } | ||
|
|
||
| fn time_from_components( | ||
| args: &[ColumnarValue], | ||
| array_size: usize, | ||
| ) -> Result<PrimitiveArray<Time64NanosecondType>> { | ||
| let (hours, minutes, seconds, nanos) = match args.len() { | ||
| 4 => { | ||
| let [hours, minutes, seconds, nanos] = take_function_args("time_from_parts", args)?; | ||
| (hours, minutes, seconds, Some(nanos)) | ||
| } | ||
| 3 => { | ||
| let [hours, minutes, seconds] = take_function_args("time_from_parts", args)?; | ||
| (hours, minutes, seconds, None) | ||
| } | ||
| _ => return internal_err!("Unsupported number of arguments"), | ||
| }; | ||
|
|
||
| let hours = to_primitive_array::<Int64Type>(&hours.cast_to(&Int64, None)?)?; | ||
| let minutes = to_primitive_array::<Int64Type>(&minutes.cast_to(&Int64, None)?)?; | ||
| let seconds = to_primitive_array::<Int64Type>(&seconds.cast_to(&Int64, None)?)?; | ||
| let nanoseconds = nanos | ||
| .map(|nanoseconds| to_primitive_array::<Int64Type>(&nanoseconds.cast_to(&Int64, None)?)) | ||
| .transpose()?; | ||
| let mut builder: PrimitiveBuilder<Time64NanosecondType> = PrimitiveArray::builder(array_size); | ||
| for i in 0..array_size { | ||
| builder.append_value(make_time( | ||
| hours.value(i), | ||
| minutes.value(i), | ||
| seconds.value(i), | ||
| nanoseconds.as_ref().map(|ns| ns.value(i)), | ||
| )); | ||
| } | ||
| Ok(builder.finish()) | ||
| } | ||
|
|
||
| super::macros::make_udf_function!(TimeFromPartsFunc); | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::datafusion::functions::time_from_parts::TimeFromPartsFunc; | ||
| use crate::datafusion::functions::timestamp_from_parts::to_primitive_array; | ||
| use arrow::datatypes::Time64NanosecondType; | ||
| use chrono::NaiveTime; | ||
| use datafusion::logical_expr::ColumnarValue; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::ScalarUDFImpl; | ||
|
|
||
| #[allow(clippy::unwrap_used)] | ||
| fn columnar_value_fn<T>(is_scalar: bool, v: T) -> ColumnarValue | ||
| where | ||
| ScalarValue: From<T>, | ||
| T: Clone, | ||
| { | ||
| if is_scalar { | ||
| ColumnarValue::Scalar(ScalarValue::from(v)) | ||
| } else { | ||
| ColumnarValue::Array(ScalarValue::from(v).to_array().unwrap()) | ||
| } | ||
| } | ||
|
|
||
| #[allow(clippy::type_complexity)] | ||
| #[allow(clippy::unwrap_used)] | ||
| #[test] | ||
| fn test_time_from_parts() { | ||
| let args: [(i64, i64, i64, Option<i64>, String); 6] = [ | ||
| (12, 0, 0, None, "12::00::00".to_string()), | ||
| (12, 10, 0, None, "12::10::00".to_string()), | ||
| (12, 10, 12, None, "12::10::12".to_string()), | ||
| (12, 10, 12, Some(255), "12::10::12.000000255".to_string()), | ||
| (12, 10, -12, Some(255), "12::09::48.000000255".to_string()), | ||
| (12, -10, -12, Some(255), "12::49::48.000000255".to_string()), | ||
| ]; | ||
|
|
||
| let is_scalar_type = [true, false]; | ||
|
|
||
| for is_scalar in is_scalar_type { | ||
| for (i, (h, mi, s, n, exp)) in args.iter().enumerate() { | ||
| let mut fn_args = vec![ | ||
| columnar_value_fn(is_scalar, *h), | ||
| columnar_value_fn(is_scalar, *mi), | ||
| columnar_value_fn(is_scalar, *s), | ||
| ]; | ||
| if let Some(nano) = n { | ||
| fn_args.push(columnar_value_fn(is_scalar, *nano)); | ||
| }; | ||
| let result = TimeFromPartsFunc::new().invoke_batch(&fn_args, 1).unwrap(); | ||
| let result = to_primitive_array::<Time64NanosecondType>(&result).unwrap(); | ||
| let seconds = result.value(0) / 1_000_000_000; | ||
| let nanoseconds = result.value(0) % 1_000_000_000; | ||
|
|
||
| let time = NaiveTime::from_num_seconds_from_midnight_opt( | ||
| u32::try_from(seconds).unwrap(), | ||
| u32::try_from(nanoseconds).unwrap(), | ||
| ) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| time.format("%H:%M:%S.%9f").to_string(), | ||
| *exp, | ||
| "failed at index {i}" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.