Skip to content

Add compression_level support to ParquetWriterOptions and enhance write_parquet to accept full options object - #1169

Merged
timsaucer merged 5 commits into
apache:mainfrom
kosiew:parquet-1162a
Jun 25, 2025
Merged

timsaucer merged 5 commits into
apache:mainfrom
kosiew:parquet-1162a

Conversation

@kosiew

@kosiew kosiew commented Jun 24, 2025

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

This change enhances the flexibility of the Parquet writing process by allowing users to specify both compression type and compression level through a unified ParquetWriterOptions object. It also prevents conflicting configurations when options are explicitly passed.

What changes are included in this PR?

  • Added compression_level parameter to ParquetWriterOptions.
  • Enhanced DataFrame.write_parquet() to accept a ParquetWriterOptions object.
  • Added logic to prevent conflicting use of compression_level when using a full options object.
  • Introduced two new tests:
    • test_write_parquet_options: Verifies functionality with custom compression and level.
    • test_write_parquet_options_error: Ensures proper error is raised for misconfiguration.

Are these changes tested?

Yes, two new tests have been added:

  • test_write_parquet_options: Confirms Parquet output matches expected data.
  • test_write_parquet_options_error: Validates error handling when conflicting options are provided.

Are there any user-facing changes?

Yes:

  • Users can now pass a ParquetWriterOptions object directly to DataFrame.write_parquet(), allowing more granular control.
  • A ValueError will be raised if compression_level is used with an already configured options object.

Comment on lines 877 to 883
def write_parquet(
self,
path: str | pathlib.Path,
compression: Union[str, Compression] = Compression.ZSTD,
compression: Union[str, Compression, ParquetWriterOptions] = Compression.ZSTD,
compression_level: int | None = None,
) -> None:
"""Execute the :py:class:`DataFrame` and write the results to a Parquet file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is nice. I think it would be a little better for type hinting in IDEs if we overload the method signatures.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add the overload.

@timsaucer
timsaucer merged commit ef62fa8 into apache:main Jun 25, 2025
timsaucer pushed a commit that referenced this pull request Oct 5, 2026
…ptions (#1761)

`write_parquet` takes `write_options`, documents it and declares it in
the `@overload` for the `ParquetWriterOptions` form, but that branch
calls `write_parquet_with_options(path, compression)` and drops it. The
destination accepts the parameter, so everything in
`DataFrameWriteOptions` (`partition_by`, `single_file_output`,
`insert_operation`, `sort_by`) was silently ignored for that one
spelling.

Measured with the same `DataFrameWriteOptions(partition_by="part")`:

    write_parquet(path, ParquetWriterOptions(), write_options=wo)
        -> ['IDuOjvMa3pdEDotb_0.parquet']     not partitioned
    write_parquet_with_options(path, ParquetWriterOptions(), write_options=wo)
        -> ['part=a', 'part=b']
    write_parquet(path, "zstd", write_options=wo)
        -> ['part=a', 'part=b']

No error and no warning: the files just land in the wrong layout.

The branch arrived in ef62fa8 (#1169) while `write_options` came
earlier in #857, so the new delegation path was written without carrying
the existing parameter over. The same `if` refuses `compression_level`
with an explicit `ValueError`, which shows that arguments incompatible
with this branch get rejected on purpose; `write_options` was not
rejected, only forgotten.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify write_parquet signatures

2 participants