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

fix(rust,python): use actual number of read rows for hive materialization #11690

Merged
merged 8 commits into from
Oct 13, 2023
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
13 changes: 5 additions & 8 deletions crates/polars-io/src/parquet/read_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,10 @@ pub(super) fn array_iter_to_series(
}

/// Materializes hive partitions.
/// We have a special num_rows arg, as df can be empty.
fn materialize_hive_partitions(
df: &mut DataFrame,
hive_partition_columns: Option<&[Series]>,
num_rows: usize,
) {
fn materialize_hive_partitions(df: &mut DataFrame, hive_partition_columns: Option<&[Series]>) {
if let Some(hive_columns) = hive_partition_columns {
let num_rows = df.height();

for s in hive_columns {
unsafe { df.with_column_unchecked(s.new_from_index(0, num_rows)) };
}
Expand Down Expand Up @@ -223,7 +220,7 @@ fn rg_to_dfs_optionally_par_over_columns(
if let Some(rc) = &row_count {
df.with_row_count_mut(&rc.name, Some(*previous_row_count + rc.offset));
}
materialize_hive_partitions(&mut df, hive_partition_columns, md.num_rows());
materialize_hive_partitions(&mut df, hive_partition_columns);

apply_predicate(&mut df, predicate.as_deref(), true)?;

Expand Down Expand Up @@ -299,7 +296,7 @@ fn rg_to_dfs_par_over_rg(
if let Some(rc) = &row_count {
df.with_row_count_mut(&rc.name, Some(row_count_start as IdxSize + rc.offset));
}
materialize_hive_partitions(&mut df, hive_partition_columns, md.num_rows());
materialize_hive_partitions(&mut df, hive_partition_columns);

apply_predicate(&mut df, predicate.as_deref(), false)?;

Expand Down
22 changes: 22 additions & 0 deletions py-polars/tests/unit/io/test_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ def test_hive_partitioned_predicate_pushdown(
assert q.filter(pl.col("sugars_g") == 25).collect().shape == (1, 4)


@pytest.mark.write_disk()
def test_hive_partitioned_slice_pushdown(io_files_path: Path, tmp_path: Path) -> None:
df = pl.read_ipc(io_files_path / "*.ipc")

root = tmp_path / "partitioned_data"

# Ignore the pyarrow legacy warning until we can write properly with new settings.
warnings.filterwarnings("ignore")
pq.write_to_dataset(
df.to_arrow(),
root_path=root,
partition_cols=["category", "fats_g"],
use_legacy_dataset=True,
)

q = pl.scan_parquet(root / "**/*.parquet", hive_partitioning=True)

# tests: 11682
assert q.head(1).collect().select(pl.all_horizontal(pl.all().count() == 1)).item()
assert q.head(0).collect().columns == ["calories", "sugars_g", "category", "fats_g"]


@pytest.mark.write_disk()
def test_hive_partitioned_projection_pushdown(
io_files_path: Path, tmp_path: Path
Expand Down