Skip to content

Commit d0da5a5

Browse files
committed
feat: add test for projection skipping unknown fields in JSON reader
1 parent 4275af5 commit d0da5a5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

arrow-json/src/reader/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,39 @@ mod tests {
18091809
);
18101810
}
18111811

1812+
#[test]
1813+
fn test_projection_skip_unknown_fields() {
1814+
// JSON has fields a, b, c but schema only has a, c
1815+
let buf = r#"
1816+
{"a": 1, "b": "ignored", "c": true}
1817+
{"a": 2, "b": "also ignored", "c": false}
1818+
"#;
1819+
1820+
let schema = Arc::new(Schema::new(vec![
1821+
Field::new("a", DataType::Int32, true),
1822+
Field::new("c", DataType::Boolean, true),
1823+
]));
1824+
1825+
// with_projection(true): skip unknown field "b" and succeed
1826+
let batch = ReaderBuilder::new(schema)
1827+
.with_projection(true)
1828+
.build(Cursor::new(buf.as_bytes()))
1829+
.unwrap()
1830+
.read()
1831+
.unwrap()
1832+
.unwrap();
1833+
1834+
assert_eq!(batch.num_rows(), 2);
1835+
assert_eq!(batch.num_columns(), 2);
1836+
1837+
let a = batch.column(0).as_primitive::<Int32Type>();
1838+
assert_eq!(a.values(), &[1, 2]);
1839+
1840+
let c = batch.column(1).as_boolean();
1841+
assert!(c.value(0));
1842+
assert!(!c.value(1));
1843+
}
1844+
18121845
fn read_file(path: &str, schema: Option<Schema>) -> Reader<BufReader<File>> {
18131846
let file = File::open(path).unwrap();
18141847
let mut reader = BufReader::new(file);

0 commit comments

Comments
 (0)