Skip to content

Commit

Permalink
Fix inferring object after field was null. (#5216)
Browse files Browse the repository at this point in the history
  • Loading branch information
kskalski committed Dec 19, 2023
1 parent 9e060dc commit c0b8055
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion arrow-json/src/reader/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ fn collect_field_types_from_object(
set_object_scalar_field_type(field_types, k, DataType::Utf8)?;
}
Value::Object(inner_map) => {
if !field_types.contains_key(k) {
if let InferredType::Any = field_types.get(k).unwrap_or(&InferredType::Any) {
field_types.insert(k.to_string(), InferredType::Object(HashMap::new()));
}
match field_types.get_mut(k).unwrap() {
Expand Down Expand Up @@ -719,4 +719,24 @@ mod tests {
]);
assert_eq!(inferred_schema, schema);
}

#[test]
fn test_infer_from_null_then_object() {
let data = r#"
{"obj":null}
{"obj":{"foo":1}}
"#;
let (inferred_schema, _) =
infer_json_schema_from_seekable(Cursor::new(data), None).expect("infer");
let schema = Schema::new(vec![Field::new(
"obj",
DataType::Struct(
[Field::new("foo", DataType::Int64, true)]
.into_iter()
.collect(),
),
true,
)]);
assert_eq!(inferred_schema, schema);
}
}

0 comments on commit c0b8055

Please sign in to comment.