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

Yuming Wang commented on SPARK-28348:
-------------------------------------

Sorry. I mean could we avoid set scale number to 6 and then cast it to 18 for 
this case?

For example:
{code:sql}
CAST((decimal(p1, s1) * decimal(p2, s2)) as decimal(p3, s3))
{code}
How about just set 
[resultType|https://github.com/apache/spark/blob/3139d642fac0e6ae6b9edd1b4c2912c3a69f71e5/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala#L103]
 to {{decimal(p3, s3)}} because finally we need to cast to {{decimal(p3, s3)}}?

> Avoid cast twice for decimal type
> ---------------------------------
>
>                 Key: SPARK-28348
>                 URL: https://issues.apache.org/jira/browse/SPARK-28348
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 2.3.3
>            Reporter: Yuming Wang
>            Priority: Major
>
> Spark 2.1:
> {code:scala}
> scala> sql("select cast(cast(-34338492.215397047 as decimal(38, 10)) * 
> cast(-34338492.215397047 as decimal(38, 10)) as decimal(38, 
> 18))").show(false);
> +-------------------------------------------------------------------------------------------------------------------+
> |CAST((CAST(-34338492.215397047 AS DECIMAL(38,10)) * CAST(-34338492.215397047 
> AS DECIMAL(38,10))) AS DECIMAL(38,18))|
> +-------------------------------------------------------------------------------------------------------------------+
> |1179132047626883.596862135856320209                                          
>                                       |
> +-------------------------------------------------------------------------------------------------------------------+
> {code}
> Spark 2.3:
> {code:scala}
> scala> sql("select cast(cast(-34338492.215397047 as decimal(38, 10)) * 
> cast(-34338492.215397047 as decimal(38, 10)) as decimal(38, 
> 18))").show(false);
> +-------------------------------------------------------------------------------------------------------------------+
> |CAST((CAST(-34338492.215397047 AS DECIMAL(38,10)) * CAST(-34338492.215397047 
> AS DECIMAL(38,10))) AS DECIMAL(38,18))|
> +-------------------------------------------------------------------------------------------------------------------+
> |1179132047626883.596862000000000000                                          
>                                       |
> +-------------------------------------------------------------------------------------------------------------------+
> {code}
> I think we do not need to cast result to {{decimal(38, 6)}} and then cast 
> result to {{decimal(38, 18)}} for this case.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

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

Reply via email to