This is an automated email from the ASF dual-hosted git repository.
tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/master by this push:
new 628920f8e Relax JSON schema inference generics (#4063)
628920f8e is described below
commit 628920f8e70744c71d8f8978862b316cf9620441
Author: Raphael Taylor-Davies <[email protected]>
AuthorDate: Fri Apr 14 22:23:16 2023 +0100
Relax JSON schema inference generics (#4063)
* Relax JSON schema inference generics
* Clippy
---
arrow-json/src/reader/schema.rs | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/arrow-json/src/reader/schema.rs b/arrow-json/src/reader/schema.rs
index 22d25c8be..427c20e02 100644
--- a/arrow-json/src/reader/schema.rs
+++ b/arrow-json/src/reader/schema.rs
@@ -20,7 +20,7 @@ use indexmap::map::IndexMap as HashMap;
use indexmap::set::IndexSet as HashSet;
use serde_json::Value;
use std::borrow::Borrow;
-use std::io::{BufRead, BufReader, Read, Seek};
+use std::io::{BufRead, Seek};
use std::sync::Arc;
#[derive(Debug, Clone)]
@@ -147,17 +147,17 @@ fn generate_schema(spec: HashMap<String, InferredType>)
-> Result<Schema, ArrowE
/// }
/// ```
#[derive(Debug)]
-pub struct ValueIter<'a, R: Read> {
- reader: &'a mut BufReader<R>,
+pub struct ValueIter<R: BufRead> {
+ reader: R,
max_read_records: Option<usize>,
record_count: usize,
// reuse line buffer to avoid allocation on each record
line_buf: String,
}
-impl<'a, R: Read> ValueIter<'a, R> {
+impl<R: BufRead> ValueIter<R> {
/// Creates a new `ValueIter`
- pub fn new(reader: &'a mut BufReader<R>, max_read_records: Option<usize>)
-> Self {
+ pub fn new(reader: R, max_read_records: Option<usize>) -> Self {
Self {
reader,
max_read_records,
@@ -167,7 +167,7 @@ impl<'a, R: Read> ValueIter<'a, R> {
}
}
-impl<'a, R: Read> Iterator for ValueIter<'a, R> {
+impl<R: BufRead> Iterator for ValueIter<R> {
type Item = Result<Value, ArrowError>;
fn next(&mut self) -> Option<Self::Item> {
@@ -228,11 +228,11 @@ impl<'a, R: Read> Iterator for ValueIter<'a, R> {
/// ```
///
/// [`Reader`]: super::Reader
-pub fn infer_json_schema_from_seekable<R: Read + Seek>(
- reader: &mut BufReader<R>,
+pub fn infer_json_schema_from_seekable<R: BufRead + Seek>(
+ mut reader: R,
max_read_records: Option<usize>,
) -> Result<Schema, ArrowError> {
- let schema = infer_json_schema(reader, max_read_records);
+ let schema = infer_json_schema(&mut reader, max_read_records);
// return the reader seek back to the start
reader.rewind()?;
@@ -265,8 +265,8 @@ pub fn infer_json_schema_from_seekable<R: Read + Seek>(
/// // seek back to start so that the original file is usable again
/// file.seek(SeekFrom::Start(0)).unwrap();
/// ```
-pub fn infer_json_schema<R: Read>(
- reader: &mut BufReader<R>,
+pub fn infer_json_schema<R: BufRead>(
+ reader: R,
max_read_records: Option<usize>,
) -> Result<Schema, ArrowError> {
infer_json_schema_from_iterator(ValueIter::new(reader, max_read_records))
@@ -515,7 +515,7 @@ mod tests {
use super::*;
use flate2::read::GzDecoder;
use std::fs::File;
- use std::io::Cursor;
+ use std::io::{BufReader, Cursor};
#[test]
fn test_json_infer_schema() {
@@ -700,8 +700,7 @@ mod tests {
#[test]
fn test_invalid_json_infer_schema() {
- let re =
- infer_json_schema_from_seekable(&mut
BufReader::new(Cursor::new(b"}")), None);
+ let re = infer_json_schema_from_seekable(Cursor::new(b"}"), None);
assert_eq!(
re.err().unwrap().to_string(),
"Json error: Not valid JSON: expected value at line 1 column 1",