You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2025/08/04 08:23:39 UTC

(shardingsphere) branch master updated: fix:Support Hive ALTER INDEX statement parse (#36167)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new c2a2c70ab4c fix:Support Hive ALTER INDEX statement parse (#36167)
c2a2c70ab4c is described below

commit c2a2c70ab4c41802f73461adc707ecfccfe6d6ae
Author: Claire <cl...@163.com>
AuthorDate: Mon Aug 4 16:23:33 2025 +0800

    fix:Support Hive ALTER INDEX statement parse (#36167)
    
    * support hive alter index statement
    
    * update release-notes
    
    * update code
---
 RELEASE-NOTES.md                                        |  1 +
 .../hive/src/main/antlr4/imports/hive/DDLStatement.g4   |  4 ++++
 .../shardingsphere/sql/parser/autogen/HiveStatement.g4  |  1 +
 .../visitor/statement/type/HiveDDLStatementVisitor.java | 12 ++++++++++++
 .../parser/src/main/resources/case/ddl/alter-index.xml  | 17 +++++++++++++++++
 .../main/resources/sql/supported/ddl/alter-index.xml    |  3 +++
 6 files changed, 38 insertions(+)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 2dd40ece5b9..f7b355c4cd9 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -84,6 +84,7 @@
 1. SQL Parser: Support Hive ALTER MATERIALIZED VIEW statement parse - [#36146](https://github.com/apache/shardingsphere/pull/36146)
 1. SQL Parser: Support Hive CREATE INDEX statement parse - [#36157](https://github.com/apache/shardingsphere/pull/36157)
 1. SQL Parser: Support Hive DROP INDEX statement parse - [#36166](https://github.com/apache/shardingsphere/pull/36166)
+1. SQL Parser: Support Hive ALTER INDEX statement parse - [#36167](https://github.com/apache/shardingsphere/pull/36167)
 1. SQL Parser: Support SQL Server xml methods parse - [#35911](https://github.com/apache/shardingsphere/pull/35911)
 1. SQL Parser: Support SQL Server CHANGETABLE function parse - [#35920](https://github.com/apache/shardingsphere/pull/35920)
 1. SQL Parser: Support SQL Server AI_GENERATE_EMBEDDINGS function parse - [#35922](https://github.com/apache/shardingsphere/pull/35922)
diff --git a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DDLStatement.g4 b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DDLStatement.g4
index 5aa58fb8c4a..5b8eef513e2 100644
--- a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DDLStatement.g4
+++ b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DDLStatement.g4
@@ -112,6 +112,10 @@ dropIndex
     : DROP INDEX ifExists? indexName ON tableNameWithDb
     ;
 
+alterIndex
+    : ALTER INDEX indexName ON tableNameWithDb partitionSpec? REBUILD
+    ;
+
 alterDatabaseSpecification_
     : SET DBPROPERTIES LP_ dbProperties RP_
     | SET OWNER (USER | ROLE) identifier
diff --git a/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4 b/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
index 8ad8f66e65f..5a5599f6639 100644
--- a/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
+++ b/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
@@ -43,6 +43,7 @@ execute
     | alterMaterializedView
     | createIndex
     | dropIndex
+    | alterIndex
     ) (SEMI_ EOF? | EOF)
     | EOF
     ;
diff --git a/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDDLStatementVisitor.java b/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDDLStatementVisitor.java
index 1b5bd20a2d0..d399266554b 100644
--- a/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDDLStatementVisitor.java
+++ b/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDDLStatementVisitor.java
@@ -44,6 +44,7 @@ import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DropMate
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.AlterMaterializedViewContext;
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.CreateIndexContext;
 import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DropIndexContext;
+import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.AlterIndexContext;
 import org.apache.shardingsphere.sql.parser.hive.visitor.statement.HiveStatementVisitor;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.column.ColumnDefinitionSegment;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.constraint.ConstraintDefinitionSegment;
@@ -58,6 +59,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.da
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.database.CreateDatabaseStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.index.CreateIndexStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.index.DropIndexStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.index.AlterIndexStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.table.AlterTableStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.table.CreateTableStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.database.DropDatabaseStatement;
@@ -385,4 +387,14 @@ public final class HiveDDLStatementVisitor extends HiveStatementVisitor implemen
         result.setSimpleTable((SimpleTableSegment) visit(ctx.tableNameWithDb()));
         return result;
     }
+    
+    @Override
+    public ASTNode visitAlterIndex(final AlterIndexContext ctx) {
+        AlterIndexStatement result = new AlterIndexStatement(getDatabaseType());
+        IndexNameSegment indexName = new IndexNameSegment(ctx.indexName().getStart().getStartIndex(), ctx.indexName().getStop().getStopIndex(),
+                new IdentifierValue(ctx.indexName().getText()));
+        result.setIndex(new IndexSegment(ctx.indexName().getStart().getStartIndex(), ctx.indexName().getStop().getStopIndex(), indexName));
+        result.setSimpleTable((SimpleTableSegment) visit(ctx.tableNameWithDb()));
+        return result;
+    }
 }
diff --git a/test/it/parser/src/main/resources/case/ddl/alter-index.xml b/test/it/parser/src/main/resources/case/ddl/alter-index.xml
index 92a457fb918..d420eb3c518 100644
--- a/test/it/parser/src/main/resources/case/ddl/alter-index.xml
+++ b/test/it/parser/src/main/resources/case/ddl/alter-index.xml
@@ -145,4 +145,21 @@
     <alter-index sql-case-id="alter_index_alter_column" />
     <alter-index sql-case-id="alter_index_with_schema" />
     <alter-index sql-case-id="alter_index_set_tablespace" />
+
+    <alter-index sql-case-id="alter_index_rebuild_basic">
+        <index name="idx_product_id" start-index="12" stop-index="25" />
+        <table name="orders" start-index="30" stop-index="35" />
+    </alter-index>
+
+    <alter-index sql-case-id="alter_index_rebuild_with_partition">
+        <index name="idx_sales_region" start-index="12" stop-index="27" />
+        <table name="sales" start-index="32" stop-index="36" />
+    </alter-index>
+
+    <alter-index sql-case-id="alter_index_rebuild_with_db">
+        <index name="idx_product_id" start-index="12" stop-index="25" />
+        <table name="orders" start-index="30" stop-index="38">
+            <owner name="db" start-index="30" stop-index="31" />
+        </table>
+    </alter-index>
 </sql-parser-test-cases>
diff --git a/test/it/parser/src/main/resources/sql/supported/ddl/alter-index.xml b/test/it/parser/src/main/resources/sql/supported/ddl/alter-index.xml
index 8975298250e..a540a0397ab 100644
--- a/test/it/parser/src/main/resources/sql/supported/ddl/alter-index.xml
+++ b/test/it/parser/src/main/resources/sql/supported/ddl/alter-index.xml
@@ -57,4 +57,7 @@
     <sql-case id="alter_index_alter_column" value="ALTER INDEX t_order_idx ALTER COLUMN 3 SET STATISTICS 1000" db-types="PostgreSQL,openGauss" />
     <sql-case id="alter_index_with_schema" value="ALTER INDEX public.t_order_idx ALTER COLUMN 3 SET STATISTICS 1000" db-types="PostgreSQL,openGauss" />
     -->
+    <sql-case id="alter_index_rebuild_basic" value="ALTER INDEX idx_product_id ON orders REBUILD" db-types="Hive" />
+    <sql-case id="alter_index_rebuild_with_partition" value="ALTER INDEX idx_sales_region ON sales PARTITION (region='Asia') REBUILD" db-types="Hive" />
+    <sql-case id="alter_index_rebuild_with_db" value="ALTER INDEX idx_product_id ON db.orders REBUILD" db-types="Hive" />
 </sql-cases>