[
https://issues.apache.org/jira/browse/SPARK-62196?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Anil K updated SPARK-62196:
---------------------------
Affects Version/s: 3.5.9
4.0.5
4.1.4
4.2.1
4.3.0
4.4.0
> Literal treats 0.0 and -0.0 as equal while hashing them apart, so the
> optimizer can fold IF and CASE WHEN branches to the wrong signed zero
> -------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: SPARK-62196
> URL: https://issues.apache.org/jira/browse/SPARK-62196
> Project: Spark
> Issue Type: Bug
> Components: SQL
> Affects Versions: 3.5.9, 4.3.0, 5.0.0, 4.2.1, 4.0.5, 4.1.4, 4.4.0
> Reporter: Anil K
> Priority: Major
>
> {{Literal.equals}} finds {{0.0}} and {{-0.0}} equal, as {{FLOAT}} and as
> {{DOUBLE}}, because it compares the boxed values with {{==}}, which is
> numeric. But {{Literal.hashCode}} keeps them apart, since
> {{java.lang.Double.hashCode}} hashes the bits. So {{Literal(0.0) ==
> Literal(-0.0)}} and {{Literal(0.0).semanticEquals(Literal(-0.0))}} are true
> while their hash codes differ, and the two literals break the
> {{equals}}/{{hashCode}} contract.
> The two values are not interchangeable: {{CAST(-0.0D AS STRING)}} is
> {{-0.0}}, and a function can tell them apart, e.g. with {{Math.copySign}}. A
> rule that drops one of two "equal" expressions can therefore change results.
> {{SimplifyConditionals}} rewrites {{If(cond, trueValue, falseValue)}} to
> {{trueValue}} when {{trueValue.semanticEquals(falseValue)}}, and does the
> same for a {{CASE WHEN}} whose branches are all equal. Measured on master
> (98bbfe57a20), against the same query with
> {{spark.sql.optimizer.excludedRules=org.apache.spark.sql.catalyst.optimizer.SimplifyConditionals}}:
> ||query||rows||expected||
> |{{SELECT id, CAST(IF(id = 0, 0.0D, -0.0D) AS STRING) FROM range(2)}}|{{[0,
> 0.0]}}, {{[1, 0.0]}}|{{[0, 0.0]}}, {{[1, -0.0]}}|
> |{{SELECT id, CAST(IF(id = 0, -0.0D, 0.0D) AS STRING) FROM range(2)}}|{{[0,
> -0.0]}}, {{[1, -0.0]}}|{{[0, -0.0]}}, {{[1, 0.0]}}|
> |{{SELECT id, CAST(CASE WHEN id = 0 THEN 0.0D ELSE -0.0D END AS STRING) FROM
> range(2)}}|{{[0, -0.0]}}, {{[1, -0.0]}}|{{[0, 0.0]}}, {{[1, -0.0]}}|
> |{{SELECT id, CAST(coalesce(IF(id = 0, 0.0D, -0.0D), 1.0D) AS STRING) FROM
> range(2)}}|{{[0, 0.0]}}, {{[1, 0.0]}}|{{[0, 0.0]}}, {{[1, -0.0]}}|
> The optimized plan replaces the whole {{IF}} or {{CASE WHEN}} with the
> constant of one branch. Other rules that keep one of two {{semanticEquals}}
> expressions, such as the {{And}}/{{Or}} simplifications in
> {{BooleanSimplification}}, read the same equality. Anything that puts
> literals in a hash-based collection ({{ExpressionSet}},
> {{EquivalentExpressions}}, a {{HashMap}} keyed by canonical expressions) can
> find two "equal" literals in different buckets, so whether they are merged
> depends on the path.
> SPARK-50593 (https://github.com/apache/spark/pull/55885) hit the same
> equality for partition transform parameters, where {{f(id, 0.0)}} and {{f(id,
> -0.0)}} were taken for one partitioning. It works around it there by
> comparing {{FLOAT}} and {{DOUBLE}} parameters by their bits, but the
> {{Literal}} equality is still master's.
> A fix could make {{Literal.equals}} tell {{0.0}} from {{-0.0}} for {{FLOAT}}
> and {{DOUBLE}}, e.g. by comparing {{java.lang.Double.compare(a, b) == 0}} (or
> the raw bits), while keeping today's rule that all NaNs are equal, so that it
> agrees with {{hashCode}}. Whether {{hashCode}} should then also normalize NaN
> payloads, and whether other places compare literal values directly, would
> need checking. This changes which expressions the optimizer treats as equal,
> so plan stability and golden files may move.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]