[ 
https://issues.apache.org/jira/browse/SPARK-62150?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Anil K updated SPARK-62150:
---------------------------
    Description: 
A V2 scan's {{KeyedPartitioning}} satisfies a {{ClusteredDistribution}} on a 
column when each partition expression is that column, or a function of it. The 
second half assumes that rows sharing a value of the column have identical 
bytes. A function of a column, such as a connector's partition transform, 
computes from the value as stored, so it keeps identical values together, not 
equal ones. The two differ for:
* a string of a non-binary collation: {{'A'}} = {{'a'}} under {{UTF8_LCASE}}, 
and {{'a'}} = {{'a '}} under {{UTF8_BINARY_RTRIM}};
* {{FLOAT}} and {{DOUBLE}}: {{0.0}} = {{-0.0}}, and all NaNs are equal. An 
aggregate normalizes its float keys, but aliases them with the column's exprId, 
so its clustering still names the column. A window clusters on the normalized 
expression and is not affected;
* any struct, array or map that holds either.

An aggregate, a window or a semi-join over the column then skips its shuffle 
and splits equal values. Partition keys are compared under their type's 
equality, so a column as a partition key of its own ({{identity}}) is correct. 
Any other function of the column can be wrong.

Measured on master (03a2b318c4f) with the test catalog, one split per row, 
against the same query with {{spark.sql.sources.v2.bucketing.enabled=false}}, 
with AQE on and off:

||partitioning||query||result||expected||shuffles||
|{{bucket(n, s)}}, {{s}} {{UTF8_LCASE}}, rows {{'A'}}, {{'a'}}|{{count(*) OVER 
(PARTITION BY s)}}|1, 1|2, 2|0|
|same|{{GROUP BY s}}|two groups of 1|one group of 2|0|
|{{bucket(n, s)}} in two scans, one row each, {{UNION ALL}}|{{GROUP BY s}}|two 
groups of 1|one group of 2|0|
|{{(identity(id), bucket(n, s))}}, rows {{(1, 'A')}}, {{(1, 'a')}}, {{(2, 
'A')}}, {{(2, 'a')}}|{{GROUP BY id, s}}|four groups of 1|two groups of 2|0|
|{{bucket(n, s)}} on both sides, {{'A'}} and {{'a'}}|{{s IN (SELECT s ...)}}, 
or the {{EXISTS}} form|no rows|{{'A'}}|0|
|{{(identity(id), bucket(n, s))}} on both sides|{{(id, s) IN (SELECT id, s 
...)}}|no rows|both rows|0|
|fact {{bucket(n, b)}} or {{identity(b)}}, {{b}} binary; dim {{c.s}} 
{{UTF8_LCASE}}; broadcast join on {{f.b = c.s COLLATE UTF8_BINARY}}|{{GROUP BY 
c.s}}, or a window over {{c.s}}|two groups of 1|one group of 2|0|
|fact {{bucket(n, str)}}; dim {{c.d DOUBLE}}; broadcast join on {{f.str = 
CAST(c.d AS STRING)}}; rows {{0.0}}, {{-0.0}}|{{GROUP BY c.d}}|two groups of 
1|one group of 2|0|
|a transform keying a {{DOUBLE}} on its sign bit, rows {{0.0}}, 
{{-0.0}}|{{GROUP BY d}}, {{DISTINCT d}}|two groups, two rows|one group, one 
row|0|

The broadcast join cases come from the layout that a broadcast hash join 
reports for the streamed side over the build side's key. {{bucket(n, c.s 
COLLATE UTF8_BINARY)}} is a function of the collated {{c.s}}.

The semi-join cases come from planning order. {{RewritePredicateSubquery}} runs 
after {{RewriteCollationJoin}}, so the semi-join compares {{s}} itself instead 
of {{CollationKey(s)}}, and clusters each side on {{s}}. With 
{{spark.sql.sources.v2.bucketing.allowKeysSubsetOfPartitionKeys.enabled}} on, 
the mixed layout's shuffle spec pairs on the positions that 
{{KeyedShuffleSpec.keyPositions}} maps, which include the bucket of {{s}}, so 
fixing the inference alone leaves that case wrong.

Other joins are correct:
* {{RewriteCollationJoin}} compares an equi-join of two collated columns 
through {{CollationKey}}, and float join keys are normalized expressions.
* A collated key left bare, as in {{t1.s = concat(t2.s, '')}}, is not paired 
either: hash joins are refused for such keys, and the one-side shuffle cannot 
rebuild a transform over a non-column key.

Fix: a partition expression's reference covers a cluster key only if values its 
type deems equal are stored alike, i.e. {{UnsafeRowUtils.isBinaryStable}} and 
no {{FLOAT}}/{{DOUBLE}} at any depth. This applies wherever 
{{KeyedPartitioning}} infers clustering from references:
* {{isFunctionOfClusterKeys}};
* {{positionsCoveringClusterKeys}};
* {{keysCanSatisfy}}, where a position still counts if it is a cluster key 
itself;
* {{createShuffleSpec}}, which projects only onto positions that both 
{{KeyedShuffleSpec.keyPositions}} maps and {{positionsCoveringClusterKeys}} 
counts.

{{identity}}, and a clustering that names the transform itself, are unchanged. 
A connector transform that respects the equality loses shuffle-free plans over 
the column, since Spark cannot tell that it does.

SPARK-50593 (https://github.com/apache/spark/pull/55885) lets {{truncate(s, 
w)}} report its partitioning, which adds another such transform over collated 
strings.


  was:
A V2 scan partitioned by a transform other than {{identity}} of a string column 
with a non-binary collation, e.g. {{bucket(n, s)}} over {{s STRING COLLATE 
UTF8_LCASE}}, reports a {{KeyedPartitioning}} that satisfies a 
{{ClusteredDistribution}} on {{s}}. A window or an aggregate over {{s}} then 
skips its shuffle and runs on the scan's partitions. That is correct only if 
values equal under the collation land in one partition. Spark merges partitions 
whose keys compare equal, and compares string keys with the collation, so 
{{identity(s)}} is fine. But a transform can map two values equal under the 
collation to keys that are not equal:
* {{bucket}} over a hash of the raw bytes puts {{'A'}} and {{'a'}} into 
different buckets;
* a prefix, such as a parameterless {{prefix1(s)}} or {{truncate(s, 1)}}, maps 
{{U+0130}} ({{İ}}) and {{i U+0307}}, which are equal under {{UTF8_LCASE}}, to 
{{'İ'}} and {{'i'}}, which are not.

Measured on master (98bbfe57a20) with the test catalog, 
{{spark.sql.sources.v2.bucketing.enabled=true}}, one split per row, against the 
same query with bucketing disabled:

||transform||rows||{{count(*) OVER (PARTITION BY s)}}||{{GROUP BY 
s}}||shuffles||
|{{bucket(5, s)}}|{{'A'}}, {{'a'}}|1, 1 (expected 2, 2)|2 groups of 1 (expected 
1 group of 2)|0|
|{{bucket(5, s)}}|{{U+0130}}, {{i U+0307}}|1, 1|2 groups of 1|0|
|{{prefix1(s)}}|{{U+0130}}, {{i U+0307}}|1, 1|2 groups of 1|0|
|{{identity(s)}}|either|2, 2|1 group of 2|0|

An equi-join on {{s}} is correct: it is planned over collation keys, so both 
sides shuffle.

SPARK-50593 (https://github.com/apache/spark/pull/55885) lets a transform with 
literal parameters, such as {{truncate(s, w)}}, report its partitioning, so 
{{truncate(s, 1)}} gives the same wrong rows as {{prefix1(s)}} above, where it 
shuffled before.

A fix could make a transform other than {{identity}} over a column whose type 
is not binary stable ({{UnsafeRowUtils.isBinaryStable}}) not satisfy a 
clustering on that column, e.g. in {{KeyedPartitioning.supportsExpressions}}, 
so such a scan shuffles. {{identity}} can keep its partitioning, since its keys 
are compared with the collation. A connector that knows its transform respects 
the collation has no way to say so today, so this costs those transforms their 
shuffle-free plans too.


        Summary: A keyed partitioning lets an aggregate, a window or a 
semi-join skip its shuffle and split values that are equal but stored 
differently, such as collated strings or signed zeros  (was: A partition 
transform of a collated string column lets a window or aggregate skip its 
shuffle and split values equal under the collation)

> A keyed partitioning lets an aggregate, a window or a semi-join skip its 
> shuffle and split values that are equal but stored differently, such as 
> collated strings or signed zeros
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: SPARK-62150
>                 URL: https://issues.apache.org/jira/browse/SPARK-62150
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 4.3.0, 5.0.0, 4.2.1, 4.0.5, 4.1.4, 4.4.0
>            Reporter: Anil K
>            Priority: Major
>
> A V2 scan's {{KeyedPartitioning}} satisfies a {{ClusteredDistribution}} on a 
> column when each partition expression is that column, or a function of it. 
> The second half assumes that rows sharing a value of the column have 
> identical bytes. A function of a column, such as a connector's partition 
> transform, computes from the value as stored, so it keeps identical values 
> together, not equal ones. The two differ for:
> * a string of a non-binary collation: {{'A'}} = {{'a'}} under {{UTF8_LCASE}}, 
> and {{'a'}} = {{'a '}} under {{UTF8_BINARY_RTRIM}};
> * {{FLOAT}} and {{DOUBLE}}: {{0.0}} = {{-0.0}}, and all NaNs are equal. An 
> aggregate normalizes its float keys, but aliases them with the column's 
> exprId, so its clustering still names the column. A window clusters on the 
> normalized expression and is not affected;
> * any struct, array or map that holds either.
> An aggregate, a window or a semi-join over the column then skips its shuffle 
> and splits equal values. Partition keys are compared under their type's 
> equality, so a column as a partition key of its own ({{identity}}) is 
> correct. Any other function of the column can be wrong.
> Measured on master (03a2b318c4f) with the test catalog, one split per row, 
> against the same query with {{spark.sql.sources.v2.bucketing.enabled=false}}, 
> with AQE on and off:
> ||partitioning||query||result||expected||shuffles||
> |{{bucket(n, s)}}, {{s}} {{UTF8_LCASE}}, rows {{'A'}}, {{'a'}}|{{count(*) 
> OVER (PARTITION BY s)}}|1, 1|2, 2|0|
> |same|{{GROUP BY s}}|two groups of 1|one group of 2|0|
> |{{bucket(n, s)}} in two scans, one row each, {{UNION ALL}}|{{GROUP BY 
> s}}|two groups of 1|one group of 2|0|
> |{{(identity(id), bucket(n, s))}}, rows {{(1, 'A')}}, {{(1, 'a')}}, {{(2, 
> 'A')}}, {{(2, 'a')}}|{{GROUP BY id, s}}|four groups of 1|two groups of 2|0|
> |{{bucket(n, s)}} on both sides, {{'A'}} and {{'a'}}|{{s IN (SELECT s ...)}}, 
> or the {{EXISTS}} form|no rows|{{'A'}}|0|
> |{{(identity(id), bucket(n, s))}} on both sides|{{(id, s) IN (SELECT id, s 
> ...)}}|no rows|both rows|0|
> |fact {{bucket(n, b)}} or {{identity(b)}}, {{b}} binary; dim {{c.s}} 
> {{UTF8_LCASE}}; broadcast join on {{f.b = c.s COLLATE UTF8_BINARY}}|{{GROUP 
> BY c.s}}, or a window over {{c.s}}|two groups of 1|one group of 2|0|
> |fact {{bucket(n, str)}}; dim {{c.d DOUBLE}}; broadcast join on {{f.str = 
> CAST(c.d AS STRING)}}; rows {{0.0}}, {{-0.0}}|{{GROUP BY c.d}}|two groups of 
> 1|one group of 2|0|
> |a transform keying a {{DOUBLE}} on its sign bit, rows {{0.0}}, 
> {{-0.0}}|{{GROUP BY d}}, {{DISTINCT d}}|two groups, two rows|one group, one 
> row|0|
> The broadcast join cases come from the layout that a broadcast hash join 
> reports for the streamed side over the build side's key. {{bucket(n, c.s 
> COLLATE UTF8_BINARY)}} is a function of the collated {{c.s}}.
> The semi-join cases come from planning order. {{RewritePredicateSubquery}} 
> runs after {{RewriteCollationJoin}}, so the semi-join compares {{s}} itself 
> instead of {{CollationKey(s)}}, and clusters each side on {{s}}. With 
> {{spark.sql.sources.v2.bucketing.allowKeysSubsetOfPartitionKeys.enabled}} on, 
> the mixed layout's shuffle spec pairs on the positions that 
> {{KeyedShuffleSpec.keyPositions}} maps, which include the bucket of {{s}}, so 
> fixing the inference alone leaves that case wrong.
> Other joins are correct:
> * {{RewriteCollationJoin}} compares an equi-join of two collated columns 
> through {{CollationKey}}, and float join keys are normalized expressions.
> * A collated key left bare, as in {{t1.s = concat(t2.s, '')}}, is not paired 
> either: hash joins are refused for such keys, and the one-side shuffle cannot 
> rebuild a transform over a non-column key.
> Fix: a partition expression's reference covers a cluster key only if values 
> its type deems equal are stored alike, i.e. {{UnsafeRowUtils.isBinaryStable}} 
> and no {{FLOAT}}/{{DOUBLE}} at any depth. This applies wherever 
> {{KeyedPartitioning}} infers clustering from references:
> * {{isFunctionOfClusterKeys}};
> * {{positionsCoveringClusterKeys}};
> * {{keysCanSatisfy}}, where a position still counts if it is a cluster key 
> itself;
> * {{createShuffleSpec}}, which projects only onto positions that both 
> {{KeyedShuffleSpec.keyPositions}} maps and {{positionsCoveringClusterKeys}} 
> counts.
> {{identity}}, and a clustering that names the transform itself, are 
> unchanged. A connector transform that respects the equality loses 
> shuffle-free plans over the column, since Spark cannot tell that it does.
> SPARK-50593 (https://github.com/apache/spark/pull/55885) lets {{truncate(s, 
> w)}} report its partitioning, which adds another such transform over collated 
> strings.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to