phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an...@apache.org
Subject phoenix git commit: PHOENIX-3944 ReadOnlyTableException occurs when we map Phoenix view to an existing HBase table with Namespace Mapping enabled (Toshihiro Suzuki)
Date Fri, 07 Jul 2017 11:09:40 GMT
Repository: phoenix
Updated Branches:
  refs/heads/master f5f32e3b8 -> b0109feb9


PHOENIX-3944 ReadOnlyTableException occurs when we map Phoenix view to an existing HBase table
with Namespace Mapping enabled (Toshihiro Suzuki)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/b0109feb
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/b0109feb
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/b0109feb

Branch: refs/heads/master
Commit: b0109feb92fdd9e19bb6f70412d0c476ec60d3d4
Parents: f5f32e3
Author: Ankit Singhal <ankitsinghal59@gmail.com>
Authored: Fri Jul 7 16:38:49 2017 +0530
Committer: Ankit Singhal <ankitsinghal59@gmail.com>
Committed: Fri Jul 7 16:38:49 2017 +0530

----------------------------------------------------------------------
 .../apache/phoenix/end2end/CreateTableIT.java   | 60 ++++++++++++++--
 .../end2end/IndexToolForPartialBuildIT.java     |  8 +--
 .../phoenix/end2end/StatsCollectorIT.java       |  4 +-
 .../end2end/TenantSpecificViewIndexIT.java      |  8 +--
 .../org/apache/phoenix/end2end/UpgradeIT.java   | 24 ++++++-
 .../java/org/apache/phoenix/end2end/ViewIT.java | 72 ++++++++++++++++++++
 .../phoenix/end2end/index/ViewIndexIT.java      |  6 +-
 .../apache/phoenix/compile/FromCompiler.java    |  8 +--
 .../compile/TupleProjectionCompiler.java        |  2 +-
 .../coprocessor/MetaDataEndpointImpl.java       |  8 +--
 .../query/ConnectionQueryServicesImpl.java      | 36 +++++-----
 .../apache/phoenix/schema/MetaDataClient.java   |  4 +-
 .../org/apache/phoenix/schema/PTableImpl.java   |  7 +-
 .../org/apache/phoenix/util/SchemaUtil.java     | 23 ++++---
 .../org/apache/phoenix/util/UpgradeUtil.java    |  7 +-
 15 files changed, 216 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
index 4d10e0f..5cc16a6 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/CreateTableIT.java
@@ -49,10 +49,7 @@ import org.apache.phoenix.schema.PTable.QualifierEncodingScheme;
 import org.apache.phoenix.schema.PTableKey;
 import org.apache.phoenix.schema.SchemaNotFoundException;
 import org.apache.phoenix.schema.TableAlreadyExistsException;
-import org.apache.phoenix.util.PhoenixRuntime;
-import org.apache.phoenix.util.PropertiesUtil;
-import org.apache.phoenix.util.SchemaUtil;
-import org.apache.phoenix.util.TestUtil;
+import org.apache.phoenix.util.*;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -708,4 +705,59 @@ public class CreateTableIT extends BaseClientManagedTimeIT {
         }
       }
     }
+
+    @Test
+    public void testCreateTableWithNamespaceMappingEnabled() throws Exception {
+        final String NS = "NS_" + generateUniqueName();
+        final String TBL = "TBL_" + generateUniqueName();
+        final String CF = "CF";
+
+        Properties props = new Properties();
+        props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.TRUE.toString());
+
+        try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
+            conn.createStatement().execute("CREATE SCHEMA " + NS);
+
+            // test for a table that is in non-default schema
+            {
+                String table = NS + "." + TBL;
+                conn.createStatement().execute(
+                    "CREATE TABLE " + table + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + table))
+                    .contains(NS + ":" + TBL));
+
+                conn.createStatement().execute("DROP TABLE " + table);
+            }
+
+            // test for a table whose name contains a dot (e.g. "AAA.BBB") in default schema
+            {
+                String table = "\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                    "CREATE TABLE " + table + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + table))
+                    .contains(NS + "." + TBL));
+
+                conn.createStatement().execute("DROP TABLE " + table);
+            }
+
+            // test for a view whose name contains a dot (e.g. "AAA.BBB") in non-default
schema
+            {
+                String table = NS + ".\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                    "CREATE TABLE " + table + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + table))
+                    .contains(NS + ":" + NS + "." + TBL));
+
+                conn.createStatement().execute("DROP TABLE " + table);
+            }
+
+            conn.createStatement().execute("DROP SCHEMA " + NS);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexToolForPartialBuildIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexToolForPartialBuildIT.java
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexToolForPartialBuildIT.java
index 83bda64..9c80761 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexToolForPartialBuildIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/IndexToolForPartialBuildIT.java
@@ -31,8 +31,6 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.Arrays;
-import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -227,12 +225,10 @@ public class IndexToolForPartialBuildIT extends BaseOwnClusterIT {
 		String expectedExplainPlan = "";
 		if (indxTable != null) {
 		    expectedExplainPlan = String.format("CLIENT PARALLEL 1-WAY FULL SCAN OVER %s",
-		        SchemaUtil.getPhysicalHBaseTableName(SchemaUtil.getTableName(schemaName, indxTable),
-		            isNamespaceMapped, PTableType.INDEX));
+		            SchemaUtil.getPhysicalHBaseTableName(schemaName, indxTable, isNamespaceMapped));
 		} else {
 			expectedExplainPlan = String.format("CLIENT PARALLEL 1-WAY FULL SCAN OVER %s",
-					SchemaUtil.getPhysicalHBaseTableName(SchemaUtil.getTableName(schemaName, dataTable),
-							isNamespaceMapped, PTableType.TABLE));
+			        SchemaUtil.getPhysicalHBaseTableName(schemaName, dataTable, isNamespaceMapped));
 		}
 		assertTrue(actualExplainPlan.contains(expectedExplainPlan));
 	}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/it/java/org/apache/phoenix/end2end/StatsCollectorIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/StatsCollectorIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/StatsCollectorIT.java
index a49c012..d2c8e6f 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/StatsCollectorIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/StatsCollectorIT.java
@@ -150,7 +150,7 @@ public class StatsCollectorIT extends BaseUniqueNamesOwnClusterIT {
         }
         tableName = "T_" + generateUniqueName();
         fullTableName = SchemaUtil.getTableName(schemaName, tableName);
-        physicalTableName = SchemaUtil.getPhysicalHBaseTableName(fullTableName, userTableNamespaceMapped,
PTableType.TABLE).getString();
+        physicalTableName = SchemaUtil.getPhysicalHBaseTableName(schemaName, tableName, userTableNamespaceMapped).getString();
     }
 
     private Connection getConnection() throws SQLException {
@@ -633,7 +633,7 @@ public class StatsCollectorIT extends BaseUniqueNamesOwnClusterIT {
             int endIndex = r.nextInt(strings.length - startIndex) + startIndex;
             long rows = endIndex - startIndex;
             long c2Bytes = rows * (columnEncoded ? ( mutable ? 37 : 48 ) : 35);
-            String physicalTableName = SchemaUtil.getPhysicalHBaseTableName(fullTableName,
userTableNamespaceMapped, PTableType.TABLE).getString();
+            String physicalTableName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(fullTableName),
userTableNamespaceMapped).toString();
             rs = conn.createStatement().executeQuery(
                     "SELECT COLUMN_FAMILY,SUM(GUIDE_POSTS_ROW_COUNT),SUM(GUIDE_POSTS_WIDTH)
from \"SYSTEM\".STATS where PHYSICAL_NAME = '"
                             + physicalTableName + "' AND GUIDE_POST_KEY>= cast('" + strings[startIndex]

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 6ae1445..d5da0aa 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
@@ -184,7 +184,7 @@ public class TenantSpecificViewIndexIT extends BaseTenantSpecificViewIndexIT
{
         assertFalse(rs.next());
         rs = conn.createStatement().executeQuery("explain select * from " + viewName);
         assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER "
-                + SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, PTableType.TABLE)
+ " ['"
+                + SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped)
+ " ['"
                 + tenantId + "']", QueryUtil.getExplainPlan(rs));
 
         rs = conn.createStatement().executeQuery("select pk2,col1 from " + viewName + " where
col1='f'");
@@ -195,13 +195,13 @@ public class TenantSpecificViewIndexIT extends BaseTenantSpecificViewIndexIT
{
         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) + " [1,'"
+                    + SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped)
+ " [1,'"
                     + tenantId + "','f']\n" + "    SERVER FILTER BY FIRST KEY ONLY\n" + "CLIENT
MERGE SORT",
                     QueryUtil.getExplainPlan(rs));
         } else {
             assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER "
-                    + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(SchemaUtil
-                            .getPhysicalHBaseTableName(tableName, isNamespaceMapped, PTableType.TABLE).getBytes()))
+                    + Bytes.toString(MetaDataUtil.getViewIndexPhysicalName(
+                        SchemaUtil.getPhysicalTableName(Bytes.toBytes(tableName), isNamespaceMapped).toBytes()))
                     + " [-32768,'" + tenantId + "','f']\n" + "    SERVER FILTER BY FIRST
KEY ONLY",
                     QueryUtil.getExplainPlan(rs));
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 49fdba6..8cba241 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
@@ -58,13 +58,13 @@ import org.apache.phoenix.exception.UpgradeInProgressException;
 import org.apache.phoenix.exception.UpgradeRequiredException;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
+import org.apache.phoenix.parse.PFunction;
 import org.apache.phoenix.query.ConnectionQueryServices;
 import org.apache.phoenix.query.ConnectionQueryServicesImpl;
 import org.apache.phoenix.query.DelegateConnectionQueryServices;
 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.schema.*;
 import org.apache.phoenix.util.MetaDataUtil;
 import org.apache.phoenix.util.PhoenixRuntime;
 import org.apache.phoenix.util.SchemaUtil;
@@ -228,6 +228,16 @@ public class UpgradeIT extends ParallelStatsDisabledIT {
             phxConn.close();
             props = new Properties();
             phxConn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
+            // purge MetaDataCache except for system tables
+            phxConn.getMetaDataCache().pruneTables(new PMetaData.Pruner() {
+                @Override public boolean prune(PTable table) {
+                    return table.getType() != PTableType.SYSTEM;
+                }
+
+                @Override public boolean prune(PFunction function) {
+                    return false;
+                }
+            });
             admin = phxConn.getQueryServices().getAdmin();
             String hbaseTableName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(phoenixFullTableName),
true)
                     .getNameAsString();
@@ -351,6 +361,16 @@ public class UpgradeIT extends ParallelStatsDisabledIT {
         UpgradeUtil.mapChildViewsToNamespace(phxConn,phoenixFullTableName,props);
         props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tenantId);
         phxConn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
+        // purge MetaDataCache except for system tables
+        phxConn.getMetaDataCache().pruneTables(new PMetaData.Pruner() {
+            @Override public boolean prune(PTable table) {
+                return table.getType() != PTableType.SYSTEM;
+            }
+
+            @Override public boolean prune(PFunction function) {
+                return false;
+            }
+        });
         int i = 1;
         String indexPhysicalTableName = Bytes
                 .toString(MetaDataUtil.getViewIndexPhysicalName(Bytes.toBytes(hbaseTableName)));

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 947b035..267dbc9 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
@@ -36,6 +36,9 @@ import java.sql.SQLException;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.client.HBaseAdmin;
 import org.apache.phoenix.compile.QueryPlan;
 import org.apache.phoenix.exception.SQLExceptionCode;
@@ -710,6 +713,75 @@ public class ViewIT extends BaseViewIT {
       assertTrue(rs.next());
     }
 
+    @Test
+    public void testCreateViewMappedToExistingHbaseTableWithNamespaceMappingEnabled() throws
Exception {
+        final String NS = "NS_" + generateUniqueName();
+        final String TBL = "TBL_" + generateUniqueName();
+        final String CF = "CF";
+
+        Properties props = new Properties();
+        props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.TRUE.toString());
+
+        try (Connection conn = DriverManager.getConnection(getUrl(), props);
+            HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin())
{
+
+            conn.createStatement().execute("CREATE SCHEMA " + NS);
+
+            // test for a view that is in non-default schema
+            {
+                HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(NS, TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
+
+                String view = NS + "." + TBL;
+                conn.createStatement().execute(
+                    "CREATE VIEW " + view + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + view))
+                    .contains(NS + ":" + TBL));
+
+                conn.createStatement().execute("DROP VIEW " + view);
+            }
+
+            // test for a view whose name contains a dot (e.g. "AAA.BBB") in default schema
(for backward compatibility)
+            {
+                HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(NS + "." +
TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
+
+                String view = "\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                    "CREATE VIEW " + view + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + view))
+                    .contains(NS + "." + TBL));
+
+                conn.createStatement().execute("DROP VIEW " + view);
+            }
+
+            // test for a view whose name contains a dot (e.g. "AAA.BBB") in non-default
schema
+            {
+                HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(NS, NS + "."
+ TBL));
+                desc.addFamily(new HColumnDescriptor(CF));
+                admin.createTable(desc);
+
+                String view = NS + ".\"" + NS + "." + TBL + "\"";
+                conn.createStatement().execute(
+                    "CREATE VIEW " + view + " (PK VARCHAR PRIMARY KEY, " + CF + ".COL VARCHAR)");
+
+                assertTrue(QueryUtil.getExplainPlan(
+                    conn.createStatement().executeQuery("explain select * from " + view))
+                    .contains(NS + ":" + NS + "." + TBL));
+
+                conn.createStatement().execute("DROP VIEW " + view);
+            }
+
+            conn.createStatement().execute("DROP SCHEMA " + NS);
+        }
+    }
+
     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/b0109feb/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 25cb632..e8af820 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
@@ -187,7 +187,7 @@ public class ViewIndexIT extends ParallelStatsDisabledIT {
         String sql = "SELECT * FROM " + viewName + " WHERE v2 = 100";
         ResultSet rs = conn1.prepareStatement("EXPLAIN " + sql).executeQuery();
         assertEquals(
-                "CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + SchemaUtil.getPhysicalHBaseTableName(fullTableName,
isNamespaceMapped, PTableType.TABLE) + " [1,'10',100]\n" +
+                "CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + SchemaUtil.getPhysicalTableName(Bytes.toBytes(fullTableName),
isNamespaceMapped) + " [1,'10',100]\n" +
                 "    SERVER FILTER BY FIRST KEY ONLY\n" +
                 "CLIENT MERGE SORT", QueryUtil.getExplainPlan(rs));
         rs = conn1.prepareStatement(sql).executeQuery();
@@ -362,7 +362,7 @@ public class ViewIndexIT extends ParallelStatsDisabledIT {
             tsConn.createStatement().execute("UPSERT INTO " + tsViewFullName + "(INT1,DOUBLE1,IS_BOOLEAN,TEXT1)
VALUES (10,10.0, true, 'j')");
             tsConn.commit();
             
-            String basePhysicalName = SchemaUtil.getPhysicalHBaseTableName(baseFullName,
isNamespaceMapped, PTableType.TABLE).getString();
+            String basePhysicalName = SchemaUtil.getPhysicalTableName(Bytes.toBytes(baseFullName),
isNamespaceMapped).toString();
             assertRowCount(tsConn, tsViewFullName, basePhysicalName, 10);
             
             tsConn.createStatement().execute("DELETE FROM " + tsViewFullName + " WHERE TEXT1='d'");
@@ -413,4 +413,4 @@ public class ViewIndexIT extends ParallelStatsDisabledIT {
             conn.close();
         }
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 56dfc90..f88b34b 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
@@ -184,9 +184,9 @@ public class FromCompiler {
             // A tenant-specific connection may not create a mapped VIEW.
             if (connection.getTenantId() == null && statement.getTableType() == PTableType.VIEW)
{
                 ConnectionQueryServices services = connection.getQueryServices();
-                byte[] fullTableName = SchemaUtil.getPhysicalName(
-                        SchemaUtil.getTableNameAsBytes(baseTable.getSchemaName(), baseTable.getTableName()),
-                        connection.getQueryServices().getProps()).getName();
+                boolean isNamespaceMapped = SchemaUtil.isNamespaceMappingEnabled(statement.getTableType(),
connection.getQueryServices().getProps());
+                byte[] fullTableName = SchemaUtil.getPhysicalHBaseTableName(
+                    baseTable.getSchemaName(), baseTable.getTableName(), isNamespaceMapped).getBytes();
                 HTableInterface htable = null;
                 try {
                     htable = services.getTable(fullTableName);
@@ -196,7 +196,7 @@ public class FromCompiler {
                     if (htable != null) Closeables.closeQuietly(htable);
                 }
                 tableNode = NamedTableNode.create(null, baseTable, statement.getColumnDefs());
-                return new SingleTableColumnResolver(connection, tableNode, e.getTimeStamp(),
new HashMap<String, UDFParseNode>(1), false);
+                return new SingleTableColumnResolver(connection, tableNode, e.getTimeStamp(),
new HashMap<String, UDFParseNode>(1), isNamespaceMapped);
             }
             throw e;
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/main/java/org/apache/phoenix/compile/TupleProjectionCompiler.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/TupleProjectionCompiler.java
b/phoenix-core/src/main/java/org/apache/phoenix/compile/TupleProjectionCompiler.java
index e411cdf..4ebca90 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/compile/TupleProjectionCompiler.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/TupleProjectionCompiler.java
@@ -152,7 +152,7 @@ public class TupleProjectionCompiler {
         return PTableImpl.makePTable(table.getTenantId(), table.getSchemaName(), table.getTableName(),
                 PTableType.PROJECTED, table.getIndexState(), table.getTimeStamp(), table.getSequenceNumber(),
                 table.getPKName(), table.getBucketNum(), projectedColumns, table.getParentSchemaName(),
-                table.getParentName(), table.getIndexes(), table.isImmutableRows(), Collections.<PName>
emptyList(),
+                table.getParentTableName(), table.getIndexes(), table.isImmutableRows(),
Collections.<PName> emptyList(),
                 null, null, table.isWALDisabled(), table.isMultiTenant(), table.getStoreNulls(),
table.getViewType(),
                 table.getViewIndexId(),
                 table.getIndexType(), table.rowKeyOrderOptimizable(), table.isTransactional(),
table.getUpdateCacheFrequency(), 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
index eba2e9d..537125a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
@@ -1691,8 +1691,8 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements
Coprocesso
         tableTypeFilter.setFilterIfMissing(false);
         linkFilter.setFilterIfMissing(true);
         byte[] suffix = ByteUtil.concat(QueryConstants.SEPARATOR_BYTE_ARRAY, SchemaUtil
-                .getPhysicalTableName(SchemaUtil.getTableNameAsBytes(schemaName, tableName),
table.isNamespaceMapped())
-                .getName());
+                .getPhysicalHBaseTableName(schemaName, tableName, table.isNamespaceMapped())
+                .getBytes());
         SuffixFilter rowFilter = new SuffixFilter(suffix);
         FilterList filter = new FilterList(linkFilter,tableTypeFilter,rowFilter);
         scan.setFilter(filter);
@@ -3433,8 +3433,8 @@ public class MetaDataEndpointImpl extends MetaDataProtocol implements
Coprocesso
             logger.error("Old client is not compatible when" + " system tables are upgraded
to map to namespace");
             ProtobufUtil.setControllerException(controller,
                     ServerUtil.createIOException(
-                            SchemaUtil.getPhysicalHBaseTableName(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME,
-                                    isTablesMappingEnabled, PTableType.SYSTEM).getString(),
+                            SchemaUtil.getPhysicalTableName(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES,
+                                    isTablesMappingEnabled).toString(),
                     new DoNotRetryIOException(
                             "Old client is not compatible when" + " system tables are upgraded
to map to namespace")));
         }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 a7f95a3..3e8eb07 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
@@ -748,13 +748,12 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
         }
     }
 
-    private HTableDescriptor generateTableDescriptor(byte[] tableName, HTableDescriptor existingDesc,
+    private HTableDescriptor generateTableDescriptor(byte[] physicalTableName, HTableDescriptor
existingDesc,
             PTableType tableType, Map<String, Object> tableProps, List<Pair<byte[],
Map<String, Object>>> families,
             byte[][] splits, boolean isNamespaceMapped) throws SQLException {
         String defaultFamilyName = (String)tableProps.remove(PhoenixDatabaseMetaData.DEFAULT_COLUMN_FAMILY_NAME);
         HTableDescriptor tableDescriptor = (existingDesc != null) ? new HTableDescriptor(existingDesc)
-        : new HTableDescriptor(
-                SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, tableType).getBytes());
+        : new HTableDescriptor(physicalTableName);
         // By default, do not automatically rebuild/catch up an index on a write failure
         for (Entry<String,Object> entry : tableProps.entrySet()) {
             String key = entry.getKey();
@@ -776,7 +775,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                 byte[] familyByte = family.getFirst();
                 if (tableDescriptor.getFamily(familyByte) == null) {
                     if (tableType == PTableType.VIEW) {
-                        String fullTableName = Bytes.toString(tableName);
+                        String fullTableName = Bytes.toString(physicalTableName);
                         throw new ReadOnlyTableException(
                                 "The HBase column families for a read-only table must already
exist",
                                 SchemaUtil.getSchemaNameFromFullName(fullTableName),
@@ -796,7 +795,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                 }
             }
         }
-        addCoprocessors(tableName, tableDescriptor, tableType, tableProps);
+        addCoprocessors(physicalTableName, tableDescriptor, tableType, tableProps);
 
         // PHOENIX-3072: Set index priority if this is a system table or index table
         if (tableType == PTableType.SYSTEM) {
@@ -1020,24 +1019,23 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
      * @return true if table was created and false if it already exists
      * @throws SQLException
      */
-    private HTableDescriptor ensureTableCreated(byte[] tableName, PTableType tableType, Map<String,
Object> props,
+    private HTableDescriptor ensureTableCreated(byte[] physicalTableName, PTableType tableType,
Map<String, Object> props,
             List<Pair<byte[], Map<String, Object>>> families, byte[][]
splits, boolean modifyExistingMetaData,
             boolean isNamespaceMapped) throws SQLException {
         SQLException sqlE = null;
         HTableDescriptor existingDesc = null;
-        boolean isMetaTable = SchemaUtil.isMetaTable(tableName);
-        byte[] physicalTable = SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped,
tableType).getBytes();
+        boolean isMetaTable = SchemaUtil.isMetaTable(physicalTableName);
         boolean tableExist = true;
         try (HBaseAdmin admin = getAdmin()) {
             final String quorum = ZKConfig.getZKQuorumServersString(config);
             final String znode = this.props.get(HConstants.ZOOKEEPER_ZNODE_PARENT);
             logger.debug("Found quorum: " + quorum + ":" + znode);
             try {
-                existingDesc = admin.getTableDescriptor(physicalTable);
+                existingDesc = admin.getTableDescriptor(physicalTableName);
             } catch (org.apache.hadoop.hbase.TableNotFoundException e) {
                 tableExist = false;
                 if (tableType == PTableType.VIEW) {
-                    String fullTableName = Bytes.toString(tableName);
+                    String fullTableName = Bytes.toString(physicalTableName);
                     throw new ReadOnlyTableException(
                             "An HBase table for a VIEW must already exist",
                             SchemaUtil.getSchemaNameFromFullName(fullTableName),
@@ -1045,7 +1043,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                 }
             }
 
-            HTableDescriptor newDesc = generateTableDescriptor(tableName, existingDesc, tableType,
props, families,
+            HTableDescriptor newDesc = generateTableDescriptor(physicalTableName, existingDesc,
tableType, props, families,
                     splits, isNamespaceMapped);
 
             if (!tableExist) {
@@ -1076,7 +1074,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                      * brought down.
                      */
                     newDesc.setValue(HTableDescriptor.SPLIT_POLICY, MetaDataSplitPolicy.class.getName());
-                    modifyTable(physicalTable, newDesc, true);
+                    modifyTable(physicalTableName, newDesc, true);
                 }
                 return null;
             } else {
@@ -1108,8 +1106,8 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                     // transactional, don't allow.
                     if (existingDesc.hasCoprocessor(PhoenixTransactionalProcessor.class.getName()))
{
                         throw new SQLExceptionInfo.Builder(SQLExceptionCode.TX_MAY_NOT_SWITCH_TO_NON_TX)
-                        .setSchemaName(SchemaUtil.getSchemaNameFromFullName(tableName))
-                        .setTableName(SchemaUtil.getTableNameFromFullName(tableName)).build().buildException();
+                        .setSchemaName(SchemaUtil.getSchemaNameFromFullName(physicalTableName))
+                        .setTableName(SchemaUtil.getTableNameFromFullName(physicalTableName)).build().buildException();
                     }
                     newDesc.remove(PhoenixTransactionContext.READ_NON_TX_DATA);
                 }
@@ -1117,7 +1115,7 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                     return null; // Indicate that no metadata was changed
                 }
 
-                modifyTable(physicalTable, newDesc, true);
+                modifyTable(physicalTableName, newDesc, true);
                 return newDesc;
             }
 
@@ -1392,7 +1390,8 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
         byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
         byte[] schemaBytes = rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
         byte[] tableBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
-        byte[] tableName = physicalTableName != null ? physicalTableName : SchemaUtil.getTableNameAsBytes(schemaBytes,
tableBytes);
+        byte[] tableName = physicalTableName != null ? physicalTableName :
+            SchemaUtil.getPhysicalHBaseTableName(schemaBytes, tableBytes, isNamespaceMapped).getBytes();
         boolean localIndexTable = false;
         for(Pair<byte[], Map<String, Object>> family: families) {
             if(Bytes.toString(family.getFirst()).startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX))
{
@@ -1435,9 +1434,8 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices
implement
                 familiesPlusDefault.add(new Pair<byte[],Map<String,Object>>(defaultCF,Collections.<String,Object>emptyMap()));
             }
             ensureViewIndexTableCreated(
-                    SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, tableType).getBytes(),
-                    tableProps, familiesPlusDefault, MetaDataUtil.isSalted(m, kvBuilder,
ptr) ? splits : null,
-                            MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
+                tableName, tableProps, familiesPlusDefault, MetaDataUtil.isSalted(m, kvBuilder,
ptr) ? splits : null,
+                MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
         }
 
         byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 c982e26..69cd814 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
@@ -2135,7 +2135,9 @@ public class MetaDataClient {
                 // Upsert physical name for mapped view only if the full physical table name
is different than the full table name
                 // Otherwise, we end up with a self-referencing link and then cannot ever
drop the view.
                 if (viewType != ViewType.MAPPED
-                        || !physicalNames.get(0).getString().equals(SchemaUtil.getTableName(schemaName,
tableName))) {
+                    || (!physicalNames.get(0).getString().equals(SchemaUtil.getTableName(schemaName,
tableName))
+                    && !physicalNames.get(0).getString().equals(SchemaUtil.getPhysicalHBaseTableName(
+                        schemaName, tableName, isNamespaceMapped).getString()))) {
                     // Add row linking from data table row to physical table row
                     PreparedStatement linkStatement = connection.prepareStatement(CREATE_LINK);
                     for (PName physicalName : physicalNames) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
index 20e8611..ae91d17 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
@@ -1184,8 +1184,11 @@ public class PTableImpl implements PTable {
 
     @Override
     public PName getPhysicalName() {
-        return SchemaUtil.getPhysicalHBaseTableName(physicalNames.isEmpty() ? getName() :
physicalNames.get(0),
-                isNamespaceMapped, type);
+        if (physicalNames.isEmpty()) {
+            return SchemaUtil.getPhysicalHBaseTableName(schemaName, tableName, isNamespaceMapped);
+        } else {
+            return PNameFactory.newName(physicalNames.get(0).getBytes());
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
index 6696cc7..d54670c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/SchemaUtil.java
@@ -983,14 +983,21 @@ public class SchemaUtil {
         return SchemaUtil.getTableKey(null, schemaName, MetaDataClient.EMPTY_TABLE);
     }
 
-    public static PName getPhysicalHBaseTableName(PName pName, boolean isNamespaceMapped,
PTableType type) {
-        return getPhysicalHBaseTableName(pName.toString(), isNamespaceMapped, type);
+    public static PName getPhysicalHBaseTableName(PName schemaName, PName tableName, boolean
isNamespaceMapped) {
+        return getPhysicalHBaseTableName(
+            schemaName == null ? null : schemaName.toString(), tableName.toString(), isNamespaceMapped);
     }
 
-    public static PName getPhysicalHBaseTableName(byte[] tableName, boolean isNamespaceMapped,
PTableType type) {
-        return getPhysicalHBaseTableName(Bytes.toString(tableName), isNamespaceMapped, type);
+    public static PName getPhysicalHBaseTableName(byte[] schemaName, byte[] tableName, boolean
isNamespaceMapped) {
+        return getPhysicalHBaseTableName(Bytes.toString(schemaName), Bytes.toString(tableName),
isNamespaceMapped);
     }
 
+    /**
+     * Note: the following 4 methods (getPhysicalTableName, getPhysicalName) return an unexpected
value
+     * when fullTableName is in default schema and fullTableName contains a dot. For example,
+     * if fullTableName is in default schema and fullTableName is "AAA.BBB", the expected
hbase table
+     * name is "AAA.BBB" but these methods return "AAA:BBB".
+     */
     public static TableName getPhysicalTableName(String fullTableName, ReadOnlyProps readOnlyProps)
{
         return getPhysicalName(Bytes.toBytes(fullTableName), readOnlyProps);
     }
@@ -1013,10 +1020,10 @@ public class SchemaUtil {
         return TableName.valueOf(schemaName, tableName);
     }
 
-    public static PName getPhysicalHBaseTableName(String tableName, boolean isNamespaceMapped,
PTableType type) {
-        if (!isNamespaceMapped) { return PNameFactory.newName(tableName); }
-        return PNameFactory
-                .newName(tableName.replace(QueryConstants.NAME_SEPARATOR, QueryConstants.NAMESPACE_SEPARATOR));
+    public static PName getPhysicalHBaseTableName(String schemaName, String tableName, boolean
isNamespaceMapped) {
+        if (!isNamespaceMapped) { return PNameFactory.newName(getTableNameAsBytes(schemaName,
tableName)); }
+        if (schemaName == null || schemaName.isEmpty()) { return PNameFactory.newName(tableName);
}
+        return PNameFactory.newName(schemaName + QueryConstants.NAMESPACE_SEPARATOR + tableName);
     }
 
     public static boolean isSchemaCheckRequired(PTableType tableType, ReadOnlyProps props)
{

http://git-wip-us.apache.org/repos/asf/phoenix/blob/b0109feb/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 d402c38..c06912d 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
@@ -1895,6 +1895,11 @@ public class UpgradeUtil {
             if (table.getType() == PTableType.VIEW) {
                 updateLink(conn, oldPhysicalName, newPhysicalTablename,table.getSchemaName(),table.getTableName());
                 conn.commit();
+
+                conn.getQueryServices().clearTableFromCache(
+                    conn.getTenantId() == null ? ByteUtil.EMPTY_BYTE_ARRAY : conn.getTenantId().getBytes(),
+                    table.getSchemaName().getBytes(), table.getTableName().getBytes(),
+                    PhoenixRuntime.getCurrentScn(readOnlyProps));
             }
         }
     }
@@ -1985,4 +1990,4 @@ public class UpgradeUtil {
     public static void doNotUpgradeOnFirstConnection(Properties props) {
         props.setProperty(DO_NOT_UPGRADE, String.valueOf(true));
     }
-}
\ No newline at end of file
+}


Mime
View raw message