metanil commented on PR #55885:
URL: https://github.com/apache/spark/pull/55885#issuecomment-7087046286
@sunchao About the item 3 [P2] (very good find by the way), I wanted to
investigate a little more since this affects the API design, and I feel like I
should spend more time now while we have flexibility to change it.
**Item 3. The column's position.** Confirmed: `subtract(id, 1)` against
`subtract(id, 2)` and `subtract(1, id)` against `subtract(2, id)` both pass
`sameArgumentLayout` and send the connector the identical call `reducer([1],
subtract, [2])`. With a reducer written for the column-first layout, the first
join returns 3 of 3 rows and the second 0 of 3. It isn't specific to
`subtract`: any function whose meaning changes when the column and the literal
swap places, such as `divide`, `mod` or `concat`, has the same gap. Only
commutative functions such as `add` or `multiply` are safe.
No connector can reach it today. No transform like this exists in Spark, its
tests or Iceberg. A connector that gives each partition function one meaning
either refuses the other argument order or normalizes it: Iceberg's
`Spark3Util.toPartitionSpec` reads the column from the transform's references
and the width from its literal wherever they sit, so `truncate(data, 4)` and
`truncate(4, data)` both become `truncate[4]`, and it reports one layout back
on read. It takes a connector that stores the partition expression as written
and evaluates it by position, with the column and the literal of the same type,
so that `bind()` can't tell the layouts apart either.
But the API shouldn't rely on that, and the new overload is unreleased, so
this is the time to settle it. I see three ways:
(a) Keep `Literal[]` and make the contract explicit in the javadoc: the
literal parameters must mean the same thing whatever position the column takes,
so a connector gives each partition function one argument layout. No API
change, but it holds only as long as connectors follow it.
(b) Pass the arguments in their positions, as `Transform.arguments()`
already does:
```java
default Reducer<I, O> reducer(
Expression[] thisArguments,
ReducibleFunction<?, ?> otherFunction,
Expression[] otherArguments)
```
A parameter arrives as a `Literal` and a column argument as a
`NamedReference`, so `subtract(id, 1)` arrives as `[id, 1]` and `subtract(1,
id)` as `[1, id]`, and a nested transform such as the `years(ts)` in `bucket(4,
years(ts))` can arrive as a `Transform`. The types are the public connector
expressions, **not Catalyst's**. But the column references would carry
_**planner state**_ rather than anything the connector reported. The name is
whatever the plan holds at that point: the scan's column, an alias after
`SELECT data AS d`, or the other side's column after a rebuild. And the planner
builds transforms over expressions, e.g. `bucket(4, b + 1)` for a side shuffled
onto a layout or a broadcast join's expansion, which would arrive as a
reference to `b`. Spark's own layout check keeps that from changing the
reducer's answer, _but it's a public value a connector must not read_, and a
connector checking the arguments against the transform it reported would see a
mismatch. Passing a
fixed placeholder for every column argument avoids that, at the cost of a
`NamedReference` that names nothing.
(c) Keep `Literal[]` and add where the columns sit:
```java
default Reducer<I, O> reducer(
Literal<?>[] thisParams,
int[] thisColumnPositions,
ReducibleFunction<?, ?> otherFunction,
Literal<?>[] otherParams,
int[] otherColumnPositions)
```
`subtract(id, 1)` arrives as `[1]` with column position `[0]`, and
`subtract(1, id)` as `[1]` with `[1]`. That's exactly what the gap needs and
nothing about the query crosses the boundary, and reducers that read "the n-th
literal" stay as they are. It describes no nested transform, which no scan can
report today, and its shape is the API's own rather than
`Transform.arguments()`'s.
All three close the gap for any connector that follows its contract, and
`sameArgumentLayout` stays as Spark's own check in each.
**I'm leaning towards (a)**: just making the contract explicit in the
javadoc. But if this is something thats a must fix then option (c) is what I
preferred : it makes the gap impossible rather than unlikely, it costs nothing
for released code, and it adds no names or plan shapes to the connector API,
which (b) would have to document as meaningless. (b) with placeholders is the
alternative if you'd rather the signature follow `Transform.arguments()`.
Since either changes the API that @peter-toth reviewed in detail, I'd like
you both to choose. @peter-toth, what do you think?
--
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]