Skip to content

Conversation

@Ngone51
Copy link
Member

@Ngone51 Ngone51 commented Jan 15, 2020

What changes were proposed in this pull request?

For decimal values between -1.0 and 1.0, it should has same precision and scale in Decimal, in order to make it be consistent with DecimalType.

Why are the changes needed?

Currently, for values between -1.0 and 1.0, precision and scale is inconsistent between Decimal and DecimalType. For example, for numbers like 0.3, it will have (precision, scale) as (2, 1) in Decimal, but (1, 1) in DecimalType:

scala> Literal(new BigDecimal("0.3")).dataType.asInstanceOf[DecimalType].precision
res3: Int = 1

scala> Literal(new BigDecimal("0.3")).value.asInstanceOf[Decimal].precision
res4: Int = 2

We should make Decimal be consistent with DecimalType. And, here, we change it to only count precision digits after dot for values between -1.0 and 1.0 as other DBMS does, like hive:

hive> create table testrel as select 0.3;
hive> describe testrel;
OK
_c0                 	decimal(1,1)

This could bring larger scale for values between -1.0 and 1.0.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Updated existed tests.

@SparkQA
Copy link

SparkQA commented Jan 15, 2020

Test build #116775 has finished for PR 27217 at commit 12e26c3.

  • This patch fails Spark unit tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Jan 15, 2020

Test build #116788 has finished for PR 27217 at commit ff3cd01.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

test("SPARK-28322: IntegralDivide supports decimal type") {
withSQLConf(SQLConf.LEGACY_INTEGRALDIVIDE_RETURN_LONG.key -> "false") {
checkEvaluation(IntegralDivide(Literal(Decimal(1)), Literal(Decimal(2))), Decimal(0))
checkEvaluation(IntegralDivide(Literal(Decimal(1.4)), Literal(Decimal(0.6))), Decimal(2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix changes the result?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. IntegralDivide requires same data type for left and right. And the type of Literal(Decimal(0.6)) has changed from DecimalType(2, 1) to DecimalType(1, 1).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see.

@maropu maropu changed the title [SPARK-30518] Precision and scale should be same for values between -1.0 and 1.0 in Decimal [SPARK-30518][SQL] Precision and scale should be same for values between -1.0 and 1.0 in Decimal Jan 16, 2020
@maropu maropu closed this in 5a55a5a Jan 16, 2020
@maropu
Copy link
Member

maropu commented Jan 16, 2020

Thanks! Merged to master.

@Ngone51
Copy link
Member Author

Ngone51 commented Jan 16, 2020

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants