Ngone51 commented on a change in pull request #26881: [SPARK-30252][SQL]
Disallow negative scale of Decimal under ansi mode
URL: https://github.com/apache/spark/pull/26881#discussion_r365639932
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/types/DecimalType.scala
##########
@@ -141,20 +144,24 @@ object DecimalType extends AbstractDataType {
}
private[sql] def fromLiteral(literal: Literal): DecimalType = literal.value
match {
- case v: Short => fromBigDecimal(BigDecimal(v))
- case v: Int => fromBigDecimal(BigDecimal(v))
- case v: Long => fromBigDecimal(BigDecimal(v))
+ case v: Short => fromDecimal(Decimal(BigDecimal(v)))
+ case v: Int => fromDecimal(Decimal(BigDecimal(v)))
+ case v: Long => fromDecimal(Decimal(BigDecimal(v)))
case _ => forType(literal.dataType)
}
- private[sql] def fromBigDecimal(d: BigDecimal): DecimalType = {
- DecimalType(Math.max(d.precision, d.scale), d.scale)
- }
+ private[sql] def fromDecimal(d: Decimal): DecimalType =
DecimalType(d.precision, d.scale)
private[sql] def bounded(precision: Int, scale: Int): DecimalType = {
DecimalType(min(precision, MAX_PRECISION), min(scale, MAX_SCALE))
}
+ private[sql] def checkNegativeScale(scale: Int): Unit = {
+ if (scale < 0 && !SQLConf.get.allowNegativeScaleOfDecimalEnabled) {
+ throw new AnalysisException(s"Negative scale is not allowed under ansi
mode: $scale")
Review comment:
good catch!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]