PHOENIX-3841 Creation of view fails when UPDATE_CACHE_FREQUENCY specified on table (Maddineni Sukumar)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/70c13948 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/70c13948 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/70c13948 Branch: refs/heads/4.x-HBase-1.1 Commit: 70c1394833a796cb243acb8c46e456ba767be998 Parents: 295d0bd Author: James Taylor <[email protected]> Authored: Thu May 11 17:00:06 2017 -0700 Committer: James Taylor <[email protected]> Committed: Thu May 11 17:41:54 2017 -0700 ---------------------------------------------------------------------- .../java/org/apache/phoenix/end2end/ViewIT.java | 19 +++++++++++++- .../apache/phoenix/compile/FromCompiler.java | 26 +++++++++++++++----- 2 files changed, 38 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/70c13948/phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewIT.java ---------------------------------------------------------------------- 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 512841a..947b035 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 @@ -692,7 +692,24 @@ public class ViewIT extends BaseViewIT { plan = PhoenixRuntime.getOptimizedQueryPlan(stmt); assertEquals(0, plan.getOrderBy().getOrderByExpressions().size()); } - + + @Test + public void testCreateViewWithUpdateCacheFrquency() throws Exception { + Properties props = new Properties(); + Connection conn1 = DriverManager.getConnection(getUrl(), props); + conn1.setAutoCommit(true); + String TABLE_NAME="UpdateCacheViewTest"+System.currentTimeMillis(); + String VIEW_NAME="VIEW_"+System.currentTimeMillis(); + conn1.createStatement().execute( + "CREATE TABLE "+TABLE_NAME+" (k VARCHAR PRIMARY KEY, v1 VARCHAR, v2 VARCHAR) UPDATE_CACHE_FREQUENCY=1000000"); + conn1.createStatement().execute("upsert into "+TABLE_NAME+" values ('row1', 'value1', 'key1')"); + conn1.createStatement().execute( + "CREATE VIEW "+VIEW_NAME+" (v43 VARCHAR) AS SELECT * FROM "+TABLE_NAME+" WHERE v1 = 'value1'"); + ResultSet rs = conn1.createStatement() + .executeQuery("SELECT * FROM "+TABLE_NAME+" WHERE v1 = 'value1'"); + assertTrue(rs.next()); + } + private void assertPKs(ResultSet rs, String[] expectedPKs) throws SQLException { List<String> pkCols = newArrayListWithExpectedSize(expectedPKs.length); while (rs.next()) { http://git-wip-us.apache.org/repos/asf/phoenix/blob/70c13948/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 f401aad..6440769 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 @@ -172,7 +172,11 @@ public class FromCompiler { NamedTableNode tableNode = NamedTableNode.create(null, baseTable, Collections.<ColumnDef>emptyList()); // Always use non-tenant-specific connection here try { - SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true); + // We need to always get the latest meta data for the parent table of a create view call to ensure that + // that we're copying the current table meta data as of when the view is created. Once we no longer + // copy the parent meta data, but store only the local diffs (PHOENIX-3534), we will no longer need + // to do this. + SingleTableColumnResolver visitor = new SingleTableColumnResolver(connection, tableNode, true, true); return visitor; } catch (TableNotFoundException e) { // Used for mapped VIEW, since we won't be able to resolve that. @@ -365,13 +369,22 @@ public class FromCompiler { public SingleTableColumnResolver(PhoenixConnection connection, NamedTableNode tableNode, boolean updateCacheImmediately) throws SQLException { this(connection, tableNode, updateCacheImmediately, 0, new HashMap<String,UDFParseNode>(1)); } + public SingleTableColumnResolver(PhoenixConnection connection, NamedTableNode tableNode, + boolean updateCacheImmediately, boolean alwaysHitServer) throws SQLException { + this(connection, tableNode, updateCacheImmediately, 0, new HashMap<String,UDFParseNode>(1), alwaysHitServer); + } + public SingleTableColumnResolver(PhoenixConnection connection, NamedTableNode tableNode, + boolean updateCacheImmediately, int tsAddition, + Map<String, UDFParseNode> udfParseNodes) throws SQLException { + this(connection, tableNode, updateCacheImmediately, tsAddition, udfParseNodes, false); + } public SingleTableColumnResolver(PhoenixConnection connection, NamedTableNode tableNode, boolean updateCacheImmediately, int tsAddition, - Map<String, UDFParseNode> udfParseNodes) throws SQLException { + Map<String, UDFParseNode> udfParseNodes, boolean alwaysHitServer) throws SQLException { super(connection, tsAddition, updateCacheImmediately, udfParseNodes); alias = tableNode.getAlias(); - TableRef tableRef = createTableRef(tableNode.getName().getSchemaName(), tableNode, updateCacheImmediately); + TableRef tableRef = createTableRef(tableNode.getName().getSchemaName(), tableNode, updateCacheImmediately, alwaysHitServer); PSchema schema = new PSchema(tableRef.getTable().getSchemaName().toString()); tableRefs = ImmutableList.of(tableRef); schemas = ImmutableList.of(schema); @@ -532,7 +545,8 @@ public class FromCompiler { return theSchema; } - protected TableRef createTableRef(String connectionSchemaName, NamedTableNode tableNode, boolean updateCacheImmediately) throws SQLException { + protected TableRef createTableRef(String connectionSchemaName, NamedTableNode tableNode, + boolean updateCacheImmediately, boolean alwaysHitServer) throws SQLException { String tableName = tableNode.getName().getTableName(); String schemaName = tableNode.getName().getSchemaName(); schemaName = connection.getSchema() != null && schemaName == null ? connection.getSchema() : schemaName; @@ -541,7 +555,7 @@ public class FromCompiler { PName tenantId = connection.getTenantId(); PTable theTable = null; if (updateCacheImmediately) { - MetaDataMutationResult result = client.updateCache(schemaName, tableName); + MetaDataMutationResult result = client.updateCache(tenantId, schemaName, tableName, alwaysHitServer); timeStamp = TransactionUtil.getResolvedTimestamp(connection, result); theTable = result.getTable(); if (theTable == null) { @@ -746,7 +760,7 @@ public class FromCompiler { @Override public Void visit(NamedTableNode tableNode) throws SQLException { String alias = tableNode.getAlias(); - TableRef tableRef = createTableRef(connectionSchemaName, tableNode, true); + TableRef tableRef = createTableRef(connectionSchemaName, tableNode, true, false); PTable theTable = tableRef.getTable(); if (alias != null) {
