-
Notifications
You must be signed in to change notification settings - Fork 12
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
feat: implement json_get_array UDF #36
Open
vigimite
wants to merge
7
commits into
datafusion-contrib:main
Choose a base branch
from
vigimite:main
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
531e9ca
feat: implement json_get_array UDF
vigimite 9807bc4
add column structs
vigimite 94e9ddd
add tests, run clippy fix
vigimite 02ab134
remove async from create_test_table
vigimite d1144b9
implement using JsonArrayField
vigimite a74c57d
revert behavior change
vigimite 7ea7f9b
resolve merge conflict
vigimite 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 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 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 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,95 @@ | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use arrow::array::ListArray; | ||
use arrow_schema::{DataType, Field}; | ||
use datafusion_common::arrow::array::ArrayRef; | ||
use datafusion_common::{Result as DataFusionResult, ScalarValue}; | ||
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||
use jiter::Peek; | ||
|
||
use crate::common::{check_args, get_err, invoke, jiter_json_find, GetError, JsonPath}; | ||
use crate::common_macros::make_udf_function; | ||
use crate::common_union::{JsonArrayField, JsonUnion}; | ||
|
||
make_udf_function!( | ||
JsonGetArray, | ||
json_get_array, | ||
json_data path, | ||
r#"Get an arrow array value from a JSON string by its "path""# | ||
); | ||
|
||
#[derive(Debug)] | ||
pub(super) struct JsonGetArray { | ||
signature: Signature, | ||
aliases: [String; 1], | ||
} | ||
|
||
impl Default for JsonGetArray { | ||
fn default() -> Self { | ||
Self { | ||
signature: Signature::variadic_any(Volatility::Immutable), | ||
aliases: ["json_get_array".to_string()], | ||
} | ||
} | ||
} | ||
|
||
impl ScalarUDFImpl for JsonGetArray { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn name(&self) -> &str { | ||
self.aliases[0].as_str() | ||
} | ||
|
||
fn signature(&self) -> &Signature { | ||
&self.signature | ||
} | ||
|
||
fn return_type(&self, arg_types: &[DataType]) -> DataFusionResult<DataType> { | ||
check_args(arg_types, self.name()).map(|()| DataType::List(Field::new("item", DataType::Utf8, true).into())) | ||
} | ||
|
||
fn invoke(&self, args: &[ColumnarValue]) -> DataFusionResult<ColumnarValue> { | ||
let to_array = |c: JsonUnion| { | ||
let array: ListArray = c.try_into()?; | ||
Ok(Arc::new(array) as ArrayRef) | ||
}; | ||
|
||
invoke::<JsonUnion, JsonArrayField>(args, jiter_json_get_array, to_array, |i| { | ||
i.map_or_else(|| ScalarValue::Null, Into::into) | ||
}) | ||
} | ||
|
||
fn aliases(&self) -> &[String] { | ||
&self.aliases | ||
} | ||
} | ||
|
||
fn jiter_json_get_array(json_data: Option<&str>, path: &[JsonPath]) -> Result<JsonArrayField, GetError> { | ||
if let Some((mut jiter, peek)) = jiter_json_find(json_data, path) { | ||
match peek { | ||
Peek::Array => { | ||
let mut peek_opt = jiter.known_array()?; | ||
let mut elements = Vec::new(); | ||
|
||
while let Some(peek) = peek_opt { | ||
let start = jiter.current_index(); | ||
jiter.known_skip(peek)?; | ||
let object_slice = jiter.slice_to_current(start); | ||
let object_string = std::str::from_utf8(object_slice)?; | ||
|
||
elements.push(object_string.to_owned()); | ||
|
||
peek_opt = jiter.array_step()?; | ||
} | ||
|
||
Ok(JsonArrayField(elements)) | ||
} | ||
_ => get_err!(), | ||
} | ||
} else { | ||
get_err!() | ||
} | ||
} |
This file contains 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 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.
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.
moved down to the JsonUnionField definition