Skip to content

Commit 94f29ef

Browse files
committed
Polish grammar bundle builder and stabilize grammar endpoint doctest
Signed-off-by: Eric Wei <mengwei.eric@gmail.com>
1 parent cc686dc commit 94f29ef

2 files changed

Lines changed: 10 additions & 32 deletions

File tree

docs/user/ppl/interfaces/endpoint.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,15 @@ You can send an HTTP GET request to endpoint **/_plugins/_ppl/_grammar** to fetc
211211
### Example
212212

213213
```bash
214-
curl -sS -X GET localhost:9200/_plugins/_ppl/_grammar
214+
curl -sS -X GET localhost:9200/_plugins/_ppl/_grammar | python3 -m json.tool | grep -E '"bundleVersion"|"antlrVersion"|"startRuleIndex"|"ignoredTokens"|"rulesToVisit"'
215215
```
216216

217217
Expected output (trimmed):
218218

219-
```json
220-
{
221-
"bundleVersion": "1.0",
222-
"antlrVersion": "4.13.2",
223-
"grammarHash": "sha256:...",
224-
"startRuleIndex": 0,
225-
"lexerSerializedATN": [4, ...],
226-
"parserSerializedATN": [4, ...],
227-
"lexerRuleNames": ["SEARCH", "..."],
228-
"parserRuleNames": ["root", "..."],
229-
"literalNames": [null, "'SEARCH'", "..."],
230-
"symbolicNames": [null, "SEARCH", "..."],
231-
"tokenDictionary": {"PIPE": 196, "...": 0},
232-
"ignoredTokens": [472, 473, "..."],
233-
"rulesToVisit": [200, 201, "..."]
234-
}
219+
```text
220+
"bundleVersion": "1.0",
221+
"antlrVersion": "4.13.2",
222+
"startRuleIndex": 0,
223+
"ignoredTokens": [
224+
"rulesToVisit": [
235225
```

ppl/src/main/java/org/opensearch/sql/ppl/autocomplete/PPLGrammarBundleBuilder.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public GrammarBundle build() {
8181
.startRuleIndex(resolveStartRuleIndex(parser.getRuleNames()))
8282
.literalNames(literalNames)
8383
.symbolicNames(symbolicNames)
84-
.tokenDictionary(buildTokenDictionary(vocabulary))
84+
.tokenDictionary(buildTokenDictionary())
8585
.ignoredTokens(buildIgnoredTokens(vocabulary))
8686
.rulesToVisit(buildRulesToVisit(parser.getRuleNames()))
8787
.build();
@@ -90,12 +90,10 @@ public GrammarBundle build() {
9090
/**
9191
* Build the token dictionary — semantic name → token type ID mapping. Uses lexer constants since
9292
* token type IDs are defined by the lexer. The frontend autocomplete enrichment uses these to
93-
* identify tokens like SPACE, PIPE, SOURCE by name.
93+
* identify tokens like PIPE and SOURCE by name.
9494
*/
95-
private static Map<String, Integer> buildTokenDictionary(Vocabulary vocabulary) {
95+
private static Map<String, Integer> buildTokenDictionary() {
9696
Map<String, Integer> dict = new LinkedHashMap<>();
97-
// SPACE token may not exist in this grammar (whitespace may be implicitly skipped).
98-
// Resolve by searching symbolic names; use -1 if not found.
9997
dict.put("WHITESPACE", OpenSearchPPLLexer.WHITESPACE);
10098
dict.put("FROM", OpenSearchPPLLexer.FROM);
10199
dict.put("OPENING_BRACKET", OpenSearchPPLLexer.LT_PRTHS);
@@ -182,16 +180,6 @@ private static int[] buildRulesToVisit(String[] ruleNames) {
182180
return indices;
183181
}
184182

185-
/** Resolve a token type ID from the vocabulary by symbolic name. Returns -1 if not found. */
186-
private static int resolveTokenType(Vocabulary vocabulary, String name) {
187-
for (int i = 0; i <= vocabulary.getMaxTokenType(); i++) {
188-
if (name.equals(vocabulary.getSymbolicName(i))) {
189-
return i;
190-
}
191-
}
192-
return -1;
193-
}
194-
195183
private static int resolveStartRuleIndex(String[] ruleNames) {
196184
int idx = Arrays.asList(ruleNames).indexOf("root");
197185
return Math.max(idx, 0);

0 commit comments

Comments
 (0)