-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[WIP] Explore fancier custom JSON decoding #9259
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
Draft
scovich
wants to merge
16
commits into
apache:main
Choose a base branch
from
scovich:decoder-factory-json-extras
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.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
21581ea
Add custom decoder in arrow-json
cht42 5fe3642
include field
debugmiller 53bffa2
change export
debugmiller 66b7512
cleanup
debugmiller 6aeb892
more obvious
debugmiller 93ad08c
fix doctest and more exhaustive check
debugmiller 1fb7c47
more lint fixes
debugmiller e48e1c9
Merge branch 'main' into decoder-factory-json
debugmiller 41a0dc2
Merge remote-tracking branch 'oss/main' into decoder-factory-json-extras
scovich 1f50f34
move extension type construction logic out of Field
scovich 6290db9
checkpoint - reworked signatures
scovich 06fb5f5
checkpoint - add metadata stripping
scovich f7b36d7
Ditch annotations. Got it working
scovich a5cad3f
Fix plumbing for nested factories
scovich 41583ef
got it all working nice!
scovich 1994cd5
tidy up
scovich 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,8 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use crate::StructMode; | ||
| use crate::reader::tape::{Tape, TapeElement}; | ||
| use crate::reader::{ArrayDecoder, make_decoder}; | ||
| use crate::reader::{ArrayDecoder, DecoderContext}; | ||
| use arrow_array::OffsetSizeTrait; | ||
| use arrow_array::builder::{BooleanBufferBuilder, BufferBuilder}; | ||
| use arrow_buffer::buffer::NullBuffer; | ||
|
|
@@ -34,27 +33,19 @@ pub struct ListArrayDecoder<O> { | |
|
|
||
| impl<O: OffsetSizeTrait> ListArrayDecoder<O> { | ||
| pub fn new( | ||
| data_type: DataType, | ||
| coerce_primitive: bool, | ||
| strict_mode: bool, | ||
| ctx: &DecoderContext, | ||
| data_type: &DataType, | ||
| is_nullable: bool, | ||
| struct_mode: StructMode, | ||
| ) -> Result<Self, ArrowError> { | ||
| let field = match &data_type { | ||
| let field = match data_type { | ||
| DataType::List(f) if !O::IS_LARGE => f, | ||
| DataType::LargeList(f) if O::IS_LARGE => f, | ||
| _ => unreachable!(), | ||
| }; | ||
| let decoder = make_decoder( | ||
| field.data_type().clone(), | ||
| coerce_primitive, | ||
| strict_mode, | ||
| field.is_nullable(), | ||
| struct_mode, | ||
| )?; | ||
| let decoder = ctx.make_decoder(field.data_type(), field.is_nullable(), field.metadata())?; | ||
|
|
||
| Ok(Self { | ||
| data_type, | ||
| data_type: data_type.clone(), | ||
|
Contributor
Author
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. leaf-level clone needed since we no longer pass owned DataType instances |
||
| decoder, | ||
| phantom: Default::default(), | ||
| is_nullable, | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -15,9 +15,8 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use crate::StructMode; | ||
| use crate::reader::tape::{Tape, TapeElement}; | ||
| use crate::reader::{ArrayDecoder, make_decoder}; | ||
| use crate::reader::{ArrayDecoder, DecoderContext}; | ||
| use arrow_array::builder::{BooleanBufferBuilder, BufferBuilder}; | ||
| use arrow_buffer::ArrowNativeType; | ||
| use arrow_buffer::buffer::NullBuffer; | ||
|
|
@@ -33,46 +32,41 @@ pub struct MapArrayDecoder { | |
|
|
||
| impl MapArrayDecoder { | ||
| pub fn new( | ||
| data_type: DataType, | ||
| coerce_primitive: bool, | ||
| strict_mode: bool, | ||
| ctx: &DecoderContext, | ||
| data_type: &DataType, | ||
| is_nullable: bool, | ||
| struct_mode: StructMode, | ||
| ) -> Result<Self, ArrowError> { | ||
| let fields = match &data_type { | ||
| DataType::Map(_, true) => { | ||
| return Err(ArrowError::NotYetImplemented( | ||
| "Decoding MapArray with sorted fields".to_string(), | ||
| )); | ||
| let DataType::Map(f, false) = data_type else { | ||
| return Err(ArrowError::NotYetImplemented( | ||
| "Decoding MapArray with sorted fields".to_string(), | ||
| )); | ||
| }; | ||
|
Comment on lines
-42
to
+43
Contributor
Author
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. opportunistic simplification |
||
|
|
||
| // TODO: Once MSRV bumps to 1.88+, use an if-let chain to directly unpack the slice | ||
| let fields = match f.data_type() { | ||
| DataType::Struct(fields) if fields.len() == 2 => fields, | ||
| d => { | ||
| return Err(ArrowError::InvalidArgumentError(format!( | ||
| "MapArray must contain struct with two fields, got {d}" | ||
| ))); | ||
| } | ||
| DataType::Map(f, _) => match f.data_type() { | ||
| DataType::Struct(fields) if fields.len() == 2 => fields, | ||
| d => { | ||
| return Err(ArrowError::InvalidArgumentError(format!( | ||
| "MapArray must contain struct with two fields, got {d}" | ||
| ))); | ||
| } | ||
| }, | ||
| _ => unreachable!(), | ||
| }; | ||
|
|
||
| let keys = make_decoder( | ||
| fields[0].data_type().clone(), | ||
| coerce_primitive, | ||
| strict_mode, | ||
| fields[0].is_nullable(), | ||
| struct_mode, | ||
| let (key_field, value_field) = (&fields[0], &fields[1]); | ||
|
|
||
| let keys = ctx.make_decoder( | ||
| key_field.data_type(), | ||
| key_field.is_nullable(), | ||
| key_field.metadata(), | ||
| )?; | ||
| let values = make_decoder( | ||
| fields[1].data_type().clone(), | ||
| coerce_primitive, | ||
| strict_mode, | ||
| fields[1].is_nullable(), | ||
| struct_mode, | ||
| let values = ctx.make_decoder( | ||
| value_field.data_type(), | ||
| value_field.is_nullable(), | ||
| value_field.metadata(), | ||
| )?; | ||
|
|
||
| Ok(Self { | ||
| data_type, | ||
| data_type: data_type.clone(), | ||
| keys, | ||
| values, | ||
| is_nullable, | ||
|
|
@@ -82,12 +76,11 @@ impl MapArrayDecoder { | |
|
|
||
| impl ArrayDecoder for MapArrayDecoder { | ||
| fn decode(&mut self, tape: &Tape<'_>, pos: &[u32]) -> Result<ArrayData, ArrowError> { | ||
| let s = match &self.data_type { | ||
| DataType::Map(f, _) => match f.data_type() { | ||
| s @ DataType::Struct(_) => s, | ||
| _ => unreachable!(), | ||
| }, | ||
| _ => unreachable!(), | ||
| let DataType::Map(f, _) = &self.data_type else { | ||
| unreachable!() | ||
| }; | ||
| let s @ DataType::Struct(_) = f.data_type() else { | ||
| unreachable!() | ||
| }; | ||
|
|
||
| let mut offsets = BufferBuilder::<i32>::new(pos.len() + 1); | ||
|
|
||
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.
I changed all the decoder paths to pass borrowed refs because it was leading to a lot of clone calls otherwise.
Plus, one of the example decoders relies on the datatype instances to have stable memory locations during the recursive
make_decodercall.