-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[datafusion-spark] Implement Spark luhn_check
function
#16580
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
tlm365
wants to merge
4
commits into
apache:main
Choose a base branch
from
tlm365:spark_luhn_check
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.
+313
−7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,281 @@ | ||
// 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::{Array, AsArray, BooleanArray}; | ||
use arrow::datatypes::DataType; | ||
use arrow::datatypes::DataType::Boolean; | ||
use datafusion_common::types::logical_string; | ||
use datafusion_common::utils::take_function_args; | ||
use datafusion_common::{exec_err, Result, ScalarValue}; | ||
use datafusion_expr::{ | ||
Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
TypeSignatureClass, Volatility, | ||
}; | ||
|
||
/// Spark-compatible `luhn_check` expression | ||
/// <https://spark.apache.org/docs/latest/api/sql/index.html#luhn_check> | ||
#[derive(Debug)] | ||
pub struct SparkLuhnCheck { | ||
signature: Signature, | ||
} | ||
|
||
impl Default for SparkLuhnCheck { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl SparkLuhnCheck { | ||
pub fn new() -> Self { | ||
Self { | ||
signature: Signature::coercible( | ||
vec![Coercion::new_exact(TypeSignatureClass::Native( | ||
logical_string(), | ||
))], | ||
Volatility::Immutable, | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for SparkLuhnCheck { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
"luhn_check" | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
Ok(Boolean) | ||
} | ||
|
||
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
let [array] = take_function_args(self.name(), &args.args)?; | ||
|
||
match array { | ||
ColumnarValue::Array(array) => match array.data_type() { | ||
DataType::Utf8View => { | ||
let str_array = array.as_string_view(); | ||
let values = str_array | ||
.iter() | ||
.map(|s| s.map(luhn_check_impl)) | ||
.collect::<BooleanArray>(); | ||
Ok(ColumnarValue::Array(Arc::new(values))) | ||
} | ||
DataType::Utf8 => { | ||
let str_array = array.as_string::<i32>(); | ||
let values = str_array | ||
.iter() | ||
.map(|s| s.map(luhn_check_impl)) | ||
.collect::<BooleanArray>(); | ||
Ok(ColumnarValue::Array(Arc::new(values))) | ||
} | ||
DataType::LargeUtf8 => { | ||
let str_array = array.as_string::<i64>(); | ||
let values = str_array | ||
.iter() | ||
.map(|s| s.map(luhn_check_impl)) | ||
.collect::<BooleanArray>(); | ||
Ok(ColumnarValue::Array(Arc::new(values))) | ||
} | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function `luhn_check`") | ||
} | ||
}, | ||
ColumnarValue::Scalar(ScalarValue::Utf8(Some(s))) | ||
| ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(s))) | ||
| ColumnarValue::Scalar(ScalarValue::Utf8View(Some(s))) => Ok( | ||
ColumnarValue::Scalar(ScalarValue::Boolean(Some(luhn_check_impl(s)))), | ||
), | ||
ColumnarValue::Scalar(ScalarValue::Utf8(None)) | ||
| ColumnarValue::Scalar(ScalarValue::LargeUtf8(None)) | ||
| ColumnarValue::Scalar(ScalarValue::Utf8View(None)) => { | ||
Ok(ColumnarValue::Scalar(ScalarValue::Boolean(None))) | ||
} | ||
other => { | ||
exec_err!("Unsupported data type {other:?} for function `luhn_check`") | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Validates a string using the Luhn algorithm. | ||
/// Returns `true` if the input is a valid Luhn number. | ||
fn luhn_check_impl(input: &str) -> bool { | ||
let mut sum = 0u32; | ||
let mut alt = false; | ||
let mut digits_processed = 0; | ||
|
||
for b in input.as_bytes().iter().rev() { | ||
let digit = match b { | ||
b'0'..=b'9' => { | ||
digits_processed += 1; | ||
b - b'0' | ||
} | ||
_ => return false, | ||
}; | ||
|
||
let mut val = digit as u32; | ||
if alt { | ||
val *= 2; | ||
if val > 9 { | ||
val -= 9; | ||
} | ||
} | ||
sum += val; | ||
alt = !alt; | ||
} | ||
|
||
digits_processed > 0 && sum % 10 == 0 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use arrow::array::{ArrayRef, StringArray, StringViewArray}; | ||
use arrow::datatypes::DataType::Utf8; | ||
use arrow::datatypes::Field; | ||
|
||
fn test_luhn_check_array(input: ArrayRef, expected: ArrayRef) -> Result<()> { | ||
let func = SparkLuhnCheck::new(); | ||
|
||
let arg_field = Field::new("a", input.data_type().clone(), true).into(); | ||
let args = ScalarFunctionArgs { | ||
number_rows: input.len(), | ||
args: vec![ColumnarValue::Array(input)], | ||
arg_fields: vec![arg_field], | ||
return_field: Field::new("f", Utf8, true).into(), | ||
}; | ||
|
||
let result = match func.invoke_with_args(args)? { | ||
ColumnarValue::Array(result) => result, | ||
_ => unreachable!("luhn_check"), | ||
}; | ||
|
||
assert_eq!(&expected, &result); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn test_array_utf8() -> Result<()> { | ||
let input = Arc::new(StringArray::from(vec![ | ||
Some("79927398713"), // valid | ||
Some("4417123456789113"), // valid | ||
Some("7992 7398 714"), // invalid | ||
Some("79927398714"), // invalid | ||
Some(""), // invalid | ||
Some("abc123"), // invalid | ||
None, // null | ||
])) as ArrayRef; | ||
|
||
let expected = Arc::new(BooleanArray::from(vec![ | ||
Some(true), | ||
Some(true), | ||
Some(false), | ||
Some(false), | ||
Some(false), | ||
Some(false), | ||
None, | ||
])) as ArrayRef; | ||
|
||
test_luhn_check_array(input, expected) | ||
} | ||
|
||
#[test] | ||
fn test_array_utf8view() -> Result<()> { | ||
let input = Arc::new(StringViewArray::from(vec![ | ||
Some("79927398713"), // valid | ||
Some("4417123456789113"), // valid | ||
Some("7992 7398 714"), // invalid | ||
Some("79927398714"), // invalid | ||
Some(""), // invalid | ||
Some("abc123"), // invalid | ||
None, // null | ||
])) as ArrayRef; | ||
|
||
let expected = Arc::new(BooleanArray::from(vec![ | ||
Some(true), | ||
Some(true), | ||
Some(false), | ||
Some(false), | ||
Some(false), | ||
Some(false), | ||
None, | ||
])) as ArrayRef; | ||
|
||
test_luhn_check_array(input, expected) | ||
} | ||
|
||
fn test_luhn_check_scalar_utf8( | ||
input: ScalarValue, | ||
expected: ScalarValue, | ||
) -> Result<()> { | ||
let func = SparkLuhnCheck::new(); | ||
|
||
let result = func.invoke_with_args(ScalarFunctionArgs { | ||
number_rows: 1, | ||
args: vec![ColumnarValue::Scalar(input)], | ||
arg_fields: vec![Field::new("a", Utf8, true).into()], | ||
return_field: Field::new("f", Boolean, true).into(), | ||
})?; | ||
|
||
match result { | ||
ColumnarValue::Scalar(actual) => { | ||
assert_eq!(actual, expected); | ||
Ok(()) | ||
} | ||
ColumnarValue::Array(_) => { | ||
panic!("Expected scalar output, but got array"); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_scalar_utf8_variants() -> Result<()> { | ||
test_luhn_check_scalar_utf8( | ||
ScalarValue::Utf8(Some("79927398713".into())), | ||
ScalarValue::Boolean(Some(true)), | ||
)?; | ||
|
||
test_luhn_check_scalar_utf8( | ||
ScalarValue::Utf8(Some("79927398714".into())), | ||
ScalarValue::Boolean(Some(false)), | ||
)?; | ||
|
||
test_luhn_check_scalar_utf8( | ||
ScalarValue::Utf8(Some("abc123".into())), | ||
ScalarValue::Boolean(Some(false)), | ||
)?; | ||
|
||
test_luhn_check_scalar_utf8( | ||
ScalarValue::Utf8(Some(" 7992 7398 713 ".into())), | ||
ScalarValue::Boolean(Some(false)), | ||
)?; | ||
|
||
test_luhn_check_scalar_utf8(ScalarValue::Utf8(None), ScalarValue::Boolean(None))?; | ||
|
||
Ok(()) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -23,16 +23,34 @@ | |
|
||
## Original Query: SELECT luhn_check('79927398713'); | ||
## PySpark 3.5.5 Result: {'luhn_check(79927398713)': True, 'typeof(luhn_check(79927398713))': 'boolean', 'typeof(79927398713)': 'string'} | ||
#query | ||
#SELECT luhn_check('79927398713'::string); | ||
query B | ||
SELECT luhn_check('79927398713'::string); | ||
---- | ||
true | ||
|
||
## Original Query: SELECT luhn_check('79927398714'); | ||
## PySpark 3.5.5 Result: {'luhn_check(79927398714)': False, 'typeof(luhn_check(79927398714))': 'boolean', 'typeof(79927398714)': 'string'} | ||
#query | ||
#SELECT luhn_check('79927398714'::string); | ||
query B | ||
SELECT luhn_check('79927398714'::string); | ||
---- | ||
false | ||
|
||
## Original Query: SELECT luhn_check('8112189876'); | ||
## PySpark 3.5.5 Result: {'luhn_check(8112189876)': True, 'typeof(luhn_check(8112189876))': 'boolean', 'typeof(8112189876)': 'string'} | ||
#query | ||
#SELECT luhn_check('8112189876'::string); | ||
query B | ||
SELECT luhn_check('8112189876'::string); | ||
---- | ||
true | ||
|
||
query B | ||
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. can we also add test with extra large numbers, not valid numbers, fractional, exponential and negative? |
||
SELECT luhn_check(a) | ||
FROM VALUES | ||
('12344'), | ||
('12345'), | ||
('1234 4'), | ||
(NULL) AS t(a); | ||
---- | ||
true | ||
false | ||
false | ||
NULL |
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 wonder if we can come up with a good pattern to test these functions with the different kinds of strings
For example the main datafusion code uses this pattern: https://github.com/apache/datafusion/blob/main/datafusion/sqllogictest/test_files/string/README.md
Maybe we could do something similar for string functions in Spark (so we dont have to maintain 3 sets of expected outputs)