Skip to content

[spark] Support write.parallelism for fixed-bucket writes. - #11413

Open
fengruotj wants to merge 2 commits into
apache:masterfrom
fengruotj:feat/spark-sink-parallelism
Open

fengruotj wants to merge 2 commits into
apache:masterfrom
fengruotj:feat/spark-sink-parallelism

Conversation

@fengruotj

@fengruotj fengruotj commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Closes #11412 . Related to #4742.

Add write.parallelism to Spark's V1 and V2 write paths for tables in HASH_FIXED bucket mode. A positive value sets the number of Spark partitions in the final shuffle by table partition and bucket. This lets users tune the number of write tasks independently
of the table's bucket count and other shuffles in the query. When the option is unset, zero, or negative, Spark continues to choose the write parallelism.

For example, the following table uses 16 buckets and requests 7 write tasks:

CREATE TABLE events (id INT) USING paimon 
TBLPROPERTIES ( 
  'primary-key' = 'id', 
  'bucket' = '16', 
  'write.parallelism' = '7' 
); 

Users can also set a session default with SET spark.paimon.write.parallelism=7.

Tests

  • Extended V2WriteRequireDistributionTest to check the actual write-stage task count with AQE enabled for both V1 and V2. Coverage includes explicit and default parallelism, a custom bucket function, and bucket-unaware tables retaining their input partitions.
  • Ran the suite locally on Spark 3.2.4, 3.3.4, and 3.4.4, with 43 tests passing on each version. On Spark 3.5.9, 35 passed and 8 version-specific tests were skipped.

@fengruotj
fengruotj force-pushed the feat/spark-sink-parallelism branch 2 times, most recently from 2190ef0 to 9f66a97 Compare November 24, 2026 11:20
@fengruotj fengruotj changed the title feat(spark): support sink.parallelism for fixed-bucket writes. spark] Support sink.parallelism for fixed-bucket writes. Nov 24, 2026
@fengruotj fengruotj changed the title spark] Support sink.parallelism for fixed-bucket writes. [spark] Support sink.parallelism for fixed-bucket writes. Nov 24, 2026
@fengruotj
fengruotj marked this pull request as draft November 24, 2026 11:45
@fengruotj
fengruotj marked this pull request as ready for review November 24, 2026 12:03
@fengruotj
fengruotj force-pushed the feat/spark-sink-parallelism branch 10 times, most recently from 67cd38c to 1aa676b Compare November 24, 2026 16:45

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On Spark 3.5.9, 35 write-distribution cases passed and eight version-specific cases were skipped. The AQE tests verified actual stage task counts and row readback. The new connector option description needs its scope stated.

.intType()
.noDefaultValue()
.withDescription(
"Defines a custom parallelism for the sink. "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Document that this setting controls only the fixed-bucket write shuffle

This description becomes the public Spark connector configuration. It currently promises custom sink parallelism with no mention of table mode, and only describes an unset value. The implementation and passing tests show that bucket-unaware writes retain their input partition count, and zero or negative values leave Spark's choice in place. Please state that a positive value controls the final fixed-bucket V1/V2 write shuffle and that unset or non-positive values use Spark's default planning.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reran V2WriteRequireDistributionTest on f56878b404 with Spark 3.5.9. All 35 applicable cases passed, including the V1 and V2 writes with write.parallelism=7, 0, and -1. The documentation now states the fixed-bucket scope. Please also say that a positive value sets the write shuffle parallelism and that zero or negative values use the same Spark planning as an unset option.

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.

@JingsongLi Thanks for reviewing! Agreed. I’ve updated the write.parallelism documentation to clarify that, for fixed-bucket tables, a positive value sets the number of shuffle partitions for the write. Setting it to zero or a negative value uses the same Spark planning as leaving it unset.

@fengruotj
fengruotj force-pushed the feat/spark-sink-parallelism branch from 1aa676b to f56878b Compare November 27, 2026 09:36
@fengruotj
fengruotj force-pushed the feat/spark-sink-parallelism branch from f56878b to 785cc85 Compare November 27, 2026 10:30
@fengruotj fengruotj changed the title [spark] Support sink.parallelism for fixed-bucket writes. [spark] Support write.parallelism for fixed-bucket writes. Nov 27, 2026
@JingsongLi

Copy link
Copy Markdown
Contributor

What is the relationship to sink.striped-bucket-write.enabled?

@fengruotj

fengruotj commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@JingsongLi Thanks for reviewing! I thinksink.striped-bucket-write.enabled is a Flink connector option that lets multiple writer subtasks write to the same bucket. This PR adds write.parallelism for Spark fixed-bucket writes. It sets the number of partitions in the write
shuffle and preserves the existing grouping by table partition and bucket.

Both options address write parallelism for fixed-bucket tables, but they solve different problems. Before this PR, the Paimon Spark connector had no dedicated option to set the number of shuffle partitions for a write. write.parallelism fills that gap while preserving Spark’s existing grouping by table partition and bucket.

  • In Flink, sink.striped-bucket-write.enabled allows multiple writer subtasks to write to the same bucket.
  • In Spark, write.parallelism sets the total number of partitions in the write shuffle. Rows with the same table partition and bucket still go to one write task.

@JingsongLi JingsongLi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I verified on Spark 3.2.4 and 3.5.9 that fixed-bucket INSERT and INSERT OVERWRITE respect the table/session setting under AQE, including row readback on a partitioned primary-key table. The per-write option behaves differently.


override def requiredNumPartitions(): Int = {
if (!PaimonWriteRequirement.requiresQueryPreparation && table.bucketMode() == HASH_FIXED) {
writeParallelism(coreOptions).getOrElse(0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Make the scope of per-write parallelism explicit

On Spark 4.0.5 with AQE enabled, an existing fixed-bucket table with write.parallelism=7 writes with seven tasks even when df.writeTo(table).option("write.parallelism", "3").append() requests three, or "0" requests Spark's adaptive default. The physical plan confirms AppendDataExec. V1 saveAsTable append has the same behavior. This makes it easy for a job to appear to set its own task count while still using the table's value. Please either bind the operation option ahead of the table/session value in both paths, preserving zero as an explicit fallback, or document that this setting is supported only as a table or session property.

@fengruotj fengruotj Nov 30, 2026 •

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.

@JingsongLi Thanks for reviewing!I agreed. I updated both V2 writeTo(...).append() and V1 saveAsTable append to honor the per-write write.parallelism option for fixed-bucket tables. I added regression tests for both paths. With the table set to 7, an explicit 3 produces three write tasks. An explicit 0 lets AQE choose the task count even with the session set to 5.

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.

[Feature] Support sink.parallelism for fixed-bucket writes in Spark.

2 participants