[ 
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.

Found by sunchao while reviewing SPARK-50593 
(https://github.com/apache/spark/pull/55885, 
[comment|https://github.com/apache/spark/pull/55885#issuecomment-7046189335]): 
{{truncate(s, 1)}} over a {{UTF8_LCASE}} column split {{U+0130}} from {{i 
U+0307}} under a window. That PR lets {{truncate(s, w)}} report its 
partitioning, which adds another such transform over collated strings, but 
master already gets the same queries wrong for {{bucket}} and other transforms, 
so the fix is separate and can be backported on its own. Fix: 
https://github.com/apache/spark/pull/60793.


  was:
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.



> 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
>              Labels: pull-request-available
>
> 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.
> Found by sunchao while reviewing SPARK-50593 
> (https://github.com/apache/spark/pull/55885, 
> [comment|https://github.com/apache/spark/pull/55885#issuecomment-7046189335]):
>  {{truncate(s, 1)}} over a {{UTF8_LCASE}} column split {{U+0130}} from {{i 
> U+0307}} under a window. That PR lets {{truncate(s, w)}} report its 
> partitioning, which adds another such transform over collated strings, but 
> master already gets the same queries wrong for {{bucket}} and other 
> transforms, so the fix is separate and can be backported on its own. Fix: 
> https://github.com/apache/spark/pull/60793.



--
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