Skip to content

Commit 3c4fc4e

Browse files
authored
Support Hive DESCRIBE statement parse (#36350)
* support describe database&connector statement * support describe statement * support describe statement * Update HiveDALStatementVisitor.java
1 parent 72e88fb commit 3c4fc4e

File tree

10 files changed

+189
-8
lines changed

10 files changed

+189
-8
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
1. SQL Parser: Support Hive SHOW INDEX & SHOW COLUMNS & SHOW FUNCTIONS statement parse - [#36284](https://github.com/apache/shardingsphere/pull/36284)
8181
1. SQL Parser: Support Hive Show Granted Roles and Privileges & SHOW LOCKS & SHOW CONF statement parse - [#36300](https://github.com/apache/shardingsphere/pull/36300)
8282
1. SQL Parser: Support Hive SHOW TRANSACTIONS & SHOW COMPACTIONS statement parse - [#36301](https://github.com/apache/shardingsphere/pull/36301)
83+
1. SQL Parser: Support Hive DESCRIBE statement parse - [#36350](https://github.com/apache/shardingsphere/pull/36350)
8384
1. SQL Parser: Support Hive Inserting data into Hive Tables from queries statement parse - [#36320](https://github.com/apache/shardingsphere/pull/36320)
8485
1. SQL Parser: Support SQL Server xml methods parse - [#35911](https://github.com/apache/shardingsphere/pull/35911)
8586
1. SQL Parser: Support SQL Server CHANGETABLE function parse - [#35920](https://github.com/apache/shardingsphere/pull/35920)

parser/sql/dialect/hive/src/main/antlr4/imports/hive/BaseRule.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ columnName
648648
: identifier
649649
;
650650

651+
connectorName
652+
: identifier
653+
;
654+
651655
indexName
652656
: identifier
653657
;

parser/sql/dialect/hive/src/main/antlr4/imports/hive/DALStatement.g4

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ show
3939
| showCompactions
4040
;
4141

42+
describe
43+
: describeDatabase
44+
| describeConnector
45+
| describeTable
46+
;
47+
4248
showDatabases
4349
: SHOW (DATABASES|SCHEMAS) showLike?
4450
;
@@ -125,3 +131,28 @@ showFrom
125131
showLike
126132
: LIKE stringLiterals
127133
;
134+
135+
describeDatabase
136+
: DESCRIBE (DATABASE | SCHEMA) EXTENDED? databaseName
137+
;
138+
139+
describeConnector
140+
: DESCRIBE CONNECTOR EXTENDED? connectorName
141+
;
142+
143+
describeTable
144+
: DESCRIBE (EXTENDED | FORMATTED)? tableName partitionSpec? columnClause?
145+
;
146+
147+
columnClause
148+
: columnName columnOptions
149+
;
150+
151+
columnOptions
152+
: columnOption*
153+
;
154+
155+
columnOption
156+
: DOT_ identifier
157+
| DOT_ string_
158+
;

parser/sql/dialect/hive/src/main/antlr4/imports/hive/HiveKeyword.g4

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,6 +3374,10 @@ JAR
33743374
: J A R
33753375
;
33763376

3377+
CONNECTOR
3378+
: C O N N E C T O R
3379+
;
3380+
33773381
CONNECTORS
33783382
: C O N N E C T O R S
33793383
;

parser/sql/dialect/hive/src/main/antlr4/org/apache/shardingsphere/sql/parser/autogen/HiveStatement.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ execute
5050
| dropFunction
5151
| reloadFunction
5252
| show
53+
| describe
5354
) (SEMI_ EOF? | EOF)
5455
| EOF
5556
;

parser/sql/dialect/hive/src/main/java/org/apache/shardingsphere/sql/parser/hive/visitor/statement/type/HiveDALStatementVisitor.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.UseContext;
2424
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowDatabasesContext;
2525
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowLikeContext;
26+
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowFromContext;
2627
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowConnectorsContext;
2728
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowTablesContext;
2829
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowViewsContext;
@@ -39,10 +40,14 @@
3940
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowConfContext;
4041
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowTransactionsContext;
4142
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.ShowCompactionsContext;
43+
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeDatabaseContext;
44+
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeConnectorContext;
45+
import org.apache.shardingsphere.sql.parser.autogen.HiveStatementParser.DescribeTableContext;
4246
import org.apache.shardingsphere.sql.parser.hive.visitor.statement.HiveStatementVisitor;
4347
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.FromDatabaseSegment;
4448
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowFilterSegment;
4549
import org.apache.shardingsphere.sql.parser.statement.core.segment.dal.ShowLikeSegment;
50+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
4651
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.DatabaseSegment;
4752
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
4853
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
@@ -59,12 +64,14 @@
5964
import org.apache.shardingsphere.sql.parser.statement.hive.dal.show.HiveShowTransactionsStatement;
6065
import org.apache.shardingsphere.sql.parser.statement.hive.dal.show.HiveShowCompactionsStatement;
6166
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLUseStatement;
67+
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.column.MySQLDescribeStatement;
6268
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.database.MySQLShowDatabasesStatement;
6369
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.privilege.MySQLShowGrantsStatement;
6470
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.table.MySQLShowCreateTableStatement;
6571
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.table.MySQLShowTablesStatement;
6672
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.index.MySQLShowIndexStatement;
6773
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.show.column.MySQLShowColumnsStatement;
74+
import org.apache.shardingsphere.sql.parser.statement.hive.dal.HiveDescribeStatement;
6875

6976
/**
7077
* DAL statement visitor for Hive.
@@ -157,14 +164,19 @@ public ASTNode visitShowCreateTable(final ShowCreateTableContext ctx) {
157164
public ASTNode visitShowIndex(final ShowIndexContext ctx) {
158165
FromDatabaseSegment fromDatabase = null;
159166
if (null != ctx.showFrom()) {
160-
ASTNode showFromNode = visit(ctx.showFrom());
161-
if (showFromNode instanceof DatabaseSegment) {
162-
fromDatabase = new FromDatabaseSegment(ctx.showFrom().getStart().getStartIndex(), (DatabaseSegment) showFromNode);
163-
}
167+
fromDatabase = createFromDatabaseSegment(ctx.showFrom());
164168
}
165169
return new MySQLShowIndexStatement(getDatabaseType(), (SimpleTableSegment) visit(ctx.tableName()), fromDatabase);
166170
}
167171

172+
private FromDatabaseSegment createFromDatabaseSegment(final ShowFromContext showFromContext) {
173+
ASTNode showFromNode = visit(showFromContext);
174+
if (showFromNode instanceof DatabaseSegment) {
175+
return new FromDatabaseSegment(showFromContext.getStart().getStartIndex(), (DatabaseSegment) showFromNode);
176+
}
177+
return null;
178+
}
179+
168180
@Override
169181
public ASTNode visitShowColumns(final ShowColumnsContext ctx) {
170182
SimpleTableSegment table = null;
@@ -173,10 +185,7 @@ public ASTNode visitShowColumns(final ShowColumnsContext ctx) {
173185
}
174186
FromDatabaseSegment fromDatabase = null;
175187
if (null != ctx.showFrom()) {
176-
ASTNode showFromNode = visit(ctx.showFrom());
177-
if (showFromNode instanceof DatabaseSegment) {
178-
fromDatabase = new FromDatabaseSegment(ctx.showFrom().getStart().getStartIndex(), (DatabaseSegment) showFromNode);
179-
}
188+
fromDatabase = createFromDatabaseSegment(ctx.showFrom());
180189
}
181190
ShowFilterSegment filter = null;
182191
if (null != ctx.showLike()) {
@@ -215,4 +224,24 @@ public ASTNode visitShowTransactions(final ShowTransactionsContext ctx) {
215224
public ASTNode visitShowCompactions(final ShowCompactionsContext ctx) {
216225
return new HiveShowCompactionsStatement(getDatabaseType());
217226
}
227+
228+
@Override
229+
public ASTNode visitDescribeDatabase(final DescribeDatabaseContext ctx) {
230+
return new HiveDescribeStatement(getDatabaseType());
231+
}
232+
233+
@Override
234+
public ASTNode visitDescribeConnector(final DescribeConnectorContext ctx) {
235+
return new HiveDescribeStatement(getDatabaseType());
236+
}
237+
238+
@Override
239+
public ASTNode visitDescribeTable(final DescribeTableContext ctx) {
240+
SimpleTableSegment table = (SimpleTableSegment) visit(ctx.tableName());
241+
ColumnSegment columnWildcard = null;
242+
if (null != ctx.columnClause()) {
243+
columnWildcard = (ColumnSegment) visit(ctx.columnClause().columnName());
244+
}
245+
return new MySQLDescribeStatement(getDatabaseType(), table, columnWildcard);
246+
}
218247
}

parser/sql/engine/src/main/java/org/apache/shardingsphere/sql/parser/core/database/visitor/SQLVisitorRule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ public enum SQLVisitorRule {
363363

364364
DESC("Desc", SQLStatementType.DAL),
365365

366+
DESCRIBE("Describe", SQLStatementType.DAL),
367+
366368
HELP("Help", SQLStatementType.DAL),
367369

368370
EXPLAIN("Explain", SQLStatementType.DAL),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.sql.parser.statement.hive.dal;
19+
20+
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
21+
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
22+
23+
/**
24+
* Hive describe statement.
25+
*/
26+
public final class HiveDescribeStatement extends DALStatement {
27+
28+
public HiveDescribeStatement(final DatabaseType databaseType) {
29+
super(databaseType);
30+
}
31+
}

test/it/parser/src/main/resources/case/dal/describe.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,65 @@
5454
<simple-table name="tableName" start-index="5" stop-index="13" />
5555
<column-wild name="u%" start-delimiter="`" end-delimiter="`" start-index="15" stop-index="18" />
5656
</describe>
57+
58+
<describe sql-case-id="describe_database_basic" />
59+
<describe sql-case-id="describe_database_extended" />
60+
<describe sql-case-id="describe_schema_basic" />
61+
<describe sql-case-id="describe_schema_extended" />
62+
<describe sql-case-id="describe_connector_basic" />
63+
<describe sql-case-id="describe_connector_extended" />
64+
65+
<describe sql-case-id="describe_basic_table">
66+
<simple-table name="user_table" start-index="9" stop-index="18" />
67+
</describe>
68+
69+
<describe sql-case-id="describe_db_table">
70+
<simple-table name="user_table" start-index="9" stop-index="27" >
71+
<owner start-index="9" stop-index="16" name="sales_db" />
72+
</simple-table>
73+
</describe>
74+
75+
<describe sql-case-id="describe_extended_table">
76+
<simple-table name="user_table" start-index="18" stop-index="27" />
77+
</describe>
78+
79+
<describe sql-case-id="describe_formatted_table">
80+
<simple-table name="user_table" start-index="19" stop-index="28" />
81+
</describe>
82+
83+
<describe sql-case-id="describe_table_partition">
84+
<simple-table name="user_table" start-index="9" stop-index="18" />
85+
</describe>
86+
87+
<describe sql-case-id="describe_struct_field">
88+
<simple-table name="profile_table" start-index="9" stop-index="21" />
89+
<column-wild name="user_info" start-index="23" stop-index="31" />
90+
</describe>
91+
92+
<describe sql-case-id="describe_array_element">
93+
<simple-table name="order_table" start-index="9" stop-index="19" />
94+
<column-wild name="user_info" start-index="21" stop-index="29" />
95+
</describe>
96+
97+
<describe sql-case-id="describe_map_key">
98+
<simple-table name="product_table" start-index="9" stop-index="21" />
99+
<column-wild name="attributes" start-index="23" stop-index="32" />
100+
</describe>
101+
102+
<describe sql-case-id="describe_map_value">
103+
<simple-table name="event_log" start-index="9" stop-index="17" />
104+
<column-wild name="params" start-index="19" stop-index="24" />
105+
</describe>
106+
107+
<describe sql-case-id="describe_nested_struct_in_array">
108+
<simple-table name="posts" start-index="9" stop-index="13" />
109+
<column-wild name="comments" start-index="15" stop-index="22" />
110+
</describe>
111+
112+
<describe sql-case-id="describe_with_partition_and_complex">
113+
<simple-table name="actions" start-index="9" stop-index="24" >
114+
<owner start-index="9" stop-index="16" name="sales_db" />
115+
</simple-table>
116+
<column-wild name="products" start-index="54" stop-index="61" />
117+
</describe>
57118
</sql-parser-test-cases>

test/it/parser/src/main/resources/sql/supported/dal/describe.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,21 @@
2525
<sql-case id="describe_table_with_col_name" value="DESCRIBE tableName colName" db-types="MySQL" />
2626
<sql-case id="describe_table_with_placeholder" value="DESC tableName ___" db-types="MySQL" />
2727
<sql-case id="describe_table_with_wild" value="DESC tableName `u%`" db-types="MySQL" />
28+
<sql-case id="describe_database_basic" value="DESCRIBE DATABASE sales_db;" db-types="Hive" />
29+
<sql-case id="describe_database_extended" value="DESCRIBE DATABASE EXTENDED sales_db;" db-types="Hive" />
30+
<sql-case id="describe_schema_basic" value="DESCRIBE SCHEMA user_info;" db-types="Hive" />
31+
<sql-case id="describe_schema_extended" value="DESCRIBE SCHEMA EXTENDED product_db;" db-types="Hive" />
32+
<sql-case id="describe_connector_basic" value="DESCRIBE CONNECTOR mysql_connector;" db-types="Hive" />
33+
<sql-case id="describe_connector_extended" value="DESCRIBE CONNECTOR EXTENDED hive_connector;" db-types="Hive" />
34+
<sql-case id="describe_basic_table" value="DESCRIBE user_table;" db-types="Hive" />
35+
<sql-case id="describe_db_table" value="DESCRIBE sales_db.user_table;" db-types="Hive" />
36+
<sql-case id="describe_extended_table" value="DESCRIBE EXTENDED user_table;" db-types="Hive" />
37+
<sql-case id="describe_formatted_table" value="DESCRIBE FORMATTED user_table;" db-types="Hive" />
38+
<sql-case id="describe_table_partition" value="DESCRIBE user_table PARTITION (region='Asia');" db-types="Hive" />
39+
<sql-case id="describe_struct_field" value="DESCRIBE profile_table user_info.name;" db-types="Hive" />
40+
<sql-case id="describe_array_element" value="DESCRIBE order_table user_info.'$elem$';" db-types="Hive" />
41+
<sql-case id="describe_map_key" value="DESCRIBE product_table attributes.'$key$';" db-types="Hive" />
42+
<sql-case id="describe_map_value" value="DESCRIBE event_log params.'$value$';" db-types="Hive" />
43+
<sql-case id="describe_nested_struct_in_array" value="DESCRIBE posts comments.'$elem$'.author;" db-types="Hive" />
44+
<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" />
2845
</sql-cases>

0 commit comments

Comments
 (0)