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

fix: fix schema inference for json #13637

Merged
merged 1 commit into from
Jan 11, 2024
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
2 changes: 1 addition & 1 deletion crates/polars-io/src/ndjson/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn deserialize_all<'a>(
if ignore_errors {
return Ok(AnyValue::Null);
}
polars_bail!(ComputeError: "expected list/array in json value, got {}", dtype);
polars_bail!(ComputeError: "expected dtype '{}' in JSON value, got dtype: Array\n\nEncountered value: {}", dtype, json);
};
let vals: Vec<AnyValue> = arr
.iter()
Expand Down
6 changes: 6 additions & 0 deletions crates/polars-io/src/ndjson/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ where
self.low_memory = toggle;
self
}

/// Set values as `Null` if parsing fails because of schema mismatches.
pub fn with_ignore_errors(mut self, ignore_errors: bool) -> Self {
self.ignore_errors = ignore_errors;
self
}
}

impl<'a> JsonLineReader<'a, File> {
Expand Down
15 changes: 11 additions & 4 deletions crates/polars-json/src/json/infer_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Borrow;

use arrow::datatypes::{ArrowDataType, Field};
use indexmap::map::Entry;
use indexmap::IndexMap;
use simd_json::borrowed::Object;
use simd_json::{BorrowedValue, StaticNode};

Expand Down Expand Up @@ -91,7 +90,7 @@ pub(crate) fn coerce_data_type<A: Borrow<ArrowDataType>>(datatypes: &[A]) -> Arr
});
// group fields by unique
let fields = fields.iter().fold(
IndexMap::<&str, PlHashSet<&ArrowDataType>, ahash::RandomState>::default(),
PlIndexMap::<&str, PlHashSet<&ArrowDataType>>::default(),
|mut acc, field| {
match acc.entry(field.name.as_str()) {
Entry::Occupied(mut v) => {
Expand Down Expand Up @@ -132,7 +131,13 @@ pub(crate) fn coerce_data_type<A: Borrow<ArrowDataType>>(datatypes: &[A]) -> Arr
true,
)));
} else if datatypes.len() > 2 {
return LargeUtf8;
return datatypes
.iter()
.map(|dt| dt.borrow().clone())
.reduce(|a, b| coerce_data_type(&[a, b]))
.unwrap()
.borrow()
.clone();
}
let (lhs, rhs) = (datatypes[0].borrow(), datatypes[1].borrow());

Expand All @@ -142,7 +147,7 @@ pub(crate) fn coerce_data_type<A: Borrow<ArrowDataType>>(datatypes: &[A]) -> Arr
let inner = coerce_data_type(&[lhs.data_type(), rhs.data_type()]);
LargeList(Box::new(Field::new(ITEM_NAME, inner, true)))
},
(scalar, List(list)) => {
(scalar, LargeList(list)) => {
let inner = coerce_data_type(&[scalar, list.data_type()]);
LargeList(Box::new(Field::new(ITEM_NAME, inner, true)))
},
Expand All @@ -154,6 +159,8 @@ pub(crate) fn coerce_data_type<A: Borrow<ArrowDataType>>(datatypes: &[A]) -> Arr
(Int64, Float64) => Float64,
(Int64, Boolean) => Int64,
(Boolean, Int64) => Int64,
(Null, rhs) => rhs.clone(),
(lhs, Null) => lhs.clone(),
(_, _) => LargeUtf8,
};
}
16 changes: 12 additions & 4 deletions crates/polars-lazy/src/physical_plan/executors/scan/ndjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,29 @@ impl AnonymousScan for LazyJsonLineReader {
.with_chunk_size(self.batch_size)
.low_memory(self.low_memory)
.with_n_rows(scan_opts.n_rows)
.with_ignore_errors(self.ignore_errors)
.with_chunk_size(self.batch_size)
.finish()
}

fn schema(&self, infer_schema_length: Option<usize>) -> PolarsResult<SchemaRef> {
// Short-circuit schema inference if the schema has been explicitly provided.
if let Some(schema) = &self.schema {
// Short-circuit schema inference if the schema has been explicitly provided,
// or already inferred
if let Some(schema) = &(*self.schema.read().unwrap()) {
return Ok(schema.clone());
}

let f = polars_utils::open_file(&self.path)?;
let mut reader = std::io::BufReader::new(f);

let schema = polars_io::ndjson::infer_schema(&mut reader, infer_schema_length)?;
Ok(Arc::new(schema))
let schema = Arc::new(polars_io::ndjson::infer_schema(
&mut reader,
infer_schema_length,
)?);
let mut guard = self.schema.write().unwrap();
*guard = Some(schema.clone());

Ok(schema)
}
fn allows_projection_pushdown(&self) -> bool {
true
Expand Down
18 changes: 14 additions & 4 deletions crates/polars-lazy/src/scan/ndjson.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::{Path, PathBuf};
use std::sync::RwLock;

use polars_core::prelude::*;
use polars_io::RowIndex;
Expand All @@ -13,10 +14,11 @@ pub struct LazyJsonLineReader {
pub(crate) batch_size: Option<usize>,
pub(crate) low_memory: bool,
pub(crate) rechunk: bool,
pub(crate) schema: Option<SchemaRef>,
pub(crate) schema: Arc<RwLock<Option<SchemaRef>>>,
pub(crate) row_index: Option<RowIndex>,
pub(crate) infer_schema_length: Option<usize>,
pub(crate) n_rows: Option<usize>,
pub(crate) ignore_errors: bool,
}

impl LazyJsonLineReader {
Expand All @@ -31,9 +33,10 @@ impl LazyJsonLineReader {
batch_size: None,
low_memory: false,
rechunk: false,
schema: None,
schema: Arc::new(Default::default()),
row_index: None,
infer_schema_length: Some(100),
ignore_errors: false,
n_rows: None,
}
}
Expand All @@ -43,6 +46,13 @@ impl LazyJsonLineReader {
self.row_index = row_index;
self
}

/// Set values as `Null` if parsing fails because of schema mismatches.
#[must_use]
pub fn with_ignore_errors(mut self, ignore_errors: bool) -> Self {
self.ignore_errors = ignore_errors;
self
}
/// Try to stop parsing when `n` rows are parsed. During multithreaded parsing the upper bound `n` cannot
/// be guaranteed.
#[must_use]
Expand All @@ -62,7 +72,7 @@ impl LazyJsonLineReader {
/// Set the JSON file's schema
#[must_use]
pub fn with_schema(mut self, schema: Option<SchemaRef>) -> Self {
self.schema = schema;
self.schema = Arc::new(RwLock::new(schema));
self
}

Expand All @@ -87,7 +97,7 @@ impl LazyFileListReader for LazyJsonLineReader {
infer_schema_length: self.infer_schema_length,
n_rows: self.n_rows,
row_index: self.row_index.clone(),
schema: self.schema.clone(),
schema: self.schema.read().unwrap().clone(),
..ScanArgsAnonymous::default()
};

Expand Down
6 changes: 5 additions & 1 deletion py-polars/polars/io/ndjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def scan_ndjson(
batch_size: int | None = 1024,
n_rows: int | None = None,
low_memory: bool = False,
rechunk: bool = True,
rechunk: bool = False,
row_index_name: str | None = None,
row_index_offset: int = 0,
schema: SchemaDefinition | None = None,
ignore_errors: bool = False,
) -> LazyFrame:
"""
Lazily read from a newline delimited JSON file or multiple files via glob patterns.
Expand Down Expand Up @@ -103,6 +104,8 @@ def scan_ndjson(
If you supply a list of column names that does not match the names in the
underlying data, the names given here will overwrite them. The number
of names given in the schema should match the underlying data dimensions.
ignore_errors
Return `Null` if parsing fails because of schema mismatches.
"""
return pl.LazyFrame._scan_ndjson(
source,
Expand All @@ -114,4 +117,5 @@ def scan_ndjson(
rechunk=rechunk,
row_index_name=row_index_name,
row_index_offset=row_index_offset,
ignore_errors=ignore_errors,
)
4 changes: 3 additions & 1 deletion py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,10 @@ def _scan_ndjson(
batch_size: int | None = None,
n_rows: int | None = None,
low_memory: bool = False,
rechunk: bool = True,
rechunk: bool = False,
row_index_name: str | None = None,
row_index_offset: int = 0,
ignore_errors: bool = False,
) -> Self:
"""
Lazily read from a newline delimited JSON file.
Expand Down Expand Up @@ -561,6 +562,7 @@ def _scan_ndjson(
low_memory,
rechunk,
_prepare_row_index_args(row_index_name, row_index_offset),
ignore_errors,
)
return self

Expand Down
4 changes: 3 additions & 1 deletion py-polars/src/lazyframe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl PyLazyFrame {
#[staticmethod]
#[cfg(feature = "json")]
#[allow(clippy::too_many_arguments)]
#[pyo3(signature = (path, paths, infer_schema_length, schema, batch_size, n_rows, low_memory, rechunk, row_index))]
#[pyo3(signature = (path, paths, infer_schema_length, schema, batch_size, n_rows, low_memory, rechunk, row_index, ignore_errors))]
fn new_from_ndjson(
path: Option<PathBuf>,
paths: Vec<PathBuf>,
Expand All @@ -125,6 +125,7 @@ impl PyLazyFrame {
low_memory: bool,
rechunk: bool,
row_index: Option<(String, IdxSize)>,
ignore_errors: bool,
) -> PyResult<Self> {
let row_index = row_index.map(|(name, offset)| RowIndex { name, offset });

Expand All @@ -142,6 +143,7 @@ impl PyLazyFrame {
.with_rechunk(rechunk)
.with_schema(schema.map(|schema| Arc::new(schema.0)))
.with_row_index(row_index)
.with_ignore_errors(ignore_errors)
.finish()
.map_err(PyPolarsErr::from)?;

Expand Down