-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Feat: [datafusion-spark] Implement ceil function. #18174
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 2 commits
4490588
46f7904
918b7df
3eda333
d306988
4750825
c0a8058
ea71cdc
1894043
83730f7
ac4fd02
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 std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::array::{Array, ArrayRef, AsArray}; | ||
| use arrow::datatypes::DataType; | ||
| use arrow::datatypes::DataType::{Float32, Float64, Int64}; | ||
| use datafusion_common::{exec_err, Result}; | ||
| use datafusion_expr::Signature; | ||
| use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Volatility}; | ||
| use datafusion_functions::utils::make_scalar_function; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#ceil> | ||
| /// Difference between spark: There is no second optional argument to control the rounding behaviour. | ||
| /// Takes an Int64/Float32/Float64 input and returns the smallest number after rounding up that is | ||
| /// not smaller than the input. | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkCeil { | ||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
|
codetyri0n marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| impl Default for SparkCeil { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkCeil { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::numeric(1, Volatility::Immutable), | ||
|
codetyri0n marked this conversation as resolved.
Outdated
|
||
| aliases: vec![], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkCeil { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "ceil" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(Int64) | ||
|
codetyri0n marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| make_scalar_function(spark_ceil, vec![])(&args.args) | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
| } | ||
|
Comment on lines
+132
to
+147
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. This needs refactoring; it has an unused parameter Also the amount of comments here compared to whats being done here is overkill 🙁 (It feels like LLM comments)
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. just wanted to be as descriptive as possible 😅 - since there are larger changes to the existing code. (On a side note - there is agent usage here however for writing the code) |
||
|
|
||
| pub fn spark_ceil(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
|
codetyri0n marked this conversation as resolved.
Outdated
|
||
| if args.len() != 1 { | ||
| return exec_err!("ceil expects exactly 1 argument, got {}", args.len()); | ||
| } | ||
|
|
||
| let array: &dyn Array = args[0].as_ref(); | ||
| match args[0].data_type() { | ||
| Float32 => { | ||
| let array = array | ||
| .as_primitive::<arrow::datatypes::Float32Type>() | ||
| .unary::<_, arrow::datatypes::Int64Type>(|value: f32| { | ||
| value.ceil() as i64 | ||
| }); | ||
| Ok(Arc::new(array)) | ||
| } | ||
| Float64 => { | ||
| let array = array | ||
| .as_primitive::<arrow::datatypes::Float64Type>() | ||
| .unary::<_, arrow::datatypes::Int64Type>(|value: f64| { | ||
| value.ceil() as i64 | ||
| }); | ||
| Ok(Arc::new(array)) | ||
| } | ||
| Int64 => Ok(Arc::clone(&args[0])), | ||
| _ => { | ||
|
codetyri0n marked this conversation as resolved.
Outdated
|
||
| exec_err!( | ||
| "ceil expects a numeric argument, got {}", | ||
| args[0].data_type() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.