This is an automated email from the ASF dual-hosted git repository. achouhan pushed a commit to branch 4.14-HBase-1.4 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.14-HBase-1.4 by this push: new 3e8ddde PHOENIX-5529 Creating a grand-child view on a table with an index fails 3e8ddde is described below commit 3e8ddde696fb5b7ca006857e01ec7d471c163e05 Author: Abhishek Singh Chouhan <achou...@apache.org> AuthorDate: Fri Feb 14 10:45:15 2020 -0800 PHOENIX-5529 Creating a grand-child view on a table with an index fails --- .../it/java/org/apache/phoenix/end2end/ViewIT.java | 70 +++++++++++++++++++++- .../org/apache/phoenix/schema/MetaDataClient.java | 2 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java index a6e066b..61c809b 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java @@ -47,6 +47,7 @@ import org.apache.phoenix.jdbc.PhoenixConnection; import org.apache.phoenix.query.KeyRange; import org.apache.phoenix.query.QueryServices; import org.apache.phoenix.schema.ColumnAlreadyExistsException; +import org.apache.phoenix.schema.PTable; import org.apache.phoenix.schema.ReadOnlyTableException; import org.apache.phoenix.schema.TableNotFoundException; import org.apache.phoenix.util.PhoenixRuntime; @@ -559,7 +560,74 @@ public class ViewIT extends BaseViewIT { "CLIENT PARALLEL 1-WAY SKIP SCAN ON 4 KEYS OVER " + fullIndexName1 + " [1,100] - [2,109]\n" + " SERVER FILTER BY (\"S2\" = 'bas' AND \"S1\" = 'foo')", queryPlan); } - } + } + + @Test + public void testCreateChildViewWithBaseTableLocalIndex() throws Exception { + testCreateChildViewWithBaseTableIndex(true); + } + + @Test + public void testCreateChildViewWithBaseTableGlobalIndex() throws Exception { + testCreateChildViewWithBaseTableIndex(false); + } + + public void testCreateChildViewWithBaseTableIndex(boolean localIndex) throws Exception { + String schema1 = "S_" + generateUniqueName(); + String schema2 = "S_" + generateUniqueName(); + String fullTableName = SchemaUtil.getTableName(schema1, generateUniqueName()); + String fullViewName = SchemaUtil.getTableName(schema2, generateUniqueName()); + String indexName = "I_" + generateUniqueName(); + String fullChildViewName = SchemaUtil.getTableName(schema2, generateUniqueName()); + try (Connection conn = DriverManager.getConnection(getUrl())) { + String sql = + "CREATE TABLE " + fullTableName + + " (ID INTEGER NOT NULL PRIMARY KEY, HOST VARCHAR(10), FLAG BOOLEAN)"; + conn.createStatement().execute(sql); + sql = + "CREATE VIEW " + fullViewName + + " (COL1 INTEGER, COL2 INTEGER, COL3 INTEGER, COL4 INTEGER) AS SELECT * FROM " + + fullTableName + " WHERE ID > 5"; + conn.createStatement().execute(sql); + sql = + "CREATE " + (localIndex ? "LOCAL " : "") + " INDEX " + indexName + " ON " + + fullTableName + "(HOST)"; + conn.createStatement().execute(sql); + sql = + "CREATE VIEW " + fullChildViewName + " AS SELECT * FROM " + fullViewName + + " WHERE COL1 > 2"; + conn.createStatement().execute(sql); + // Sanity upserts in baseTable, view, child view + conn.createStatement() + .executeUpdate("upsert into " + fullTableName + " values (1, 'host1', TRUE)"); + conn.createStatement() + .executeUpdate("upsert into " + fullTableName + " values (5, 'host5', FALSE)"); + conn.createStatement() + .executeUpdate("upsert into " + fullTableName + " values (7, 'host7', TRUE)"); + conn.commit(); + // View is not updateable + try { + conn.createStatement().executeUpdate("upsert into " + fullViewName + + " (ID, HOST, FLAG, COL1) values (7, 'host7', TRUE, 1)"); + fail(); + } catch (Exception e) { + } + // Check view inherits index, but child view doesn't + PTable table = PhoenixRuntime.getTable(conn, fullViewName); + assertEquals(1, table.getIndexes().size()); + table = PhoenixRuntime.getTable(conn, fullChildViewName); + assertEquals(0, table.getIndexes().size()); + + ResultSet rs = + conn.createStatement().executeQuery("select count(*) from " + fullTableName); + assertTrue(rs.next()); + assertEquals(3, rs.getInt(1)); + + rs = conn.createStatement().executeQuery("select count(*) from " + fullViewName); + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + } + } @Test public void testCreateViewDefinesPKColumn() throws Exception { 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 dda8895..dffeb44 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 @@ -885,7 +885,7 @@ public class MetaDataClient { // Ensure that constant columns (i.e. columns matched in the view WHERE clause) // all exist in the index on the parent table. for (PColumn col : view.getColumns()) { - if (col.getViewConstant() != null) { + if (col.isViewReferenced() || col.getViewConstant() != null) { try { // It'd be possible to use a local index that doesn't have all view constants, // but the WHERE clause for the view statement (which is added to the index below)