You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ch...@apache.org on 2019/08/07 06:22:00 UTC

[phoenix] branch 4.14-HBase-1.4 updated: PHOENIX-5416: Fix Array2IT testArrayRefToLiteral

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

chinmayskulkarni pushed a commit to branch 4.14-HBase-1.4
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/4.14-HBase-1.4 by this push:
     new b8cc92a  PHOENIX-5416: Fix Array2IT testArrayRefToLiteral
b8cc92a is described below

commit b8cc92a64073e8d9b255f070ed11d9f497406cbd
Author: Chinmay Kulkarni <ch...@gmail.com>
AuthorDate: Tue Aug 6 23:18:20 2019 -0700

    PHOENIX-5416: Fix Array2IT testArrayRefToLiteral
---
 .../java/org/apache/phoenix/end2end/Array2IT.java  | 51 +++++++++++++++++-----
 .../phoenix/expression/LiteralExpression.java      | 11 +++++
 2 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Array2IT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Array2IT.java
index 52bfb86..9386cde 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/Array2IT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/Array2IT.java
@@ -18,6 +18,7 @@
 package org.apache.phoenix.end2end;
 
 import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
@@ -40,6 +41,9 @@ import org.apache.phoenix.util.StringUtil;
 import org.junit.Test;
 
 public class Array2IT extends ArrayIT {
+
+    private static final String TEST_QUERY = "select ?[2] from \"SYSTEM\".\"CATALOG\" limit 1";
+
     @Test
     public void testFixedWidthCharArray() throws Exception {
         Connection conn;
@@ -670,26 +674,51 @@ public class Array2IT extends ArrayIT {
     }
 
     @Test
-    public void testArrayRefToLiteral() throws Exception {
-        Connection conn;
-
+    public void testArrayRefToLiteralCharArraySameLengths() throws Exception {
         Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
-        conn = DriverManager.getConnection(getUrl(), props);
-        try {
-            PreparedStatement stmt = conn.prepareStatement("select ?[2] from \"SYSTEM\".\"catalog\" limit 1");
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            PreparedStatement stmt = conn.prepareStatement(TEST_QUERY);
+            // Test with each element of the char array having same lengths
             Array array = conn.createArrayOf("CHAR", new String[] {"a","b","c"});
             stmt.setArray(1, array);
             ResultSet rs = stmt.executeQuery();
             assertTrue(rs.next());
             assertEquals("b", rs.getString(1));
             assertFalse(rs.next());
-        } catch (SQLException e) {
-        } finally {
-            if (conn != null) {
-                conn.close();
-            }
         }
+    }
+
+    @Test
+    public void testArrayRefToLiteralCharArrayDiffLengths() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            PreparedStatement stmt = conn.prepareStatement(TEST_QUERY);
+            // Test with each element of the char array having different lengths
+            Array array = conn.createArrayOf("CHAR", new String[] {"a","bb","ccc"});
+            stmt.setArray(1, array);
+            ResultSet rs = stmt.executeQuery();
+            assertTrue(rs.next());
+            assertEquals("bb", rs.getString(1));
+            assertFalse(rs.next());
+        }
+    }
 
+    @Test
+    public void testArrayRefToLiteralBinaryArray() throws Exception {
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            PreparedStatement stmt = conn.prepareStatement(TEST_QUERY);
+            // Test with each element of the binary array having different lengths
+            byte[][] bytes = {{0,0,1}, {0,0,2,0}, {0,0,0,3,4}};
+            Array array = conn.createArrayOf("BINARY", bytes);
+            stmt.setArray(1, array);
+            ResultSet rs = stmt.executeQuery();
+            assertTrue(rs.next());
+            // Note that all elements are padded to be of the same length
+            // as the longest element of the byte array
+            assertArrayEquals(new byte[] {0,0,2,0,0}, rs.getBytes(1));
+            assertFalse(rs.next());
+        }
     }
     
     @Test
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
index 110177a..de15164 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
@@ -338,6 +338,17 @@ public class LiteralExpression extends BaseTerminalExpression {
 
     @Override
     public Integer getMaxLength() {
+        // For literals representing arrays of CHAR or BINARY, the byte size is null and the max
+        // length of the expression is also null, so we must get the max length of the
+        // actual underlying array
+        if (maxLength == null && getDataType() != null && getDataType().isArrayType() &&
+                PDataType.arrayBaseType(getDataType()).getByteSize() == null) {
+            Object value = getValue();
+            if (value instanceof PhoenixArray) {
+                // Return the max length of the underlying PhoenixArray data
+                return ((PhoenixArray) value).getMaxLength();
+            }
+        }
         return maxLength;
     }