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

Pass schema_infer_max_records to JsonFormat. #6945

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions datafusion/core/src/datasource/file_format/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ impl ReadOptions<'_> for ParquetReadOptions<'_> {
impl ReadOptions<'_> for NdJsonReadOptions<'_> {
fn to_listing_options(&self, config: &SessionConfig) -> ListingOptions {
let file_format = JsonFormat::default()
.with_schema_infer_max_rec(Some(self.schema_infer_max_records))
.with_file_compression_type(self.file_compression_type.to_owned());

ListingOptions::new(Arc::new(file_format))
Expand Down
30 changes: 30 additions & 0 deletions datafusion/core/src/datasource/physical_plan/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,34 @@ mod tests {
assert_eq!("Arrow error: Parser error: Error while parsing value d for column 0 at line 4", format!("{e}"));
Ok(())
}

#[tokio::test]
async fn ndjson_schema_infer_max_records() -> Result<()> {
async fn read_test_data(schema_infer_max_records: usize) -> Result<SchemaRef> {
let ctx = SessionContext::new();

let options = NdJsonReadOptions {
schema_infer_max_records,
..Default::default()
};

let batches = ctx
.read_json("tests/data/4.json", options)
.await?
.collect()
.await?;

Ok(batches[0].schema())
}

// Use only the first 2 rows to infer the schema, those have 2 fields.
let schema = read_test_data(2).await?;
assert_eq!(schema.fields().len(), 2);

// Use all rows to infer the schema, those have 5 fields.
let schema = read_test_data(10).await?;
assert_eq!(schema.fields().len(), 5);

Ok(())
}
}
4 changes: 4 additions & 0 deletions datafusion/core/tests/data/4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"a":1, "b":[2.0, 1.3, -6.1]}
{"a":2, "b":[3.0, 4.3]}
{"c":[false, true], "d":{"c1": 23, "c2": 32}}
{"e": {"e1": 2, "e2": 12.3}}