Skip to content

Commit

Permalink
[KYUUBI #1784] Fix float types lose precision
Browse files Browse the repository at this point in the history
<!--
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>
  • Loading branch information
cfmcgrady authored and ulysses-you committed Jan 18, 2022
1 parent c418f29 commit 53bbbb7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Expand Up @@ -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 =>
Expand Down Expand Up @@ -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 =>
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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
}

0 comments on commit 53bbbb7

Please sign in to comment.