|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Decoder-projection construction for the parquet scan. |
| 19 | +//! |
| 20 | +//! [`DecoderProjection`] owns the two halves of "project a decoded parquet |
| 21 | +//! batch onto the scan's output schema": |
| 22 | +//! |
| 23 | +//! * the [`ProjectionMask`] installed on every parquet decoder run, and |
| 24 | +//! * the per-batch transform ([`DecoderProjection::map`]) that applies the |
| 25 | +//! projector and, when needed, rebuilds the batch with the user's |
| 26 | +//! `output_schema` to recover metadata / nullability the file schema does |
| 27 | +//! not carry. |
| 28 | +//! |
| 29 | +//! The opener constructs one [`DecoderProjection`] per file via |
| 30 | +//! [`DecoderProjection::try_new`] and hands it to the push-decoder stream, |
| 31 | +//! which calls [`map`](DecoderProjection::map) on every decoded batch. |
| 32 | +
|
| 33 | +use std::sync::Arc; |
| 34 | + |
| 35 | +use arrow::array::{RecordBatch, RecordBatchOptions}; |
| 36 | +use arrow::datatypes::SchemaRef; |
| 37 | + |
| 38 | +use datafusion_common::Result; |
| 39 | +use datafusion_physical_expr::projection::{ProjectionExprs, Projector}; |
| 40 | +use datafusion_physical_expr::utils::reassign_expr_columns; |
| 41 | + |
| 42 | +use parquet::arrow::ProjectionMask; |
| 43 | +use parquet::schema::types::SchemaDescriptor; |
| 44 | + |
| 45 | +use crate::row_filter::build_projection_read_plan; |
| 46 | + |
| 47 | +/// Per-file decoder projection: the [`ProjectionMask`] installed on every |
| 48 | +/// parquet decoder run, plus the per-batch transform that maps the decoder's |
| 49 | +/// output onto the scan's `output_schema`. |
| 50 | +/// |
| 51 | +/// Built once per file by the opener via [`Self::try_new`]; the |
| 52 | +/// push-decoder stream installs [`Self::projection_mask`] on each decoder |
| 53 | +/// and calls [`Self::map`] on every decoded batch. |
| 54 | +pub(crate) struct DecoderProjection { |
| 55 | + projection_mask: ProjectionMask, |
| 56 | + projector: Projector, |
| 57 | + output_schema: SchemaRef, |
| 58 | + /// `true` when the projector's output schema differs from `output_schema` |
| 59 | + /// in metadata / nullability and [`map`](Self::map) must rebuild the batch |
| 60 | + /// with `output_schema`. |
| 61 | + replace_schema: bool, |
| 62 | +} |
| 63 | + |
| 64 | +impl DecoderProjection { |
| 65 | + /// Build the decoder projection for a file. |
| 66 | + /// |
| 67 | + /// `projection` references columns in `physical_file_schema` (i.e. already |
| 68 | + /// adapted by the per-file expr adapter); `parquet_schema` is the |
| 69 | + /// corresponding parquet [`SchemaDescriptor`]. `output_schema` is what |
| 70 | + /// consumers of the scan stream expect. |
| 71 | + pub(crate) fn try_new( |
| 72 | + projection: &ProjectionExprs, |
| 73 | + physical_file_schema: &SchemaRef, |
| 74 | + parquet_schema: &SchemaDescriptor, |
| 75 | + output_schema: &SchemaRef, |
| 76 | + ) -> Result<Self> { |
| 77 | + let read_plan = build_projection_read_plan( |
| 78 | + projection.expr_iter(), |
| 79 | + physical_file_schema, |
| 80 | + parquet_schema, |
| 81 | + ); |
| 82 | + |
| 83 | + let stream_schema = read_plan.projected_schema; |
| 84 | + |
| 85 | + // Rebase the projection onto the decoder's stream schema (column |
| 86 | + // indices change because the decoder yields only the masked columns). |
| 87 | + let rebased_projection = projection |
| 88 | + .clone() |
| 89 | + .try_map_exprs(|expr| reassign_expr_columns(expr, &stream_schema))?; |
| 90 | + let projector = rebased_projection.make_projector(&stream_schema)?; |
| 91 | + |
| 92 | + // Compare against the projector's *output* schema rather than the |
| 93 | + // stream schema, so future widening of the mask (e.g. for post-scan |
| 94 | + // filter columns) does not flip this flag. |
| 95 | + let replace_schema = projector.output_schema() != output_schema; |
| 96 | + |
| 97 | + Ok(Self { |
| 98 | + projection_mask: read_plan.projection_mask, |
| 99 | + projector, |
| 100 | + output_schema: Arc::clone(output_schema), |
| 101 | + replace_schema, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + /// The projection mask to install on every parquet decoder in the scan. |
| 106 | + pub(crate) fn projection_mask(&self) -> &ProjectionMask { |
| 107 | + &self.projection_mask |
| 108 | + } |
| 109 | + |
| 110 | + /// Map a decoded batch onto the scan's output schema. |
| 111 | + /// |
| 112 | + /// Applies the [`Projector`] and, when the projector's output schema |
| 113 | + /// differs from `output_schema` in metadata or nullability, rebuilds the |
| 114 | + /// batch with `output_schema` (some writers emit OPTIONAL fields even when |
| 115 | + /// the data has no nulls; some logical schemas carry field-level metadata |
| 116 | + /// the file schema does not). |
| 117 | + pub(crate) fn map(&self, batch: &RecordBatch) -> Result<RecordBatch> { |
| 118 | + let projected = self.projector.project_batch(batch)?; |
| 119 | + if !self.replace_schema { |
| 120 | + return Ok(projected); |
| 121 | + } |
| 122 | + let (_stream_schema, arrays, num_rows) = projected.into_parts(); |
| 123 | + let options = RecordBatchOptions::new().with_row_count(Some(num_rows)); |
| 124 | + Ok(RecordBatch::try_new_with_options( |
| 125 | + Arc::clone(&self.output_schema), |
| 126 | + arrays, |
| 127 | + &options, |
| 128 | + )?) |
| 129 | + } |
| 130 | +} |
0 commit comments