-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[datafusion-spark] Add Spark-compatible char
expression
#15994
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
andygrove
wants to merge
5
commits into
apache:main
Choose a base branch
from
andygrove:spark-char
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.
+138
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a35122f
Add Spark-compatible char expression
andygrove ff5a4cd
Add slt test
andygrove 6a5d953
Merge branch 'main' into spark-char
andygrove 1c74dd8
Merge branch 'main' into spark-char
andygrove bc42db2
Merge branch 'main' into spark-char
andygrove 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// 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, sync::Arc}; | ||
|
||
use arrow::{ | ||
array::{ArrayRef, StringArray}, | ||
datatypes::{ | ||
DataType, | ||
DataType::{Int64, Utf8}, | ||
}, | ||
}; | ||
|
||
use datafusion_common::{cast::as_int64_array, exec_err, Result, ScalarValue}; | ||
use datafusion_expr::{ | ||
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, | ||
}; | ||
|
||
/// Spark-compatible `char` expression | ||
/// <https://spark.apache.org/docs/latest/api/sql/index.html#char> | ||
#[derive(Debug)] | ||
pub struct SparkChar { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for SparkChar { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl SparkChar { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::uniform(1, vec![Int64], Volatility::Immutable), | ||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for SparkChar { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"char" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(Utf8) | ||
} | ||
|
||
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
spark_chr(&args.args) | ||
} | ||
} | ||
|
||
/// Returns the ASCII character having the binary equivalent to the input expression. | ||
/// E.g., chr(65) = 'A'. | ||
/// Compatible with Apache Spark's Chr function | ||
fn spark_chr(args: &[ColumnarValue]) -> Result<ColumnarValue> { | ||
let array = args[0].clone(); | ||
match array { | ||
ColumnarValue::Array(array) => { | ||
let array = chr(&[array])?; | ||
Ok(ColumnarValue::Array(array)) | ||
} | ||
ColumnarValue::Scalar(ScalarValue::Int64(Some(value))) => { | ||
if value < 0 { | ||
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some( | ||
"".to_string(), | ||
)))) | ||
} else { | ||
match core::char::from_u32((value % 256) as u32) { | ||
Some(ch) => Ok(ColumnarValue::Scalar(ScalarValue::Utf8(Some( | ||
ch.to_string(), | ||
)))), | ||
None => { | ||
exec_err!("requested character was incompatible for encoding.") | ||
} | ||
} | ||
} | ||
} | ||
_ => exec_err!("The argument must be an Int64 array or scalar."), | ||
} | ||
} | ||
|
||
fn chr(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
let integer_array = as_int64_array(&args[0])?; | ||
|
||
// first map is the iterator, second is for the `Option<_>` | ||
let result = integer_array | ||
.iter() | ||
.map(|integer: Option<i64>| { | ||
integer | ||
.map(|integer| { | ||
if integer < 0 { | ||
return Ok("".to_string()); // Return empty string for negative integers | ||
} | ||
match core::char::from_u32((integer % 256) as u32) { | ||
Some(ch) => Ok(ch.to_string()), | ||
None => { | ||
exec_err!("requested character not compatible for encoding.") | ||
} | ||
} | ||
}) | ||
.transpose() | ||
}) | ||
.collect::<Result<StringArray>>()?; | ||
|
||
Ok(Arc::new(result) as ArrayRef) | ||
} |
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
Binary file not shown.
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.
I think this part can be more optimal by using
Stringbuilder
(like this) 🤔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.
Thanks. I filed a tracking issue #16009