metanil opened a new pull request, #60747: URL: https://github.com/apache/spark/pull/60747
### What changes were proposed in this pull request? A storage-partitioned join can evaluate a connector's partition transform itself: in the identity reduce, and in the one-side shuffle (`spark.sql.sources.v2.bucketing.shuffle.enabled`). Since SPARK-60027, it takes an argument as it is when it is stored the way the function's declared input type is (`TransformExpression.storedAlike`). For a top-level decimal of the declared scale, that held for any precision, so a `DECIMAL(12,2)` column under a function declaring `DECIMAL(10,2)` reached the function as it is, while the write path (`DistributionAndOrderingUtils`) casts it to the declared type: a value of more digits than declared becomes `NULL` with `spark.sql.ansi.enabled=false`, and fails with `true`. This PR applies the rule SPARK-61650 (#60431) applies to datetime and interval types: an argument goes as it is only where Spark's cast to the declared type keeps every value. For a decimal of the declared scale, that is no more precision than declared. At the top level the function gets the `Decimal` itself, so either storage goes, e.g. a `DECIMAL(18,2)` against a declared `DECIMAL(20,2)`. Anything else is refused, so Spark does not evaluate the function there, and the join plans as for any layout Spark cannot evaluate. A transform rebuilt over a cogroup's key of another type than the layout's column (`TransformExpression.storedKey`, from #60431) still takes a decimal of the declared scale whatever its precision, a user-defined type by its `sqlType`, in `atDeclaredType`: the keyed side's column holds no value of more digits than declared, so such a key pairs with nothing wherever it lands, and one that fits is placed by the digits the keyed side's are. ### Why are the changes needed? Spark gave the function a value its own write path would have cast to `NULL`, or failed on, which is outside what the function declared. Measured on master: - for a `DECIMAL(12,2)` column under a function declaring `DECIMAL(10,2)`, and likewise `DECIMAL(20,2)` under `DECIMAL(18,2)` and `DECIMAL(38,10)` under `DECIMAL(20,10)`, a value that fits, e.g. `12.34`, gives the function the same digits either way, and one that overflows the declared precision, e.g. `1234567890.12`, reaches it as it is in a storage-partitioned join, while the write path's cast gives `NULL` or throws `SparkArithmeticException`; - with the test catalog bucketing a decimal by its unscaled digits, as its function does, a join over values that fit and values that overflow matched every row with ANSI on and off, and `shuffle.enabled` on and off. So a join returns wrong rows only when the connector's stored partition value for an overflowing row differs from what its function computes on the raw value, e.g. a connector following the write path's cast, or a function that misreads a `Decimal` of more precision than it declared. Refusing such an argument is correct whatever the connector stored: the join shuffles both sides. ### Does this PR introduce _any_ user-facing change? Yes. Over a layout whose function declares a decimal of the column's scale with less precision than the column, Spark no longer evaluates the function, even when every value fits, since it cannot know they do: - a join no longer uses the identity reduce onto that layout, nor shuffles the other side onto it; it shuffles as for a layout Spark cannot evaluate, e.g. both sides in the tests here, with the layout on either side. A reduce between two transforms that a `ReducibleFunction` relates, e.g. `bucket(8)` and `bucket(4)`, evaluates neither function, so it is unaffected. Two sides on the same such layout need no evaluation and keep their plan. The per-partition runtime bloom filter (SPARK-60541) is not added for such a layout either. Join results do not change; - a cogroup over such a layout takes the hash shuffle, as for any layout Spark cannot evaluate. Keys of one type pair correctly. A key stored otherwise than the layout's column, e.g. a `DECIMAL(20,2)` key against the `DECIMAL(12,2)` column, failed while planning with `spark.sql.sources.v2.bucketing.shuffle.enabled=true` and now pairs its groups wrongly, as it already does with the default `false`: a Pandas or Arrow cogroup does not coerce its keys (SPARK-60782). A function declaring the column's own or a wider decimal keeps its plan; Iceberg's `bucket` declares the column's own decimal type, so it is not affected. A cogroup whose key has more precision than the layout's column, also as a user-defined type over a decimal, is placed as before. ### How was this patch tested? New tests and test rows. Each test fails without the change; the rows with a wider declared decimal are controls: - `TransformExpressionSuite` "SPARK-60027: an argument is taken as it is when it is stored as the declared type is": a top-level decimal of more precision than declared is refused, in either storage, and one of less precision across the storages is taken and evaluated from an unsafe row; - `TransformExpressionSuite` "SPARK-61812: a decimal goes as another one only as its cast keeps it": for every pair of 20 decimal types the rule takes, Spark's cast to the declared type keeps the values of the most digits the column holds, with ANSI on and off; - `TransformExpressionSuite` "SPARK-61812: a cogroup's key takes a decimal of more precision as it is": a marked transform takes one, also as a user-defined type over a decimal, or over another user-defined type, on either side, but not one of another scale or a nested one of more precision; unmarked, none is taken; - `ShuffleSpecSuite` "SPARK-60027: KeyedShuffleSpec refuses a transform whose arguments miss its input types": narrower and wider declared decimals, through the identity reduce in both directions and the one-side shuffle; - `EnsureRequirementsSuite` "SPARK-61812: a join shuffles both sides, not onto a layout declaring a narrower decimal": a narrower declared decimal shuffles both sides, with the layout on either side, the column's own or a wider one keeps the layout, and two sides on one narrower-declared layout keep it; - `EnsureRequirementsSuite` "SPARK-61812: a cogroup takes the hash shuffle over a layout declaring a narrower decimal (a key stored otherwise pairs wrongly, SPARK-60782)": both sides are hash-shuffled, with a key of the column's type and with one stored otherwise, documenting today's behavior; - `PerPartitionRuntimeFilterSuite` "SPARK-61812: no per-partition bloom filter for a function declaring a narrower decimal": none for a narrower declared decimal, one for the column's own; - `KeyGroupedPartitioningSuite` "SPARK-61812: a decimal column is evaluated only by a function declaring no less": joins over a `DECIMAL(12,2)` column with values that fit and values that overflow `DECIMAL(10,2)`. Declaring `DECIMAL(12,2)`, the identity reduce and the one-side shuffle keep their plan; declaring `DECIMAL(10,2)`, both shuffle both sides. The rows match either way, since the test catalog, which now buckets a decimal by its unscaled digits, stores the bucket of the raw value. Guards, which pass without the change and fail without the `storedKey` arm: `EnsureRequirementsSuite` "SPARK-61812: a cogroup places a decimal key of more precision, keyed on either side" (a `DECIMAL(12,2)` key, and a user-defined type over one, onto a `DECIMAL(10,2)` bucket, with the keyed side on the left and on the right), and the existing "SPARK-60027: a cogroup shuffled onto a decimal bucket places a key of more precision". Also ran `SparkThrowableSuite`, `ShuffleSpecSuite`, `TransformExpressionSuite`, `DistributionSuite`, the `KeyGroupedPartitioning*` suites, `EnsureRequirementsSuite`, `WriteDistributionAndOrderingSuite`, `DataSourceV2FunctionSuite`, `GroupPartitionsExecSuite`, `ProjectedOrderingAndPartitioningSuite`, `ValidateRequirementsSuite`, `PlannerSuite`, `SparkPlannerSuite` and `PerPartitionRuntimeFilterSuite`: 889 tests in all, and `dev/lint-scala`. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Claude Opus 5.5) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
