[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

f6c7dae8 [Fu Chen] fix row-based row set
89e72b73 [Fu Chen] fix ut
cef54bc0 [Fu Chen] fix float types lose precision

Authored-by: Fu Chen <cfmcgrady@gmail.com>
Signed-off-by: ulysses-you <ulyssesyou@apache.org>
This commit is contained in:
Fu Chen 2022-01-18 19:34:03 +08:00 committed by ulysses-you
parent c418f29011
commit 53bbbb7161
No known key found for this signature in database
GPG Key ID: 4C500BC62D576766
3 changed files with 15 additions and 4 deletions

View File

@ -90,7 +90,7 @@ object RowSet {
case FloatType =>
val values = getOrSetAsNull[java.lang.Float](rows, ordinal, nulls, 0.toFloat)
.asScala.map(n => java.lang.Double.valueOf(n.toDouble)).asJava
.asScala.map(n => java.lang.Double.valueOf(n.toString)).asJava
TColumn.doubleVal(new TDoubleColumn(values, nulls))
case DoubleType =>
@ -176,7 +176,10 @@ object RowSet {
case FloatType =>
val tDoubleValue = new TDoubleValue
if (!row.isNullAt(ordinal)) tDoubleValue.setValue(row.getFloat(ordinal))
if (!row.isNullAt(ordinal)) {
val doubleValue = java.lang.Double.valueOf(row.getFloat(ordinal).toString)
tDoubleValue.setValue(doubleValue)
}
TColumnValue.doubleVal(tDoubleValue)
case DoubleType =>

View File

@ -142,7 +142,7 @@ class RowSetSuite extends KyuubiFunSuite {
val floatCol = cols.next().getDoubleVal
floatCol.getValues.asScala.zipWithIndex.foreach {
case (b, 11) => assert(b === 0)
case (b, i) => assert(b === java.lang.Float.valueOf(s"$i.$i"))
case (b, i) => assert(b.toFloat === java.lang.Float.valueOf(s"$i.$i"))
}
val doubleCol = cols.next().getDoubleVal
@ -226,7 +226,7 @@ class RowSetSuite extends KyuubiFunSuite {
val r4 = iter.next().getColVals
assert(r4.get(4).getI64Val.getValue === 3)
assert(r4.get(5).getDoubleVal.getValue === 3.3f)
assert(r4.get(5).getDoubleVal.getValue.toFloat === 3.3f)
val r5 = iter.next().getColVals
assert(r5.get(6).getDoubleVal.getValue === 4.4d)

View File

@ -124,5 +124,13 @@ class SparkSqlEngineSuite extends WithKyuubiServer with HiveJDBCTestHelper {
}
}
test("KYUUBI-1784: float types should not lose precision") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SELECT cast(0.1 as float) AS col")
assert(resultSet.next())
assert(resultSet.getString("col") == "0.1")
}
}
override protected def jdbcUrl: String = getJdbcUrl
}