[ 
https://issues.apache.org/jira/browse/SPARK-62225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18148965#comment-18148965
 ] 

Vivek Gangavarapu commented on SPARK-62225:
-------------------------------------------

I'd like to pick this up once SPARK-60178 is merged, since it builds on the 
same code.

My plan is to split it in two. The first part is canonicalization: treat 
integral Add/Multiply in ANSI mode as non-associative, the same way SPARK-60178 
handles FLOAT/DOUBLE and 38-digit DECIMAL, so differently associated forms stop 
matching in GROUP BY. That one is small.

The second part is ReorderAssociativeOperator, and that's where I'm less sure. 
It folds literals across the whole chain, so in ANSI mode {{(a + 10) + (-10)}} 
becomes just {{a}}, and a non-nullable {{a * b * 0}} becomes {{0}}, which can 
hide an overflow the original expression would have thrown. Turning the rule 
off for ANSI would lose constant folding for most queries now that ANSI is the 
default. A middle ground would be to fold only when no intermediate step can 
overflow, but that's more involved.

Does it make sense to do the canonicalization part first and handle the 
optimizer separately, or would you rather fix both together? Happy to go either 
way.


> Re-association of integral Add/Multiply hides ANSI overflow in 
> canonicalization
> -------------------------------------------------------------------------------
>
>                 Key: SPARK-62225
>                 URL: https://issues.apache.org/jira/browse/SPARK-62225
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 5.0.0
>            Reporter: Vivek Gangavarapu
>            Priority: Major
>
> Under ANSI mode, integral Add/Multiply can overflow, so re-associating them 
> is not semantics-preserving. Canonicalization still treats them as 
> associative, so two expressions that behave differently are considered 
> semantically equal.
> Repro (spark.sql.ansi.enabled=true):
> {code:sql}
> CREATE TABLE t (a BIGINT, b BIGINT, c BIGINT) USING parquet;
> INSERT INTO t VALUES (1099511627776, 1099511627776, 0); -- a = b = 2^40, c = 0
> SELECT (a * b) * c FROM t;                     -- ARITHMETIC_OVERFLOW
> SELECT a * (b * c) FROM t;                     -- 0
> SELECT (a * b) * c FROM t GROUP BY a * (b * c); -- passes analysis, returns 0 
> without error
> {code}
> Expected: the last query should not match the select expression to the 
> grouping expression, since the two associations behave differently under ANSI.
> Fixing this needs more than a canonicalization change, since 
> ReorderAssociativeOperator also re-associates integral Add.
> Follow-up from the review of SPARK-60178 
> (https://github.com/apache/spark/pull/59437), which covers FLOAT, DOUBLE and 
> max-precision DECIMAL.



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