[SPARK-60324][SQL] Normalize -0.0 and NaN in floating-point dynamic partition values #59583
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #59583"
general
weight: 0.35
SEPURI-SAI-KRISHNA
What changes were proposed in this pull request?
Before a write, the values of
FLOATandDOUBLEdynamic partition columns are now normalized as grouping keys are (NormalizeFloatingNumbers.normalize): -0.0 becomes 0.0 and every NaN becomes the same NaN. This happens in the projection that already converts empty strings to null in string partition columns, so it applies with planned writes (V1Writes) and without them (FileFormatWriter).V1WritesUtils.convertEmptyToNullis renamed tonormalizePartitionValuesto match.Static partition columns are left as they are. Their directory is named by the partition spec, and an
INSERT OVERWRITE ... PARTITION (p = '-0.0')must replace the directory it named, not write to another one.AliasAwareQueryOutputOrdering.stripnow looks through the normalization, as it already does throughEmpty2Null. The normalization keeps the values in order, so rows already sorted by a partition column are not sorted again.Why are the changes needed?
A partitioned write fails when a floating-point partition column holds both 0.0 and -0.0 in one task, and nothing is written. With the default configuration, on 3.5.3, 4.0.3 and 4.2.0 (official binaries) and on master:
The same happens with ORC, CSV and JSON,
INSERT OVERWRITE, CTAS,df.write.partitionBy,FLOAT, other partition columns before it, and Hive tables (with and withoutspark.sql.hive.convertMetastoreParquet).The write sorts the rows of a task by the dynamic partition columns, where -0.0 = 0.0 and all NaNs are equal, so the zeros can stay interleaved: 0.0, -0.0, 0.0.
DynamicPartitionDataSingleWriterstarts a new directory whenever the bytes of the partition values change, so it writesd=0.0, thend=-0.0, thend=0.0again, and its second file ind=0.0has the same name as the first. NaNs with different bits, e.g.NaNand-NaN, fail the same way ond=NaN. Withspark.sql.maxConcurrentOutputFileWriters, the writers are keyed by those bytes, so NaNs with different bits open the samed=NaNfile, and the zeros fail once the writers fall back to sorting.When the zeros are in different tasks the write succeeds but creates both
d=0.0andd=-0.0, so where a row ends up depends on how the rows were split into tasks. The file stream sink names every file with a random UUID, so it does not fail, but it writes bothd=0.0andd=-0.0even within one task.Spark already normalizes these values in grouping, join and window partition keys (SPARK-26021) and in array set operations (SPARK-54918). Dynamic partition values are grouping keys too. SPARK-26012 fixed the same failure for null and empty strings, which both go to the default partition, with the
Empty2Nullconversion that this PR extends.Does this PR introduce _any_ user-facing change?
Yes. A write whose
FLOATorDOUBLEdynamic partition column holds 0.0 and -0.0, or NaNs with different bits, in one task now succeeds, where it failed before. -0.0 is now written to the0.0partition, where it could go to a-0.0partition before, depending on how the rows were split into tasks. Withspark.sql.sources.partitionOverwriteMode=dynamic, a dynamic -0.0 now overwrites the0.0partition. Static partition values are written as before.How was this patch tested?
New tests:
V1WriteCommandSuite, "v1 write with 0.0, -0.0 and NaN partition column values": writes 0.0, -0.0,NaNand-NaNinterleaved in one task, forDOUBLEandFLOAT, with and without planned writes, and withmaxConcurrentOutputFileWriters0 (sorted), 1 (concurrent writer falling back to sorting) and 4 (concurrent writers only). It checks that the directories arep=0.0,p=1.0andp=NaNand reads the rows back. It also checks that overwriting a static partitionp = '-0.0'twice leaves only the rows of the second write.V1WriteCommandSuite, "v1 write with already sorted plan - floating-point partition column":INSERT ... ORDER BYaDOUBLEpartition column plans a single sort.V1WriteHiveCommandSuite: an insert into a Hive table partitioned by a staticSTRINGcolumn and a dynamicDOUBLEcolumn, with and without converting the metastore table.The first test fails without this change. Each part of the change was also undone on its own, and each variant fails one of the new tests: no normalization in
V1Writes, no normalization inFileFormatWriter, static columns normalized in either of them (the second overwrite appends), andstripnot looking through the normalization (a second sort is planned).These suites pass:
V1WriteCommandSuite,PartitionedWriteSuite,InsertSuite,FileFormatWriterSuite,BucketedWriteWithoutHiveSupportSuite,ProjectedOrderingAndPartitioningSuite,DataFrameReaderWriterSuite,FileStreamSinkV1Suite,FileStreamSinkV2SuiteandPlannerSuiteinsql/core, andV1WriteHiveCommandSuite,InsertSuite,HiveSQLInsertTestSuite,PartitionProviderCompatibilitySuite,BucketedWriteWithHiveSupportSuiteandHiveParquetSourceSuiteinsql/hive.A separate matrix wrote 0.0 and -0.0, NaNs with different bits, and NULL interleaved in one task: Parquet, ORC, CSV and JSON,
FLOATandDOUBLE, planned writes on and off,maxConcurrentOutputFileWriters0, 1 and 8, with and without another partition column before it. 160 of 180 writes failed before this change, and the other 20 created bothp=0.0andp=-0.0. After it, all 180 succeed with one directory per value, and static partition specs write the same directories as before.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)