Skip to content

Commit 791e17d

Browse files
script variable replaces a number that exceeds a Long type value and converts it to scientific notation
1 parent a571911 commit 791e17d

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/utils/VariableUtils.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,11 @@ import org.apache.linkis.common.conf.Configuration
2121
import org.apache.linkis.common.exception.LinkisCommonErrorException
2222
import org.apache.linkis.common.variable
2323
import org.apache.linkis.common.variable._
24-
import org.apache.linkis.common.variable.DateTypeUtils.{
25-
getCurHour,
26-
getMonthDay,
27-
getToday,
28-
getYesterday
29-
}
30-
31-
import org.apache.commons.lang3.StringUtils
24+
import org.apache.linkis.common.variable.DateTypeUtils.{getCurHour, getMonthDay, getToday, getYesterday}
25+
import org.apache.commons.lang3.{StringUtils, Strings}
3226

3327
import java.time.ZonedDateTime
3428
import java.util
35-
3629
import scala.collection.JavaConverters._
3730
import scala.collection.mutable
3831
import scala.util.control.Exception.allCatch
@@ -121,8 +114,9 @@ object VariableUtils extends Logging {
121114
}
122115
case _ =>
123116
if (!nameAndType.contains(key) && StringUtils.isNotEmpty(value)) {
124-
if ((allCatch opt value.toDouble).isDefined) {
125-
nameAndType(key) = variable.DoubleValue(value.toDouble)
117+
// if ((allCatch opt value.toDouble).isDefined) {
118+
if ((allCatch opt BigDecimal(value)).isDefined && !Strings.CS.startsWith(value, "0")) {
119+
nameAndType(key) = variable.BigDecimalValue(BigDecimal(value))
126120
} else {
127121
nameAndType(key) = variable.StringType(value)
128122
}

linkis-commons/linkis-common/src/main/scala/org/apache/linkis/common/variable/VariableType.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,44 @@ case class YearType(value: CustomYearType) extends VariableType {
110110

111111
}
112112

113+
case class BigDecimalValue(value: BigDecimal) extends VariableType {
114+
override def getValue: String = {
115+
val result = bigDecimalOrLong(value)
116+
result match {
117+
case bd: BigDecimal => bd.bigDecimal.toPlainString
118+
case _ => result.toString
119+
}
120+
}
121+
122+
def calculator(signal: String, bValue: String): String = {
123+
signal match {
124+
case "+" => val res = value + BigDecimal(bValue); formatResult(res)
125+
case "-" => val res = value - BigDecimal(bValue); formatResult(res)
126+
case "*" => val res = value * BigDecimal(bValue); formatResult(res)
127+
case "/" => val res = value / BigDecimal(bValue); formatResult(res)
128+
case _ =>
129+
throw new LinkisCommonErrorException(20050, s"BigDecimal class is not supported to use:$signal")
130+
}
131+
}
132+
133+
private def formatResult(bd: BigDecimal): String = {
134+
val result = bigDecimalOrLong(bd)
135+
result match {
136+
case bd: BigDecimal => bd.bigDecimal.toPlainString
137+
case _ => result.toString
138+
}
139+
}
140+
141+
private def bigDecimalOrLong(bd: BigDecimal): BigDecimal = {
142+
// 检查是否为整数且在 Long 范围内
143+
if (bd.isWhole && bd.isValidLong) {
144+
bd.longValue
145+
} else {
146+
bd
147+
}
148+
}
149+
}
150+
113151
case class LongType(value: Long) extends VariableType {
114152
override def getValue: String = value.toString
115153

linkis-commons/linkis-common/src/test/scala/org/apache/linkis/common/utils/VariableUtilsTest.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,36 @@ class VariableUtilsTest {
9090
assertEquals(nameAndValue.size, 2)
9191
}
9292

93+
@Test def testReplaceBigDecimal(): Unit = {
94+
val sql = """select
95+
|${num} as num,
96+
|${num-2} as num_sub2,
97+
|${num+2} as num_add2,
98+
|${long_num} as long_num,
99+
|${long_num-1} as long_num_sub1,
100+
|${long_num+1} as long_num_add1,
101+
|${big_num} as big_num,
102+
|${big_num-1} as big_num_sub1,
103+
|${big_num+1} as big_num_add1,
104+
|'${str_num}' as str_num""".stripMargin
105+
val varMap = new util.HashMap[String, String]()
106+
varMap.put("num", "301")
107+
varMap.put("long_num", "9223372036854775807")
108+
varMap.put("big_num", "3000102010000000000000000200001")
109+
varMap.put("str_num", "03000102010000000000000000200001")
110+
111+
val resultSql = """select
112+
|301 as num,
113+
|299 as num_sub2,
114+
|303 as num_add2,
115+
|9223372036854775807 as long_num,
116+
|9223372036854775806 as long_num_sub1,
117+
|9223372036854775808 as long_num_add1,
118+
|3000102010000000000000000200001 as big_num,
119+
|3000102010000000000000000200000 as big_num_sub1,
120+
|3000102010000000000000000200002 as big_num_add1,
121+
|'03000102010000000000000000200001' as str_num""".stripMargin
122+
assertEquals(VariableUtils.replace(sql, "sql", varMap), resultSql)
123+
}
124+
93125
}

0 commit comments

Comments
 (0)