Skip to content

Commit 53bbbb7

Browse files
cfmcgradyulysses-you
authored andcommitted
[KYUUBI #1784] Fix float types lose precision
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html 2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'. 3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'. --> ### _Why are the changes needed?_ <!-- Please clarify why the changes are needed. For instance, 1. If you add a feature, you can talk about the use case of it. 2. If you fix a bug, you can clarify why it is a bug. --> close #1784 ### _How was this patch tested?_ - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #1790 from cfmcgrady/kyuubi-1784. Closes #1784 f6c7dae [Fu Chen] fix row-based row set 89e72b7 [Fu Chen] fix ut cef54bc [Fu Chen] fix float types lose precision Authored-by: Fu Chen <cfmcgrady@gmail.com> Signed-off-by: ulysses-you <ulyssesyou@apache.org>
1 parent c418f29 commit 53bbbb7

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/schema/RowSet.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ object RowSet {
9090

9191
case FloatType =>
9292
val values = getOrSetAsNull[java.lang.Float](rows, ordinal, nulls, 0.toFloat)
93-
.asScala.map(n => java.lang.Double.valueOf(n.toDouble)).asJava
93+
.asScala.map(n => java.lang.Double.valueOf(n.toString)).asJava
9494
TColumn.doubleVal(new TDoubleColumn(values, nulls))
9595

9696
case DoubleType =>
@@ -176,7 +176,10 @@ object RowSet {
176176

177177
case FloatType =>
178178
val tDoubleValue = new TDoubleValue
179-
if (!row.isNullAt(ordinal)) tDoubleValue.setValue(row.getFloat(ordinal))
179+
if (!row.isNullAt(ordinal)) {
180+
val doubleValue = java.lang.Double.valueOf(row.getFloat(ordinal).toString)
181+
tDoubleValue.setValue(doubleValue)
182+
}
180183
TColumnValue.doubleVal(tDoubleValue)
181184

182185
case DoubleType =>

externals/kyuubi-spark-sql-engine/src/test/scala/org/apache/kyuubi/engine/spark/schema/RowSetSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class RowSetSuite extends KyuubiFunSuite {
142142
val floatCol = cols.next().getDoubleVal
143143
floatCol.getValues.asScala.zipWithIndex.foreach {
144144
case (b, 11) => assert(b === 0)
145-
case (b, i) => assert(b === java.lang.Float.valueOf(s"$i.$i"))
145+
case (b, i) => assert(b.toFloat === java.lang.Float.valueOf(s"$i.$i"))
146146
}
147147

148148
val doubleCol = cols.next().getDoubleVal
@@ -226,7 +226,7 @@ class RowSetSuite extends KyuubiFunSuite {
226226

227227
val r4 = iter.next().getColVals
228228
assert(r4.get(4).getI64Val.getValue === 3)
229-
assert(r4.get(5).getDoubleVal.getValue === 3.3f)
229+
assert(r4.get(5).getDoubleVal.getValue.toFloat === 3.3f)
230230

231231
val r5 = iter.next().getColVals
232232
assert(r5.get(6).getDoubleVal.getValue === 4.4d)

kyuubi-server/src/test/scala/org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,13 @@ class SparkSqlEngineSuite extends WithKyuubiServer with HiveJDBCTestHelper {
124124
}
125125
}
126126

127+
test("KYUUBI-1784: float types should not lose precision") {
128+
withJdbcStatement() { statement =>
129+
val resultSet = statement.executeQuery("SELECT cast(0.1 as float) AS col")
130+
assert(resultSet.next())
131+
assert(resultSet.getString("col") == "0.1")
132+
}
133+
}
134+
127135
override protected def jdbcUrl: String = getJdbcUrl
128136
}

0 commit comments

Comments
 (0)