RaigorJiang opened a new issue #11784:
URL: https://github.com/apache/shardingsphere/issues/11784


   Hi community,
   
   I am designing `Preview SQL` syntax for DistSQL, which has the following 
characteristics:
   1. `Preview` is a keyword, `SQL` can be any statement, such as SELECT, 
CREATE, DROP, etc.;
   2. There is at least one blank character between the `Preview` keyword and 
the `SQL` statement;
   3. It is expected that after ANTLR parses the input text, it will recognize 
`SQL` as a `String object`;
   
   ### Here is an example:
   input text:
   ```sql
   preview select * from t_order;
   ```
   parse result:
   
![image](https://user-images.githubusercontent.com/5668787/129182411-bfa61a79-4c1a-4c3d-95a0-cc16ca299bd8.png)
   
   
   
   ### There are some difficulties:
   1. We need a lexer grammar that can match any input sentence after the 
keyword, which did not exist before;
   2. In the existing ANTLR grammar file, we define the following lexer grammar 
to ignore whitespace characters
   ```antlr
   WS
       : [ \t\r\n] + ->skip
       ;
   ```
   But the `Preview` statement needs to recognize blank characters to 
distinguish between the keyword and the statement after it;
   3. If the lexer grammar not ignoring spaces is declared in the existing 
grammar, it will cause the original grammar parsing to be abnormal;
   
   ### Current solution:
   Add `xxxKeyword.g4` and `xxxStatement.g4` to match the special syntax of 
`Preview SQL` without affecting the original syntax;
   
   The content of `xxxKeyword.g4` is : 
   ```g4
   import Alphabet;
   
   WS
       : [ \t\r\n] + -> skip
       ;
   
   PREVIEW
       : P R E V I E W
       ;
   
   SQLString
       : WS.*
       ;
   ```
   
   and the content of `xxxStatement.g4` is : 
   ```g4
   previewSQL
       : PREVIEW sql
       ;
   
   sql
       : SQLString
       ;
   ```
   
   But there are some effects:
   1. `Preview SQL` cannot be included in CommonDistSQLStatement, a separate 
file is required;
   2. An additional try catch is required in `DistSQLStatementParserEngine` 
(because the `preview` grammar cannot be combined with other rules), it may be 
changed from:
   ```java
   public SQLStatement parse(final String sql) {
           try {
               return new CommonDistSQLStatementParserEngine().parse(sql);
           } catch (final ParseCancellationException | SQLParsingException 
ignored) {
               return new FeaturedDistSQLStatementParserEngine().parse(sql);
           }
       }
   ```
   
   to 
   
   ```java
   public SQLStatement parse(final String sql) {
           try {
               return new CommonDistSQLStatementParserEngine().parse(sql);
           } catch (final ParseCancellationException | SQLParsingException 
ignored) {
               try {
                   return new FeaturedDistSQLStatementParserEngine().parse(sql);
               } catch (final SQLParsingException ignoredToo) {
                   return new XXXDistSQLStatementParserEngine().parse(sql);
               }
           }
       }
   ```
   
   3. At the same time, `xxx` now only has `preview grammar`, and will include 
`parse` and `optimize` in the future.
   So we need to think of a good name.  What about `Special` or 
`CommonDistSQL2`?
   
   The above are the problems encountered in the design of `Preview SQL` 
grammar and the solution at present. I hope to get good suggestions, thank you!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to