You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ul...@apache.org on 2022/01/18 11:34:43 UTC

[incubator-kyuubi] branch branch-1.3 updated: [KYUUBI #1784] Fix float types lose precision

This is an automated email from the ASF dual-hosted git repository.

ulyssesyou pushed a commit to branch branch-1.3
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/branch-1.3 by this push:
     new d190cce  [KYUUBI #1784] Fix float types lose precision
d190cce is described below

commit d190cce47d71915e84ddb88af1063ce18d07272e
Author: Fu Chen <cf...@gmail.com>
AuthorDate: Tue Jan 18 19:34:03 2022 +0800

    [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 <cf...@gmail.com>
    Signed-off-by: ulysses-you <ul...@apache.org>
    (cherry picked from commit 53bbbb71616c6857fe6cbb730956542bd26e57b7)
    Signed-off-by: ulysses-you <ul...@apache.org>
---
 .../src/main/scala/org/apache/kyuubi/schema/RowSet.scala          | 7 +++++--
 .../src/test/scala/org/apache/kyuubi/schema/RowSetSuite.scala     | 4 ++--
 .../org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala      | 8 ++++++++
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/schema/RowSet.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/schema/RowSet.scala
index 9a4d22e..ad7f97d 100644
--- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/schema/RowSet.scala
+++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/schema/RowSet.scala
@@ -93,7 +93,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 =>
@@ -183,7 +183,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 =>
diff --git a/externals/kyuubi-spark-sql-engine/src/test/scala/org/apache/kyuubi/schema/RowSetSuite.scala b/externals/kyuubi-spark-sql-engine/src/test/scala/org/apache/kyuubi/schema/RowSetSuite.scala
index 9ee0fde..d61b164 100644
--- a/externals/kyuubi-spark-sql-engine/src/test/scala/org/apache/kyuubi/schema/RowSetSuite.scala
+++ b/externals/kyuubi-spark-sql-engine/src/test/scala/org/apache/kyuubi/schema/RowSetSuite.scala
@@ -143,7 +143,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
@@ -225,7 +225,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)
diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala
index a761add..766a027 100644
--- a/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala
+++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/engine/spark/SparkSqlEngineSuite.scala
@@ -124,5 +124,13 @@ class SparkSqlEngineSuite extends WithKyuubiServer with JDBCTestUtils {
     }
   }
 
+  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
 }