Github user JamesRTaylor commented on a diff in the pull request: https://github.com/apache/phoenix/pull/168#discussion_r64124199 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/util/UpgradeUtil.java --- @@ -292,6 +294,119 @@ private static void preSplitSequenceTable(PhoenixConnection conn, int nSaltBucke } } } + + public static void upgradeLocalIndexes(PhoenixConnection metaConnection) throws SQLException, + IOException, org.apache.hadoop.hbase.TableNotFoundException { + HBaseAdmin admin = null; + try { + admin = metaConnection.getQueryServices().getAdmin(); + ResultSet rs = metaConnection.createStatement().executeQuery("SELECT TABLE_SCHEM, TABLE_NAME, DATA_TABLE_NAME FROM SYSTEM.CATALOG " + + " WHERE COLUMN_NAME IS NULL" + + " AND COLUMN_FAMILY IS NULL" + + " AND INDEX_TYPE=2"); + boolean droppedLocalIndexes = false; + while (rs.next()) { + if(!droppedLocalIndexes) { + HTableDescriptor[] localIndexTables = admin.listTables(MetaDataUtil.LOCAL_INDEX_TABLE_PREFIX+".*"); + String localIndexSplitter = LocalIndexSplitter.class.getName(); + for (HTableDescriptor table : localIndexTables) { + HTableDescriptor dataTableDesc = admin.getTableDescriptor(TableName.valueOf(MetaDataUtil.getUserTableName(table.getNameAsString()))); + HColumnDescriptor[] columnFamilies = dataTableDesc.getColumnFamilies(); + boolean modifyTable = false; + for(HColumnDescriptor cf : columnFamilies) { + String localIndexCf = QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX+cf.getNameAsString(); + if(dataTableDesc.getFamily(Bytes.toBytes(localIndexCf))==null){ + HColumnDescriptor colDef = + new HColumnDescriptor(localIndexCf); + for(Entry<ImmutableBytesWritable, ImmutableBytesWritable>keyValue: cf.getValues().entrySet()){ + colDef.setValue(keyValue.getKey().copyBytes(), keyValue.getValue().copyBytes()); + } + dataTableDesc.addFamily(colDef); + modifyTable = true; + } + } + List<String> coprocessors = dataTableDesc.getCoprocessors(); + for(String coprocessor: coprocessors) { + if(coprocessor.equals(localIndexSplitter)) { + dataTableDesc.removeCoprocessor(localIndexSplitter); + modifyTable = true; + } + } + if(modifyTable) { + admin.modifyTable(dataTableDesc.getName(), dataTableDesc); + } + } + admin.disableTables(MetaDataUtil.LOCAL_INDEX_TABLE_PREFIX+".*"); + admin.deleteTables(MetaDataUtil.LOCAL_INDEX_TABLE_PREFIX+".*"); + droppedLocalIndexes = true; + } + String getColumns = + "SELECT COLUMN_NAME, COLUMN_FAMILY FROM SYSTEM.CATALOG WHERE TABLE_SCHEM " + + (rs.getString(1) == null ? "IS NULL " : "='" + rs.getString(1) + + "'") + " and TABLE_NAME='" + rs.getString(2) + + "' AND COLUMN_NAME IS NOT NULL"; + ResultSet getColumnsRs = metaConnection.createStatement().executeQuery(getColumns); + List<String> indexedColumns = new ArrayList<String>(1); + List<String> coveredColumns = new ArrayList<String>(1); + + while (getColumnsRs.next()) { + String column = getColumnsRs.getString(1); + String columnName = IndexUtil.getDataColumnName(column); + if (columnName.equals(MetaDataUtil.getViewIndexIdColumnName())) { + continue; + } + String columnFamily = IndexUtil.getDataColumnFamilyName(column); + if (getColumnsRs.getString(2) == null) { + if (columnFamily != null && !columnFamily.isEmpty()) { + if (columnFamily.equals(QueryConstants.DEFAULT_COLUMN_FAMILY)) { + indexedColumns.add(columnName); + } else { + indexedColumns.add(SchemaUtil.getColumnName(columnFamily, + columnName)); + } + } + } else { + coveredColumns.add(SchemaUtil.getColumnName(columnFamily, columnName)); + } + } + StringBuilder createIndex = new StringBuilder("CREATE LOCAL INDEX "); + createIndex.append(rs.getString(2)); + createIndex.append(" ON "); + createIndex.append(SchemaUtil.getTableName(rs.getString(1), rs.getString(3))); + createIndex.append("("); + for (int i = 0; i < indexedColumns.size(); i++) { + createIndex.append(indexedColumns.get(i)); + if (i < indexedColumns.size() - 1) { + createIndex.append(","); + } + } + createIndex.append(")"); + + if (!coveredColumns.isEmpty()) { + createIndex.append(" INCLUDE("); + for (int i = 0; i < coveredColumns.size(); i++) { + createIndex.append(coveredColumns.get(i)); + if (i < coveredColumns.size() - 1) { + createIndex.append(","); + } + } + createIndex.append(")"); --- End diff -- Can we just add the ASYNC keyword here so that the rebuilds are asynchronous, can be started when the user is ready, and will run through MR?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---