Repository: kudu
Updated Branches:
  refs/heads/master 0a37d1f3b -> 9749d4cde


KUDU-2338: [Java] Coerce BigDecimal values in KuduPredicate

We need to coerce the BigDecimal values to the expected
scale to ensure they can be correctly decoded server side.

This was missed in the KuduPredicate implementation though
we did it everywhere else.

I added the coercion and a test that will fail if it is ever removed.

Change-Id: Ia7e1e397524dfc2725b33b9683ebbdc38fe8cfc8
Reviewed-on: http://gerrit.cloudera.org:8080/9598
Reviewed-by: Mike Percy <mpe...@apache.org>
Tested-by: Kudu Jenkins


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/9749d4cd
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/9749d4cd
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/9749d4cd

Branch: refs/heads/master
Commit: 9749d4cde4c475783b0e936d641749b3394c5aad
Parents: 0a37d1f
Author: Grant Henke <granthe...@gmail.com>
Authored: Mon Mar 12 21:08:44 2018 -0500
Committer: Grant Henke <granthe...@gmail.com>
Committed: Tue Mar 13 03:42:58 2018 +0000

----------------------------------------------------------------------
 .../java/org/apache/kudu/client/KuduPredicate.java |  3 +++
 .../org/apache/kudu/client/TestKuduPredicate.java  | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/9749d4cd/java/kudu-client/src/main/java/org/apache/kudu/client/KuduPredicate.java
----------------------------------------------------------------------
diff --git 
a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduPredicate.java 
b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduPredicate.java
index 19a73bd..814b7de 100644
--- a/java/kudu-client/src/main/java/org/apache/kudu/client/KuduPredicate.java
+++ b/java/kudu-client/src/main/java/org/apache/kudu/client/KuduPredicate.java
@@ -244,6 +244,9 @@ public class KuduPredicate {
     int precision = typeAttributes.getPrecision();
     int scale = typeAttributes.getScale();
 
+    // Coerce the value to have the same precision and scale
+    value = DecimalUtil.coerce(value, precision, scale);
+
     BigDecimal minValue = DecimalUtil.minValue(precision, scale);
     BigDecimal maxValue = DecimalUtil.maxValue(precision, scale);
     Preconditions.checkArgument(value.compareTo(maxValue) <= 0 && 
value.compareTo(minValue) >= 0,

http://git-wip-us.apache.org/repos/asf/kudu/blob/9749d4cd/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduPredicate.java
----------------------------------------------------------------------
diff --git 
a/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduPredicate.java 
b/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduPredicate.java
index e7c40c0..1caf626 100644
--- 
a/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduPredicate.java
+++ 
b/java/kudu-client/src/test/java/org/apache/kudu/client/TestKuduPredicate.java
@@ -1147,4 +1147,21 @@ public class TestKuduPredicate {
         binaryCol, ImmutableList.of(new byte[] { (byte) 0xAB, (byte) 0x01, 
(byte) 0xCD },
                                     new byte[] { (byte) 0x00 })).toString());
   }
+
+  @Test
+  public void TestDecimalCoercion() {
+    Assert.assertEquals(
+        KuduPredicate.newComparisonPredicate(decimal32Col, LESS, 
BigDecimal.valueOf(123)),
+        KuduPredicate.newComparisonPredicate(decimal32Col, LESS, 
BigDecimal.valueOf(12300, 2))
+    );
+    Assert.assertEquals(
+        KuduPredicate.newComparisonPredicate(decimal32Col, GREATER, 
BigDecimal.valueOf(123, 1)),
+        KuduPredicate.newComparisonPredicate(decimal32Col, GREATER, 
BigDecimal.valueOf(1230, 2))
+    );
+    Assert.assertEquals(
+        KuduPredicate.newComparisonPredicate(decimal32Col, EQUAL, 
BigDecimal.valueOf(1, 0)),
+        KuduPredicate.newComparisonPredicate(decimal32Col, EQUAL, 
BigDecimal.valueOf(100, 2))
+    );
+  }
+
 }

Reply via email to