Permalink
Browse files

Reformat and remove some dead rules

  • Loading branch information...
1 parent 404cfd9 commit b124bd24a2f99cdc174e317f64899da63478aa08 thiruvel committed Dec 3, 2012
Showing with 49 additions and 119 deletions.
  1. +1 −1 pom.xml
  2. +45 −116 src/main/antlr3/com/yahoo/antlr/Insert.g
  3. +3 −2 src/main/java/com/yahoo/antlr/HiveANTLRTest.java
View
@@ -9,7 +9,7 @@
<url>http://maven.apache.org</url>
<properties>
- <antlr.version>3.2</antlr.version>
+ <antlr.version>3.4</antlr.version>
</properties>
<build>
@@ -1,19 +1,3 @@
-/**
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-*/
grammar Insert;
options
@@ -31,6 +15,7 @@ TOK_SELECT;
TOK_SELEXPR;
TOK_FROM;
TOK_TAB;
+TOK_TAB_OR_PART;
TOK_PARTSPEC;
TOK_PARTVAL;
TOK_DIR;
@@ -71,106 +56,54 @@ catch (RecognitionException e) {
}
}
-// starting rule
-statement
- : explainStatement EOF
- | execStatement EOF
- ;
-
-explainStatement
-@init { msgs.push("explain statement"); }
+insertClause
+@init { msgs.push("insert clause"); }
@after { msgs.pop(); }
- : KW_EXPLAIN (explainOptions=KW_EXTENDED|explainOptions=KW_FORMATTED)? execStatement
- -> ^(TOK_EXPLAIN execStatement $explainOptions?)
- ;
+ : KW_INSERT KW_OVERWRITE destination ifNotExists?
+ -> ^(TOK_DESTINATION destination ifNotExists?)
+ | KW_INSERT KW_INTO KW_TABLE tableOrPartition
+ -> ^(TOK_INSERT_INTO ^(tableOrPartition))
+ ;
-execStatement
-@init { msgs.push("statement"); }
-@after { msgs.pop(); }
- : queryStatementExpression
- ;
-
ifNotExists
@init { msgs.push("if not exists clause"); }
@after { msgs.pop(); }
: KW_IF KW_NOT KW_EXISTS
- -> ^(TOK_IFNOTEXISTS)
- ;
-
-// select statement select ... from ... where ... group by ... order by ...
-queryStatementExpression
- : queryStatement
+ -> ^(TOK_IFNOTEXISTS)
;
-queryStatement
- :
- fromClause
- ( b+=body )+ -> ^(TOK_QUERY fromClause body+)
- | regular_body
- ;
-
-regular_body
- :
- insertClause
- selectClause
- fromClause -> ^(TOK_QUERY fromClause ^(TOK_INSERT insertClause
- selectClause ))
- |
- selectStatement
- ;
-
-selectStatement
- :
- selectClause
- fromClause -> ^(TOK_QUERY fromClause ^(TOK_INSERT ^(TOK_DESTINATION ^(TOK_DIR TOK_TMP_FILE))
- selectClause ))
- ;
-
-body
- :
- insertClause
- ;
-
-insertClause
-@init { msgs.push("insert clause"); }
-@after { msgs.pop(); }
- :
- KW_INSERT KW_OVERWRITE destination ifNotExists? -> ^(TOK_DESTINATION destination ifNotExists?)
- | KW_INSERT KW_INTO KW_TABLE tableOrPartition
- -> ^(TOK_INSERT_INTO ^(tableOrPartition))
- ;
-
destination
@init { msgs.push("destination specification"); }
@after { msgs.pop(); }
- :
- KW_LOCAL KW_DIRECTORY StringLiteral -> ^(TOK_LOCAL_DIR StringLiteral)
- | KW_DIRECTORY StringLiteral -> ^(TOK_DIR StringLiteral)
- | KW_TABLE tableOrPartition -> ^(tableOrPartition)
+ : KW_LOCAL KW_DIRECTORY StringLiteral
+ -> ^(TOK_LOCAL_DIR StringLiteral)
+ | KW_DIRECTORY StringLiteral
+ -> ^(TOK_DIR StringLiteral)
+ | KW_TABLE tableOrPartition
+ -> ^(tableOrPartition)
;
//----------------------- Rules for parsing selectClause -----------------------------
// select a,b,c ...
selectClause
@init { msgs.push("select clause"); }
@after { msgs.pop(); }
- :
- KW_SELECT (((KW_ALL | dist=KW_DISTINCT)? selectList))
- -> ^(TOK_SELECT selectList)
+ : KW_SELECT (((KW_ALL | dist=KW_DISTINCT)? selectList))
+ -> ^(TOK_SELECT selectList)
;
selectList
@init { msgs.push("select list"); }
@after { msgs.pop(); }
- :
- selectItem ( COMMA selectItem )* -> selectItem+
+ : selectItem ( COMMA selectItem )*
+ -> selectItem+
;
selectItem
@init { msgs.push("selection target"); }
@after { msgs.pop(); }
- :
- ( selectExpression ((KW_AS? Identifier) | (KW_AS LPAREN Identifier (COMMA Identifier)* RPAREN))?) -> ^(TOK_SELEXPR selectExpression Identifier*)
+ : ( selectExpression ((KW_AS? Identifier) | (KW_AS LPAREN Identifier (COMMA Identifier)* RPAREN))?)
+ -> ^(TOK_SELEXPR selectExpression Identifier*)
;
@@ -185,7 +118,8 @@ selectExpressionList
@init { msgs.push("select expression list"); }
@after { msgs.pop(); }
:
- selectExpression (COMMA selectExpression)* -> ^(TOK_EXPLIST selectExpression+)
+ selectExpression (COMMA selectExpression)*
+ -> ^(TOK_EXPLIST selectExpression+)
;
@@ -202,17 +136,17 @@ tableAllColumns
tableOrColumn
@init { msgs.push("table or column identifier"); }
@after { msgs.pop(); }
- :
- Identifier -> ^(TOK_TABLE_OR_COL Identifier)
+ : Identifier
+ -> ^(TOK_TABLE_OR_COL Identifier)
;
//----------------------- Rules for parsing fromClause ------------------------------
// from [col1, col2, col3] table1, [col4, col5] table2
fromClause
@init { msgs.push("from clause"); }
@after { msgs.pop(); }
- :
- KW_FROM joinSource -> ^(TOK_FROM joinSource)
+ : KW_FROM joinSource
+ -> ^(TOK_FROM joinSource)
;
joinSource
@@ -224,8 +158,8 @@ joinSource
tableAlias
@init {msgs.push("table alias"); }
@after {msgs.pop(); }
- :
- Identifier -> ^(TOK_TABALIAS Identifier)
+ : Identifier
+ -> ^(TOK_TABALIAS Identifier)
;
fromSource
@@ -238,21 +172,20 @@ tableSource
@init { msgs.push("table source"); }
@after { msgs.pop(); }
: tabname=tableName (alias=Identifier)?
- -> ^(TOK_TABREF $tabname $alias?)
+ -> ^(TOK_TABREF $tabname $alias?)
;
tableName
@init { msgs.push("table name"); }
@after { msgs.pop(); }
: (db=Identifier DOT)? tab=Identifier
- -> ^(TOK_TABNAME $db? $tab)
+ -> ^(TOK_TABNAME $db? $tab)
;
constant
@init { msgs.push("constant"); }
@after { msgs.pop(); }
- :
- Number
+ : Number
| StringLiteral
| stringLiteralSequence
| BigintLiteral
@@ -262,31 +195,30 @@ constant
;
stringLiteralSequence
- :
- StringLiteral StringLiteral+ -> ^(TOK_STRINGLITERALSEQUENCE StringLiteral StringLiteral+)
+ : StringLiteral StringLiteral+
+ -> ^(TOK_STRINGLITERALSEQUENCE StringLiteral StringLiteral+)
;
charSetStringLiteral
@init { msgs.push("character string literal"); }
@after { msgs.pop(); }
- :
- csName=CharSetName csLiteral=CharSetLiteral -> ^(TOK_CHARSETLITERAL $csName $csLiteral)
+ : csName=CharSetName csLiteral=CharSetLiteral
+ -> ^(TOK_CHARSETLITERAL $csName $csLiteral)
;
tableOrPartition
- :
- tableName partitionSpec? -> ^(TOK_TAB tableName partitionSpec?)
+ : tableName partitionSpec?
+ -> ^(TOK_TAB tableName partitionSpec?)
;
partitionSpec
- :
- KW_PARTITION
- LPAREN partitionVal (COMMA partitionVal )* RPAREN -> ^(TOK_PARTSPEC partitionVal +)
+ : KW_PARTITION LPAREN partitionVal (COMMA partitionVal )* RPAREN
+ -> ^(TOK_PARTSPEC partitionVal +)
;
partitionVal
- :
- Identifier (EQUAL constant)? -> ^(TOK_PARTVAL Identifier constant?)
+ : Identifier (EQUAL constant)?
+ -> ^(TOK_PARTVAL Identifier constant?)
;
KW_ALL : 'ALL';
@@ -334,19 +266,16 @@ HexDigit
fragment
Digit
- :
- '0'..'9'
+ : '0'..'9'
;
fragment
Exponent
- :
- ('e' | 'E') ( PLUS|MINUS )? (Digit)+
+ : ('e' | 'E') ( PLUS|MINUS )? (Digit)+
;
StringLiteral
- :
- ( '\'' ( ~('\''|'\\') | ('\\' .) )* '\''
+ : ( '\'' ( ~('\''|'\\') | ('\\' .) )* '\''
| '\"' ( ~('\"'|'\\') | ('\\' .) )* '\"'
)+
;
@@ -7,6 +7,7 @@
public class HiveANTLRTest {
public static void main(String[] args) {
+// String query = "insert overwrite local directory '/tmp' select cols from table_y";
String query = "insert overwrite table table_x partition (dim_1='a', dim_2='b') select cols from table_y";
String query_2 = "insert overwrite table x select cols from y";
@@ -16,10 +17,10 @@ public static void main(String[] args) {
CommonTreeAdaptor adaptor = new CommonTreeAdaptor();
parser.setTreeAdaptor(adaptor);
- InsertParser.body_return r;
+ InsertParser.insertClause_return r;
try {
- r = parser.body();
+ r = parser.insertClause();
} catch (RecognitionException e) {
throw new RuntimeException("parse exception");
}

0 comments on commit b124bd2

Please sign in to comment.