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

Fix generate_unions_case for Rust case #1677

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions arrow/src/datatypes/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,52 @@ impl DataType {
))
}
}
Some(s) if s == "union" => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't parse union type from json before.

if let Some(Value::String(mode)) = map.get("mode") {
let union_mode = if mode == "SPARSE" {
UnionMode::Sparse
} else if mode == "DENSE" {
UnionMode::Dense
} else {
return Err(ArrowError::ParseError(format!(
"Unknown union mode {:?} for union",
mode
)));
};
if let Some(type_ids) = map.get("typeIds") {
let type_ids = type_ids
.as_array()
.unwrap()
.iter()
.map(|t| t.as_i64().unwrap())
.collect::<Vec<_>>();

let default_fields = type_ids
.iter()
.map(|t| {
Field::new("", DataType::Boolean, true).with_metadata(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it cool to have a field without a name 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only a placeholder, as above other nested types (list, map, etc) do. The actual fields will replace them (Field::from).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because, type (union, map, list, etc) is defined in "type" of the Json map, fields are defined in "children" of the map. We parse "type" to get the nested type and parse "children" to get actual fields after that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation

Some(
[("type_id".to_string(), t.to_string())]
.iter()
.cloned()
.collect(),
),
)
})
.collect::<Vec<_>>();

Ok(DataType::Union(default_fields, union_mode))
} else {
Err(ArrowError::ParseError(
"Expecting a typeIds for union ".to_string(),
))
}
} else {
Err(ArrowError::ParseError(
"Expecting a mode for union".to_string(),
))
}
}
Some(other) => Err(ArrowError::ParseError(format!(
"invalid or unsupported type name: {} in {:?}",
other, json
Expand Down
24 changes: 24 additions & 0 deletions arrow/src/datatypes/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,30 @@ impl Field {
}
}
}
DataType::Union(fields, mode) => match map.get("children") {
Some(Value::Array(values)) => {
let mut union_fields: Vec<Field> =
values.iter().map(Field::from).collect::<Result<_>>()?;
fields.iter().zip(union_fields.iter_mut()).for_each(
|(f, union_field)| {
union_field.set_metadata(Some(
f.metadata().unwrap().clone(),
));
},
);
DataType::Union(union_fields, mode)
}
Some(_) => {
return Err(ArrowError::ParseError(
"Field 'children' must be an array".to_string(),
))
}
None => {
return Err(ArrowError::ParseError(
"Field missing 'children' attribute".to_string(),
));
}
},
_ => data_type,
};

Expand Down
4 changes: 4 additions & 0 deletions arrow/src/util/integration_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ pub struct ArrowJsonColumn {
pub data: Option<Vec<Value>>,
#[serde(rename = "OFFSET")]
pub offset: Option<Vec<Value>>, // leaving as Value as 64-bit offsets are strings
#[serde(rename = "TYPE_ID")]
pub type_id: Option<Vec<i8>>,
pub children: Option<Vec<ArrowJsonColumn>>,
}

Expand Down Expand Up @@ -472,6 +474,7 @@ impl ArrowJsonBatch {
validity: Some(validity),
data: Some(data),
offset: None,
type_id: None,
children: None,
}
}
Expand All @@ -481,6 +484,7 @@ impl ArrowJsonBatch {
validity: None,
data: None,
offset: None,
type_id: None,
children: None,
},
};
Expand Down
55 changes: 55 additions & 0 deletions integration-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,61 @@ fn array_from_json(
let array = MapArray::from(array_data);
Ok(Arc::new(array))
}
DataType::Union(fields, _) => {
let field_type_ids = fields
.iter()
.enumerate()
.into_iter()
.map(|(idx, f)| {
(
f.metadata()
.and_then(|m| m.get("type_id"))
.unwrap()
.parse::<i8>()
.unwrap(),
idx,
)
})
.collect::<HashMap<_, _>>();

let type_ids = if let Some(type_id) = json_col.type_id {
type_id
.iter()
.map(|t| {
if field_type_ids.contains_key(t) {
Ok(*(field_type_ids.get(t).unwrap()) as i8)
} else {
Err(ArrowError::JsonError(format!(
"Unable to find type id {:?}",
t
)))
}
})
.collect::<Result<_>>()?
} else {
vec![]
};

let offset: Option<Buffer> = json_col.offset.map(|offsets| {
let offsets: Vec<i32> =
offsets.iter().map(|v| v.as_i64().unwrap() as i32).collect();
Buffer::from(&offsets.to_byte_slice())
});

let mut children: Vec<(Field, Arc<dyn Array>)> = vec![];
for (field, col) in fields.iter().zip(json_col.children.unwrap()) {
let array = array_from_json(field, col, dictionaries)?;
children.push((field.clone(), array));
}

let array = UnionArray::try_new(
Buffer::from(&type_ids.to_byte_slice()),
offset,
children,
)
.unwrap();
Ok(Arc::new(array))
}
t => Err(ArrowError::JsonError(format!(
"data type {:?} not supported",
t
Expand Down