polars_io/json/
infer.rs
1use std::num::NonZeroUsize;
2
3use polars_core::prelude::DataType;
4use polars_core::utils::try_get_supertype;
5use polars_error::{PolarsResult, polars_bail};
6use simd_json::BorrowedValue;
7
8pub(crate) fn json_values_to_supertype(
9 values: &[BorrowedValue],
10 infer_schema_len: NonZeroUsize,
11) -> PolarsResult<DataType> {
12 values
14 .iter()
15 .take(infer_schema_len.into())
16 .map(|value| polars_json::json::infer(value).map(|dt| DataType::from_arrow_dtype(&dt)))
17 .reduce(|l, r| {
18 let l = l?;
19 let r = r?;
20 try_get_supertype(&l, &r)
21 })
22 .unwrap_or_else(|| polars_bail!(ComputeError: "could not infer data-type"))
23}
24
25pub(crate) fn dtypes_to_supertype<I: Iterator<Item = DataType>>(
26 datatypes: I,
27) -> PolarsResult<DataType> {
28 datatypes
29 .map(Ok)
30 .reduce(|l, r| {
31 let l = l?;
32 let r = r?;
33 try_get_supertype(&l, &r)
34 })
35 .unwrap_or_else(|| polars_bail!(ComputeError: "could not infer data-type"))
36}