Skip to content

Commit

Permalink
PHOENIX-914 Native HBase timestamp support to optimize date range que…
Browse files Browse the repository at this point in the history
…ries in Phoenix
  • Loading branch information
samarthjain committed Oct 3, 2015
1 parent ff35d95 commit c2cb510
Show file tree
Hide file tree
Showing 52 changed files with 1,879 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.apache.phoenix.util.TestUtil.closeStatement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand Down Expand Up @@ -1998,5 +1999,74 @@ public void testClientCacheUpdatedOnChangingPhoenixTableProperties() throws Exce
conn.close();
}
}

@Test
public void testDeclaringColumnAsRowTimestamp() throws Exception {
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.createStatement().execute("CREATE TABLE T1 (PK1 DATE NOT NULL, PK2 VARCHAR NOT NULL, KV1 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1 ROW_TIMESTAMP, PK2)) ");
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable table = phxConn.getMetaDataCache().getTable(new PTableKey(phxConn.getTenantId(), "T1"));
// Assert that the column shows up as row time stamp in the cache.
assertTrue(table.getColumn("PK1").isRowTimestamp());
assertFalse(table.getColumn("PK2").isRowTimestamp());
assertIsRowTimestampSet("T1", "PK1");

conn.createStatement().execute("CREATE TABLE T6 (PK1 VARCHAR, PK2 DATE PRIMARY KEY ROW_TIMESTAMP, KV1 VARCHAR, KV2 INTEGER)");
table = phxConn.getMetaDataCache().getTable(new PTableKey(phxConn.getTenantId(), "T6"));
// Assert that the column shows up as row time stamp in the cache.
assertFalse(table.getColumn("PK1").isRowTimestamp());
assertTrue(table.getColumn("PK2").isRowTimestamp());
assertIsRowTimestampSet("T6", "PK2");

// Create an index on a table has a row time stamp pk column. The column should show up as a row time stamp column for the index too.
conn.createStatement().execute("CREATE INDEX T6_IDX ON T6 (KV1) include (KV2)");
PTable indexTable = phxConn.getMetaDataCache().getTable(new PTableKey(phxConn.getTenantId(), "T6_IDX"));
String indexColName = IndexUtil.getIndexColumnName(table.getColumn("PK2"));
// Assert that the column shows up as row time stamp in the cache.
assertTrue(indexTable.getColumn(indexColName).isRowTimestamp());
assertIsRowTimestampSet("T6_IDX", indexColName);

// Creating a view with a row_timestamp column in its pk constraint is not allowed
try {
conn.createStatement().execute("CREATE VIEW T6_VIEW (KV3 VARCHAR, KV4 DATE, KV5 INTEGER, CONSTRAINT PK PRIMARY KEY (KV3, KV4 ROW_TIMESTAMP) ) AS SELECT * FROM T6");
fail("Creating a view with a row_timestamp column in its pk constraint is not allowed");
} catch (SQLException e) {
assertEquals(SQLExceptionCode.ROWTIMESTAMP_NOT_ALLOWED_ON_VIEW.getErrorCode(), e.getErrorCode());
}

// Make sure that the base table column declared as row_timestamp is also row_timestamp for view
conn.createStatement().execute("CREATE VIEW T6_VIEW (KV3 VARCHAR, KV4 VARCHAR, KV5 INTEGER, CONSTRAINT PK PRIMARY KEY (KV3, KV4) ) AS SELECT * FROM T6");
PTable view = phxConn.getMetaDataCache().getTable(new PTableKey(phxConn.getTenantId(), "T6_VIEW"));
assertNotNull(view.getPKColumn("PK2"));
assertTrue(view.getPKColumn("PK2").isRowTimestamp());
}
}

private void assertIsRowTimestampSet(String tableName, String columnName) throws SQLException {
String sql = "SELECT IS_ROW_TIMESTAMP FROM SYSTEM.CATALOG WHERE TABLE_SCHEM IS NULL AND TABLE_NAME = ? AND COLUMN_FAMILY IS NULL AND COLUMN_NAME = ?";
try(Connection conn = DriverManager.getConnection(getUrl())) {
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, tableName);
stmt.setString(2, columnName);
ResultSet rs = stmt.executeQuery();
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
}
}

@Test
public void testAddingRowTimestampColumnNotAllowedViaAlterTable() throws Exception {
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.createStatement().execute("CREATE TABLE T1 (PK1 VARCHAR NOT NULL, PK2 VARCHAR NOT NULL, KV1 VARCHAR CONSTRAINT PK PRIMARY KEY(PK1, PK2)) ");
// adding a new pk column that is also row_timestamp is not allowed
try {
conn.createStatement().execute("ALTER TABLE T1 ADD PK3 DATE PRIMARY KEY ROW_TIMESTAMP");
fail("Altering table to add a PK column as row_timestamp column should fail");
} catch (SQLException e) {
assertEquals(SQLExceptionCode.ROWTIMESTAMP_CREATE_ONLY.getErrorCode(), e.getErrorCode());
}
}
}

}

63 changes: 63 additions & 0 deletions phoenix-core/src/it/java/org/apache/phoenix/end2end/DeleteIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,69 @@ private void testDeleteAllFromTable(boolean autoCommit) throws SQLException {
}
}
}

@Test
public void testDeleteForTableWithRowTimestampColServer() throws Exception {
testDeleteForTableWithRowTimestampCol(true);
}

@Test
public void testDeleteForTableWithRowTimestampColClient() throws Exception {
testDeleteForTableWithRowTimestampCol(false);
}

private void testDeleteForTableWithRowTimestampCol(boolean autoCommit) throws Exception {
String tableName = "testDeleteForTableWithRowTimestampCol".toUpperCase();
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.setAutoCommit(autoCommit);
Statement stm = conn.createStatement();
stm.execute("CREATE TABLE IF NOT EXISTS " + tableName +
" (HOST CHAR(2) NOT NULL," +
"STAT_DATE DATE NOT NULL, \n" +
"USAGE.CORE BIGINT," +
"USAGE.DB BIGINT," +
"CONSTRAINT PK PRIMARY KEY (HOST, STAT_DATE ROW_TIMESTAMP))");
stm.close();

PreparedStatement psInsert = conn
.prepareStatement("UPSERT INTO " + tableName + " (HOST, STAT_DATE, CORE, DB) VALUES(?,?,?,?)");
psInsert.setString(1, "AA");
psInsert.setDate(2, new Date(100));
psInsert.setLong(3, 1L);
psInsert.setLong(4, 2L);
psInsert.execute();
psInsert.close();
if (!autoCommit) {
conn.commit();
}
conn.createStatement().execute("DELETE FROM " + tableName);
if (!autoCommit) {
conn.commit();
}
ResultSet rs = conn.createStatement().executeQuery("SELECT count(*) FROM " + tableName);
assertTrue(rs.next());
assertEquals(0, rs.getLong(1));

// try with value for row_timestamp column generated by phoenix
psInsert = conn
.prepareStatement("UPSERT INTO " + tableName + " (HOST, CORE, DB) VALUES(?,?,?)");
psInsert.setString(1, "BB");
psInsert.setLong(2, 1L);
psInsert.setLong(3, 2L);
psInsert.execute();
psInsert.close();
if (!autoCommit) {
conn.commit();
}
conn.createStatement().execute("DELETE FROM " + tableName);
if (!autoCommit) {
conn.commit();
}
rs = conn.createStatement().executeQuery("SELECT count(*) FROM " + tableName);
assertTrue(rs.next());
assertEquals(0, rs.getLong(1));
}
}
}


Loading

0 comments on commit c2cb510

Please sign in to comment.