Skip to content

Commit

Permalink
Fix handling JSON type when reading FlatGeobuf (#901)
Browse files Browse the repository at this point in the history
Closes #821
  • Loading branch information
kylebarron authored Dec 4, 2024
1 parent 29d3491 commit 901c8ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
Binary file added fixtures/flatgeobuf/alldatatypes.fgb
Binary file not shown.
39 changes: 21 additions & 18 deletions rust/geoarrow/src/io/flatgeobuf/reader/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,29 +189,32 @@ mod test {
));
}

#[ignore = "fails on JSON columns"]
#[test]
fn test_all_datatypes() {
let mut filein =
BufReader::new(File::open("fixtures/flatgeobuf/alldatatypes.fgb").unwrap());
let table = read_flatgeobuf(&mut filein, Default::default()).unwrap();

let _geom_col = table.geometry_column(None).unwrap();
// assert!(matches!(geom_col.data_type(), NativeType::Polygon(_, _)));

// let (batches, schema) = table.into_inner();
// assert_eq!(batches[0].num_rows(), 10);
// assert!(matches!(
// schema.field_with_name("AREA").unwrap().data_type(),
// DataType::Float64
// ));
// assert!(matches!(
// schema.field_with_name("EAS_ID").unwrap().data_type(),
// DataType::Int64
// ));
// assert!(matches!(
// schema.field_with_name("PRFEDEA").unwrap().data_type(),
// DataType::Utf8
// ));
let geom_col = table.geometry_column(None).unwrap();
assert!(matches!(geom_col.data_type(), NativeType::Point(_, _)));

let (batches, schema) = table.into_inner();
assert_eq!(batches[0].num_rows(), 1);
assert!(matches!(
schema.field_with_name("byte").unwrap().data_type(),
DataType::Int8
));
assert!(matches!(
schema.field_with_name("float").unwrap().data_type(),
DataType::Float32
));
assert!(matches!(
schema.field_with_name("json").unwrap().data_type(),
DataType::Utf8
));
assert!(matches!(
schema.field_with_name("binary").unwrap().data_type(),
DataType::Binary
));
}
}
14 changes: 11 additions & 3 deletions rust/geoarrow/src/io/geozero/table/builder/anyvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,17 @@ impl AnyBuilder {
}
}

pub fn from_data_type_with_capacity(data_type: &DataType, capacity: usize) -> Self {
pub fn from_field_with_capacity(field: &Field, capacity: usize) -> Self {
use AnyBuilder::*;
match data_type {

// Short circuit check for JSON type
if let Some(ext_val) = field.metadata().get("ARROW:extension:name") {
if ext_val.as_str() == "arrow.json" {
return Json(StringBuilder::with_capacity(capacity, 0));
}
}

match field.data_type() {
DataType::Boolean => Bool(BooleanBuilder::with_capacity(capacity)),
DataType::Int8 => Int8(Int8Builder::with_capacity(capacity)),
DataType::UInt8 => Uint8(UInt8Builder::with_capacity(capacity)),
Expand All @@ -146,7 +154,7 @@ impl AnyBuilder {
TimestampMicrosecondBuilder::with_capacity(capacity),
tz.clone(),
)),
_ => todo!("Unsupported type {data_type}"),
_ => todo!("Unsupported type {}", field.data_type()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion rust/geoarrow/src/io/geozero/table/builder/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl PropertiesBatchBuilder {
for field in schema.fields().iter() {
columns.insert(
field.name().clone(),
AnyBuilder::from_data_type_with_capacity(field.data_type(), capacity),
AnyBuilder::from_field_with_capacity(field, capacity),
);
}

Expand Down

0 comments on commit 901c8ab

Please sign in to comment.