You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by an...@apache.org on 2014/07/10 18:35:25 UTC

git commit: PHOENIX-1080 Fix PhoenixRuntime.decodepk for salted tables. Add integration tests. (Samarth)

Repository: phoenix
Updated Branches:
  refs/heads/master 1e6157855 -> 97423a2b9


PHOENIX-1080 Fix PhoenixRuntime.decodepk for salted tables. Add integration tests. (Samarth)


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

Branch: refs/heads/master
Commit: 97423a2b925864e5c5ad05a15a9342a75b60840b
Parents: 1e61578
Author: anoopsjohn <an...@gmail.com>
Authored: Thu Jul 10 22:04:37 2014 +0530
Committer: anoopsjohn <an...@gmail.com>
Committed: Thu Jul 10 22:04:37 2014 +0530

----------------------------------------------------------------------
 .../end2end/ConvertTimezoneFunctionIT.java      |   7 +-
 .../phoenix/end2end/PhoenixEncodeDecodeIT.java  | 187 +++++++++++++++++++
 .../org/apache/phoenix/util/PhoenixRuntime.java |  12 +-
 .../apache/phoenix/util/PhoenixRuntimeTest.java |  22 +--
 4 files changed, 201 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/97423a2b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
index 4cdf6ac..d39e37a 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ConvertTimezoneFunctionIT.java
@@ -16,18 +16,21 @@
 package org.apache.phoenix.end2end;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
+
 import org.apache.phoenix.schema.IllegalDataException;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 import org.junit.Test;
+import org.junit.experimental.categories.Category;
 
 /**
  * CONVERT_TZ(date, 'from_timezone', 'to_timezone') tests
  *
  */
+@Category(HBaseManagedTimeTest.class)
 public class ConvertTimezoneFunctionIT extends BaseHBaseManagedTimeIT {
 
     @Test

http://git-wip-us.apache.org/repos/asf/phoenix/blob/97423a2b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java
new file mode 100644
index 0000000..fc01730
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ *distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you maynot use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicablelaw or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.phoenix.end2end;
+
+import static org.junit.Assert.assertEquals;
+
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.Arrays;
+import java.util.Properties;
+
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(HBaseManagedTimeTest.class)
+public class PhoenixEncodeDecodeIT extends BaseHBaseManagedTimeIT {
+    
+    private static String tenantId = "ABC";
+    
+    @Test
+    public void testEncodeDecode() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute(
+                "CREATE TABLE t(org_id CHAR(3) not null, p_id CHAR(3) not null, date DATE not null, e_id CHAR(3) not null, old_value VARCHAR, new_value VARCHAR " +
+                "CONSTRAINT pk PRIMARY KEY (org_id, p_id, date, e_id))");
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO t VALUES (?, ?, ?, ?, ?)");
+        Date date = new Date(System.currentTimeMillis());
+        stmt.setString(1,  "abc");
+        stmt.setString(2,  "def");
+        stmt.setDate(3,  date);
+        stmt.setString(4,  "eid");
+        stmt.setString(5, "old");
+        stmt.executeUpdate();
+        conn.commit();
+
+        stmt = conn.prepareStatement("SELECT org_id, p_id, date, e_id FROM T");
+
+        Object[] retrievedValues = new Object[4];
+        ResultSet rs = stmt.executeQuery();
+        rs.next();
+        retrievedValues[0] = rs.getString(1);
+        retrievedValues[1] = rs.getString(2);
+        retrievedValues[2] = rs.getDate(3);
+        retrievedValues[3] = rs.getString(4);
+
+        byte[] value = PhoenixRuntime.encodePK(conn, "T", retrievedValues);
+        Object[] decodedValues = PhoenixRuntime.decodePK(conn, "T", value);
+
+        assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues));
+    }
+
+    @Test
+    public void testEncodeDecodeSalted() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute(
+                "CREATE TABLE t(org_id CHAR(3) not null, p_id CHAR(3) not null, date DATE not null, e_id CHAR(3) not null, old_value VARCHAR, new_value VARCHAR " +
+                "CONSTRAINT pk PRIMARY KEY (org_id, p_id, date, e_id)) SALT_BUCKETS = 2");
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO t VALUES (?, ?, ?, ?, ?)");
+        Date date = new Date(System.currentTimeMillis());
+        stmt.setString(1,  "abc");
+        stmt.setString(2,  "def");
+        stmt.setDate(3,  date);
+        stmt.setString(4,  "eid");
+        stmt.setString(5, "old");
+        stmt.executeUpdate();
+        conn.commit();
+
+        stmt = conn.prepareStatement("SELECT org_id, p_id, date, e_id FROM T");
+
+        Object[] retrievedValues = new Object[4];
+        ResultSet rs = stmt.executeQuery();
+        rs.next();
+        retrievedValues[0] = rs.getString(1);
+        retrievedValues[1] = rs.getString(2);
+        retrievedValues[2] = rs.getDate(3);
+        retrievedValues[3] = rs.getString(4);
+
+        byte[] value = PhoenixRuntime.encodePK(conn, "T", retrievedValues);
+        Object[] decodedValues = PhoenixRuntime.decodePK(conn, "T", value);
+
+        assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues));
+    }
+
+    @Test
+    public void testEncodeDecodeMultiTenant() throws Exception {
+        Connection globalConn = DriverManager.getConnection(getUrl());
+        try {
+            globalConn.createStatement().execute(
+                    "CREATE TABLE T(tenant_id CHAR(3) not null, p_id CHAR(3) not null, date DATE not null, e_id CHAR(3) not null, old_value VARCHAR, new_value VARCHAR " +
+                    "CONSTRAINT pk PRIMARY KEY (tenant_id, p_id, date, e_id)) MULTI_TENANT = true");
+        } finally {
+            globalConn.close();
+        }
+
+        Connection tenantConn = getTenantSpecificConnection();
+
+        //create tenant-specific view. 
+        tenantConn.createStatement().execute("CREATE VIEW TENANT_TABLE AS SELECT * FROM T");
+        
+        PreparedStatement stmt = tenantConn.prepareStatement("UPSERT INTO TENANT_TABLE (p_id, date, e_id) VALUES (?, ?, ?)");
+        Date date = new Date(System.currentTimeMillis());
+        stmt.setString(1,  "def");
+        stmt.setDate(2,  date);
+        stmt.setString(3,  "eid");
+        stmt.executeUpdate();
+        tenantConn.commit();
+
+        stmt = tenantConn.prepareStatement("SELECT p_id, date, e_id FROM TENANT_TABLE");
+
+        Object[] retrievedValues = new Object[3];
+        ResultSet rs = stmt.executeQuery();
+        rs.next();
+        retrievedValues[0] = rs.getString(1);
+        retrievedValues[1] = rs.getDate(2);
+        retrievedValues[2] = rs.getString(3);
+
+        byte[] value = PhoenixRuntime.encodePK(tenantConn, "TENANT_TABLE", retrievedValues);
+        Object[] decodedValues = PhoenixRuntime.decodePK(tenantConn, "TENANT_TABLE", value);
+
+        assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues));
+    }
+
+    @Test
+    public void testEncodeDecodeSaltedMultiTenant() throws Exception {
+        Connection globalConn = DriverManager.getConnection(getUrl());
+        try {
+            globalConn.createStatement().execute(
+                    "CREATE TABLE T(tenant_id CHAR(3) not null, p_id CHAR(3) not null, date DATE not null, e_id CHAR(3) not null, old_value VARCHAR, new_value VARCHAR " +
+                    "CONSTRAINT pk PRIMARY KEY (tenant_id, p_id, date, e_id)) MULTI_TENANT = true, SALT_BUCKETS = 2");
+        } finally {
+            globalConn.close();
+        }
+
+        Connection tenantConn = getTenantSpecificConnection();
+
+        //create tenant-specific view. 
+        tenantConn.createStatement().execute("CREATE VIEW TENANT_TABLE AS SELECT * FROM T");
+        
+        PreparedStatement stmt = tenantConn.prepareStatement("UPSERT INTO TENANT_TABLE (p_id, date, e_id) VALUES (?, ?, ?)");
+        Date date = new Date(System.currentTimeMillis());
+        stmt.setString(1,  "def");
+        stmt.setDate(2,  date);
+        stmt.setString(3,  "eid");
+        stmt.executeUpdate();
+        tenantConn.commit();
+
+        stmt = tenantConn.prepareStatement("SELECT p_id, date, e_id FROM TENANT_TABLE");
+
+        Object[] retrievedValues = new Object[3];
+        ResultSet rs = stmt.executeQuery();
+        rs.next();
+        retrievedValues[0] = rs.getString(1);
+        retrievedValues[1] = rs.getDate(2);
+        retrievedValues[2] = rs.getString(3);
+
+        byte[] value = PhoenixRuntime.encodePK(tenantConn, "TENANT_TABLE", retrievedValues);
+        Object[] decodedValues = PhoenixRuntime.decodePK(tenantConn, "TENANT_TABLE", value);
+
+        assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues));
+    }
+
+    private static Connection getTenantSpecificConnection() throws Exception {
+        Properties props = new Properties();
+        props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
+        return DriverManager.getConnection(getUrl(), props);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/97423a2b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
index dbb9634..a52cd95 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/PhoenixRuntime.java
@@ -435,7 +435,7 @@ public class PhoenixRuntime {
      * been encoded and will not appear in the returned values.
      * @param conn an open connection
      * @param name the full table name
-     * @param value the value that was encoded with {@link #encodePK(Connection, String, Object[])}
+     * @param encodedValue the value that was encoded with {@link #encodePK(Connection, String, Object[])}
      * @return the Object values encoded in the byte array value
      * @throws SQLException
      */
@@ -443,15 +443,17 @@ public class PhoenixRuntime {
         PTable table = getTable(conn, name);
         PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
         int offset = (table.getBucketNum() == null ? 0 : 1) + (table.isMultiTenant() && pconn.getTenantId() != null ? 1 : 0);
+        int nValues = table.getPKColumns().size() - offset;
         RowKeySchema schema = table.getRowKeySchema();
-        int nValues = schema.getMaxFields()-offset;
         Object[] values = new Object[nValues];
         ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+        schema.iterator(value, ptr);
         int i = 0;
-        schema.iterator(value, ptr, offset);
-        while (i < nValues && schema.next(ptr, i, value.length) != null) {
-            values[i] = schema.getField(i).getDataType().toObject(ptr);
+        int fieldIdx = offset;
+        while (i < nValues && schema.next(ptr, fieldIdx, value.length) != null) {
+            values[i] = schema.getField(fieldIdx).getDataType().toObject(ptr);
             i++;
+            fieldIdx++;
         }
         return values;
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/97423a2b/phoenix-core/src/test/java/org/apache/phoenix/util/PhoenixRuntimeTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/PhoenixRuntimeTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/PhoenixRuntimeTest.java
index a386093..9a277ad 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/util/PhoenixRuntimeTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/PhoenixRuntimeTest.java
@@ -23,30 +23,12 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.File;
-import java.sql.Connection;
-import java.sql.Date;
-import java.sql.DriverManager;
-import java.util.Arrays;
-
-import com.google.common.collect.ImmutableList;
 import org.apache.phoenix.query.BaseConnectionlessQueryTest;
 import org.junit.Test;
 
-public class PhoenixRuntimeTest extends BaseConnectionlessQueryTest {
-    @Test
-    public void testEncodeDecode() throws Exception {
-        Connection conn = DriverManager.getConnection(getUrl());
-        conn.createStatement().execute(
-                "CREATE TABLE t(org_id CHAR(3) not null, p_id CHAR(3) not null, date DATE not null, e_id CHAR(3) not null, old_value VARCHAR, new_value VARCHAR " +
-                "CONSTRAINT pk PRIMARY KEY (org_id, p_id, date, e_id))");
-        Date date = new Date(System.currentTimeMillis());
-        Object[] expectedValues = new Object[] {"abc", "def", date, "123"};
-        byte[] value = PhoenixRuntime.encodePK(conn, "T", expectedValues);
-        Object[] actualValues = PhoenixRuntime.decodePK(conn, "T", value);
-        assertEquals(Arrays.asList(expectedValues), Arrays.asList(actualValues));
-    }
+import com.google.common.collect.ImmutableList;
 
+public class PhoenixRuntimeTest extends BaseConnectionlessQueryTest {
     @Test
     public void testParseArguments_MinimalCase() {
         PhoenixRuntime.ExecutionCommand execCmd = PhoenixRuntime.ExecutionCommand.parseArgs(