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 3c4fc4e097f Support Hive DESCRIBE statement parse (#36350)
3c4fc4e097f is described below
commit 3c4fc4e097fb3488a2c5d6270ac682c938a1ff09
Author: Claire <[email protected]>
AuthorDate: Thu Aug 21 11:02:33 2025 +0800
Support Hive DESCRIBE statement parse (#36350)
* support describe database&connector statement
* support describe statement
* support describe statement
* Update HiveDALStatementVisitor.java
---
RELEASE-NOTES.md | 1 +
.../hive/src/main/antlr4/imports/hive/BaseRule.g4 | 4 ++
.../src/main/antlr4/imports/hive/DALStatement.g4 | 31 +++++++++++
.../src/main/antlr4/imports/hive/HiveKeyword.g4 | 4 ++
.../sql/parser/autogen/HiveStatement.g4 | 1 +
.../statement/type/HiveDALStatementVisitor.java | 45 +++++++++++++---
.../core/database/visitor/SQLVisitorRule.java | 2 +
.../statement/hive/dal/HiveDescribeStatement.java} | 48 +++++------------
.../src/main/resources/case/dal/describe.xml | 61 ++++++++++++++++++++++
.../main/resources/sql/supported/dal/describe.xml | 17 ++++++
10 files changed, 170 insertions(+), 44 deletions(-)
diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 862810eb96c..9a011ec1c29 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -80,6 +80,7 @@
1. SQL Parser: Support Hive SHOW INDEX & SHOW COLUMNS & SHOW FUNCTIONS
statement parse - [#36284](https://github.com/apache/shardingsphere/pull/36284)
1. SQL Parser: Support Hive Show Granted Roles and Privileges & SHOW LOCKS &
SHOW CONF statement parse -
[#36300](https://github.com/apache/shardingsphere/pull/36300)
1. SQL Parser: Support Hive SHOW TRANSACTIONS & SHOW COMPACTIONS statement
parse - [#36301](https://github.com/apache/shardingsphere/pull/36301)
+1. SQL Parser: Support Hive DESCRIBE statement parse -
[#36350](https://github.com/apache/shardingsphere/pull/36350)
1. SQL Parser: Support Hive Inserting data into Hive Tables from queries
statement parse - [#36320](https://github.com/apache/shardingsphere/pull/36320)
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)
diff --git a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/BaseRule.g4
b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/BaseRule.g4
index 847eee1bff4..8cb7f68163d 100644
--- a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/BaseRule.g4
+++ b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/BaseRule.g4
@@ -648,6 +648,10 @@ columnName
: identifier
;
+connectorName
+ : identifier
+ ;
+
indexName
: identifier
;
diff --git
a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DALStatement.g4
b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DALStatement.g4
index 16102107496..4b3f189e2b0 100644
--- a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DALStatement.g4
+++ b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/DALStatement.g4
@@ -39,6 +39,12 @@ show
| showCompactions
;
+describe
+ : describeDatabase
+ | describeConnector
+ | describeTable
+ ;
+
showDatabases
: SHOW (DATABASES|SCHEMAS) showLike?
;
@@ -125,3 +131,28 @@ showFrom
showLike
: LIKE stringLiterals
;
+
+describeDatabase
+ : DESCRIBE (DATABASE | SCHEMA) EXTENDED? databaseName
+ ;
+
+describeConnector
+ : DESCRIBE CONNECTOR EXTENDED? connectorName
+ ;
+
+describeTable
+ : DESCRIBE (EXTENDED | FORMATTED)? tableName partitionSpec? columnClause?
+ ;
+
+columnClause
+ : columnName columnOptions
+ ;
+
+columnOptions
+ : columnOption*
+ ;
+
+columnOption
+ : DOT_ identifier
+ | DOT_ string_
+ ;
diff --git
a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/HiveKeyword.g4
b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/HiveKeyword.g4
index d250c8aad8f..56c899cdaf6 100644
--- a/parser/sql/dialect/hive/src/main/antlr4/imports/hive/HiveKeyword.g4
+++ b/parser/sql/dialect/hive/src/main/antlr4/imports/hive/HiveKeyword.g4
@@ -3374,6 +3374,10 @@ JAR
: J A R
;
+CONNECTOR
+ : C O N N E C T O R
+ ;
+
CONNECTORS
: C O N N E C T O R S
;
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 cdf233feb67..69a72b1d51a 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
@@ -50,6 +50,7 @@ execute
| dropFunction
| reloadFunction
| show
+ | describe
) (SEMI_ EOF? | EOF)
| EOF
;
diff --git
a/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDALStatementVisitor.java
b/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDALStatementVisitor.java
index 707f69948de..77acec43d66 100644
---
a/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDALStatementVisitor.java
+++
b/parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDALStatementVisitor.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.sql.parser.api.visitor.statement.type.DALStatem
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.UseContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowDatabasesContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowLikeContext;
+import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowFromContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowConnectorsContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowTablesContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowViewsContext;
@@ -39,10 +40,14 @@ import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowLock
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowConfContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowTransactionsContext;
import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowCompactionsContext;
+import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeDatabaseContext;
+import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeConnectorContext;
+import
org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeTableContext;
import
org.apache.shardingsphere.sql.parser.hive.visitor.statement.HiveStatementVisitor;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowFilterSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowLikeSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -59,12 +64,14 @@ import
org.apache.shardingsphere.sql.parser.statement.hive.dal.show.HiveShowConf
import
org.apache.shardingsphere.sql.parser.statement.hive.dal.show.HiveShowTransactionsStatement;
import
org.apache.shardingsphere.sql.parser.statement.hive.dal.show.HiveShowCompactionsStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLUseStatement;
+import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.column.MySQLDescribeStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.database.MySQLShowDatabasesStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.privilege.MySQLShowGrantsStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.table.MySQLShowCreateTableStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.table.MySQLShowTablesStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.index.MySQLShowIndexStatement;
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.column.MySQLShowColumnsStatement;
+import
org.apache.shardingsphere.sql.parser.statement.hive.dal.HiveDescribeStatement;
/**
* DAL statement visitor for Hive.
@@ -157,14 +164,19 @@ public final class HiveDALStatementVisitor extends
HiveStatementVisitor implemen
public ASTNode visitShowIndex(final ShowIndexContext ctx) {
FromDatabaseSegment fromDatabase = null;
if (null != ctx.showFrom()) {
- ASTNode showFromNode = visit(ctx.showFrom());
- if (showFromNode instanceof DatabaseSegment) {
- fromDatabase = new
FromDatabaseSegment(ctx.showFrom().getStart().getStartIndex(),
(DatabaseSegment) showFromNode);
- }
+ fromDatabase = createFromDatabaseSegment(ctx.showFrom());
}
return new MySQLShowIndexStatement(getDatabaseType(),
(SimpleTableSegment) visit(ctx.tableName()), fromDatabase);
}
+ private FromDatabaseSegment createFromDatabaseSegment(final
ShowFromContext showFromContext) {
+ ASTNode showFromNode = visit(showFromContext);
+ if (showFromNode instanceof DatabaseSegment) {
+ return new
FromDatabaseSegment(showFromContext.getStart().getStartIndex(),
(DatabaseSegment) showFromNode);
+ }
+ return null;
+ }
+
@Override
public ASTNode visitShowColumns(final ShowColumnsContext ctx) {
SimpleTableSegment table = null;
@@ -173,10 +185,7 @@ public final class HiveDALStatementVisitor extends
HiveStatementVisitor implemen
}
FromDatabaseSegment fromDatabase = null;
if (null != ctx.showFrom()) {
- ASTNode showFromNode = visit(ctx.showFrom());
- if (showFromNode instanceof DatabaseSegment) {
- fromDatabase = new
FromDatabaseSegment(ctx.showFrom().getStart().getStartIndex(),
(DatabaseSegment) showFromNode);
- }
+ fromDatabase = createFromDatabaseSegment(ctx.showFrom());
}
ShowFilterSegment filter = null;
if (null != ctx.showLike()) {
@@ -215,4 +224,24 @@ public final class HiveDALStatementVisitor extends
HiveStatementVisitor implemen
public ASTNode visitShowCompactions(final ShowCompactionsContext ctx) {
return new HiveShowCompactionsStatement(getDatabaseType());
}
+
+ @Override
+ public ASTNode visitDescribeDatabase(final DescribeDatabaseContext ctx) {
+ return new HiveDescribeStatement(getDatabaseType());
+ }
+
+ @Override
+ public ASTNode visitDescribeConnector(final DescribeConnectorContext ctx) {
+ return new HiveDescribeStatement(getDatabaseType());
+ }
+
+ @Override
+ public ASTNode visitDescribeTable(final DescribeTableContext ctx) {
+ SimpleTableSegment table = (SimpleTableSegment) visit(ctx.tableName());
+ ColumnSegment columnWildcard = null;
+ if (null != ctx.columnClause()) {
+ columnWildcard = (ColumnSegment)
visit(ctx.columnClause().columnName());
+ }
+ return new MySQLDescribeStatement(getDatabaseType(), table,
columnWildcard);
+ }
}
diff --git
a/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
b/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
index 78eaacc5a79..c32ba289b0a 100644
---
a/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
+++
b/parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java
@@ -363,6 +363,8 @@ public enum SQLVisitorRule {
DESC("Desc", SQLStatementType.DAL),
+ DESCRIBE("Describe", SQLStatementType.DAL),
+
HELP("Help", SQLStatementType.DAL),
EXPLAIN("Explain", SQLStatementType.DAL),
diff --git
a/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
b/parser/sql/statement/type/hive/src/main/java/org/apache/shardingsphere/sql/parser/statement/hive/dal/HiveDescribeStatement.java
similarity index 52%
copy from
parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
copy to
parser/sql/statement/type/hive/src/main/java/org/apache/shardingsphere/sql/parser/statement/hive/dal/HiveDescribeStatement.java
index cdf233feb67..ab431d99d57 100644
---
a/parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4
+++
b/parser/sql/statement/type/hive/src/main/java/org/apache/shardingsphere/sql/parser/statement/hive/dal/HiveDescribeStatement.java
@@ -15,41 +15,17 @@
* limitations under the License.
*/
-grammar HiveStatement;
+package org.apache.shardingsphere.sql.parser.statement.hive.dal;
-import Comments, DMLStatement, DDLStatement, DALStatement;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
-// TODO correct hive SQL parsing according to official documentation
-execute
- : (select
- | insert
- | update
- | delete
- | loadStatement
- | createDatabase
- | dropDatabase
- | alterDatabase
- | use
- | createTable
- | dropTable
- | truncateTable
- | msckStatement
- | alterTable
- | createView
- | dropView
- | alterView
- | createMaterializedView
- | dropMaterializedView
- | alterMaterializedView
- | createIndex
- | dropIndex
- | alterIndex
- | createMacro
- | dropMacro
- | createFunction
- | dropFunction
- | reloadFunction
- | show
- ) (SEMI_ EOF? | EOF)
- | EOF
- ;
+/**
+ * Hive describe statement.
+ */
+public final class HiveDescribeStatement extends DALStatement {
+
+ public HiveDescribeStatement(final DatabaseType databaseType) {
+ super(databaseType);
+ }
+}
diff --git a/test/it/parser/src/main/resources/case/dal/describe.xml
b/test/it/parser/src/main/resources/case/dal/describe.xml
index c17d7cd0f94..d412bd86118 100644
--- a/test/it/parser/src/main/resources/case/dal/describe.xml
+++ b/test/it/parser/src/main/resources/case/dal/describe.xml
@@ -54,4 +54,65 @@
<simple-table name="tableName" start-index="5" stop-index="13" />
<column-wild name="u%" start-delimiter="`" end-delimiter="`"
start-index="15" stop-index="18" />
</describe>
+
+ <describe sql-case-id="describe_database_basic" />
+ <describe sql-case-id="describe_database_extended" />
+ <describe sql-case-id="describe_schema_basic" />
+ <describe sql-case-id="describe_schema_extended" />
+ <describe sql-case-id="describe_connector_basic" />
+ <describe sql-case-id="describe_connector_extended" />
+
+ <describe sql-case-id="describe_basic_table">
+ <simple-table name="user_table" start-index="9" stop-index="18" />
+ </describe>
+
+ <describe sql-case-id="describe_db_table">
+ <simple-table name="user_table" start-index="9" stop-index="27" >
+ <owner start-index="9" stop-index="16" name="sales_db" />
+ </simple-table>
+ </describe>
+
+ <describe sql-case-id="describe_extended_table">
+ <simple-table name="user_table" start-index="18" stop-index="27" />
+ </describe>
+
+ <describe sql-case-id="describe_formatted_table">
+ <simple-table name="user_table" start-index="19" stop-index="28" />
+ </describe>
+
+ <describe sql-case-id="describe_table_partition">
+ <simple-table name="user_table" start-index="9" stop-index="18" />
+ </describe>
+
+ <describe sql-case-id="describe_struct_field">
+ <simple-table name="profile_table" start-index="9" stop-index="21" />
+ <column-wild name="user_info" start-index="23" stop-index="31" />
+ </describe>
+
+ <describe sql-case-id="describe_array_element">
+ <simple-table name="order_table" start-index="9" stop-index="19" />
+ <column-wild name="user_info" start-index="21" stop-index="29" />
+ </describe>
+
+ <describe sql-case-id="describe_map_key">
+ <simple-table name="product_table" start-index="9" stop-index="21" />
+ <column-wild name="attributes" start-index="23" stop-index="32" />
+ </describe>
+
+ <describe sql-case-id="describe_map_value">
+ <simple-table name="event_log" start-index="9" stop-index="17" />
+ <column-wild name="params" start-index="19" stop-index="24" />
+ </describe>
+
+ <describe sql-case-id="describe_nested_struct_in_array">
+ <simple-table name="posts" start-index="9" stop-index="13" />
+ <column-wild name="comments" start-index="15" stop-index="22" />
+ </describe>
+
+ <describe sql-case-id="describe_with_partition_and_complex">
+ <simple-table name="actions" start-index="9" stop-index="24" >
+ <owner start-index="9" stop-index="16" name="sales_db" />
+ </simple-table>
+ <column-wild name="products" start-index="54" stop-index="61" />
+ </describe>
</sql-parser-test-cases>
diff --git a/test/it/parser/src/main/resources/sql/supported/dal/describe.xml
b/test/it/parser/src/main/resources/sql/supported/dal/describe.xml
index d1dbd0949d7..263dd732b34 100644
--- a/test/it/parser/src/main/resources/sql/supported/dal/describe.xml
+++ b/test/it/parser/src/main/resources/sql/supported/dal/describe.xml
@@ -25,4 +25,21 @@
<sql-case id="describe_table_with_col_name" value="DESCRIBE tableName
colName" db-types="MySQL" />
<sql-case id="describe_table_with_placeholder" value="DESC tableName ___"
db-types="MySQL" />
<sql-case id="describe_table_with_wild" value="DESC tableName `u%`"
db-types="MySQL" />
+ <sql-case id="describe_database_basic" value="DESCRIBE DATABASE sales_db;"
db-types="Hive" />
+ <sql-case id="describe_database_extended" value="DESCRIBE DATABASE
EXTENDED sales_db;" db-types="Hive" />
+ <sql-case id="describe_schema_basic" value="DESCRIBE SCHEMA user_info;"
db-types="Hive" />
+ <sql-case id="describe_schema_extended" value="DESCRIBE SCHEMA EXTENDED
product_db;" db-types="Hive" />
+ <sql-case id="describe_connector_basic" value="DESCRIBE CONNECTOR
mysql_connector;" db-types="Hive" />
+ <sql-case id="describe_connector_extended" value="DESCRIBE CONNECTOR
EXTENDED hive_connector;" db-types="Hive" />
+ <sql-case id="describe_basic_table" value="DESCRIBE user_table;"
db-types="Hive" />
+ <sql-case id="describe_db_table" value="DESCRIBE sales_db.user_table;"
db-types="Hive" />
+ <sql-case id="describe_extended_table" value="DESCRIBE EXTENDED
user_table;" db-types="Hive" />
+ <sql-case id="describe_formatted_table" value="DESCRIBE FORMATTED
user_table;" db-types="Hive" />
+ <sql-case id="describe_table_partition" value="DESCRIBE user_table
PARTITION (region='Asia');" db-types="Hive" />
+ <sql-case id="describe_struct_field" value="DESCRIBE profile_table
user_info.name;" db-types="Hive" />
+ <sql-case id="describe_array_element" value="DESCRIBE order_table
user_info.'$elem$';" db-types="Hive" />
+ <sql-case id="describe_map_key" value="DESCRIBE product_table
attributes.'$key$';" db-types="Hive" />
+ <sql-case id="describe_map_value" value="DESCRIBE event_log
params.'$value$';" db-types="Hive" />
+ <sql-case id="describe_nested_struct_in_array" value="DESCRIBE posts
comments.'$elem$'.author;" db-types="Hive" />
+ <sql-case id="describe_with_partition_and_complex" value="DESCRIBE
sales_db.actions PARTITION (dt='2025-08-17') products.'$elem$'.details.price;"
db-types="Hive" />
</sql-cases>