Path: blob/main/crates/polars-parquet/src/parquet/schema/io_thrift/to_thrift.rs
8512 views
use polars_parquet_format::{ConvertedType, SchemaElement};12use super::super::types::ParquetType;3use crate::parquet::schema::types::PrimitiveType;45impl ParquetType {6/// Method to convert to Thrift.7pub(crate) fn to_thrift(&self) -> Vec<SchemaElement> {8let mut elements: Vec<SchemaElement> = Vec::new();9to_thrift_helper(self, &mut elements, true);10elements11}12}1314/// Constructs list of `SchemaElement` from the schema using depth-first traversal.15/// Here we assume that schema is always valid and starts with group type.16fn to_thrift_helper(schema: &ParquetType, elements: &mut Vec<SchemaElement>, is_root: bool) {17match schema {18ParquetType::PrimitiveType(PrimitiveType {19field_info,20logical_type,21converted_type,22physical_type,23}) => {24let (type_, type_length) = (*physical_type).into();25let (converted_type, maybe_decimal) = converted_type26.map(|x| x.into())27.map(|x: (ConvertedType, Option<(i32, i32)>)| (Some(x.0), x.1))28.unwrap_or((None, None));2930let element = SchemaElement {31type_: Some(type_),32type_length,33repetition_type: Some(field_info.repetition.into()),34name: field_info.name.to_string(),35num_children: None,36converted_type,37precision: maybe_decimal.map(|x| x.0),38scale: maybe_decimal.map(|x| x.1),39field_id: field_info.id,40logical_type: logical_type.map(|x| x.into()),41};4243elements.push(element);44},45ParquetType::GroupType {46field_info,47fields,48logical_type,49converted_type,50} => {51let converted_type = converted_type.map(|x| x.into());5253let repetition_type = if is_root {54// https://github.com/apache/parquet-format/blob/7f06e838cbd1b7dbd722ff2580b9c2525e37fc46/src/main/thrift/parquet.thrift#L36355None56} else {57Some(field_info.repetition)58};5960let element = SchemaElement {61type_: None,62type_length: None,63repetition_type: repetition_type.map(|x| x.into()),64name: field_info.name.to_string(),65num_children: Some(fields.len() as i32),66converted_type,67scale: None,68precision: None,69field_id: field_info.id,70logical_type: logical_type.map(|x| x.into()),71};7273elements.push(element);7475// Add child elements for a group76for field in fields {77to_thrift_helper(field, elements, false);78}79},80}81}828384