-
Notifications
You must be signed in to change notification settings - Fork 4.8k
[WIP] HIVE-29668: Add -rebuildIndexes utility to reconstruct backend Metastore indexes #6545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
soumyakanti3578
wants to merge
1
commit into
apache:master
Choose a base branch
from
soumyakanti3578:HIVE-29668
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...c/main/java/org/apache/hadoop/hive/metastore/tools/schematool/AbstractIndexRebuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hive.metastore.tools.schematool; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.hadoop.hive.metastore.HiveMetaException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Shared duplicate-check and DDL execution logic for {@link IndexRebuilder}. */ | ||
| public abstract class AbstractIndexRebuilder implements IndexRebuilder { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(AbstractIndexRebuilder.class); | ||
|
|
||
| protected final Connection conn; | ||
| protected final boolean needsQuotedIdentifier; | ||
| protected final String quoteCharacter; | ||
|
|
||
| protected AbstractIndexRebuilder(Connection conn, boolean needsQuotedIdentifier, | ||
| String quoteCharacter) { | ||
| this.conn = conn; | ||
| this.needsQuotedIdentifier = needsQuotedIdentifier; | ||
| this.quoteCharacter = quoteCharacter; | ||
| } | ||
|
|
||
| @Override | ||
| public long findDuplicates(IndexInfo index) throws HiveMetaException { | ||
| if (!index.unique()) { | ||
| return 0; | ||
| } | ||
| String quotedCols = index.columns().stream() | ||
| .map(c -> "<qa>" + c + "<qa>") | ||
| .collect(Collectors.joining(", ")); | ||
| String sql = MetastoreSchemaTool.quote( | ||
| "SELECT COUNT(*) FROM (SELECT " + quotedCols | ||
| + " FROM <qa>" + index.tableName() + "<qa>" | ||
| + " GROUP BY " + quotedCols | ||
| + " HAVING COUNT(*) > 1) duplicates", | ||
| needsQuotedIdentifier, quoteCharacter); | ||
| try (PreparedStatement ps = conn.prepareStatement(sql); | ||
| ResultSet rs = ps.executeQuery()) { | ||
| return rs.next() ? rs.getLong(1) : 0; | ||
| } catch (SQLException e) { | ||
| throw new HiveMetaException( | ||
| "Failed to check for duplicate rows for index \"" + index.indexName() + "\"", e); | ||
| } | ||
| } | ||
|
|
||
| /** Executes one or more DDL statements, logging each before execution. */ | ||
| protected void executeRebuild(IndexInfo index, String... ddls) throws HiveMetaException { | ||
| try (Statement stmt = conn.createStatement()) { | ||
| for (String ddl : ddls) { | ||
| LOG.info("Executing: {}", ddl); | ||
| stmt.execute(ddl); | ||
| } | ||
| } catch (SQLException e) { | ||
| throw new HiveMetaException("Failed to rebuild index \"" + index.indexName() + "\"", e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/IndexInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hive.metastore.tools.schematool; | ||
|
|
||
| import java.util.List; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** | ||
| * Record for a DB index, used by {@link IndexRebuilder}. | ||
| */ | ||
| public record IndexInfo( | ||
| String indexName, | ||
| String tableName, | ||
| boolean unique, | ||
| boolean constraintBacked, | ||
| List<String> columns) { | ||
|
|
||
| public IndexInfo { | ||
| columns = List.copyOf(columns); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String toString() { | ||
| return (constraintBacked ? "CONSTRAINT" : "INDEX") | ||
| + " \"" + indexName + "\" ON \"" + tableName + "\""; | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
...erver/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/IndexRebuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hive.metastore.tools.schematool; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.hadoop.hive.metastore.HiveMetaException; | ||
|
|
||
| /** | ||
| * Interface for rebuilding indexes in the HMS backend database. | ||
| * Implementations should extend {@link AbstractIndexRebuilder}. | ||
| */ | ||
| public interface IndexRebuilder { | ||
|
|
||
| /** Returns all B-tree indexes in the current HMS schema. */ | ||
| List<IndexInfo> loadIndexes() throws HiveMetaException; | ||
|
|
||
| /** | ||
| * Returns duplicate key-group count for the index. Rebuild must be blocked when this is | ||
| * greater than zero. Always returns zero for non-unique indexes. | ||
| */ | ||
| long findDuplicates(IndexInfo index) throws HiveMetaException; | ||
|
|
||
| void rebuildIndex(IndexInfo index) throws HiveMetaException; | ||
|
|
||
| /** Returns a description of the DDL that would be executed to rebuild the index. */ | ||
| String describeRebuildDDL(IndexInfo index); | ||
| } |
48 changes: 48 additions & 0 deletions
48
...rc/main/java/org/apache/hadoop/hive/metastore/tools/schematool/IndexRebuilderFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hive.metastore.tools.schematool; | ||
|
|
||
| import java.sql.Connection; | ||
|
|
||
| import org.apache.hadoop.hive.metastore.HiveMetaException; | ||
|
|
||
| /** | ||
| * Factory for creating {@link IndexRebuilder} instances for the given database type. | ||
| * Add support for a new backend by adding a {@code case} branch. | ||
| */ | ||
| public final class IndexRebuilderFactory { | ||
|
|
||
| private IndexRebuilderFactory() { | ||
| } | ||
|
|
||
| public static IndexRebuilder create(String dbType, Connection conn, | ||
| MetastoreSchemaTool schemaTool) throws HiveMetaException { | ||
| return switch (dbType.toLowerCase()) { | ||
| case HiveSchemaHelper.DB_POSTGRES -> | ||
| new PostgresIndexRebuilder(conn, schemaTool.needsQuotedIdentifier, schemaTool.quoteCharacter); | ||
| case HiveSchemaHelper.DB_MYSQL -> | ||
| new MySQLIndexRebuilder(conn, schemaTool.needsQuotedIdentifier, schemaTool.quoteCharacter); | ||
| case HiveSchemaHelper.DB_ORACLE -> | ||
| new OracleIndexRebuilder(conn, schemaTool.needsQuotedIdentifier, schemaTool.quoteCharacter); | ||
| case HiveSchemaHelper.DB_MSSQL -> | ||
| new MSSQLIndexRebuilder(conn, schemaTool.needsQuotedIdentifier, schemaTool.quoteCharacter); | ||
| default -> throw new HiveMetaException( | ||
| "-rebuildIndexes is not supported for -dbType " + dbType + "."); | ||
| }; | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
.../src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/MSSQLIndexRebuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.hive.metastore.tools.schematool; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.hadoop.hive.metastore.HiveMetaException; | ||
|
|
||
| /** | ||
| * SQL Server implementation of {@link IndexRebuilder}. | ||
| * | ||
| * <p>Uses {@code sys.indexes} / {@code sys.index_columns} / {@code sys.columns} and rebuilds | ||
| * with {@code ALTER INDEX name ON table REBUILD}. | ||
| */ | ||
| class MSSQLIndexRebuilder extends AbstractIndexRebuilder { | ||
|
|
||
| // i.type: 1 = clustered, 2 = nonclustered. | ||
| // i.name IS NOT NULL excludes heap pseudo-entries (type 0). | ||
| private static final String QUERY_INDEXES = """ | ||
| SELECT i.name AS index_name, | ||
| t.name AS table_name, | ||
| i.is_unique | ||
| FROM sys.indexes i | ||
| JOIN sys.tables t ON t.object_id = i.object_id | ||
| WHERE t.is_ms_shipped = 0 | ||
| AND i.type IN (1, 2) | ||
| AND i.name IS NOT NULL | ||
| ORDER BY t.name, i.name"""; | ||
|
|
||
| // INCLUDE columns are not key columns; exclude them. | ||
| private static final String QUERY_INDEX_COLUMNS = """ | ||
| SELECT c.name AS column_name | ||
| FROM sys.indexes i | ||
| JOIN sys.tables t ON t.object_id = i.object_id | ||
| JOIN sys.index_columns ic ON ic.object_id = i.object_id | ||
| AND ic.index_id = i.index_id | ||
| JOIN sys.columns c ON c.object_id = ic.object_id | ||
| AND c.column_id = ic.column_id | ||
| WHERE t.name = ? AND i.name = ? | ||
| AND ic.is_included_column = 0 | ||
| ORDER BY ic.key_ordinal"""; | ||
|
|
||
| MSSQLIndexRebuilder(Connection conn, boolean needsQuotedIdentifier, String quoteCharacter) { | ||
| super(conn, needsQuotedIdentifier, quoteCharacter); | ||
| } | ||
|
|
||
| @Override | ||
| public List<IndexInfo> loadIndexes() throws HiveMetaException { | ||
| try (Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery(QUERY_INDEXES)) { | ||
| List<IndexInfo> indexes = new ArrayList<>(); | ||
| while (rs.next()) { | ||
| String indexName = rs.getString("index_name"); | ||
| String tableName = rs.getString("table_name"); | ||
| boolean isUnique = rs.getBoolean("is_unique"); | ||
| List<String> columns = loadIndexColumns(tableName, indexName); | ||
| // constraintBacked is irrelevant for MSSQL: ALTER INDEX REBUILD works identically | ||
| // for all index types | ||
| indexes.add(new IndexInfo(indexName, tableName, isUnique, false, columns)); | ||
| } | ||
| return indexes; | ||
| } catch (SQLException e) { | ||
| throw new HiveMetaException("Failed to load indexes from SQL Server catalog", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void rebuildIndex(IndexInfo index) throws HiveMetaException { | ||
| executeRebuild(index, buildRebuildDdl(index)); | ||
| } | ||
|
|
||
| @Override | ||
| public String describeRebuildDDL(IndexInfo index) { | ||
| return buildRebuildDdl(index) + ";"; | ||
| } | ||
|
|
||
| private String buildRebuildDdl(IndexInfo index) { | ||
| // MSSQL requires the table name because index names are unique per-table, not per-schema. | ||
| return MetastoreSchemaTool.quote( | ||
| "ALTER INDEX <qa>" + index.indexName() + "<qa>" | ||
| + " ON <qa>" + index.tableName() + "<qa> REBUILD", | ||
| needsQuotedIdentifier, quoteCharacter); | ||
| } | ||
|
|
||
| private List<String> loadIndexColumns(String tableName, String indexName) throws SQLException { | ||
| List<String> columns = new ArrayList<>(); | ||
| try (PreparedStatement ps = conn.prepareStatement(QUERY_INDEX_COLUMNS)) { | ||
| ps.setString(1, tableName); | ||
| ps.setString(2, indexName); | ||
| try (ResultSet rs = ps.executeQuery()) { | ||
| while (rs.next()) { | ||
| columns.add(rs.getString("column_name")); | ||
| } | ||
| } | ||
| } | ||
| return columns; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need @NotNull annotation here ? I think, record will never return null.