-
Notifications
You must be signed in to change notification settings - Fork 2k
Spark is_valid_utf8 function implementation #21627
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
kazantsev-maksim
wants to merge
8
commits into
apache:main
Choose a base branch
from
kazantsev-maksim:is_valid_utf8
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.
+329
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c8c9bb9
Spark is_valid_utf8 function implementation
614a477
Fix tests
160d25f
Fix tests
21187d1
More tests
d5057f1
fmt
8bb0bc8
Merge branch 'main' into is_valid_utf8
kazantsev-maksim e843fcd
Merge remote-tracking branch 'origin/main' into is_valid_utf8
aa571f9
Add more tests
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,118 @@ | ||
| // 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 arrow::datatypes::{DataType, Field, FieldRef}; | ||
| use datafusion::logical_expr::{ColumnarValue, Signature, Volatility}; | ||
| use datafusion_common::{Result, internal_err}; | ||
| use datafusion_expr::{ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl}; | ||
|
|
||
| use arrow::array::{Array, ArrayRef, BooleanArray}; | ||
| use arrow::buffer::BooleanBuffer; | ||
| use datafusion_common::cast::{ | ||
| as_binary_array, as_binary_view_array, as_large_binary_array, | ||
| }; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_functions::utils::make_scalar_function; | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| /// Spark-compatible `is_valid_utf8` expression | ||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#is_valid_utf8> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkIsValidUtf8 { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for SparkIsValidUtf8 { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkIsValidUtf8 { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::uniform( | ||
| 1, | ||
| vec![ | ||
| DataType::Utf8, | ||
| DataType::LargeUtf8, | ||
| DataType::Utf8View, | ||
| DataType::Binary, | ||
| DataType::BinaryView, | ||
| DataType::LargeBinary, | ||
| ], | ||
| Volatility::Immutable, | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkIsValidUtf8 { | ||
| fn name(&self) -> &str { | ||
| "is_valid_utf8" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| internal_err!("return_field_from_args should be used instead") | ||
| } | ||
|
|
||
| fn return_field_from_args(&self, _args: ReturnFieldArgs) -> Result<FieldRef> { | ||
| Ok(Arc::new(Field::new(self.name(), DataType::Boolean, true))) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| make_scalar_function(spark_is_valid_utf8_inner, vec![])(&args.args) | ||
| } | ||
| } | ||
|
|
||
| fn spark_is_valid_utf8_inner(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
| let [array] = take_function_args("is_valid_utf8", args)?; | ||
| match array.data_type() { | ||
| DataType::Utf8 | DataType::Utf8View | DataType::LargeUtf8 => { | ||
| Ok(Arc::new(BooleanArray::new( | ||
| BooleanBuffer::new_set(array.len()), | ||
| array.nulls().cloned(), | ||
| ))) | ||
| } | ||
| DataType::Binary => Ok(Arc::new( | ||
| as_binary_array(array)? | ||
| .iter() | ||
| .map(|x| x.map(|y| str::from_utf8(y).is_ok())) | ||
| .collect::<BooleanArray>(), | ||
| )), | ||
| DataType::LargeBinary => Ok(Arc::new( | ||
| as_large_binary_array(array)? | ||
| .iter() | ||
| .map(|x| x.map(|y| str::from_utf8(y).is_ok())) | ||
| .collect::<BooleanArray>(), | ||
| )), | ||
| DataType::BinaryView => Ok(Arc::new( | ||
| as_binary_view_array(array)? | ||
| .iter() | ||
| .map(|x| x.map(|y| str::from_utf8(y).is_ok())) | ||
| .collect::<BooleanArray>(), | ||
| )), | ||
| data_type => { | ||
| internal_err!("is_valid_utf8 does not support: {data_type}") | ||
| } | ||
| } | ||
| } |
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
203 changes: 203 additions & 0 deletions
203
datafusion/sqllogictest/test_files/spark/string/is_valid_utf8.slt
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,203 @@ | ||
| # 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. | ||
|
|
||
| statement ok | ||
| CREATE TABLE test_is_valid_utf8(value STRING) AS VALUES | ||
| (arrow_cast('Hello, world!', 'Utf8')), | ||
| (arrow_cast('Spark', 'Utf8')), | ||
| (arrow_cast('DataFusion', 'Utf8')), | ||
| (arrow_cast('ASCII only 123 !@#', 'Utf8')), | ||
| (arrow_cast(NULL, 'Utf8')); | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(value) FROM test_is_valid_utf8; | ||
| ---- | ||
| true | ||
| true | ||
| true | ||
| true | ||
| NULL | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(NULL::string); | ||
| ---- | ||
| NULL | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8('Hello, world!'::string); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8('😀🎉✨'::string); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(''::string); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8('ASCII only 123 !@#'::string); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'C2A9', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'C2AE', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'E282AC', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'E284A2', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F09F9880', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F09F8E89', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'80', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'BF', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'808080', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'C2', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'E2', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F0', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'E282', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'C081', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'E08080', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F0808080', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'FE', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'FF', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'61C262', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'41BF42', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'ED9FBF', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'EDA080', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'EDBFBF', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F48FBFBF', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'F4908080', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'6162C2A963', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'6162806364', 'Binary')); | ||
| ---- | ||
| false | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'610062', 'Binary')); | ||
| ---- | ||
| true | ||
|
|
||
| query B | ||
| SELECT is_valid_utf8(arrow_cast(x'', 'Binary')); | ||
| ---- | ||
| true | ||
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.
Would be a good idea to have null test too, as well as array input test (currently these are all scalars)