Repository: phoenix Updated Branches: refs/heads/4.x-HBase-1.1 891131c78 -> 5fc6fa184
PHOENIX-2901 If namespaces are enabled, check for existence of schema when sequence created Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/a50bee21 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/a50bee21 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/a50bee21 Branch: refs/heads/4.x-HBase-1.1 Commit: a50bee213269e249c8f4bbae0242aa5a3a3b8341 Parents: 891131c Author: Ankit Singhal <ankitsingha...@gmail.com> Authored: Fri Jun 10 16:09:13 2016 +0530 Committer: Ankit Singhal <ankitsingha...@gmail.com> Committed: Fri Jun 10 16:09:13 2016 +0530 ---------------------------------------------------------------------- .../org/apache/phoenix/end2end/SequenceIT.java | 58 ++++- .../end2end/TenantSpecificViewIndexIT.java | 211 +++++++++++++------ .../org/apache/phoenix/end2end/UpgradeIT.java | 41 +++- .../phoenix/end2end/index/ViewIndexIT.java | 22 +- .../apache/phoenix/compile/FromCompiler.java | 4 + .../phoenix/query/ConnectionQueryServices.java | 1 - .../query/ConnectionQueryServicesImpl.java | 26 ++- .../apache/phoenix/schema/MetaDataClient.java | 21 +- .../org/apache/phoenix/util/MetaDataUtil.java | 36 ++-- .../org/apache/phoenix/util/PhoenixRuntime.java | 6 +- .../org/apache/phoenix/util/UpgradeUtil.java | 55 ++++- 11 files changed, 367 insertions(+), 114 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java index 3e7ec31..3ed4fd7 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/SequenceIT.java @@ -37,6 +37,7 @@ import java.util.Properties; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.jdbc.PhoenixStatement; import org.apache.phoenix.query.QueryServices; +import org.apache.phoenix.schema.SchemaNotFoundException; import org.apache.phoenix.schema.SequenceAlreadyExistsException; import org.apache.phoenix.schema.SequenceNotFoundException; import org.apache.phoenix.util.PhoenixRuntime; @@ -110,6 +111,49 @@ public class SequenceIT extends BaseClientManagedTimeIT { } } + @Test + public void testCreateSequenceWhenNamespaceEnabled() throws Exception { + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true)); + String sequenceSchemaName = "ALPHA"; + String sequenceName = sequenceSchemaName + ".M_OMEGA"; + + nextConnection(props); + try { + conn.createStatement().execute("CREATE SEQUENCE " + sequenceName + " START WITH 2 INCREMENT BY 4"); + fail(); + } catch (SchemaNotFoundException e) { + // expected + } + + conn.createStatement().execute("CREATE SCHEMA " + sequenceSchemaName); + nextConnection(props); + conn.createStatement().execute("CREATE SEQUENCE " + sequenceName + " START WITH 2 INCREMENT BY 4"); + sequenceSchemaName = "TEST_SEQ_SCHEMA"; + sequenceName = "M_SEQ"; + conn.createStatement().execute("CREATE SCHEMA " + sequenceSchemaName); + nextConnection(props); + conn.createStatement().execute("USE " + sequenceSchemaName); + conn.createStatement().execute("CREATE SEQUENCE " + sequenceName + " START WITH 2 INCREMENT BY 4"); + nextConnection(props); + String query = "SELECT sequence_schema, sequence_name, current_value, increment_by FROM SYSTEM.\"SEQUENCE\" WHERE sequence_name='" + + sequenceName + "'"; + ResultSet rs = conn.prepareStatement(query).executeQuery(); + assertTrue(rs.next()); + assertEquals(sequenceSchemaName, rs.getString("sequence_schema")); + assertEquals(sequenceName, rs.getString("sequence_name")); + assertEquals(2, rs.getInt("current_value")); + assertEquals(4, rs.getInt("increment_by")); + assertFalse(rs.next()); + try { + conn.createStatement().execute( + "CREATE SEQUENCE " + sequenceSchemaName + "." + sequenceName + " START WITH 2 INCREMENT BY 4"); + fail(); + } catch (SequenceAlreadyExistsException e) { + + } + } + @Test public void testCreateSequence() throws Exception { nextConnection(); @@ -696,15 +740,19 @@ public class SequenceIT extends BaseClientManagedTimeIT { assertTrue(rs.next()); assertEquals(4, rs.getInt(1)); } - - // if nextConnection() is not used to get to get a connection, make sure you call .close() so that connections are not leaked - private void nextConnection() throws Exception { + + private void nextConnection(Properties props) throws Exception { if (conn != null) conn.close(); long ts = nextTimestamp(); - Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts)); conn = DriverManager.getConnection(getUrl(), props); - } + } + + // if nextConnection() is not used to get to get a connection, make sure you call .close() so that connections are + // not leaked + private void nextConnection() throws Exception { + nextConnection(PropertiesUtil.deepCopy(TEST_PROPERTIES)); + } @Test public void testSequenceDefault() throws Exception { http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificViewIndexIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificViewIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificViewIndexIT.java index fc9489d..69d9140 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificViewIndexIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/TenantSpecificViewIndexIT.java @@ -26,11 +26,19 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Properties; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.jdbc.PhoenixConnection; +import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; +import org.apache.phoenix.query.QueryServices; import org.apache.phoenix.schema.ColumnNotFoundException; +import org.apache.phoenix.schema.PTableType; +import org.apache.phoenix.util.MetaDataUtil; import org.apache.phoenix.util.PhoenixRuntime; import org.apache.phoenix.util.QueryUtil; +import org.apache.phoenix.util.SchemaUtil; import org.junit.Test; @@ -63,98 +71,179 @@ public class TenantSpecificViewIndexIT extends BaseTenantSpecificViewIndexIT { @Test public void testMultiCFViewIndex() throws Exception { - testMultiCFViewIndex(false); + testMultiCFViewIndex(false, false); + } + + @Test + public void testMultiCFViewIndexWithNamespaceMapping() throws Exception { + testMultiCFViewIndex(false, true); } @Test public void testMultiCFViewLocalIndex() throws Exception { - testMultiCFViewIndex(true); + testMultiCFViewIndex(true, false); } - - private void testMultiCFViewIndex(boolean localIndex) throws Exception { - Connection conn = DriverManager.getConnection(getUrl()); - String ddl = "CREATE TABLE MT_BASE (PK1 VARCHAR not null, PK2 VARCHAR not null, " - + "MYCF1.COL1 varchar,MYCF2.COL2 varchar " - + "CONSTRAINT pk PRIMARY KEY(PK1,PK2)) MULTI_TENANT=true"; + + private void createTableAndValidate(String tableName, boolean isNamespaceEnabled) throws Exception { + Properties props = new Properties(); + if (isNamespaceEnabled) { + props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.toString(true)); + } + Connection conn = DriverManager.getConnection(getUrl(), props); + if (isNamespaceEnabled) { + conn.createStatement().execute("CREATE SCHEMA " + SchemaUtil.getSchemaNameFromFullName(tableName)); + } + String ddl = "CREATE TABLE " + tableName + " (PK1 VARCHAR not null, PK2 VARCHAR not null, " + + "MYCF1.COL1 varchar,MYCF2.COL2 varchar " + "CONSTRAINT pk PRIMARY KEY(PK1,PK2)) MULTI_TENANT=true"; conn.createStatement().execute(ddl); - conn.createStatement().execute("UPSERT INTO MT_BASE values ('a','b','c','d')"); + + conn.createStatement().execute("UPSERT INTO " + tableName + " values ('a','b','c','d')"); conn.commit(); - - ResultSet rs = conn.createStatement().executeQuery("select * from mt_base where (pk1,pk2) IN (('a','b'),('b','b'))"); + + ResultSet rs = conn.createStatement() + .executeQuery("select * from " + tableName + " where (pk1,pk2) IN (('a','b'),('b','b'))"); assertTrue(rs.next()); - assertEquals("a",rs.getString(1)); - assertEquals("b",rs.getString(2)); + assertEquals("a", rs.getString(1)); + assertEquals("b", rs.getString(2)); assertFalse(rs.next()); - conn.close(); - String tenantId = "a"; + } + + private void testMultiCFViewIndex(boolean localIndex, boolean isNamespaceEnabled) throws Exception { + String tableName = "A.MT_BASE"; + String baseViewName = "acme"; + createTableAndValidate(tableName, isNamespaceEnabled); + createViewAndIndexesWithTenantId(tableName, baseViewName, localIndex, "b", isNamespaceEnabled); + createViewAndIndexesWithTenantId(tableName, baseViewName, localIndex, "a", isNamespaceEnabled); + validateSequence(tableName, isNamespaceEnabled, "-32767,-32767"); Properties props = new Properties(); - props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId); - conn = DriverManager.getConnection(getUrl(),props); - conn.createStatement().execute("CREATE VIEW acme AS SELECT * FROM MT_BASE"); - rs = conn.createStatement().executeQuery("select * from acme"); - assertTrue(rs.next()); - assertEquals("b",rs.getString(1)); - assertEquals("c",rs.getString(2)); - assertEquals("d",rs.getString(3)); + props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "a"); + try (Connection conn = DriverManager.getConnection(getUrl(), props)) { + conn.createStatement().execute("DROP VIEW " + baseViewName + "_a"); + } + props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "b"); + try (Connection conn = DriverManager.getConnection(getUrl(), props)) { + conn.createStatement().execute("DROP VIEW " + baseViewName + "_b"); + } + DriverManager.getConnection(getUrl()).createStatement().execute("DROP TABLE " + tableName + " CASCADE"); + validateSequence(tableName, isNamespaceEnabled, null); + } + + private void validateSequence(String tableName, boolean isNamespaceEnabled, String expectedResult) + throws SQLException { + PhoenixConnection phxConn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class); + ResultSet rs = phxConn.createStatement().executeQuery("SELECT " + PhoenixDatabaseMetaData.CURRENT_VALUE + + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE); + if (expectedResult != null) { + String[] splits = expectedResult.split(","); + for (String seq : splits) { + assertTrue(rs.next()); + assertEquals(seq, rs.getString(1)); + } + } else { + assertFalse(rs.next()); + } + phxConn.close(); + } + + private void createViewAndIndexesWithTenantId(String tableName,String baseViewName, boolean localIndex, String tenantId, + boolean isNamespaceMapped) throws Exception { + Properties props = new Properties(); + String viewName = baseViewName + "_" + tenantId; + String indexName = "idx_" + viewName; + if (tenantId != null) { + props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId); + } + Connection conn = DriverManager.getConnection(getUrl(), props); + conn.createStatement().execute("CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName); + ResultSet rs = conn.createStatement().executeQuery("select * from " + viewName); + + int i = 1; + if ("a".equals(tenantId)) { + assertTrue(rs.next()); + assertEquals("b", rs.getString(i++)); + assertEquals("c", rs.getString(i++)); + assertEquals("d", rs.getString(i++)); + } assertFalse(rs.next()); - conn.createStatement().execute("UPSERT INTO acme VALUES ('e','f','g')"); + conn.createStatement().execute("UPSERT INTO " + viewName + " VALUES ('e','f','g')"); conn.commit(); - if(localIndex){ - conn.createStatement().execute("create local index idx_acme on acme (COL1)"); + if (localIndex) { + conn.createStatement().execute("create local index " + indexName + " on " + viewName + " (COL1)"); } else { - conn.createStatement().execute("create index idx_acme on acme (COL1)"); + conn.createStatement().execute("create index " + indexName + " on " + viewName + " (COL1)"); + } + rs = conn.createStatement().executeQuery("select * from " + viewName); + i = 1; + if ("a".equals(tenantId)) { + assertTrue(rs.next()); + assertEquals("b", rs.getString(i++)); + assertEquals("c", rs.getString(i++)); + assertEquals("d", rs.getString(i++)); } - rs = conn.createStatement().executeQuery("select * from acme"); - assertTrue(rs.next()); - assertEquals("b",rs.getString(1)); - assertEquals("c",rs.getString(2)); - assertEquals("d",rs.getString(3)); assertTrue(rs.next()); - assertEquals("e",rs.getString(1)); - assertEquals("f",rs.getString(2)); - assertEquals("g",rs.getString(3)); + assertEquals("e", rs.getString(1)); + assertEquals("f", rs.getString(2)); + assertEquals("g", rs.getString(3)); assertFalse(rs.next()); - rs = conn.createStatement().executeQuery("explain select * from acme"); - assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER MT_BASE ['a']",QueryUtil.getExplainPlan(rs)); + rs = conn.createStatement().executeQuery("explain select * from " + viewName); + assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + + SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, PTableType.TABLE) + " ['" + + tenantId + "']", QueryUtil.getExplainPlan(rs)); - rs = conn.createStatement().executeQuery("select pk2,col1 from acme where col1='f'"); + rs = conn.createStatement().executeQuery("select pk2,col1 from " + viewName + " where col1='f'"); assertTrue(rs.next()); - assertEquals("e",rs.getString(1)); - assertEquals("f",rs.getString(2)); + assertEquals("e", rs.getString(1)); + assertEquals("f", rs.getString(2)); assertFalse(rs.next()); - rs = conn.createStatement().executeQuery("explain select pk2,col1 from acme where col1='f'"); - if(localIndex){ - assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER _LOCAL_IDX_MT_BASE ['a',-32768,'f']\n" - + " SERVER FILTER BY FIRST KEY ONLY\n" - + "CLIENT MERGE SORT",QueryUtil.getExplainPlan(rs)); + rs = conn.createStatement().executeQuery("explain select pk2,col1 from " + viewName + " where col1='f'"); + if (localIndex) { + assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + + SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, PTableType.TABLE) + " ['" + + tenantId + "',1,'f']\n" + " SERVER FILTER BY FIRST KEY ONLY\n" + "CLIENT MERGE SORT", + QueryUtil.getExplainPlan(rs)); } else { - assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER _IDX_MT_BASE ['a',-32768,'f']\n" - + " SERVER FILTER BY FIRST KEY ONLY",QueryUtil.getExplainPlan(rs)); + assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(SchemaUtil + .getPhysicalHBaseTableName(tableName, isNamespaceMapped, PTableType.TABLE).getBytes())) + + " ['" + tenantId + "',-32768,'f']\n" + " SERVER FILTER BY FIRST KEY ONLY", + QueryUtil.getExplainPlan(rs)); } - + try { // Cannot reference tenant_id column in tenant specific connection - conn.createStatement().executeQuery("select * from mt_base where (pk1,pk2) IN (('a','b'),('b','b'))"); - fail(); + conn.createStatement() + .executeQuery("select * from " + tableName + " where (pk1,pk2) IN (('a','b'),('b','b'))"); + if (tenantId != null) { + fail(); + } } catch (ColumnNotFoundException e) { + if (tenantId == null) { + fail(); + } } - + // This is ok, though - rs = conn.createStatement().executeQuery("select * from mt_base where pk2 IN ('b','e')"); - assertTrue(rs.next()); - assertEquals("b",rs.getString(1)); + rs = conn.createStatement().executeQuery("select * from " + tableName + " where pk2 IN ('b','e')"); + if ("a".equals(tenantId)) { + assertTrue(rs.next()); + assertEquals("b", rs.getString(1)); + } assertTrue(rs.next()); - assertEquals("e",rs.getString(1)); + assertEquals("e", rs.getString(1)); assertFalse(rs.next()); - - rs = conn.createStatement().executeQuery("select * from acme where pk2 IN ('b','e')"); - assertTrue(rs.next()); - assertEquals("b",rs.getString(1)); + + rs = conn.createStatement().executeQuery("select * from " + viewName + " where pk2 IN ('b','e')"); + if ("a".equals(tenantId)) { + assertTrue(rs.next()); + assertEquals("b", rs.getString(1)); + } assertTrue(rs.next()); - assertEquals("e",rs.getString(1)); + assertEquals("e", rs.getString(1)); assertFalse(rs.next()); - + + conn.close(); + } @Test http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpgradeIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpgradeIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpgradeIT.java index 37d285f..f77f0f3 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpgradeIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpgradeIT.java @@ -45,6 +45,8 @@ import org.apache.phoenix.jdbc.PhoenixConnection; import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; import org.apache.phoenix.query.QueryConstants; import org.apache.phoenix.query.QueryServices; +import org.apache.phoenix.schema.PName; +import org.apache.phoenix.schema.PNameFactory; import org.apache.phoenix.util.MetaDataUtil; import org.apache.phoenix.util.PhoenixRuntime; import org.apache.phoenix.util.SchemaUtil; @@ -130,6 +132,7 @@ public class UpgradeIT extends BaseHBaseManagedTimeIT { String localIndexName = "LIDX"; String[] tableNames = new String[] { phoenixFullTableName, schemaName + "." + indexName, schemaName + "." + localIndexName, "diff.v", "test.v","v"}; + String[] viewIndexes = new String[] { "diff.v_idx", "test.v_idx" }; conn.createStatement().execute("CREATE TABLE " + phoenixFullTableName + "(k VARCHAR PRIMARY KEY, v INTEGER, f INTEGER, g INTEGER NULL, h INTEGER NULL)"); PreparedStatement upsertStmt = conn @@ -164,6 +167,15 @@ public class UpgradeIT extends BaseHBaseManagedTimeIT { } } + // validate view Index data + for (String viewIndex : viewIndexes) { + ResultSet rs = conn.createStatement().executeQuery("select * from " + viewIndex); + for (String str : strings) { + assertTrue(rs.next()); + assertEquals(str, rs.getString(2)); + } + } + HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin(); assertTrue(admin.tableExists(phoenixFullTableName)); assertTrue(admin.tableExists(MetaDataUtil.getLocalIndexPhysicalName(Bytes.toBytes(phoenixFullTableName)))); @@ -179,7 +191,7 @@ public class UpgradeIT extends BaseHBaseManagedTimeIT { for (String viewName : viewNames) { UpgradeUtil.upgradeTable(phxConn, viewName); } - admin = phxConn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin(); + admin = phxConn.getQueryServices().getAdmin(); String hbaseTableName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(phoenixFullTableName), true) .getNameAsString(); assertTrue(admin.tableExists(hbaseTableName)); @@ -195,8 +207,35 @@ public class UpgradeIT extends BaseHBaseManagedTimeIT { assertEquals(str, rs.getString(1)); } } + // validate view Index data + for (String viewIndex : viewIndexes) { + ResultSet rs = conn.createStatement().executeQuery("select * from " + viewIndex); + for (String str : strings) { + assertTrue(rs.next()); + assertEquals(str, rs.getString(2)); + } + } + PName tenantId = phxConn.getTenantId(); + PName physicalName = PNameFactory.newName(hbaseTableName); + String oldSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(PNameFactory.newName(phoenixFullTableName), + false); + String newSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(physicalName, true); + String newSequenceName = MetaDataUtil.getViewIndexSequenceName(physicalName, tenantId, true); + ResultSet rs = phxConn.createStatement() + .executeQuery("SELECT " + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + PhoenixDatabaseMetaData.TENANT_ID + + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + newSchemaName + + "' AND " + PhoenixDatabaseMetaData.SEQUENCE_NAME + "='" + newSequenceName + "'"); + assertTrue(rs.next()); + assertEquals("-32765", rs.getString(1)); + rs = phxConn.createStatement().executeQuery("SELECT " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.CURRENT_VALUE + " FROM " + + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + PhoenixDatabaseMetaData.TENANT_ID + + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + oldSchemaName + "'"); + assertFalse(rs.next()); phxConn.close(); admin.close(); + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ViewIndexIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ViewIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ViewIndexIT.java index 450cffa..c139a0c 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ViewIndexIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/ViewIndexIT.java @@ -37,6 +37,7 @@ import org.apache.phoenix.end2end.BaseHBaseManagedTimeIT; import org.apache.phoenix.end2end.Shadower; import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; import org.apache.phoenix.query.QueryServices; +import org.apache.phoenix.schema.PNameFactory; import org.apache.phoenix.util.MetaDataUtil; import org.apache.phoenix.util.QueryUtil; import org.apache.phoenix.util.ReadOnlyProps; @@ -116,21 +117,32 @@ public class ViewIndexIT extends BaseHBaseManagedTimeIT { this.viewIndexPhysicalTableName = MetaDataUtil.getLocalIndexTableName(physicalTableName.getNameAsString()); } - @Test public void testDeleteViewIndexSequences() throws Exception { createBaseTable(tableName, false, null, null); Connection conn1 = getConnection(); Connection conn2 = getConnection(); - conn1.createStatement().execute("CREATE VIEW " + VIEW_NAME + " AS SELECT * FROM " + tableName); - conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + VIEW_NAME + " (v1)"); + String viewName = schemaName + "." + VIEW_NAME; + conn1.createStatement().execute("CREATE VIEW " + viewName + " AS SELECT * FROM " + tableName); + conn1.createStatement().execute("CREATE INDEX " + indexName + " ON " + viewName + " (v1)"); conn2.createStatement().executeQuery("SELECT * FROM " + tableName).next(); + String query = "SELECT sequence_schema, sequence_name, current_value, increment_by FROM SYSTEM.\"SEQUENCE\" WHERE sequence_schema like '%" + + schemaName + "%'"; + ResultSet rs = conn1.prepareStatement(query).executeQuery(); + assertTrue(rs.next()); + assertEquals(MetaDataUtil.getViewIndexSequenceSchemaName(PNameFactory.newName(tableName), isNamespaceMapped), + rs.getString("sequence_schema")); + assertEquals(MetaDataUtil.getViewIndexSequenceName(PNameFactory.newName(tableName), null, isNamespaceMapped), + rs.getString("sequence_name")); + assertEquals(-32767, rs.getInt("current_value")); + assertEquals(1, rs.getInt("increment_by")); + assertFalse(rs.next()); HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin(); - conn1.createStatement().execute("DROP VIEW " + VIEW_NAME); + conn1.createStatement().execute("DROP VIEW " + viewName); conn1.createStatement().execute("DROP TABLE "+ tableName); admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin(); assertFalse("View index table should be deleted.", admin.tableExists(TableName.valueOf(viewIndexPhysicalTableName))); - ResultSet rs = conn2.createStatement().executeQuery("SELECT " + rs = conn2.createStatement().executeQuery("SELECT " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + "," + PhoenixDatabaseMetaData.SEQUENCE_NAME + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE); http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java index ddf9c0c..46e7d14 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java @@ -222,6 +222,10 @@ public class FromCompiler { return new SchemaResolver(connection, SchemaUtil.normalizeIdentifier(statement.getSchemaName()), true); } + public static ColumnResolver getResolverForSchema(String schema, PhoenixConnection connection) throws SQLException { + return new SchemaResolver(connection, SchemaUtil.normalizeIdentifier(schema), true); + } + public static ColumnResolver getResolver(NamedTableNode tableNode, PhoenixConnection connection) throws SQLException { SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true); return visitor; http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java index 408b144..6ed0b74 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java @@ -43,7 +43,6 @@ import org.apache.phoenix.schema.Sequence; import org.apache.phoenix.schema.SequenceAllocation; import org.apache.phoenix.schema.SequenceKey; import org.apache.phoenix.schema.stats.PTableStats; - import org.apache.tephra.TransactionSystemClient; http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java index 888c481..a51197b 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java @@ -188,6 +188,11 @@ import org.apache.phoenix.util.ReadOnlyProps; import org.apache.phoenix.util.SchemaUtil; import org.apache.phoenix.util.ServerUtil; import org.apache.phoenix.util.UpgradeUtil; +import org.apache.tephra.TransactionSystemClient; +import org.apache.tephra.TxConstants; +import org.apache.tephra.distributed.PooledClientProvider; +import org.apache.tephra.distributed.TransactionServiceClient; +import org.apache.tephra.zookeeper.TephraZKClientService; import org.apache.twill.discovery.ZKDiscoveryService; import org.apache.twill.zookeeper.RetryStrategies; import org.apache.twill.zookeeper.ZKClientService; @@ -196,12 +201,6 @@ import org.apache.twill.zookeeper.ZKClients; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.tephra.TransactionSystemClient; -import org.apache.tephra.TxConstants; -import org.apache.tephra.distributed.PooledClientProvider; -import org.apache.tephra.distributed.TransactionServiceClient; -import org.apache.tephra.zookeeper.TephraZKClientService; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Throwables; @@ -546,7 +545,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement return locations; } catch (org.apache.hadoop.hbase.TableNotFoundException e) { String fullName = Bytes.toString(tableName); - throw new TableNotFoundException(SchemaUtil.getSchemaNameFromFullName(fullName), SchemaUtil.getTableNameFromFullName(fullName)); + throw new TableNotFoundException(fullName); } catch (IOException e) { if (retryCount++ < maxRetryCount) { // One retry, in case split occurs while navigating reload = true; @@ -1471,9 +1470,10 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement familiesPlusDefault = Lists.newArrayList(families); familiesPlusDefault.add(new Pair<byte[],Map<String,Object>>(defaultCF,Collections.<String,Object>emptyMap())); } - ensureViewIndexTableCreated(tableName, tableProps, familiesPlusDefault, - MetaDataUtil.isSalted(m, kvBuilder, ptr) ? splits : null, MetaDataUtil.getClientTimeStamp(m), - isNamespaceMapped); + ensureViewIndexTableCreated( + SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, tableType).getBytes(), + tableProps, familiesPlusDefault, MetaDataUtil.isSalted(m, kvBuilder, ptr) ? splits : null, + MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped); } byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes); @@ -2629,8 +2629,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement if (tableNames.contains(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME)) { if (!admin.tableExists(mappedSystemTable)) { UpgradeUtil.mapTableToNamespace(admin, metatable, - PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, props, - MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0, PTableType.SYSTEM); + PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, props, null, PTableType.SYSTEM); ConnectionQueryServicesImpl.this.removeTable(null, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0); @@ -2638,8 +2637,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement tableNames.remove(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME); } for (String table : tableNames) { - UpgradeUtil.mapTableToNamespace(admin, metatable, table, props, - MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0, PTableType.SYSTEM); + UpgradeUtil.mapTableToNamespace(admin, metatable, table, props, null, PTableType.SYSTEM); ConnectionQueryServicesImpl.this.removeTable(null, table, null, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP_4_1_0); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java index 34cedce..a1b7acc 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java @@ -211,11 +211,10 @@ import org.apache.phoenix.util.SchemaUtil; import org.apache.phoenix.util.StringUtil; import org.apache.phoenix.util.TransactionUtil; import org.apache.phoenix.util.UpgradeUtil; +import org.apache.tephra.TxConstants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.tephra.TxConstants; - import com.google.common.base.Objects; import com.google.common.collect.Iterators; import com.google.common.collect.ListMultimap; @@ -1432,7 +1431,8 @@ public class MetaDataClient { String tenantIdStr = tenantId == null ? null : connection.getTenantId().getString(); PName physicalName = dataTable.getPhysicalName(); int nSequenceSaltBuckets = connection.getQueryServices().getSequenceSaltBuckets(); - SequenceKey key = MetaDataUtil.getViewIndexSequenceKey(tenantIdStr, physicalName, nSequenceSaltBuckets); + SequenceKey key = MetaDataUtil.getViewIndexSequenceKey(tenantIdStr, physicalName, + nSequenceSaltBuckets, dataTable.isNamespaceMapped()); // if scn is set create at scn-1, so we can see the sequence or else use latest timestamp (so that latest server time is used) long sequenceTimestamp = scn!=null ? scn-1 : HConstants.LATEST_TIMESTAMP; createSequence(key.getTenantId(), key.getSchemaName(), key.getSequenceName(), @@ -1511,7 +1511,16 @@ public class MetaDataClient { long timestamp = scn == null ? HConstants.LATEST_TIMESTAMP : scn; String tenantId = connection.getTenantId() == null ? null : connection.getTenantId().getString(); - return createSequence(tenantId, statement.getSequenceName().getSchemaName(), statement + String schemaName=statement.getSequenceName().getSchemaName(); + if (SchemaUtil.isNamespaceMappingEnabled(null, connection.getQueryServices().getProps())) { + if (schemaName == null || schemaName.equals(StringUtil.EMPTY_STRING)) { + schemaName = connection.getSchema(); + } + if (schemaName != null) { + FromCompiler.getResolverForSchema(schemaName, connection); + } + } + return createSequence(tenantId, schemaName, statement .getSequenceName().getTableName(), statement.ifNotExists(), startWith, incrementBy, cacheSize, statement.getCycle(), minValue, maxValue, timestamp); } @@ -2576,7 +2585,7 @@ public class MetaDataClient { if (tableType == PTableType.TABLE && (table.isMultiTenant() || hasViewIndexTable || hasLocalIndexTable)) { - MetaDataUtil.deleteViewIndexSequences(connection, table.getPhysicalName()); + MetaDataUtil.deleteViewIndexSequences(connection, table.getPhysicalName(), table.isNamespaceMapped()); if (hasViewIndexTable) { String viewIndexSchemaName = null; String viewIndexTableName = null; @@ -3113,7 +3122,7 @@ public class MetaDataClient { && Boolean.FALSE.equals(multiTenant) && MetaDataUtil.hasViewIndexTable(connection, table.getPhysicalName())) { connection.setAutoCommit(true); - MetaDataUtil.deleteViewIndexSequences(connection, table.getPhysicalName()); + MetaDataUtil.deleteViewIndexSequences(connection, table.getPhysicalName(), table.isNamespaceMapped()); // If we're not dropping metadata, then make sure no rows are left in // our view index physical table. // TODO: remove this, as the DROP INDEX commands run when the DROP VIEW http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java index 116b62b..d74ecfc 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/MetaDataUtil.java @@ -65,6 +65,7 @@ import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; import org.apache.phoenix.query.QueryConstants; import org.apache.phoenix.schema.PColumn; import org.apache.phoenix.schema.PName; +import org.apache.phoenix.schema.PNameFactory; import org.apache.phoenix.schema.PTable; import org.apache.phoenix.schema.PTable.LinkType; import org.apache.phoenix.schema.PTableType; @@ -388,18 +389,25 @@ public class MetaDataUtil { } } - public static String getViewIndexSchemaName(PName physicalName) { - return VIEW_INDEX_SEQUENCE_PREFIX + physicalName.getString(); + public static String getViewIndexSequenceSchemaName(PName physicalName, boolean isNamespaceMapped) { + if (!isNamespaceMapped) { return VIEW_INDEX_SEQUENCE_PREFIX + physicalName.getString(); } + return SchemaUtil.getSchemaNameFromFullName(physicalName.toString()); } - - public static SequenceKey getViewIndexSequenceKey(String tenantId, PName physicalName, int nSaltBuckets) { + + public static String getViewIndexSequenceName(PName physicalName, PName tenantId, boolean isNamespaceMapped) { + if (!isNamespaceMapped) { return VIEW_INDEX_SEQUENCE_NAME_PREFIX + (tenantId == null ? "" : tenantId); } + return SchemaUtil.getTableNameFromFullName(physicalName.toString()) + VIEW_INDEX_SEQUENCE_NAME_PREFIX; + } + + public static SequenceKey getViewIndexSequenceKey(String tenantId, PName physicalName, int nSaltBuckets, + boolean isNamespaceMapped) { // Create global sequence of the form: <prefixed base table name><tenant id> // rather than tenant-specific sequence, as it makes it much easier // to cleanup when the physical table is dropped, as we can delete // all global sequences leading with <prefix> + physical name. - String schemaName = getViewIndexSchemaName(physicalName); - String tableName = VIEW_INDEX_SEQUENCE_NAME_PREFIX + (tenantId == null ? "" : tenantId); - return new SequenceKey(null, schemaName, tableName, nSaltBuckets); + String schemaName = getViewIndexSequenceSchemaName(physicalName, isNamespaceMapped); + String tableName = getViewIndexSequenceName(physicalName, PNameFactory.newName(tenantId), isNamespaceMapped); + return new SequenceKey(isNamespaceMapped ? tenantId : null, schemaName, tableName, nSaltBuckets); } public static PDataType getViewIndexIdDataType() { @@ -439,12 +447,14 @@ public class MetaDataUtil { } } - public static void deleteViewIndexSequences(PhoenixConnection connection, PName name) throws SQLException { - String schemaName = getViewIndexSchemaName(name); - connection.createStatement().executeUpdate("DELETE FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + - " WHERE " + PhoenixDatabaseMetaData.TENANT_ID + " IS NULL AND " + - PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + schemaName + "'"); - + public static void deleteViewIndexSequences(PhoenixConnection connection, PName name, boolean isNamespaceMapped) + throws SQLException { + String schemaName = getViewIndexSequenceSchemaName(name, isNamespaceMapped); + String sequenceName = getViewIndexSequenceName(name, null, isNamespaceMapped); + connection.createStatement().executeUpdate("DELETE FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + + (schemaName.length() > 0 ? "='" + schemaName + "'" : " IS NULL") + (isNamespaceMapped + ? " AND " + PhoenixDatabaseMetaData.SEQUENCE_NAME + " = '" + sequenceName + "'" : "")); } /** http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/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 a8981a4..d42b4f9 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 @@ -85,14 +85,13 @@ import org.apache.phoenix.schema.RowKeyValueAccessor; import org.apache.phoenix.schema.TableNotFoundException; import org.apache.phoenix.schema.ValueBitSet; import org.apache.phoenix.schema.types.PDataType; +import org.apache.tephra.util.TxUtils; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import org.apache.tephra.util.TxUtils; - /** * * Collection of non JDBC compliant utility methods @@ -215,9 +214,10 @@ public class PhoenixRuntime { conn = DriverManager.getConnection(jdbcUrl, props).unwrap(PhoenixConnection.class); if (execCmd.isMapNamespace()) { String srcTable = execCmd.getSrcTable(); + System.out.println("Starting upgrading table:" + srcTable + "... please don't kill it in between!!"); UpgradeUtil.upgradeTable(conn, srcTable); Set<String> viewNames = MetaDataUtil.getViewNames(conn, srcTable); - System.out.println("Views found:"+viewNames); + System.out.println("upgrading following views:"+viewNames); for (String viewName : viewNames) { UpgradeUtil.upgradeTable(conn, viewName); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/a50bee21/phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java index 8d00b2b..a91a33f 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java @@ -19,14 +19,22 @@ package org.apache.phoenix.util; import static com.google.common.base.Preconditions.checkNotNull; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ARRAY_SIZE; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.CACHE_SIZE; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_FAMILY; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_NAME; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.COLUMN_SIZE; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.CURRENT_VALUE; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.CYCLE_FLAG; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.DATA_TYPE; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.DECIMAL_DIGITS; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.INCREMENT_BY; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.LIMIT_REACHED_FLAG; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.LINK_TYPE; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.MAX_VALUE; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.MIN_VALUE; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.ORDINAL_POSITION; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SORT_ORDER; +import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.START_WITH; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA; import static org.apache.phoenix.jdbc.PhoenixDatabaseMetaData.SYSTEM_CATALOG_TABLE; @@ -478,7 +486,8 @@ public class UpgradeUtil { byte[] tableName = rowKeyMetaData[PhoenixDatabaseMetaData.TABLE_NAME_INDEX]; PName physicalName = PNameFactory.newName(unprefixedSchemaName); // Reformulate key based on correct data - newBuf = MetaDataUtil.getViewIndexSequenceKey(tableName == null ? null : Bytes.toString(tableName), physicalName, nSaltBuckets).getKey(); + newBuf = MetaDataUtil.getViewIndexSequenceKey(tableName == null ? null : Bytes.toString(tableName), + physicalName, nSaltBuckets, false).getKey(); } else { newBuf = new byte[length + 1]; System.arraycopy(buf, offset, newBuf, SaltingUtil.NUM_SALTING_BYTES, length); @@ -1335,10 +1344,23 @@ public class UpgradeUtil { admin.deleteSnapshot(snapshotName); } } + + byte[] tableKey = SchemaUtil.getTableKey(null, SchemaUtil.getSchemaNameFromFullName(phoenixTableName), + SchemaUtil.getTableNameFromFullName(phoenixTableName)); + List<Cell> columnCells = metatable.get(new Get(tableKey)) + .getColumnCells(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES); + if (ts == null) { + if (!columnCells.isEmpty()) { + ts = columnCells.get(0).getTimestamp(); + } else { + throw new IllegalArgumentException( + "Timestamp passed is null and cannot derive timestamp for " + tableKey + " from meta table!!"); + } + } // Update flag to represent table is mapped to namespace - logger.info(String.format("Updating meta information of phoenix table '%s' to map to namespace..", phoenixTableName)); - Put put = new Put(SchemaUtil.getTableKey(null, SchemaUtil.getSchemaNameFromFullName(phoenixTableName), - SchemaUtil.getTableNameFromFullName(phoenixTableName)), ts); + logger.info(String.format("Updating meta information of phoenix table '%s' to map to namespace..", + phoenixTableName)); + Put put = new Put(tableKey, ts); put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.IS_NAMESPACE_MAPPED_BYTES, PBoolean.INSTANCE.toBytes(Boolean.TRUE)); metatable.put(put); @@ -1443,11 +1465,14 @@ public class UpgradeUtil { if (updateLink) { logger.info(String.format("Updating link information for index '%s' ..", index.getName())); updateLink(conn, srcTableName, destTableName); + conn.commit(); } conn.getQueryServices().clearTableFromCache(ByteUtil.EMPTY_BYTE_ARRAY, index.getSchemaName().getBytes(), index.getTableName().getBytes(), PhoenixRuntime.getCurrentScn(readOnlyProps)); } + updateIndexesSequenceIfPresent(conn, table); + conn.commit(); } else { throw new RuntimeException("Error: problem occured during upgrade. Table is not upgraded successfully"); @@ -1455,6 +1480,27 @@ public class UpgradeUtil { } } + private static void updateIndexesSequenceIfPresent(PhoenixConnection connection, PTable dataTable) + throws SQLException { + PName tenantId = connection.getTenantId(); + PName physicalName = dataTable.getPhysicalName(); + PName oldPhysicalName = PNameFactory.newName( + physicalName.toString().replace(QueryConstants.NAMESPACE_SEPARATOR, QueryConstants.NAME_SEPARATOR)); + String oldSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(oldPhysicalName, false); + String newSchemaName = MetaDataUtil.getViewIndexSequenceSchemaName(physicalName, true); + String newSequenceName = MetaDataUtil.getViewIndexSequenceName(physicalName, tenantId, true); + // create new entry with new schema format + String upsert = "UPSERT INTO " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " SELECT " + TENANT_ID + ",\'" + + newSchemaName + "\',\'" + newSequenceName + "\'," + START_WITH + "," + CURRENT_VALUE + "," + + INCREMENT_BY + "," + CACHE_SIZE + "," + MIN_VALUE + "," + MAX_VALUE + "," + CYCLE_FLAG + "," + + LIMIT_REACHED_FLAG + " FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE " + + PhoenixDatabaseMetaData.TENANT_ID + " IS NULL AND " + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA + " = '" + + oldSchemaName + "'"; + connection.createStatement().executeUpdate(upsert); + // delete old sequence + MetaDataUtil.deleteViewIndexSequences(connection, oldPhysicalName, false); + } + private static void updateLink(PhoenixConnection conn, String srcTableName, String destTableName) throws SQLException { PreparedStatement deleteLinkStatment = conn.prepareStatement(DELETE_LINK); @@ -1463,7 +1509,6 @@ public class UpgradeUtil { updateLinkStatment.setString(1, srcTableName); deleteLinkStatment.execute(); updateLinkStatment.execute(); - conn.commit(); } } \ No newline at end of file