Repository: phoenix Updated Branches: refs/heads/4.0 af1bd4e9c -> 64a2af142
PHOENIX-1038 Dynamically add INDEX_TYPE column to SYSTEM.CATALOG if not already there Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/20c37e33 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/20c37e33 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/20c37e33 Branch: refs/heads/4.0 Commit: 20c37e33ffa61b2d39ee79e762cfbc1bfcb86e60 Parents: aa30b14 Author: James Taylor <jtay...@salesforce.com> Authored: Thu Jul 17 12:43:20 2014 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Fri Jul 18 00:06:37 2014 -0700 ---------------------------------------------------------------------- .../apache/phoenix/end2end/AlterTableIT.java | 23 ++++++++++++++++++++ phoenix-core/src/main/antlr3/PhoenixSQL.g | 2 +- .../phoenix/jdbc/PhoenixDatabaseMetaData.java | 2 +- .../query/ConnectionQueryServicesImpl.java | 8 +++++-- .../apache/phoenix/query/QueryConstants.java | 2 +- 5 files changed, 32 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/20c37e33/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java index cdf405b..495b2ad 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterTableIT.java @@ -39,12 +39,15 @@ import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.client.HTableInterface; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.phoenix.coprocessor.MetaDataProtocol; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.jdbc.PhoenixConnection; +import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData; import org.apache.phoenix.query.QueryConstants; import org.apache.phoenix.schema.PTableKey; import org.apache.phoenix.schema.TableNotFoundException; import org.apache.phoenix.util.IndexUtil; +import org.apache.phoenix.util.PhoenixRuntime; import org.apache.phoenix.util.SchemaUtil; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -83,6 +86,26 @@ public class AlterTableIT extends BaseHBaseManagedTimeIT { } @Test + public void testAddColsIntoSystemTable() throws Exception { + Properties props = new Properties(TEST_PROPERTIES); + props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP + 1)); + Connection conn = DriverManager.getConnection(getUrl(), props); + + try{ + conn.createStatement().executeUpdate("ALTER TABLE " + PhoenixDatabaseMetaData.SYSTEM_CATALOG + + " ADD IF NOT EXISTS testNewColumn integer"); + String query = "SELECT testNewColumn FROM " + PhoenixDatabaseMetaData.SYSTEM_CATALOG; + try { + conn.createStatement().executeQuery(query); + } catch(SQLException e) { + assertFalse("testNewColumn wasn't created successfully:" + e, true); + } + } finally { + conn.close(); + } + } + + @Test public void testAddVarCharColToPK() throws Exception { Properties props = new Properties(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(getUrl(), props); http://git-wip-us.apache.org/repos/asf/phoenix/blob/20c37e33/phoenix-core/src/main/antlr3/PhoenixSQL.g ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g index 5f52da4..e5156fd 100644 --- a/phoenix-core/src/main/antlr3/PhoenixSQL.g +++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g @@ -483,7 +483,7 @@ alter_index_node returns [AlterIndexStatement ret] alter_table_node returns [AlterTableStatement ret] : ALTER (TABLE | v=VIEW) t=from_table_name ( (DROP COLUMN (IF ex=EXISTS)? c=column_names) | (ADD (IF NOT ex=EXISTS)? (d=column_defs) (p=properties)?) | (SET (p=properties)) ) - { PTableType tt = v==null ? PTableType.TABLE : PTableType.VIEW; ret = ( c == null ? factory.addColumn(factory.namedTable(null,t), tt, d, ex!=null, p) : factory.dropColumn(factory.namedTable(null,t), tt, c, ex!=null) ); } + { PTableType tt = v==null ? (QueryConstants.SYSTEM_SCHEMA_NAME.equals(t.getSchemaName()) ? PTableType.SYSTEM : PTableType.TABLE) : PTableType.VIEW; ret = ( c == null ? factory.addColumn(factory.namedTable(null,t), tt, d, ex!=null, p) : factory.dropColumn(factory.namedTable(null,t), tt, c, ex!=null) ); } ; prop_name returns [String ret] http://git-wip-us.apache.org/repos/asf/phoenix/blob/20c37e33/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java index 67b6913..23d7275 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixDatabaseMetaData.java @@ -98,7 +98,7 @@ public class PhoenixDatabaseMetaData implements DatabaseMetaData, org.apache.pho public static final int SCHEMA_NAME_INDEX = 1; public static final int TENANT_ID_INDEX = 0; - public static final String SYSTEM_CATALOG_SCHEMA = "SYSTEM"; + public static final String SYSTEM_CATALOG_SCHEMA = QueryConstants.SYSTEM_SCHEMA_NAME; public static final String SYSTEM_CATALOG_TABLE = "CATALOG"; public static final String SYSTEM_CATALOG = SYSTEM_CATALOG_SCHEMA + ".\"" + SYSTEM_CATALOG_TABLE + "\""; public static final byte[] SYSTEM_CATALOG_SCHEMA_BYTES = Bytes.toBytes(SYSTEM_CATALOG_TABLE); http://git-wip-us.apache.org/repos/asf/phoenix/blob/20c37e33/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 0d9553f..20d354f 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 @@ -1484,14 +1484,14 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement } // Keeping this to use for further upgrades - protected PhoenixConnection addColumnsIfNotExists(PhoenixConnection oldMetaConnection, long timestamp, String columns) throws SQLException { + protected PhoenixConnection addColumnsIfNotExists(PhoenixConnection oldMetaConnection, String tableName, long timestamp, String columns) throws SQLException { Properties props = new Properties(oldMetaConnection.getClientInfo()); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(timestamp)); // Cannot go through DriverManager or you end up in an infinite loop because it'll call init again PhoenixConnection metaConnection = new PhoenixConnection(this, oldMetaConnection.getURL(), props, oldMetaConnection.getMetaDataCache()); SQLException sqlE = null; try { - metaConnection.createStatement().executeUpdate("ALTER TABLE " + PhoenixDatabaseMetaData.SYSTEM_CATALOG + " ADD IF NOT EXISTS " + columns ); + metaConnection.createStatement().executeUpdate("ALTER TABLE " + tableName + " ADD IF NOT EXISTS " + columns ); } catch (SQLException e) { sqlE = e; } finally { @@ -1552,6 +1552,10 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement } catch (NewerTableAlreadyExistsException ignore) { // Ignore, as this will happen if the SYSTEM.TABLE already exists at this fixed timestamp. // A TableAlreadyExistsException is not thrown, since the table only exists *after* this fixed timestamp. + } catch (TableAlreadyExistsException ignore) { + // This will occur if we have an older SYSTEM.CATALOG and we need to update it to include + // any new columns we've added. + metaConnection = addColumnsIfNotExists(metaConnection, PhoenixDatabaseMetaData.SYSTEM_CATALOG, MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP, PhoenixDatabaseMetaData.INDEX_TYPE + " " + PDataType.UNSIGNED_TINYINT.getSqlTypeName()); } try { metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_SEQUENCE_METADATA); http://git-wip-us.apache.org/repos/asf/phoenix/blob/20c37e33/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java index bf80630..455b20d 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/QueryConstants.java @@ -105,7 +105,7 @@ public interface QueryConstants { public static final long UNSET_TIMESTAMP = -1; public enum JoinType {INNER, LEFT_OUTER} - public final static String PHOENIX_SCHEMA = "system"; + public final static String SYSTEM_SCHEMA_NAME = "SYSTEM"; public final static String PHOENIX_METADATA = "table"; public final static PName SINGLE_COLUMN_NAME = PNameFactory.newNormalizedName("s");