Skip to content
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

RecordBatch normalization (flattening) #6758

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Built out a bit more of the iterative normalize.
nglime committed Nov 23, 2024
commit 55eb9533f7fd0da4b6b36e9cc7d059191e90f250
72 changes: 42 additions & 30 deletions arrow-array/src/record_batch.rs
Original file line number Diff line number Diff line change
@@ -18,9 +18,11 @@
//! A two-dimensional batch of column-oriented data with a defined
//! [schema](arrow_schema::Schema).

use std::collections::{BinaryHeap, VecDeque};
use crate::{new_empty_array, Array, ArrayRef, StructArray};
use arrow_schema::{ArrowError, DataType, Field, FieldRef, Fields, Schema, SchemaBuilder, SchemaRef};
use arrow_schema::{
ArrowError, DataType, Field, FieldRef, Fields, Schema, SchemaBuilder, SchemaRef,
};
use std::collections::VecDeque;
use std::ops::{Deref, Index};
use std::sync::Arc;

@@ -412,51 +414,61 @@ impl RecordBatch {
}
if self.num_rows() == 0 {
ngli-me marked this conversation as resolved.
Show resolved Hide resolved
// No data, only need to normalize the schema
return Ok(Self::new_empty(Arc::new(self.schema.normalize(separator, max_level)?)));
return Ok(Self::new_empty(Arc::new(
self.schema.normalize(separator, max_level)?,
)));
}
let mut queue: VecDeque<(usize, &Arc<dyn Array>, &FieldRef)> = VecDeque::new();

// push fields
for (c, f) in self.columns.iter().zip(self.schema().fields()) {
for (c, f) in self.columns.iter().zip(self.schema.fields()) {
queue.push_front((0, c, f));
}

while !queue.is_empty() {
match queue.pop_front() {
Some((depth, c, f)) => {
match f.data_type() {
//DataType::List(f) => field,
//DataType::ListView(_) => field,
//DataType::FixedSizeList(_, _) => field,
//DataType::LargeList(_) => field,
//DataType::LargeListView(_) => field,
DataType::Struct(nested_fields) => {
let field_name = f.name().as_str();
/*new_fields = [
new_fields,
Self::normalizer(
nested_fields.to_vec(),
field_name,
separator,
max_level - 1,
),
]
.concat();*/

if depth < max_level {
match (c.data_type(), f.data_type()) {
//DataType::List(f) => field,
ngli-me marked this conversation as resolved.
Show resolved Hide resolved
//DataType::ListView(_) => field,
//DataType::FixedSizeList(_, _) => field,
//DataType::LargeList(_) => field,
//DataType::LargeListView(_) => field,
(DataType::Struct(cf), DataType::Struct(ff)) => {
let field_name = f.name().as_str();
let new_key = format!("{key_string}{separator}{field_name}");
ff.iter().rev().zip(cf.iter().rev()).map(|(field, ())| {
let updated_field = Field::new(
format!("{key_string}{separator}{}", field.name()),
field.data_type().clone(),
field.is_nullable(),
);
queue.push_front((
depth + 1,
c, // TODO: need to modify c -- if it's a StructArray, it needs to have the fields modified.
&Arc::new(updated_field),
))
});
}
//DataType::Union(_, _) => field,
//DataType::Dictionary(_, _) => field,
//DataType::Map(_, _) => field,
//DataType::RunEndEncoded(_, _) => field, // not sure how to support this field
_ => queue.push_front((depth, c, f)),
}
//DataType::Union(_, _) => field,
//DataType::Dictionary(_, _) => field,
//DataType::Map(_, _) => field,
//DataType::RunEndEncoded(_, _) => field, // not sure how to support this field
_ => queue.push_front((0, c, f)),
} else {
queue.push_front((depth, c, f));
}
},
}
None => break,
ngli-me marked this conversation as resolved.
Show resolved Hide resolved
};
}
todo!()
}

/// Returns the number of columns in the record batch.
/// Returns the number of columns in the record batch.
///
/// # Example
///
@@ -1269,12 +1281,12 @@ mod tests {
let n_legs_field = Arc::new(Field::new("n_legs", DataType::Int64, true));
let year_field = Arc::new(Field::new("year", DataType::Int64, true));


let a = Arc::new(StructArray::from(vec![
(animals_field.clone(), Arc::new(animals) as ArrayRef),
(n_legs_field.clone(), Arc::new(n_legs) as ArrayRef),
(year_field.clone(), Arc::new(year) as ArrayRef),
]));

let month = Arc::new(Int64Array::from(vec![Some(4), Some(6)]));

let schema = Schema::new(vec![