Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove extra hashmap construction in new-streaming parquet #18792

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ impl ParquetSourceNode {
};

ensure_metadata_has_projected_fields(
projected_arrow_schema.as_ref(),
&metadata,
projected_arrow_schema.as_ref(),
)?;

PolarsResult::Ok((path_index, byte_source, metadata))
Expand Down
25 changes: 9 additions & 16 deletions crates/polars-stream/src/nodes/parquet_source/metadata_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use polars_core::prelude::{ArrowSchema, DataType, PlHashMap};
use polars_core::prelude::{ArrowSchema, DataType};
use polars_error::{polars_bail, PolarsResult};
use polars_io::prelude::FileMetadata;
use polars_io::utils::byte_source::{ByteSource, DynByteSource};
use polars_utils::mmap::MemSlice;
use polars_utils::pl_str::PlSmallStr;

/// Read the metadata bytes of a parquet file, does not decode the bytes. If during metadata fetch
/// the bytes of the entire file are loaded, it is returned in the second return value.
Expand Down Expand Up @@ -124,26 +123,20 @@ pub(super) async fn read_parquet_metadata_bytes(
/// Ensures that a parquet file has all the necessary columns for a projection with the correct
/// dtype. There are no ordering requirements and extra columns are permitted.
pub(super) fn ensure_metadata_has_projected_fields(
projected_fields: &ArrowSchema,
metadata: &FileMetadata,
projected_fields: &ArrowSchema,
) -> PolarsResult<()> {
let schema = polars_parquet::arrow::read::infer_schema(metadata)?;

// Note: We convert to Polars-native dtypes for timezone normalization.
let mut schema = schema
.into_iter_values()
.map(|x| {
let dtype = DataType::from_arrow(&x.dtype, true);
(x.name, dtype)
})
.collect::<PlHashMap<PlSmallStr, DataType>>();

for field in projected_fields.iter_values() {
let Some(dtype) = schema.remove(&field.name) else {
polars_bail!(SchemaMismatch: "did not find column: {}", field.name)
};

// Note: We convert to Polars-native dtypes for timezone normalization.
let expected_dtype = DataType::from_arrow(&field.dtype, true);
let dtype = {
let Some(field) = schema.get(&field.name) else {
polars_bail!(SchemaMismatch: "did not find column: {}", field.name)
};
DataType::from_arrow(&field.dtype, true)
};

if dtype != expected_dtype {
polars_bail!(SchemaMismatch: "data type mismatch for column {}: found: {}, expected: {}",
Expand Down