This is an automated email from the ASF dual-hosted git repository. achouhan pushed a commit to branch 4.x-HBase-1.5 in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.x-HBase-1.5 by this push: new c891ca9 PHOENIX-5529 Creating a grand-child view on a table with an index fails c891ca9 is described below commit c891ca97acd3bdc4534234e9a944e60fdc1fd0d0 Author: Abhishek Singh Chouhan <achou...@apache.org> AuthorDate: Tue Feb 11 11:31:06 2020 -0800 PHOENIX-5529 Creating a grand-child view on a table with an index fails --- .../it/java/org/apache/phoenix/end2end/ViewIT.java | 67 +++++++++++++++++++++- .../java/org/apache/phoenix/util/ViewUtil.java | 2 +- 2 files changed, 67 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 aeec7f0..c962740 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 @@ -410,7 +410,72 @@ public class ViewIT extends SplitSystemCatalogIT { "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 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/util/ViewUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/ViewUtil.java index 22e3a40..7e622b5 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/util/ViewUtil.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/util/ViewUtil.java @@ -294,7 +294,7 @@ public class ViewUtil { // 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)