Repository: phoenix Updated Branches: refs/heads/4.0 6a6a9c0f2 -> 85d91bde7
PHOENIX-1131 PhoenixRuntime.encodePk needs to pad row key values to max column length (Samarth Jain) Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/85d91bde Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/85d91bde Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/85d91bde Branch: refs/heads/4.0 Commit: 85d91bde7f2ffe39774e484ab41f64513789077c Parents: 6a6a9c0 Author: James Taylor <jtay...@salesforce.com> Authored: Tue Jul 29 22:35:44 2014 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Tue Jul 29 22:36:38 2014 -0700 ---------------------------------------------------------------------- .../phoenix/end2end/PhoenixEncodeDecodeIT.java | 28 ++++++++++++++++++++ .../org/apache/phoenix/util/PhoenixRuntime.java | 5 +++- 2 files changed, 32 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/85d91bde/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 index fc01730..bdb0745 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/PhoenixEncodeDecodeIT.java @@ -177,6 +177,34 @@ public class PhoenixEncodeDecodeIT extends BaseHBaseManagedTimeIT { assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues)); } + + @Test + public void testEncodeDecodePaddingPks() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + conn.createStatement().execute( + "CREATE TABLE T(pk1 CHAR(15) not null, pk2 CHAR(15) not null, v1 DATE " + + "CONSTRAINT pk PRIMARY KEY (pk1, pk2))"); + + PreparedStatement stmt = conn.prepareStatement("UPSERT INTO T (pk1, pk2, v1) VALUES (?, ?, ?)"); + stmt.setString(1, "def"); + stmt.setString(2, "eid"); + stmt.setDate(3, new Date(100)); + stmt.executeUpdate(); + conn.commit(); + + stmt = conn.prepareStatement("SELECT pk1, pk2 FROM T"); + + Object[] retrievedValues = new Object[2]; + ResultSet rs = stmt.executeQuery(); + rs.next(); + retrievedValues[0] = rs.getString(1); + retrievedValues[1] = rs.getString(2); + + byte[] value = PhoenixRuntime.encodePK(conn, "T", retrievedValues); + Object[] decodedValues = PhoenixRuntime.decodePK(conn, "T", value); + + assertEquals(Arrays.asList(decodedValues), Arrays.asList(retrievedValues)); + } private static Connection getTenantSpecificConnection() throws Exception { Properties props = new Properties(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/85d91bde/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 b73185f..0f5e863 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 @@ -417,7 +417,10 @@ public class PhoenixRuntime { output.write(QueryConstants.SEPARATOR_BYTE); } type = pkColumns.get(i).getDataType(); - byte[] value = type.toBytes(values[i - offset]); + + //for fixed width data types like CHAR and BINARY, we need to pad values to be of max length. + Object paddedObj = type.pad(values[i - offset], pkColumns.get(i).getMaxLength()); + byte[] value = type.toBytes(paddedObj); output.write(value); } return output.toByteArray();