PHOENIX-3991 ROW_TIMESTAMP on TIMESTAMP column type throws ArrayOutOfBound when upserting without providing a value.
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/2a0b56a4 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/2a0b56a4 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/2a0b56a4 Branch: refs/heads/4.x-cdh5.14 Commit: 2a0b56a4667c6476e14c8b419d8d49011afb2612 Parents: 09ceb39 Author: Sergey Soldatov <s...@apache.org> Authored: Wed Jul 25 20:48:03 2018 +0100 Committer: Pedro Boado <pbo...@apache.org> Committed: Sun Jul 29 10:06:57 2018 +0100 ---------------------------------------------------------------------- .../apache/phoenix/end2end/RowTimestampIT.java | 26 +++++++++++++++++--- .../apache/phoenix/execute/MutationState.java | 6 ++++- 2 files changed, 27 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/2a0b56a4/phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampIT.java index 09fd821..2cf4d9d 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/RowTimestampIT.java @@ -73,13 +73,22 @@ public class RowTimestampIT extends ParallelStatsDisabledIT { } @Test - public void testWithUpsertingRowTimestampColSpecified() throws Exception { + public void testUpsertingRowTimestampColSpecifiedWithTimestamp() throws Exception { + upsertingRowTimestampColSpecified("TIMESTAMP"); + } + + @Test + public void testUpsertingRowTimestampColSpecifiedWithDate() throws Exception { + upsertingRowTimestampColSpecified("DATE"); + } + + private void upsertingRowTimestampColSpecified(String type) throws Exception { String tableName = generateUniqueName(); String indexName = generateUniqueName(); try (Connection conn = DriverManager.getConnection(getUrl())) { conn.createStatement() .execute("CREATE TABLE IF NOT EXISTS " + tableName - + " (PK1 VARCHAR NOT NULL, PK2 DATE NOT NULL, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2 " + + " (PK1 VARCHAR NOT NULL, PK2 " + type + " NOT NULL, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2 " + sortOrder + " ROW_TIMESTAMP)) " + tableDDLOptions); } try (Connection conn = DriverManager.getConnection(getUrl())) { @@ -195,14 +204,23 @@ public class RowTimestampIT extends ParallelStatsDisabledIT { } @Test - public void testAutomaticallySettingRowTimestampForImmutableTableAndIndexes() throws Exception { + public void testAutomaticallySettingRowTimestampWithTimestamp () throws Exception { + automaticallySettingRowTimestampForImmutableTableAndIndexes("TIMESTAMP"); + } + + @Test + public void testAutomaticallySettingRowTimestampWithDate () throws Exception { + automaticallySettingRowTimestampForImmutableTableAndIndexes("DATE"); + } + + private void automaticallySettingRowTimestampForImmutableTableAndIndexes(String type) throws Exception { long startTime = EnvironmentEdgeManager.currentTimeMillis(); String tableName = generateUniqueName(); String indexName = generateUniqueName(); try (Connection conn = DriverManager.getConnection(getUrl())) { conn.createStatement() .execute("CREATE TABLE IF NOT EXISTS " + tableName - + " (PK1 VARCHAR NOT NULL, PK2 DATE NOT NULL, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2 " + + " (PK1 VARCHAR NOT NULL, PK2 " + type + " NOT NULL, KV1 VARCHAR, KV2 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2 " + sortOrder + " ROW_TIMESTAMP)) " + tableDDLOptions); } try (Connection conn = DriverManager.getConnection(getUrl())) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/2a0b56a4/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java b/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java index c29d6b5..d2d1eea 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/execute/MutationState.java @@ -25,6 +25,7 @@ import static org.apache.phoenix.monitoring.GlobalClientMetrics.GLOBAL_MUTATION_ import java.io.IOException; import java.sql.SQLException; +import java.sql.Timestamp; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -85,6 +86,7 @@ import org.apache.phoenix.schema.TableNotFoundException; import org.apache.phoenix.schema.TableRef; import org.apache.phoenix.schema.ValueSchema.Field; import org.apache.phoenix.schema.types.PLong; +import org.apache.phoenix.schema.types.PTimestamp; import org.apache.phoenix.trace.util.Tracing; import org.apache.phoenix.transaction.PhoenixTransactionContext; import org.apache.phoenix.transaction.PhoenixTransactionContext.PhoenixVisibilityLevel; @@ -474,7 +476,9 @@ public class MutationState implements SQLCloseable { RowKeySchema schema = table.getRowKeySchema(); int rowTimestampColPos = table.getRowTimestampColPos(); Field rowTimestampField = schema.getField(rowTimestampColPos); - byte[] rowTimestampBytes = PLong.INSTANCE.toBytes(rowTimestamp, rowTimestampField.getSortOrder()); + byte[] rowTimestampBytes = rowTimestampField.getDataType() == PTimestamp.INSTANCE ? + PTimestamp.INSTANCE.toBytes(new Timestamp(rowTimestamp), rowTimestampField.getSortOrder()) : + PLong.INSTANCE.toBytes(rowTimestamp, rowTimestampField.getSortOrder()); int oldOffset = ptr.getOffset(); int oldLength = ptr.getLength(); // Move the pointer to the start byte of the row timestamp pk