-
Notifications
You must be signed in to change notification settings - Fork 340
feat: support gzip compression in native Parquet writes #4930
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
Open
andygrove
wants to merge
7
commits into
apache:main
Choose a base branch
from
andygrove:feat-parquet-write-gzip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−25
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29e1f72
feat: support gzip compression in the native Parquet writer
andygrove 4f40cb3
feat: write Parquet natively when the compression codec is gzip
andygrove 7b449bd
fix: request parquet-rs codec features explicitly for native Parquet …
andygrove 45c08fb
fix: honor parquet.compression precedence and uncompressed codec alias
andygrove 8a1fa5e
refactor: inline single-call-site compression_to_parquet delegate
andygrove be81af0
test: replace brittle brotli fallback test with lz4_raw round trip
andygrove 3e0572f
test: skip lz4_raw fallback test on Spark 3.4
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,13 @@ package org.apache.comet.parquet | |
|
|
||
| import java.io.File | ||
|
|
||
| import scala.jdk.CollectionConverters._ | ||
| import scala.util.{Random, Using} | ||
|
|
||
| import org.apache.hadoop.fs.{FileSystem, Path} | ||
| import org.apache.parquet.hadoop.ParquetFileReader | ||
| import org.apache.parquet.hadoop.metadata.CompressionCodecName | ||
| import org.apache.parquet.hadoop.util.HadoopInputFile | ||
| import org.apache.spark.sql.{CometTestBase, DataFrame, Row} | ||
| import org.apache.spark.sql.comet.{CometBatchScanExec, CometNativeScanExec, CometNativeWriteExec, CometScanExec} | ||
| import org.apache.spark.sql.execution.{FileSourceScanExec, QueryExecution, SparkPlan} | ||
|
|
@@ -32,6 +36,7 @@ import org.apache.spark.sql.internal.SQLConf | |
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| import org.apache.comet.CometConf | ||
| import org.apache.comet.CometSparkSessionExtensions.isSpark35Plus | ||
| import org.apache.comet.testing.{DataGenOptions, FuzzDataGenerator, SchemaGenOptions} | ||
|
|
||
| class CometParquetWriterSuite extends CometTestBase { | ||
|
|
@@ -138,6 +143,71 @@ class CometParquetWriterSuite extends CometTestBase { | |
| } | ||
| } | ||
|
|
||
| test("parquet write with each supported compression codec") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a requirement but do you think it might be useful to add a test to cover codec precedence? I.e. specify zstd in the conf, but then override it to gzip |
||
| Seq("none", "uncompressed", "snappy", "lz4", "zstd", "gzip").foreach { codec => | ||
| withTempPath { dir => | ||
| val outputPath = new File(dir, s"output_$codec.parquet").getAbsolutePath | ||
| val df = spark.range(0, 100).selectExpr("id", "cast(id as string) as name") | ||
|
|
||
| withSQLConf( | ||
| CometConf.COMET_NATIVE_PARQUET_WRITE_ENABLED.key -> "true", | ||
| CometConf.getOperatorAllowIncompatConfigKey(classOf[DataWritingCommandExec]) -> "true", | ||
| CometConf.COMET_EXEC_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_COMPRESSION.key -> codec) { | ||
|
|
||
| val plan = captureWritePlan(path => df.write.parquet(path), outputPath) | ||
| assertHasCometNativeWriteExec(plan) | ||
| } | ||
|
|
||
| checkAnswer(spark.read.parquet(outputPath), df.collect()) | ||
| assertParquetCodec(outputPath, expectedCodecName(codec)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("parquet write honors parquet.compression option over SQLConf default") { | ||
| withTempPath { dir => | ||
| val outputPath = new File(dir, "output.parquet").getAbsolutePath | ||
| val df = spark.range(0, 100).selectExpr("id", "cast(id as string) as name") | ||
|
|
||
| withSQLConf( | ||
| CometConf.COMET_NATIVE_PARQUET_WRITE_ENABLED.key -> "true", | ||
| CometConf.getOperatorAllowIncompatConfigKey(classOf[DataWritingCommandExec]) -> "true", | ||
| CometConf.COMET_EXEC_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_COMPRESSION.key -> "snappy") { | ||
|
|
||
| val plan = captureWritePlan( | ||
| path => df.write.option("parquet.compression", "gzip").parquet(path), | ||
| outputPath) | ||
| assertHasCometNativeWriteExec(plan) | ||
| } | ||
|
|
||
| checkAnswer(spark.read.parquet(outputPath), df.collect()) | ||
| assertParquetCodec(outputPath, CompressionCodecName.GZIP) | ||
| } | ||
| } | ||
|
|
||
| test("parquet write with unsupported compression codec falls back to Spark") { | ||
| assume(isSpark35Plus, "lz4_raw was added in Spark 3.5") | ||
| withTempPath { dir => | ||
| val outputPath = new File(dir, "output.parquet").getAbsolutePath | ||
| val df = spark.range(0, 100).selectExpr("id", "cast(id as string) as name") | ||
|
|
||
| withSQLConf( | ||
| CometConf.COMET_NATIVE_PARQUET_WRITE_ENABLED.key -> "true", | ||
| CometConf.getOperatorAllowIncompatConfigKey(classOf[DataWritingCommandExec]) -> "true", | ||
| CometConf.COMET_EXEC_ENABLED.key -> "true", | ||
| SQLConf.PARQUET_COMPRESSION.key -> "lz4_raw") { | ||
|
|
||
| val plan = captureWritePlan(path => df.write.parquet(path), outputPath) | ||
| assertNoCometNativeWriteExec(plan) | ||
| } | ||
|
|
||
| checkAnswer(spark.read.parquet(outputPath), df.collect()) | ||
| assertParquetCodec(outputPath, CompressionCodecName.LZ4_RAW) | ||
| } | ||
| } | ||
|
|
||
| test("parquet write with array type") { | ||
| withTempPath { dir => | ||
| val outputPath = new File(dir, "output.parquet").getAbsolutePath | ||
|
|
@@ -437,6 +507,22 @@ class CometParquetWriterSuite extends CometTestBase { | |
| s"Expected exactly one CometNativeWriteExec in the plan, but found $nativeWriteCount:\n${plan.treeString}") | ||
| } | ||
|
|
||
| private def assertNoCometNativeWriteExec(plan: SparkPlan): Unit = { | ||
| val hasNativeWrite = plan.exists { | ||
| case _: CometNativeWriteExec => true | ||
| case d: DataWritingCommandExec => | ||
| d.child.exists { | ||
| case _: CometNativeWriteExec => true | ||
| case _ => false | ||
| } | ||
| case _ => false | ||
| } | ||
|
|
||
| assert( | ||
| !hasNativeWrite, | ||
| s"Expected no CometNativeWriteExec in the plan, but found one:\n${plan.treeString}") | ||
| } | ||
|
|
||
| private def writeWithCometNativeWriteExec( | ||
| inputPath: String, | ||
| outputPath: String, | ||
|
|
@@ -470,6 +556,40 @@ class CometParquetWriterSuite extends CometTestBase { | |
| compareRows(schema, sparkRows, cometRows) | ||
| } | ||
|
|
||
| private def expectedCodecName(sparkCodec: String): CompressionCodecName = sparkCodec match { | ||
| case "none" | "uncompressed" => CompressionCodecName.UNCOMPRESSED | ||
| case "snappy" => CompressionCodecName.SNAPPY | ||
| case "lz4" => CompressionCodecName.LZ4 | ||
| case "zstd" => CompressionCodecName.ZSTD | ||
| case "gzip" => CompressionCodecName.GZIP | ||
| case other => fail(s"unexpected codec: $other") | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that every column chunk in every part file under `outputPath` reports `expected` as | ||
| * its compression codec. Reading the data back is not enough on its own: a Parquet reader | ||
| * honors whatever the footer says, so a write that silently ignored the requested codec would | ||
| * still round-trip. | ||
| */ | ||
| private def assertParquetCodec(outputPath: String, expected: CompressionCodecName): Unit = { | ||
| val conf = spark.sparkContext.hadoopConfiguration | ||
| val partFiles = new File(outputPath).listFiles().filter(_.getName.startsWith("part-")) | ||
| assert(partFiles.nonEmpty, s"No part files found under $outputPath") | ||
|
|
||
| partFiles.foreach { partFile => | ||
| val inputFile = HadoopInputFile.fromPath(new Path(partFile.getAbsolutePath), conf) | ||
| Using.resource(ParquetFileReader.open(inputFile)) { reader => | ||
| val codecs = reader.getFooter.getBlocks.asScala | ||
| .flatMap(_.getColumns.asScala) | ||
| .map(_.getCodec) | ||
| .toSet | ||
| assert( | ||
| codecs == Set(expected), | ||
| s"Expected all column chunks in ${partFile.getName} to use $expected, found $codecs") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def writeComplexTypeData( | ||
| inputDf: DataFrame, | ||
| outputPath: String, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this enum, cant we reuse
Compression?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the PR description:
"Give the native Parquet writer its own ParquetCompression enum instead of borrowing the shuffle crate's CompressionCodec. gzip is a Parquet codec, not a shuffle codec, and the shuffle path has no business carrying it."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case we prob can just add GZIP variant into
CompressionCodecand reuse, however if we follow Apache Spark there are 2 enums indeed, for parquet and internal compression like shuffle.