-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat:implement sql style 'ends_with' and 'instr' string function #8862
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
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
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 |
---|---|---|
|
@@ -543,6 +543,15 @@ pub fn create_physical_fun( | |
internal_err!("Unsupported data type {other:?} for function initcap") | ||
} | ||
}), | ||
BuiltinScalarFunction::InStr => Arc::new(|args| match args[0].data_type() { | ||
DataType::Utf8 => { | ||
make_scalar_function(string_expressions::instr::<i32>)(args) | ||
} | ||
DataType::LargeUtf8 => { | ||
make_scalar_function(string_expressions::instr::<i64>)(args) | ||
} | ||
other => internal_err!("Unsupported data type {other:?} for function instr"), | ||
}), | ||
BuiltinScalarFunction::Left => Arc::new(|args| match args[0].data_type() { | ||
DataType::Utf8 => { | ||
let func = invoke_if_unicode_expressions_feature_flag!(left, i32, "left"); | ||
|
@@ -765,6 +774,17 @@ pub fn create_physical_fun( | |
internal_err!("Unsupported data type {other:?} for function starts_with") | ||
} | ||
}), | ||
BuiltinScalarFunction::EndsWith => Arc::new(|args| match args[0].data_type() { | ||
DataType::Utf8 => { | ||
make_scalar_function(string_expressions::ends_with::<i32>)(args) | ||
} | ||
DataType::LargeUtf8 => { | ||
make_scalar_function(string_expressions::ends_with::<i64>)(args) | ||
} | ||
other => { | ||
internal_err!("Unsupported data type {other:?} for function ends_with") | ||
} | ||
}), | ||
BuiltinScalarFunction::Strpos => Arc::new(|args| match args[0].data_type() { | ||
DataType::Utf8 => { | ||
let func = invoke_if_unicode_expressions_feature_flag!( | ||
|
@@ -987,7 +1007,7 @@ mod tests { | |
use arrow::{ | ||
array::{ | ||
Array, ArrayRef, BinaryArray, BooleanArray, Float32Array, Float64Array, | ||
Int32Array, StringArray, UInt64Array, | ||
Int32Array, Int64Array, StringArray, UInt64Array, | ||
}, | ||
datatypes::Field, | ||
record_batch::RecordBatch, | ||
|
@@ -1379,6 +1399,95 @@ mod tests { | |
Utf8, | ||
StringArray | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit("abc"), lit("b")], | ||
Ok(Some(2)), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit("abc"), lit("c")], | ||
Ok(Some(3)), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit("abc"), lit("d")], | ||
Ok(Some(0)), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
alamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
InStr, | ||
&[lit("abc"), lit("")], | ||
Ok(Some(1)), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit("Helloworld"), lit("world")], | ||
Ok(Some(6)), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit("Helloworld"), lit(ScalarValue::Utf8(None))], | ||
Ok(None), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[lit(ScalarValue::Utf8(None)), lit("Hello")], | ||
Ok(None), | ||
i32, | ||
Int32, | ||
Int32Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[ | ||
lit(ScalarValue::LargeUtf8(Some("Helloworld".to_string()))), | ||
lit(ScalarValue::LargeUtf8(Some("world".to_string()))) | ||
], | ||
Ok(Some(6)), | ||
i64, | ||
Int64, | ||
Int64Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[ | ||
lit(ScalarValue::LargeUtf8(None)), | ||
lit(ScalarValue::LargeUtf8(Some("world".to_string()))) | ||
], | ||
Ok(None), | ||
i64, | ||
Int64, | ||
Int64Array | ||
); | ||
test_function!( | ||
InStr, | ||
&[ | ||
lit(ScalarValue::LargeUtf8(Some("Helloworld".to_string()))), | ||
lit(ScalarValue::LargeUtf8(None)) | ||
], | ||
Ok(None), | ||
i64, | ||
Int64, | ||
Int64Array | ||
); | ||
#[cfg(feature = "unicode_expressions")] | ||
test_function!( | ||
Left, | ||
|
@@ -2497,6 +2606,38 @@ mod tests { | |
Boolean, | ||
BooleanArray | ||
); | ||
test_function!( | ||
EndsWith, | ||
&[lit("alphabet"), lit("alph"),], | ||
Ok(Some(false)), | ||
bool, | ||
Boolean, | ||
BooleanArray | ||
); | ||
test_function!( | ||
EndsWith, | ||
&[lit("alphabet"), lit("bet"),], | ||
Ok(Some(true)), | ||
bool, | ||
Boolean, | ||
BooleanArray | ||
); | ||
test_function!( | ||
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. 💯 for testing with NULL |
||
EndsWith, | ||
&[lit(ScalarValue::Utf8(None)), lit("alph"),], | ||
Ok(None), | ||
bool, | ||
Boolean, | ||
BooleanArray | ||
); | ||
test_function!( | ||
EndsWith, | ||
&[lit("alphabet"), lit(ScalarValue::Utf8(None)),], | ||
Ok(None), | ||
bool, | ||
Boolean, | ||
BooleanArray | ||
); | ||
#[cfg(feature = "unicode_expressions")] | ||
test_function!( | ||
Strpos, | ||
|
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.
PostgreSQL has
POSITION
(see https://www.postgresql.org/docs/9.1/functions-string.html)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.
Does the function name we implement need to follow postgresql? If so, I will change the name of this function
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.
Spark has both position and instr and they are not quite the same signature:
https://spark.apache.org/docs/latest/api/sql/#position
https://spark.apache.org/docs/latest/api/sql/#instr