Path: blob/main/crates/polars-stream/src/nodes/io_sources/parquet/projection.rs
6939 views
use std::borrow::Cow;1use std::sync::Arc;23use arrow::datatypes::ArrowSchema;4use polars_core::prelude::{ArrowField, Column, DataType};5use polars_core::schema::Schema;6use polars_error::PolarsResult;7use polars_plan::dsl::CastColumnsPolicy;8use polars_utils::pl_str::PlSmallStr;910use crate::nodes::io_sources::multi_scan::components::column_selector::ColumnSelector;11use crate::nodes::io_sources::multi_scan::components::projection::MappedProjectionRef;12use crate::nodes::io_sources::multi_scan::components::projection::builder::ProjectionBuilder;13use crate::nodes::io_sources::multi_scan::reader_interface::Projection;1415pub fn resolve_arrow_field_projections(16file_arrow_schema: &ArrowSchema,17file_schema: &Schema,18projection: Projection,19cast_columns_policy: CastColumnsPolicy,20) -> PolarsResult<Arc<[ArrowFieldProjection]>> {21let projection: Projection = match projection {22Projection::Plain(projected_schema) => ProjectionBuilder::new(projected_schema, None, None)23.build_projection(Some(file_schema), None, cast_columns_policy, usize::MAX)?,24Projection::Mapped { .. } => projection,25};2627Ok(projection28.iter_non_missing_columns()29.map(30|MappedProjectionRef {31source_name,32output_name,33output_dtype,34resolved_transform,35}| {36let arrow_field = file_arrow_schema.get(source_name.as_str()).unwrap().clone();3738let Some(resolved_transform) = resolved_transform else {39assert_eq!(source_name, output_name);4041return ArrowFieldProjection::Plain(arrow_field);42};4344assert_eq!(45resolved_transform.source_dtype,46file_schema.get(source_name.as_str()).unwrap()47);4849ArrowFieldProjection::Mapped {50arrow_field,51output_name: output_name.clone(),52output_dtype: output_dtype.clone(),53transform: resolved_transform.attach_transforms(ColumnSelector::Position(0)),54}55},56)57.collect::<Arc<[ArrowFieldProjection]>>())58}5960/// Represents a potentially mapped (i.e. casted and/or renamed) arrow field projection.61#[derive(Debug)]62pub enum ArrowFieldProjection {63Plain(ArrowField),64Mapped {65arrow_field: ArrowField,66output_name: PlSmallStr,67output_dtype: DataType,68transform: ColumnSelector,69},70}7172impl ArrowFieldProjection {73pub fn arrow_field(&self) -> &ArrowField {74match self {75Self::Plain(field) => field,76Self::Mapped { arrow_field, .. } => arrow_field,77}78}7980pub fn output_name(&self) -> &PlSmallStr {81match self {82Self::Plain(field) => &field.name,83Self::Mapped { output_name, .. } => output_name,84}85}8687#[expect(unused)]88pub fn output_dtype(&self) -> Cow<'_, DataType> {89match self {90Self::Plain(field) => Cow::Owned(DataType::from_arrow_field(field)),91Self::Mapped { output_dtype, .. } => Cow::Borrowed(output_dtype),92}93}9495pub fn apply_transform(&self, column: Column) -> PolarsResult<Column> {96match self {97Self::Plain(_) => Ok(column),98Self::Mapped {99transform,100output_dtype,101..102} => {103let output_height = column.len();104let out = transform.select_from_columns(&[column], output_height)?;105106debug_assert_eq!(out.dtype(), output_dtype);107108Ok(out)109},110}111}112}113114115