forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
18642: feat: support spark csc #13
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
Open
martin-augment
wants to merge
1
commit into
main
Choose a base branch
from
pr-18642-2025-11-12-09-05-38
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,193 @@ | ||
| // 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 crate::function::error_utils::{ | ||
| invalid_arg_count_exec_err, unsupported_data_type_exec_err, | ||
| }; | ||
| use arrow::array::{ArrayRef, AsArray}; | ||
| use arrow::datatypes::{DataType, Float64Type}; | ||
| use datafusion_common::{Result, ScalarValue}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, | ||
| }; | ||
| use std::any::Any; | ||
| use std::sync::Arc; | ||
|
|
||
| static CSC_FUNCTION_NAME: &str = "csc"; | ||
|
|
||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#csc> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkCsc { | ||
| signature: Signature, | ||
| aliases: Vec<String>, | ||
| } | ||
|
|
||
| impl Default for SparkCsc { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkCsc { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::user_defined(Volatility::Immutable), | ||
| aliases: vec![], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkCsc { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| CSC_FUNCTION_NAME | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::Float64) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| spark_csc(&args.args) | ||
| } | ||
|
|
||
| fn aliases(&self) -> &[String] { | ||
| &self.aliases | ||
| } | ||
|
|
||
| fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
| if arg_types.len() != 1 { | ||
| return Err(invalid_arg_count_exec_err( | ||
| CSC_FUNCTION_NAME, | ||
| (1, 1), | ||
| arg_types.len(), | ||
| )); | ||
| } | ||
| if arg_types[0].is_numeric() { | ||
| Ok(vec![DataType::Float64]) | ||
| } else { | ||
| Err(unsupported_data_type_exec_err( | ||
| CSC_FUNCTION_NAME, | ||
| "Numeric Type", | ||
| &arg_types[0], | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn spark_csc(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
| if args.len() != 1 { | ||
| return Err(invalid_arg_count_exec_err( | ||
| CSC_FUNCTION_NAME, | ||
| (1, 1), | ||
| args.len(), | ||
| )); | ||
| } | ||
| match &args[0] { | ||
| ColumnarValue::Scalar(ScalarValue::Float64(value)) => Ok(ColumnarValue::Scalar( | ||
| ScalarValue::Float64(value.map(|x| 1.0 / x.sin())), | ||
| )), | ||
| ColumnarValue::Array(array) => match array.data_type() { | ||
| DataType::Float64 => Ok(ColumnarValue::Array(Arc::new( | ||
| array | ||
| .as_primitive::<Float64Type>() | ||
| .unary::<_, Float64Type>(|x| 1.0 / x.sin()), | ||
| ) as ArrayRef)), | ||
| other => Err(unsupported_data_type_exec_err( | ||
| CSC_FUNCTION_NAME, | ||
| format!("{}", DataType::Float64).as_str(), | ||
| other, | ||
| )), | ||
| }, | ||
| other => Err(unsupported_data_type_exec_err( | ||
| CSC_FUNCTION_NAME, | ||
| format!("{}", DataType::Float64).as_str(), | ||
| &other.data_type(), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::function::math::trigonometry::{spark_csc, SparkCsc}; | ||
| use crate::function::utils::test::test_scalar_function; | ||
| use arrow::array::{Array, Float64Array}; | ||
| use arrow::datatypes::DataType::Float64; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::{ColumnarValue, ScalarUDFImpl}; | ||
| use std::f64::consts::PI; | ||
| use std::sync::Arc; | ||
|
|
||
| macro_rules! test_trig_float64_invoke { | ||
| ($FUNC: expr, $INPUT:expr, $EXPECTED:expr) => { | ||
| test_scalar_function!( | ||
| $FUNC, | ||
| vec![ColumnarValue::Scalar(ScalarValue::Float64($INPUT))], | ||
| $EXPECTED, | ||
| f64, | ||
| Float64, | ||
| Float64Array | ||
| ); | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_csc_invoke() { | ||
| test_trig_float64_invoke!(SparkCsc::new(), Some(0f64), Ok(Some(f64::INFINITY))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_csc_array() { | ||
| let input = Float64Array::from(vec![1f64, 0f64, -1f64]); | ||
| let expected = Float64Array::from(vec![ | ||
| 1.1883951057781212, | ||
| f64::INFINITY, | ||
| -1.1883951057781212, | ||
| ]); | ||
| let args = ColumnarValue::Array(Arc::new(input)); | ||
|
|
||
| if let Ok(ColumnarValue::Array(result_array)) = spark_csc(&[args]) { | ||
| let output = result_array | ||
| .as_any() | ||
| .downcast_ref::<Float64Array>() | ||
| .unwrap(); | ||
| assert_eq!(output, &expected); | ||
| } else { | ||
| panic!("Expected array result"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_csc_scalar() { | ||
| let input = ScalarValue::Float64(Some(PI / 2.0)); | ||
| let expected = ScalarValue::Float64(Some(1.0)); | ||
| let args = ColumnarValue::Scalar(input); | ||
|
|
||
| if let Ok(ColumnarValue::Scalar(result_scalar)) = spark_csc(&[args]) { | ||
| assert_eq!(result_scalar, expected); | ||
| } else { | ||
| panic!("Expected scalar result"); | ||
| } | ||
| } | ||
| } | ||
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.
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.
Handle NULL inputs without throwing — With the current type coercion you reject
DataType::Null, so a plainSELECT csc(NULL)surfacesUnsupported Data Typeinstead of propagating Spark’s expectedNULLresult. That’s a logic bug users will hit when optional columns bubble through this UDF. Please acceptNullduring coercion and short-circuit to aFloat64(None)result in the evaluator.Also applies to: 107-128
🤖 Prompt for AI Agents
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.
value:useful; category:bug; feedback:The CodeRabbit AI reviewer is correct that Null handling is missing. Also tests for this use case should be added. This would make it behave as Apache Spark