-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): read metadata for parquet
- Loading branch information
1 parent
83b4c17
commit 2370526
Showing
4 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use polars_core::chunked_array::Metadata; | ||
use polars_core::datatypes::{ | ||
BinaryType, BooleanType, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, | ||
PolarsDataType, StringType, UInt16Type, UInt32Type, UInt64Type, UInt8Type, | ||
}; | ||
use polars_parquet::parquet::statistics::{ | ||
BinaryStatistics, BooleanStatistics, PrimitiveStatistics, Statistics, | ||
}; | ||
|
||
pub trait ToMetadata<D: PolarsDataType + 'static>: Statistics + Sized + 'static { | ||
fn to_metadata(&self) -> Metadata<D>; | ||
} | ||
|
||
impl ToMetadata<BooleanType> for BooleanStatistics { | ||
fn to_metadata(&self) -> Metadata<BooleanType> { | ||
let mut md = Metadata::default(); | ||
|
||
if let Some(distinct_count) = self.distinct_count.and_then(|v| v.try_into().ok()) { | ||
md.set_distinct_count(distinct_count); | ||
} | ||
if let Some(min_value) = self.min_value { | ||
md.set_min_value(min_value); | ||
} | ||
if let Some(max_value) = self.max_value { | ||
md.set_max_value(max_value); | ||
} | ||
|
||
md | ||
} | ||
} | ||
|
||
impl ToMetadata<BinaryType> for BinaryStatistics { | ||
fn to_metadata(&self) -> Metadata<BinaryType> { | ||
let mut md = Metadata::default(); | ||
|
||
if let Some(distinct_count) = self.distinct_count.and_then(|v| v.try_into().ok()) { | ||
md.set_distinct_count(distinct_count); | ||
} | ||
if let Some(min_value) = self.min_value.as_ref() { | ||
md.set_min_value(min_value.clone().into_boxed_slice()); | ||
} | ||
if let Some(max_value) = self.max_value.as_ref() { | ||
md.set_max_value(max_value.clone().into_boxed_slice()); | ||
} | ||
|
||
md | ||
} | ||
} | ||
|
||
impl ToMetadata<StringType> for BinaryStatistics { | ||
fn to_metadata(&self) -> Metadata<StringType> { | ||
let mut md = Metadata::default(); | ||
|
||
if let Some(distinct_count) = self.distinct_count.and_then(|v| v.try_into().ok()) { | ||
md.set_distinct_count(distinct_count); | ||
} | ||
if let Some(min_value) = self | ||
.min_value | ||
.as_ref() | ||
.and_then(|s| String::from_utf8(s.clone()).ok()) | ||
{ | ||
md.set_min_value(min_value); | ||
} | ||
if let Some(max_value) = self | ||
.max_value | ||
.as_ref() | ||
.and_then(|s| String::from_utf8(s.clone()).ok()) | ||
{ | ||
md.set_max_value(max_value); | ||
} | ||
|
||
md | ||
} | ||
} | ||
|
||
macro_rules! prim_statistics { | ||
($(($bstore:ty, $pltype:ty),)+) => { | ||
$( | ||
impl ToMetadata<$pltype> for PrimitiveStatistics<$bstore> { | ||
fn to_metadata(&self) -> Metadata<$pltype> { | ||
let mut md = Metadata::default(); | ||
|
||
if let Some(distinct_count) = self.distinct_count.and_then(|v| v.try_into().ok()) | ||
{ | ||
md.set_distinct_count(distinct_count); | ||
} | ||
if let Some(min_value) = self.min_value { | ||
md.set_min_value(min_value as <$pltype as PolarsDataType>::OwnedPhysical); | ||
} | ||
if let Some(max_value) = self.max_value { | ||
md.set_max_value(max_value as <$pltype as PolarsDataType>::OwnedPhysical); | ||
} | ||
|
||
md | ||
} | ||
} | ||
)+ | ||
} | ||
} | ||
|
||
prim_statistics! { | ||
(i32, Int8Type), | ||
(i32, Int16Type), | ||
(i32, Int32Type), | ||
(i64, Int64Type), | ||
|
||
(i32, UInt8Type), | ||
(i32, UInt16Type), | ||
(i32, UInt32Type), | ||
(i64, UInt64Type), | ||
|
||
(f32, Float32Type), | ||
(f64, Float64Type), | ||
} |