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

phoenix git commit: PHOENIX-2138 Non equality comparisons don't work for ARRAY type columns that are DESC in row key (Dumindu Buddhika)

Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-0.98 8720caae6 -> 10fe8f249


PHOENIX-2138 Non equality comparisons don't work for ARRAY type columns
that are DESC in row key (Dumindu Buddhika)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/10fe8f24
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/10fe8f24
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/10fe8f24

Branch: refs/heads/4.x-HBase-0.98
Commit: 10fe8f2494eb1087931253c06639c1dce69ba5ac
Parents: 8720caa
Author: ramkrishna <ra...@gmail.com>
Authored: Sun Jul 26 12:06:11 2015 +0530
Committer: ramkrishna <ra...@gmail.com>
Committed: Sun Jul 26 12:06:11 2015 +0530

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/ArrayIT.java     | 201 ++++++++++++++++++-
 .../apache/phoenix/iterate/ExplainTable.java    |   4 +-
 .../phoenix/schema/types/PArrayDataType.java    |   9 +-
 .../phoenix/schema/types/PhoenixArray.java      |   4 -
 4 files changed, 209 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/10fe8f24/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
index 89997f4..800a7b4 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ArrayIT.java
@@ -2038,5 +2038,204 @@ public class ArrayIT extends BaseClientManagedTimeIT {
         assertFalse(rs.next());
         conn.close();
     }
-    
+
+    @Test
+    public void testComparisonOperatorsForDesc1()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'c'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k >= array['a', 'b']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc2()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'c'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k >= array['a', 'c']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc3()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'c'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k > array['a', 'b']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc4()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'b'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k <= array['a', 'c']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc5()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'b'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k <= array['a', 'b']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc6()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k varchar array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array['a', 'b'])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k < array['a', 'c']");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc7()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k integer array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array[1, 2])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k < array[1, 4]");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
+
+    @Test
+    public void testComparisonOperatorsForDesc8()throws Exception{
+        long ts = nextTimestamp();
+        Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
+        Connection conn = DriverManager.getConnection(getUrl(), props);
+        String ddl = "create table a (k integer array primary key desc)";
+        conn.createStatement().execute(ddl);
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
+        conn = DriverManager.getConnection(getUrl(), props);
+        PreparedStatement stmt = conn.prepareStatement("upsert into a values (array[1, 2])");
+        stmt.execute();
+        conn.commit();
+        conn.close();
+
+        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
+        conn = DriverManager.getConnection(getUrl(), props);
+        ResultSet rs;
+        stmt = conn.prepareStatement("select * from a where k <= array[1, 2]");
+        rs = stmt.executeQuery();
+        assertTrue(rs.next());
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/10fe8f24/phoenix-core/src/main/java/org/apache/phoenix/iterate/ExplainTable.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/iterate/ExplainTable.java b/phoenix-core/src/main/java/org/apache/phoenix/iterate/ExplainTable.java
index 6560308..7b47543 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/iterate/ExplainTable.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/iterate/ExplainTable.java
@@ -172,7 +172,9 @@ public abstract class ExplainTable {
         SortOrder sortOrder = tableRef.getTable().getPKColumns().get(slotIndex).getSortOrder();
         if (sortOrder == SortOrder.DESC) {
             buf.append('~');
-            range = SortOrder.invert(range, 0, new byte[range.length], 0, range.length);
+            ImmutableBytesWritable ptr = new ImmutableBytesWritable(range);
+            type.coerceBytes(ptr, type, sortOrder, SortOrder.getDefault());
+            range = ptr.get();
         }
         Format formatter = context.getConnection().getFormatter(type);
         buf.append(type.toStringLiteral(range, formatter));

http://git-wip-us.apache.org/repos/asf/phoenix/blob/10fe8f24/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
index 6236184..28144fd 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
@@ -108,7 +108,8 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
             byteStream = new TrustedByteArrayOutputStream(size + capacity + Bytes.SIZEOF_INT + Bytes.SIZEOF_BYTE
                     + Bytes.SIZEOF_INT);
         } else {
-            int size = arr.getMaxLength() * noOfElements;
+            int elemLength = (arr.getMaxLength() == null ? baseType.getByteSize() : arr.getMaxLength());
+            int size = elemLength * noOfElements;
             // Here the int for noofelements, byte for the version
             byteStream = new TrustedByteArrayOutputStream(size);
         }
@@ -301,8 +302,10 @@ public abstract class PArrayDataType<T> extends PDataType<T> {
             }
             baseType = desiredBaseType;
         } else {
-            pArr = (PhoenixArray)value;
-            pArr = new PhoenixArray(pArr, desiredMaxLength);
+            pArr = (PhoenixArray) value;
+            if (!Objects.equal(maxLength, desiredMaxLength)) {
+                pArr = new PhoenixArray(pArr, desiredMaxLength);
+            }
         }
         ptr.set(toBytes(pArr, baseType, desiredSortOrder, expectedRowKeyOrderOptimizable));
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/10fe8f24/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
index 843c831..3dc1050 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PhoenixArray.java
@@ -111,8 +111,6 @@ public class PhoenixArray implements Array,SQLCloseable {
                         }
                     }
     		    }
-		    } else {
-		        maxLength = baseType.getByteSize();
 		    }
 		}
     this.array = convertObjectArrayToPrimitiveArray(elements);
@@ -126,8 +124,6 @@ public class PhoenixArray implements Array,SQLCloseable {
             if (baseType.getByteSize() == null) {
                 elements = coerceToNewLength(baseType, (Object[])pArr.array, desiredMaxLength);
                 maxLength = desiredMaxLength;
-            } else {
-                maxLength = baseType.getByteSize();
             }
         }
         this.array = convertObjectArrayToPrimitiveArray(elements);