0
# Parser Integration
1
2
The parser integration capabilities provide the core entry point for creating and configuring Hive SQL parsers within the Apache Calcite framework.
3
4
## Capabilities
5
6
### Parser Factory
7
8
The main factory for creating Hive SQL parser instances.
9
10
```java { .api }
11
/**
12
* Main parser implementation class generated by JavaCC/FMPP
13
* Provides factory for creating Hive SQL parser instances
14
*/
15
public class FlinkHiveSqlParserImpl extends SqlAbstractParserImpl {
16
/**
17
* Factory instance for creating Hive SQL parsers
18
* Use this factory with SqlParser.config() to create parsers
19
*/
20
public static final SqlParserImplFactory FACTORY;
21
}
22
```
23
24
**Usage Examples:**
25
26
```java
27
import org.apache.calcite.sql.SqlParser;
28
import org.apache.flink.sql.parser.hive.impl.FlinkHiveSqlParserImpl;
29
import org.apache.calcite.sql.parser.SqlParser.Config;
30
31
// Basic parser creation
32
SqlParser parser = SqlParser.create(sqlStatement,
33
SqlParser.config().withParserFactory(FlinkHiveSqlParserImpl.FACTORY));
34
35
// Advanced parser configuration
36
Config config = SqlParser.config()
37
.withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
38
.withQuoting(Quoting.DOUBLE_QUOTE)
39
.withUnquotedCasing(Casing.TO_UPPER)
40
.withQuotedCasing(Casing.UNCHANGED)
41
.withConformance(FlinkSqlConformance.HIVE);
42
43
SqlParser hiveSqlParser = SqlParser.create(sqlStatement, config);
44
45
// Parse different types of Hive statements
46
try {
47
// Parse DDL statements
48
SqlNode createTable = hiveSqlParser.parseStmt();
49
50
// Parse DML statements
51
SqlNode insertStmt = hiveSqlParser.parseStmt();
52
53
// Parse query statements
54
SqlNode selectQuery = hiveSqlParser.parseQuery();
55
56
} catch (SqlParseException e) {
57
// Handle parsing errors
58
System.err.println("Failed to parse Hive SQL: " + e.getMessage());
59
}
60
```
61
62
### SQL Conformance Integration
63
64
Integration with Flink's SQL conformance system for Hive dialect.
65
66
```java
67
// Use Hive conformance for parser configuration
68
SqlParser.Config config = SqlParser.config()
69
.withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
70
.withConformance(FlinkSqlConformance.HIVE);
71
```
72
73
### Supported SQL Statements
74
75
The parser supports parsing these categories of Hive SQL statements:
76
77
**DDL (Data Definition Language)**:
78
- `CREATE/ALTER/DROP DATABASE`
79
- `CREATE/ALTER/DROP TABLE`
80
- `CREATE/ALTER/DROP VIEW`
81
- `ALTER TABLE ADD/DROP PARTITION`
82
- `DESCRIBE TABLE`
83
84
**DML (Data Manipulation Language)**:
85
- `INSERT [OVERWRITE] INTO ... PARTITION(...)`
86
87
**Query Statements**:
88
- Standard SELECT queries with Hive-specific functions
89
- Hive-specific table hints and query syntax
90
91
### Parser Configuration Options
92
93
Common configuration patterns for the Hive SQL parser:
94
95
```java
96
// Case sensitivity configuration
97
SqlParser.Config caseSensitiveConfig = SqlParser.config()
98
.withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
99
.withUnquotedCasing(Casing.TO_UPPER) // Identifiers to uppercase
100
.withQuotedCasing(Casing.UNCHANGED); // Quoted identifiers unchanged
101
102
// Quoting configuration
103
SqlParser.Config quotingConfig = SqlParser.config()
104
.withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
105
.withQuoting(Quoting.DOUBLE_QUOTE) // Use double quotes
106
.withQuotingIdent(Quoting.BACK_TICK); // Use backticks for identifiers
107
108
// Lenient parsing configuration
109
SqlParser.Config lenientConfig = SqlParser.config()
110
.withParserFactory(FlinkHiveSqlParserImpl.FACTORY)
111
.withConformance(FlinkSqlConformance.LENIENT);
112
```
113
114
### Error Handling
115
116
The parser throws `SqlParseException` for syntax errors and validation failures:
117
118
```java
119
try {
120
SqlNode parsed = parser.parseStmt();
121
122
// Validate the parsed node
123
if (parsed instanceof SqlCreateHiveTable) {
124
SqlCreateHiveTable createTable = (SqlCreateHiveTable) parsed;
125
// Process Hive table creation
126
}
127
128
} catch (SqlParseException e) {
129
// Handle specific parsing errors
130
switch (e.getPos().getLineNum()) {
131
case 1:
132
System.err.println("Syntax error in first line: " + e.getMessage());
133
break;
134
default:
135
System.err.println("Parse error at line " + e.getPos().getLineNum() +
136
", column " + e.getPos().getColumnNum() + ": " + e.getMessage());
137
}
138
}
139
```
140
141
### Integration with Flink SQL Gateway
142
143
Example of integrating with Flink SQL Gateway for Hive compatibility:
144
145
```java
146
// Configure Flink SQL Gateway with Hive parser
147
TableEnvironment tEnv = TableEnvironment.create(EnvironmentSettings.inBatchMode());
148
149
// Set Hive dialect for parsing
150
tEnv.getConfig().setSqlDialect(SqlDialect.HIVE);
151
152
// Execute Hive DDL statements
153
tEnv.executeSql("""
154
CREATE TABLE hive_table (
155
id BIGINT,
156
name STRING,
157
created_date DATE
158
)
159
PARTITIONED BY (year INT, month INT)
160
STORED AS PARQUET
161
""");
162
```
163
164
### Generated Parser Features
165
166
The parser is generated using FMPP (FreeMarker Preprocessor) and JavaCC, providing:
167
168
- **Keyword Support**: All Hive-specific keywords (PARTITIONED, STORED, SERDE, etc.)
169
- **Grammar Extensions**: Hive-specific syntax extensions to base SQL grammar
170
- **Type System**: Support for Hive data types (STRUCT, ARRAY, MAP with specific syntax)
171
- **Error Recovery**: Improved error messages for Hive-specific syntax errors
172
173
### Thread Safety
174
175
The parser factory is thread-safe and can be used concurrently:
176
177
```java
178
// Safe to use across multiple threads
179
public class HiveSqlParsingService {
180
private static final SqlParserImplFactory PARSER_FACTORY = FlinkHiveSqlParserImpl.FACTORY;
181
182
public SqlNode parseHiveSql(String sql) throws SqlParseException {
183
SqlParser parser = SqlParser.create(sql,
184
SqlParser.config().withParserFactory(PARSER_FACTORY));
185
return parser.parseStmt();
186
}
187
}
188
```