PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/parse/Hive.g

#
text | 2309 lines | 2046 code | 263 blank | 0 comment | 0 complexity | bf8dc2eefb76e53ba4689ce71cab660b MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. grammar Hive;
  16. options
  17. {
  18. output=AST;
  19. ASTLabelType=CommonTree;
  20. backtrack=false;
  21. k=3;
  22. }
  23. tokens {
  24. TOK_INSERT;
  25. TOK_QUERY;
  26. TOK_SELECT;
  27. TOK_SELECTDI;
  28. TOK_SELEXPR;
  29. TOK_FROM;
  30. TOK_TAB;
  31. TOK_PARTSPEC;
  32. TOK_PARTVAL;
  33. TOK_DIR;
  34. TOK_LOCAL_DIR;
  35. TOK_TABREF;
  36. TOK_SUBQUERY;
  37. TOK_DESTINATION;
  38. TOK_ALLCOLREF;
  39. TOK_TABLE_OR_COL;
  40. TOK_FUNCTION;
  41. TOK_FUNCTIONDI;
  42. TOK_FUNCTIONSTAR;
  43. TOK_WHERE;
  44. TOK_OP_EQ;
  45. TOK_OP_NE;
  46. TOK_OP_LE;
  47. TOK_OP_LT;
  48. TOK_OP_GE;
  49. TOK_OP_GT;
  50. TOK_OP_DIV;
  51. TOK_OP_ADD;
  52. TOK_OP_SUB;
  53. TOK_OP_MUL;
  54. TOK_OP_MOD;
  55. TOK_OP_BITAND;
  56. TOK_OP_BITNOT;
  57. TOK_OP_BITOR;
  58. TOK_OP_BITXOR;
  59. TOK_OP_AND;
  60. TOK_OP_OR;
  61. TOK_OP_NOT;
  62. TOK_OP_LIKE;
  63. TOK_TRUE;
  64. TOK_FALSE;
  65. TOK_TRANSFORM;
  66. TOK_SERDE;
  67. TOK_SERDENAME;
  68. TOK_SERDEPROPS;
  69. TOK_EXPLIST;
  70. TOK_ALIASLIST;
  71. TOK_GROUPBY;
  72. TOK_HAVING;
  73. TOK_ORDERBY;
  74. TOK_CLUSTERBY;
  75. TOK_DISTRIBUTEBY;
  76. TOK_SORTBY;
  77. TOK_UNION;
  78. TOK_JOIN;
  79. TOK_LEFTOUTERJOIN;
  80. TOK_RIGHTOUTERJOIN;
  81. TOK_FULLOUTERJOIN;
  82. TOK_UNIQUEJOIN;
  83. TOK_LOAD;
  84. TOK_NULL;
  85. TOK_ISNULL;
  86. TOK_ISNOTNULL;
  87. TOK_TINYINT;
  88. TOK_SMALLINT;
  89. TOK_INT;
  90. TOK_BIGINT;
  91. TOK_BOOLEAN;
  92. TOK_FLOAT;
  93. TOK_DOUBLE;
  94. TOK_DATE;
  95. TOK_DATETIME;
  96. TOK_TIMESTAMP;
  97. TOK_STRING;
  98. TOK_LIST;
  99. TOK_STRUCT;
  100. TOK_MAP;
  101. TOK_UNIONTYPE;
  102. TOK_COLTYPELIST;
  103. TOK_CREATEDATABASE;
  104. TOK_CREATETABLE;
  105. TOK_CREATEINDEX;
  106. TOK_CREATEINDEX_INDEXTBLNAME;
  107. TOK_DEFERRED_REBUILDINDEX;
  108. TOK_DROPINDEX;
  109. TOK_LIKETABLE;
  110. TOK_DESCTABLE;
  111. TOK_DESCFUNCTION;
  112. TOK_ALTERTABLE_PARTITION;
  113. TOK_ALTERTABLE_RENAME;
  114. TOK_ALTERTABLE_ADDCOLS;
  115. TOK_ALTERTABLE_RENAMECOL;
  116. TOK_ALTERTABLE_REPLACECOLS;
  117. TOK_ALTERTABLE_ADDPARTS;
  118. TOK_ALTERTABLE_DROPPARTS;
  119. TOK_ALTERTABLE_ALTERPARTS_PROTECTMODE;
  120. TOK_ALTERTABLE_TOUCH;
  121. TOK_ALTERTABLE_ARCHIVE;
  122. TOK_ALTERTABLE_UNARCHIVE;
  123. TOK_ALTERTABLE_SERDEPROPERTIES;
  124. TOK_ALTERTABLE_SERIALIZER;
  125. TOK_TABLE_PARTITION;
  126. TOK_ALTERTABLE_FILEFORMAT;
  127. TOK_ALTERTABLE_LOCATION;
  128. TOK_ALTERTABLE_PROPERTIES;
  129. TOK_ALTERTABLE_CHANGECOL_AFTER_POSITION;
  130. TOK_ALTERINDEX_REBUILD;
  131. TOK_ALTERINDEX_PROPERTIES;
  132. TOK_MSCK;
  133. TOK_SHOWDATABASES;
  134. TOK_SHOWTABLES;
  135. TOK_SHOWFUNCTIONS;
  136. TOK_SHOWPARTITIONS;
  137. TOK_SHOW_TABLESTATUS;
  138. TOK_SHOWLOCKS;
  139. TOK_LOCKTABLE;
  140. TOK_UNLOCKTABLE;
  141. TOK_SWITCHDATABASE;
  142. TOK_DROPDATABASE;
  143. TOK_DROPTABLE;
  144. TOK_DATABASECOMMENT;
  145. TOK_TABCOLLIST;
  146. TOK_TABCOL;
  147. TOK_TABLECOMMENT;
  148. TOK_TABLEPARTCOLS;
  149. TOK_TABLEBUCKETS;
  150. TOK_TABLEROWFORMAT;
  151. TOK_TABLEROWFORMATFIELD;
  152. TOK_TABLEROWFORMATCOLLITEMS;
  153. TOK_TABLEROWFORMATMAPKEYS;
  154. TOK_TABLEROWFORMATLINES;
  155. TOK_TBLSEQUENCEFILE;
  156. TOK_TBLTEXTFILE;
  157. TOK_TBLRCFILE;
  158. TOK_TABLEFILEFORMAT;
  159. TOK_FILEFORMAT_GENERIC;
  160. TOK_OFFLINE;
  161. TOK_ENABLE;
  162. TOK_DISABLE;
  163. TOK_READONLY;
  164. TOK_NO_DROP;
  165. TOK_STORAGEHANDLER;
  166. TOK_ALTERTABLE_CLUSTER_SORT;
  167. TOK_TABCOLNAME;
  168. TOK_TABLELOCATION;
  169. TOK_PARTITIONLOCATION;
  170. TOK_TABLESAMPLE;
  171. TOK_TMP_FILE;
  172. TOK_TABSORTCOLNAMEASC;
  173. TOK_TABSORTCOLNAMEDESC;
  174. TOK_CHARSETLITERAL;
  175. TOK_CREATEFUNCTION;
  176. TOK_DROPFUNCTION;
  177. TOK_CREATEVIEW;
  178. TOK_DROPVIEW;
  179. TOK_ALTERVIEW_PROPERTIES;
  180. TOK_EXPLAIN;
  181. TOK_TABLESERIALIZER;
  182. TOK_TABLEPROPERTIES;
  183. TOK_TABLEPROPLIST;
  184. TOK_INDEXPROPERTIES;
  185. TOK_INDEXPROPLIST;
  186. TOK_TABTYPE;
  187. TOK_LIMIT;
  188. TOK_TABLEPROPERTY;
  189. TOK_IFEXISTS;
  190. TOK_IFNOTEXISTS;
  191. TOK_HINTLIST;
  192. TOK_HINT;
  193. TOK_MAPJOIN;
  194. TOK_STREAMTABLE;
  195. TOK_HOLD_DDLTIME;
  196. TOK_HINTARGLIST;
  197. TOK_USERSCRIPTCOLNAMES;
  198. TOK_USERSCRIPTCOLSCHEMA;
  199. TOK_RECORDREADER;
  200. TOK_RECORDWRITER;
  201. TOK_LEFTSEMIJOIN;
  202. TOK_LATERAL_VIEW;
  203. TOK_TABALIAS;
  204. TOK_ANALYZE;
  205. TOK_CREATEROLE;
  206. TOK_DROPROLE;
  207. TOK_GRANT;
  208. TOK_REVOKE;
  209. TOK_SHOW_GRANT;
  210. TOK_PRIVILEGE_LIST;
  211. TOK_PRIVILEGE;
  212. TOK_PRINCIPAL_NAME;
  213. TOK_USER;
  214. TOK_GROUP;
  215. TOK_ROLE;
  216. TOK_GRANT_WITH_OPTION;
  217. TOK_PRIV_ALL;
  218. TOK_PRIV_ALTER_METADATA;
  219. TOK_PRIV_ALTER_DATA;
  220. TOK_PRIV_DROP;
  221. TOK_PRIV_INDEX;
  222. TOK_PRIV_LOCK;
  223. TOK_PRIV_SELECT;
  224. TOK_PRIV_SHOW_DATABASE;
  225. TOK_PRIV_CREATE;
  226. TOK_PRIV_OBJECT;
  227. TOK_PRIV_OBJECT_COL;
  228. TOK_GRANT_ROLE;
  229. TOK_REVOKE_ROLE;
  230. TOK_SHOW_ROLE_GRANT;
  231. TOK_SHOWINDEXES;
  232. TOK_INDEXCOMMENT;
  233. TOK_DESCDATABASE;
  234. TOK_DATABASEPROPERTIES;
  235. TOK_DBPROPLIST;
  236. TOK_ALTERDATABASE_PROPERTIES;
  237. TOK_TABNAME;
  238. TOK_TABSRC;
  239. }
  240. // Package headers
  241. @header {
  242. package org.apache.hadoop.hive.ql.parse;
  243. }
  244. @lexer::header {package org.apache.hadoop.hive.ql.parse;}
  245. @members {
  246. Stack msgs = new Stack<String>();
  247. }
  248. @rulecatch {
  249. catch (RecognitionException e) {
  250. reportError(e);
  251. throw e;
  252. }
  253. }
  254. // starting rule
  255. statement
  256. : explainStatement EOF
  257. | execStatement EOF
  258. ;
  259. explainStatement
  260. @init { msgs.push("explain statement"); }
  261. @after { msgs.pop(); }
  262. : KW_EXPLAIN (isExtended=KW_EXTENDED)? execStatement -> ^(TOK_EXPLAIN execStatement $isExtended?)
  263. ;
  264. execStatement
  265. @init { msgs.push("statement"); }
  266. @after { msgs.pop(); }
  267. : queryStatementExpression
  268. | loadStatement
  269. | ddlStatement
  270. ;
  271. loadStatement
  272. @init { msgs.push("load statement"); }
  273. @after { msgs.pop(); }
  274. : KW_LOAD KW_DATA (islocal=KW_LOCAL)? KW_INPATH (path=StringLiteral) (isoverwrite=KW_OVERWRITE)? KW_INTO KW_TABLE (tab=tableOrPartition)
  275. -> ^(TOK_LOAD $path $tab $islocal? $isoverwrite?)
  276. ;
  277. ddlStatement
  278. @init { msgs.push("ddl statement"); }
  279. @after { msgs.pop(); }
  280. : createDatabaseStatement
  281. | switchDatabaseStatement
  282. | dropDatabaseStatement
  283. | createTableStatement
  284. | dropTableStatement
  285. | alterStatement
  286. | descStatement
  287. | showStatement
  288. | metastoreCheck
  289. | createViewStatement
  290. | dropViewStatement
  291. | createFunctionStatement
  292. | createIndexStatement
  293. | dropIndexStatement
  294. | dropFunctionStatement
  295. | analyzeStatement
  296. | lockStatement
  297. | unlockStatement
  298. | createRoleStatement
  299. | dropRoleStatement
  300. | grantPrivileges
  301. | revokePrivileges
  302. | showGrants
  303. | showRoleGrants
  304. | grantRole
  305. | revokeRole
  306. ;
  307. ifExists
  308. @init { msgs.push("if exists clause"); }
  309. @after { msgs.pop(); }
  310. : KW_IF KW_EXISTS
  311. -> ^(TOK_IFEXISTS)
  312. ;
  313. ifNotExists
  314. @init { msgs.push("if not exists clause"); }
  315. @after { msgs.pop(); }
  316. : KW_IF KW_NOT KW_EXISTS
  317. -> ^(TOK_IFNOTEXISTS)
  318. ;
  319. createDatabaseStatement
  320. @init { msgs.push("create database statement"); }
  321. @after { msgs.pop(); }
  322. : KW_CREATE (KW_DATABASE|KW_SCHEMA)
  323. ifNotExists?
  324. name=Identifier
  325. databaseComment?
  326. (KW_WITH KW_DBPROPERTIES dbprops=dbProperties)?
  327. -> ^(TOK_CREATEDATABASE $name ifNotExists? databaseComment? $dbprops?)
  328. ;
  329. dbProperties
  330. @init { msgs.push("dbproperties"); }
  331. @after { msgs.pop(); }
  332. :
  333. LPAREN dbPropertiesList RPAREN -> ^(TOK_DATABASEPROPERTIES dbPropertiesList)
  334. ;
  335. dbPropertiesList
  336. @init { msgs.push("database properties list"); }
  337. @after { msgs.pop(); }
  338. :
  339. keyValueProperty (COMMA keyValueProperty)* -> ^(TOK_DBPROPLIST keyValueProperty+)
  340. ;
  341. switchDatabaseStatement
  342. @init { msgs.push("switch database statement"); }
  343. @after { msgs.pop(); }
  344. : KW_USE Identifier
  345. -> ^(TOK_SWITCHDATABASE Identifier)
  346. ;
  347. dropDatabaseStatement
  348. @init { msgs.push("drop database statement"); }
  349. @after { msgs.pop(); }
  350. : KW_DROP (KW_DATABASE|KW_SCHEMA) ifExists? Identifier
  351. -> ^(TOK_DROPDATABASE Identifier ifExists?)
  352. ;
  353. databaseComment
  354. @init { msgs.push("database's comment"); }
  355. @after { msgs.pop(); }
  356. : KW_COMMENT comment=StringLiteral
  357. -> ^(TOK_DATABASECOMMENT $comment)
  358. ;
  359. createTableStatement
  360. @init { msgs.push("create table statement"); }
  361. @after { msgs.pop(); }
  362. : KW_CREATE (ext=KW_EXTERNAL)? KW_TABLE ifNotExists? name=tableName
  363. ( like=KW_LIKE likeName=tableName
  364. tableLocation?
  365. | (LPAREN columnNameTypeList RPAREN)?
  366. tableComment?
  367. tablePartition?
  368. tableBuckets?
  369. tableRowFormat?
  370. tableFileFormat?
  371. tableLocation?
  372. tablePropertiesPrefixed?
  373. (KW_AS selectStatement)?
  374. )
  375. -> ^(TOK_CREATETABLE $name $ext? ifNotExists?
  376. ^(TOK_LIKETABLE $likeName?)
  377. columnNameTypeList?
  378. tableComment?
  379. tablePartition?
  380. tableBuckets?
  381. tableRowFormat?
  382. tableFileFormat?
  383. tableLocation?
  384. tablePropertiesPrefixed?
  385. selectStatement?
  386. )
  387. ;
  388. createIndexStatement
  389. @init { msgs.push("create index statement");}
  390. @after {msgs.pop();}
  391. : KW_CREATE KW_INDEX indexName=Identifier
  392. KW_ON KW_TABLE tab=tableName LPAREN indexedCols=columnNameList RPAREN
  393. KW_AS typeName=StringLiteral
  394. autoRebuild?
  395. indexPropertiesPrefixed?
  396. indexTblName?
  397. tableRowFormat?
  398. tableFileFormat?
  399. tableLocation?
  400. tablePropertiesPrefixed?
  401. indexComment?
  402. ->^(TOK_CREATEINDEX $indexName $typeName $tab $indexedCols
  403. autoRebuild?
  404. indexPropertiesPrefixed?
  405. indexTblName?
  406. tableRowFormat?
  407. tableFileFormat?
  408. tableLocation?
  409. tablePropertiesPrefixed?
  410. indexComment?)
  411. ;
  412. indexComment
  413. @init { msgs.push("comment on an index");}
  414. @after {msgs.pop();}
  415. :
  416. KW_COMMENT comment=StringLiteral -> ^(TOK_INDEXCOMMENT $comment)
  417. ;
  418. autoRebuild
  419. @init { msgs.push("auto rebuild index");}
  420. @after {msgs.pop();}
  421. : KW_WITH KW_DEFERRED KW_REBUILD
  422. ->^(TOK_DEFERRED_REBUILDINDEX)
  423. ;
  424. indexTblName
  425. @init { msgs.push("index table name");}
  426. @after {msgs.pop();}
  427. : KW_IN KW_TABLE indexTbl=tableName
  428. ->^(TOK_CREATEINDEX_INDEXTBLNAME $indexTbl)
  429. ;
  430. indexPropertiesPrefixed
  431. @init { msgs.push("table properties with prefix"); }
  432. @after { msgs.pop(); }
  433. :
  434. KW_IDXPROPERTIES! indexProperties
  435. ;
  436. indexProperties
  437. @init { msgs.push("index properties"); }
  438. @after { msgs.pop(); }
  439. :
  440. LPAREN indexPropertiesList RPAREN -> ^(TOK_INDEXPROPERTIES indexPropertiesList)
  441. ;
  442. indexPropertiesList
  443. @init { msgs.push("index properties list"); }
  444. @after { msgs.pop(); }
  445. :
  446. keyValueProperty (COMMA keyValueProperty)* -> ^(TOK_INDEXPROPLIST keyValueProperty+)
  447. ;
  448. dropIndexStatement
  449. @init { msgs.push("drop index statement");}
  450. @after {msgs.pop();}
  451. : KW_DROP KW_INDEX ifExists? indexName=Identifier KW_ON tab=tableName
  452. ->^(TOK_DROPINDEX $indexName $tab ifExists?)
  453. ;
  454. dropTableStatement
  455. @init { msgs.push("drop statement"); }
  456. @after { msgs.pop(); }
  457. : KW_DROP KW_TABLE ifExists? tableName -> ^(TOK_DROPTABLE tableName ifExists?)
  458. ;
  459. alterStatement
  460. @init { msgs.push("alter statement"); }
  461. @after { msgs.pop(); }
  462. : KW_ALTER!
  463. (
  464. KW_TABLE! alterTableStatementSuffix
  465. |
  466. KW_VIEW! alterViewStatementSuffix
  467. |
  468. KW_INDEX! alterIndexStatementSuffix
  469. |
  470. KW_DATABASE! alterDatabaseStatementSuffix
  471. )
  472. ;
  473. alterTableStatementSuffix
  474. @init { msgs.push("alter table statement"); }
  475. @after { msgs.pop(); }
  476. : alterStatementSuffixRename
  477. | alterStatementSuffixAddCol
  478. | alterStatementSuffixRenameCol
  479. | alterStatementSuffixDropPartitions
  480. | alterStatementSuffixAddPartitions
  481. | alterStatementSuffixTouch
  482. | alterStatementSuffixArchive
  483. | alterStatementSuffixUnArchive
  484. | alterStatementSuffixProperties
  485. | alterStatementSuffixSerdeProperties
  486. | alterTblPartitionStatement
  487. | alterStatementSuffixClusterbySortby
  488. ;
  489. alterViewStatementSuffix
  490. @init { msgs.push("alter view statement"); }
  491. @after { msgs.pop(); }
  492. : alterViewSuffixProperties
  493. ;
  494. alterIndexStatementSuffix
  495. @init { msgs.push("alter index statement"); }
  496. @after { msgs.pop(); }
  497. : indexName=Identifier
  498. (KW_ON tableNameId=Identifier)
  499. partitionSpec?
  500. (
  501. KW_REBUILD
  502. ->^(TOK_ALTERINDEX_REBUILD $tableNameId $indexName partitionSpec?)
  503. |
  504. KW_SET KW_IDXPROPERTIES
  505. indexProperties
  506. ->^(TOK_ALTERINDEX_PROPERTIES $tableNameId $indexName indexProperties)
  507. )
  508. ;
  509. alterDatabaseStatementSuffix
  510. @init { msgs.push("alter database statement"); }
  511. @after { msgs.pop(); }
  512. : alterDatabaseSuffixProperties
  513. ;
  514. alterDatabaseSuffixProperties
  515. @init { msgs.push("alter database properties statement"); }
  516. @after { msgs.pop(); }
  517. : name=Identifier KW_SET KW_DBPROPERTIES dbProperties
  518. -> ^(TOK_ALTERDATABASE_PROPERTIES $name dbProperties)
  519. ;
  520. alterStatementSuffixRename
  521. @init { msgs.push("rename statement"); }
  522. @after { msgs.pop(); }
  523. : oldName=Identifier KW_RENAME KW_TO newName=Identifier
  524. -> ^(TOK_ALTERTABLE_RENAME $oldName $newName)
  525. ;
  526. alterStatementSuffixAddCol
  527. @init { msgs.push("add column statement"); }
  528. @after { msgs.pop(); }
  529. : Identifier (add=KW_ADD | replace=KW_REPLACE) KW_COLUMNS LPAREN columnNameTypeList RPAREN
  530. -> {$add != null}? ^(TOK_ALTERTABLE_ADDCOLS Identifier columnNameTypeList)
  531. -> ^(TOK_ALTERTABLE_REPLACECOLS Identifier columnNameTypeList)
  532. ;
  533. alterStatementSuffixRenameCol
  534. @init { msgs.push("rename column name"); }
  535. @after { msgs.pop(); }
  536. : Identifier KW_CHANGE KW_COLUMN? oldName=Identifier newName=Identifier colType (KW_COMMENT comment=StringLiteral)? alterStatementChangeColPosition?
  537. ->^(TOK_ALTERTABLE_RENAMECOL Identifier $oldName $newName colType $comment? alterStatementChangeColPosition?)
  538. ;
  539. alterStatementChangeColPosition
  540. : first=KW_FIRST|KW_AFTER afterCol=Identifier
  541. ->{$first != null}? ^(TOK_ALTERTABLE_CHANGECOL_AFTER_POSITION )
  542. -> ^(TOK_ALTERTABLE_CHANGECOL_AFTER_POSITION $afterCol)
  543. ;
  544. alterStatementSuffixAddPartitions
  545. @init { msgs.push("add partition statement"); }
  546. @after { msgs.pop(); }
  547. : Identifier KW_ADD ifNotExists? partitionSpec partitionLocation? (partitionSpec partitionLocation?)*
  548. -> ^(TOK_ALTERTABLE_ADDPARTS Identifier ifNotExists? (partitionSpec partitionLocation?)+)
  549. ;
  550. alterStatementSuffixTouch
  551. @init { msgs.push("touch statement"); }
  552. @after { msgs.pop(); }
  553. : Identifier KW_TOUCH (partitionSpec)*
  554. -> ^(TOK_ALTERTABLE_TOUCH Identifier (partitionSpec)*)
  555. ;
  556. alterStatementSuffixArchive
  557. @init { msgs.push("archive statement"); }
  558. @after { msgs.pop(); }
  559. : Identifier KW_ARCHIVE (partitionSpec)*
  560. -> ^(TOK_ALTERTABLE_ARCHIVE Identifier (partitionSpec)*)
  561. ;
  562. alterStatementSuffixUnArchive
  563. @init { msgs.push("unarchive statement"); }
  564. @after { msgs.pop(); }
  565. : Identifier KW_UNARCHIVE (partitionSpec)*
  566. -> ^(TOK_ALTERTABLE_UNARCHIVE Identifier (partitionSpec)*)
  567. ;
  568. partitionLocation
  569. @init { msgs.push("partition location"); }
  570. @after { msgs.pop(); }
  571. :
  572. KW_LOCATION locn=StringLiteral -> ^(TOK_PARTITIONLOCATION $locn)
  573. ;
  574. alterStatementSuffixDropPartitions
  575. @init { msgs.push("drop partition statement"); }
  576. @after { msgs.pop(); }
  577. : Identifier KW_DROP ifExists? partitionSpec (COMMA partitionSpec)*
  578. -> ^(TOK_ALTERTABLE_DROPPARTS Identifier partitionSpec+ ifExists?)
  579. ;
  580. alterStatementSuffixProperties
  581. @init { msgs.push("alter properties statement"); }
  582. @after { msgs.pop(); }
  583. : name=Identifier KW_SET KW_TBLPROPERTIES tableProperties
  584. -> ^(TOK_ALTERTABLE_PROPERTIES $name tableProperties)
  585. ;
  586. alterViewSuffixProperties
  587. @init { msgs.push("alter view properties statement"); }
  588. @after { msgs.pop(); }
  589. : name=Identifier KW_SET KW_TBLPROPERTIES tableProperties
  590. -> ^(TOK_ALTERVIEW_PROPERTIES $name tableProperties)
  591. ;
  592. alterStatementSuffixSerdeProperties
  593. @init { msgs.push("alter serdes statement"); }
  594. @after { msgs.pop(); }
  595. : name=Identifier KW_SET KW_SERDE serdeName=StringLiteral (KW_WITH KW_SERDEPROPERTIES tableProperties)?
  596. -> ^(TOK_ALTERTABLE_SERIALIZER $name $serdeName tableProperties?)
  597. | name=Identifier KW_SET KW_SERDEPROPERTIES tableProperties
  598. -> ^(TOK_ALTERTABLE_SERDEPROPERTIES $name tableProperties)
  599. ;
  600. tablePartitionPrefix
  601. @init {msgs.push("table partition prefix");}
  602. @after {msgs.pop();}
  603. :name=Identifier partitionSpec?
  604. ->^(TOK_TABLE_PARTITION $name partitionSpec?)
  605. ;
  606. alterTblPartitionStatement
  607. @init {msgs.push("alter table partition statement");}
  608. @after {msgs.pop();}
  609. : tablePartitionPrefix alterTblPartitionStatementSuffix
  610. -> ^(TOK_ALTERTABLE_PARTITION tablePartitionPrefix alterTblPartitionStatementSuffix)
  611. ;
  612. alterTblPartitionStatementSuffix
  613. @init {msgs.push("alter table partition statement suffix");}
  614. @after {msgs.pop();}
  615. : alterStatementSuffixFileFormat
  616. | alterStatementSuffixLocation
  617. | alterStatementSuffixProtectMode
  618. ;
  619. alterStatementSuffixFileFormat
  620. @init {msgs.push("alter fileformat statement"); }
  621. @after {msgs.pop();}
  622. : KW_SET KW_FILEFORMAT fileFormat
  623. -> ^(TOK_ALTERTABLE_FILEFORMAT fileFormat)
  624. ;
  625. alterStatementSuffixLocation
  626. @init {msgs.push("alter location");}
  627. @after {msgs.pop();}
  628. : KW_SET KW_LOCATION newLoc=StringLiteral
  629. -> ^(TOK_ALTERTABLE_LOCATION $newLoc)
  630. ;
  631. alterStatementSuffixProtectMode
  632. @init { msgs.push("alter partition protect mode statement"); }
  633. @after { msgs.pop(); }
  634. : alterProtectMode
  635. -> ^(TOK_ALTERTABLE_ALTERPARTS_PROTECTMODE alterProtectMode)
  636. ;
  637. alterProtectMode
  638. @init { msgs.push("protect mode specification enable"); }
  639. @after { msgs.pop(); }
  640. : KW_ENABLE alterProtectModeMode -> ^(TOK_ENABLE alterProtectModeMode)
  641. | KW_DISABLE alterProtectModeMode -> ^(TOK_DISABLE alterProtectModeMode)
  642. ;
  643. alterProtectModeMode
  644. @init { msgs.push("protect mode specification enable"); }
  645. @after { msgs.pop(); }
  646. : KW_OFFLINE -> ^(TOK_OFFLINE)
  647. | KW_NO_DROP -> ^(TOK_NO_DROP)
  648. | KW_READONLY -> ^(TOK_READONLY)
  649. ;
  650. alterStatementSuffixClusterbySortby
  651. @init {msgs.push("alter cluster by sort by statement");}
  652. @after{msgs.pop();}
  653. :name=Identifier tableBuckets
  654. ->^(TOK_ALTERTABLE_CLUSTER_SORT $name tableBuckets)
  655. |
  656. name=Identifier KW_NOT KW_CLUSTERED
  657. ->^(TOK_ALTERTABLE_CLUSTER_SORT $name)
  658. ;
  659. fileFormat
  660. @init { msgs.push("file format specification"); }
  661. @after { msgs.pop(); }
  662. : KW_SEQUENCEFILE -> ^(TOK_TBLSEQUENCEFILE)
  663. | KW_TEXTFILE -> ^(TOK_TBLTEXTFILE)
  664. | KW_RCFILE -> ^(TOK_TBLRCFILE)
  665. | KW_INPUTFORMAT inFmt=StringLiteral KW_OUTPUTFORMAT outFmt=StringLiteral (KW_INPUTDRIVER inDriver=StringLiteral KW_OUTPUTDRIVER outDriver=StringLiteral)?
  666. -> ^(TOK_TABLEFILEFORMAT $inFmt $outFmt $inDriver? $outDriver?)
  667. | genericSpec=Identifier -> ^(TOK_FILEFORMAT_GENERIC $genericSpec)
  668. ;
  669. tabTypeExpr
  670. @init { msgs.push("specifying table types"); }
  671. @after { msgs.pop(); }
  672. : Identifier (DOT^ (Identifier | KW_ELEM_TYPE | KW_KEY_TYPE | KW_VALUE_TYPE))*
  673. ;
  674. partTypeExpr
  675. @init { msgs.push("specifying table partitions"); }
  676. @after { msgs.pop(); }
  677. : tabTypeExpr partitionSpec? -> ^(TOK_TABTYPE tabTypeExpr partitionSpec?)
  678. ;
  679. descStatement
  680. @init { msgs.push("describe statement"); }
  681. @after { msgs.pop(); }
  682. : (KW_DESCRIBE|KW_DESC) (descOptions=KW_FORMATTED|descOptions=KW_EXTENDED)? (parttype=partTypeExpr) -> ^(TOK_DESCTABLE $parttype $descOptions?)
  683. | (KW_DESCRIBE|KW_DESC) KW_FUNCTION KW_EXTENDED? (name=descFuncNames) -> ^(TOK_DESCFUNCTION $name KW_EXTENDED?)
  684. | (KW_DESCRIBE|KW_DESC) KW_DATABASE KW_EXTENDED? (dbName=Identifier) -> ^(TOK_DESCDATABASE $dbName KW_EXTENDED?)
  685. ;
  686. analyzeStatement
  687. @init { msgs.push("analyze statement"); }
  688. @after { msgs.pop(); }
  689. : KW_ANALYZE KW_TABLE (parttype=tableOrPartition) KW_COMPUTE KW_STATISTICS -> ^(TOK_ANALYZE $parttype)
  690. ;
  691. showStatement
  692. @init { msgs.push("show statement"); }
  693. @after { msgs.pop(); }
  694. : KW_SHOW (KW_DATABASES|KW_SCHEMAS) (KW_LIKE showStmtIdentifier)? -> ^(TOK_SHOWDATABASES showStmtIdentifier?)
  695. | KW_SHOW KW_TABLES ((KW_FROM|KW_IN) db_name=Identifier)? (KW_LIKE showStmtIdentifier|showStmtIdentifier)? -> ^(TOK_SHOWTABLES (TOK_FROM $db_name)? showStmtIdentifier?)
  696. | KW_SHOW KW_FUNCTIONS showStmtIdentifier? -> ^(TOK_SHOWFUNCTIONS showStmtIdentifier?)
  697. | KW_SHOW KW_PARTITIONS Identifier partitionSpec? -> ^(TOK_SHOWPARTITIONS Identifier partitionSpec?)
  698. | KW_SHOW KW_TABLE KW_EXTENDED ((KW_FROM|KW_IN) db_name=Identifier)? KW_LIKE showStmtIdentifier partitionSpec?
  699. -> ^(TOK_SHOW_TABLESTATUS showStmtIdentifier $db_name? partitionSpec?)
  700. | KW_SHOW KW_LOCKS (parttype=partTypeExpr)? (isExtended=KW_EXTENDED)? -> ^(TOK_SHOWLOCKS $parttype? $isExtended?)
  701. | KW_SHOW (showOptions=KW_FORMATTED)? (KW_INDEX|KW_INDEXES) KW_ON showStmtIdentifier ((KW_FROM|KW_IN) db_name=Identifier)?
  702. -> ^(TOK_SHOWINDEXES showStmtIdentifier $showOptions? $db_name?)
  703. ;
  704. lockStatement
  705. @init { msgs.push("lock statement"); }
  706. @after { msgs.pop(); }
  707. : KW_LOCK KW_TABLE tableName partitionSpec? lockMode -> ^(TOK_LOCKTABLE tableName lockMode partitionSpec?)
  708. ;
  709. lockMode
  710. @init { msgs.push("lock mode"); }
  711. @after { msgs.pop(); }
  712. : KW_SHARED | KW_EXCLUSIVE
  713. ;
  714. unlockStatement
  715. @init { msgs.push("unlock statement"); }
  716. @after { msgs.pop(); }
  717. : KW_UNLOCK KW_TABLE tableName partitionSpec? -> ^(TOK_UNLOCKTABLE tableName partitionSpec?)
  718. ;
  719. createRoleStatement
  720. @init { msgs.push("create role"); }
  721. @after { msgs.pop(); }
  722. : KW_CREATE kwRole roleName=Identifier
  723. -> ^(TOK_CREATEROLE $roleName)
  724. ;
  725. dropRoleStatement
  726. @init {msgs.push("drop role");}
  727. @after {msgs.pop();}
  728. : KW_DROP kwRole roleName=Identifier
  729. -> ^(TOK_DROPROLE $roleName)
  730. ;
  731. grantPrivileges
  732. @init {msgs.push("grant privileges");}
  733. @after {msgs.pop();}
  734. : KW_GRANT privList=privilegeList
  735. privilegeObject?
  736. KW_TO principalSpecification
  737. (KW_WITH withOption)?
  738. -> ^(TOK_GRANT $privList principalSpecification privilegeObject? withOption?)
  739. ;
  740. revokePrivileges
  741. @init {msgs.push("revoke privileges");}
  742. @afer {msgs.pop();}
  743. : KW_REVOKE privilegeList privilegeObject? KW_FROM principalSpecification
  744. -> ^(TOK_REVOKE privilegeList principalSpecification privilegeObject?)
  745. ;
  746. grantRole
  747. @init {msgs.push("grant role");}
  748. @after {msgs.pop();}
  749. : KW_GRANT kwRole Identifier (COMMA Identifier)* KW_TO principalSpecification
  750. -> ^(TOK_GRANT_ROLE principalSpecification Identifier+)
  751. ;
  752. revokeRole
  753. @init {msgs.push("revoke role");}
  754. @after {msgs.pop();}
  755. : KW_REVOKE kwRole Identifier (COMMA Identifier)* KW_FROM principalSpecification
  756. -> ^(TOK_REVOKE_ROLE principalSpecification Identifier+)
  757. ;
  758. showRoleGrants
  759. @init {msgs.push("show role grants");}
  760. @after {msgs.pop();}
  761. : KW_SHOW kwRole KW_GRANT principalName
  762. -> ^(TOK_SHOW_ROLE_GRANT principalName)
  763. ;
  764. showGrants
  765. @init {msgs.push("show grants");}
  766. @after {msgs.pop();}
  767. : KW_SHOW KW_GRANT principalName privilegeIncludeColObject?
  768. -> ^(TOK_SHOW_GRANT principalName privilegeIncludeColObject?)
  769. ;
  770. privilegeIncludeColObject
  771. @init {msgs.push("privilege object including columns");}
  772. @after {msgs.pop();}
  773. : KW_ON (table=KW_TABLE|KW_DATABASE) Identifier (LPAREN cols=columnNameList RPAREN)? partitionSpec?
  774. -> ^(TOK_PRIV_OBJECT_COL Identifier $table? $cols? partitionSpec?)
  775. ;
  776. privilegeObject
  777. @init {msgs.push("privilege subject");}
  778. @after {msgs.pop();}
  779. : KW_ON (table=KW_TABLE|KW_DATABASE) Identifier partitionSpec?
  780. -> ^(TOK_PRIV_OBJECT Identifier $table? partitionSpec?)
  781. ;
  782. privilegeList
  783. @init {msgs.push("grant privilege list");}
  784. @after {msgs.pop();}
  785. : privlegeDef (COMMA privlegeDef)*
  786. -> ^(TOK_PRIVILEGE_LIST privlegeDef+)
  787. ;
  788. privlegeDef
  789. @init {msgs.push("grant privilege");}
  790. @after {msgs.pop();}
  791. : privilegeType (LPAREN cols=columnNameList RPAREN)?
  792. -> ^(TOK_PRIVILEGE privilegeType $cols?)
  793. ;
  794. privilegeType
  795. @init {msgs.push("privilege type");}
  796. @after {msgs.pop();}
  797. : KW_ALL -> ^(TOK_PRIV_ALL)
  798. | KW_ALTER -> ^(TOK_PRIV_ALTER_METADATA)
  799. | KW_UPDATE -> ^(TOK_PRIV_ALTER_DATA)
  800. | KW_CREATE -> ^(TOK_PRIV_CREATE)
  801. | KW_DROP -> ^(TOK_PRIV_DROP)
  802. | KW_INDEX -> ^(TOK_PRIV_INDEX)
  803. | KW_LOCK -> ^(TOK_PRIV_LOCK)
  804. | KW_SELECT -> ^(TOK_PRIV_SELECT)
  805. | KW_SHOW_DATABASE -> ^(TOK_PRIV_SHOW_DATABASE)
  806. ;
  807. principalSpecification
  808. @init { msgs.push("user/group/role name list"); }
  809. @after { msgs.pop(); }
  810. : principalName (COMMA principalName)* -> ^(TOK_PRINCIPAL_NAME principalName+)
  811. ;
  812. principalName
  813. @init {msgs.push("user|group|role name");}
  814. @after {msgs.pop();}
  815. : kwUser Identifier -> ^(TOK_USER Identifier)
  816. | KW_GROUP Identifier -> ^(TOK_GROUP Identifier)
  817. | kwRole Identifier -> ^(TOK_ROLE Identifier)
  818. ;
  819. withOption
  820. @init {msgs.push("grant with option");}
  821. @after {msgs.pop();}
  822. : KW_GRANT KW_OPTION
  823. -> ^(TOK_GRANT_WITH_OPTION)
  824. ;
  825. metastoreCheck
  826. @init { msgs.push("metastore check statement"); }
  827. @after { msgs.pop(); }
  828. : KW_MSCK (repair=KW_REPAIR)? (KW_TABLE table=Identifier partitionSpec? (COMMA partitionSpec)*)?
  829. -> ^(TOK_MSCK $repair? ($table partitionSpec*)?)
  830. ;
  831. createFunctionStatement
  832. @init { msgs.push("create function statement"); }
  833. @after { msgs.pop(); }
  834. : KW_CREATE KW_TEMPORARY KW_FUNCTION Identifier KW_AS StringLiteral
  835. -> ^(TOK_CREATEFUNCTION Identifier StringLiteral)
  836. ;
  837. dropFunctionStatement
  838. @init { msgs.push("drop temporary function statement"); }
  839. @after { msgs.pop(); }
  840. : KW_DROP KW_TEMPORARY KW_FUNCTION ifExists? Identifier
  841. -> ^(TOK_DROPFUNCTION Identifier ifExists?)
  842. ;
  843. createViewStatement
  844. @init {
  845. msgs.push("create view statement");
  846. }
  847. @after { msgs.pop(); }
  848. : KW_CREATE KW_VIEW ifNotExists? name=tableName
  849. (LPAREN columnNameCommentList RPAREN)? tableComment?
  850. tablePropertiesPrefixed?
  851. KW_AS
  852. selectStatement
  853. -> ^(TOK_CREATEVIEW $name ifNotExists?
  854. columnNameCommentList?
  855. tableComment?
  856. tablePropertiesPrefixed?
  857. selectStatement
  858. )
  859. ;
  860. dropViewStatement
  861. @init { msgs.push("drop view statement"); }
  862. @after { msgs.pop(); }
  863. : KW_DROP KW_VIEW ifExists? viewName -> ^(TOK_DROPVIEW viewName ifExists?)
  864. ;
  865. showStmtIdentifier
  866. @init { msgs.push("Identifier for show statement"); }
  867. @after { msgs.pop(); }
  868. : Identifier
  869. | StringLiteral
  870. ;
  871. tableComment
  872. @init { msgs.push("table's comment"); }
  873. @after { msgs.pop(); }
  874. :
  875. KW_COMMENT comment=StringLiteral -> ^(TOK_TABLECOMMENT $comment)
  876. ;
  877. tablePartition
  878. @init { msgs.push("table partition specification"); }
  879. @after { msgs.pop(); }
  880. : KW_PARTITIONED KW_BY LPAREN columnNameTypeList RPAREN
  881. -> ^(TOK_TABLEPARTCOLS columnNameTypeList)
  882. ;
  883. tableBuckets
  884. @init { msgs.push("table buckets specification"); }
  885. @after { msgs.pop(); }
  886. :
  887. KW_CLUSTERED KW_BY LPAREN bucketCols=columnNameList RPAREN (KW_SORTED KW_BY LPAREN sortCols=columnNameOrderList RPAREN)? KW_INTO num=Number KW_BUCKETS
  888. -> ^(TOK_TABLEBUCKETS $bucketCols $sortCols? $num)
  889. ;
  890. rowFormat
  891. @init { msgs.push("serde specification"); }
  892. @after { msgs.pop(); }
  893. : rowFormatSerde -> ^(TOK_SERDE rowFormatSerde)
  894. | rowFormatDelimited -> ^(TOK_SERDE rowFormatDelimited)
  895. | -> ^(TOK_SERDE)
  896. ;
  897. recordReader
  898. @init { msgs.push("record reader specification"); }
  899. @after { msgs.pop(); }
  900. : KW_RECORDREADER StringLiteral -> ^(TOK_RECORDREADER StringLiteral)
  901. | -> ^(TOK_RECORDREADER)
  902. ;
  903. recordWriter
  904. @init { msgs.push("record writer specification"); }
  905. @after { msgs.pop(); }
  906. : KW_RECORDWRITER StringLiteral -> ^(TOK_RECORDWRITER StringLiteral)
  907. | -> ^(TOK_RECORDWRITER)
  908. ;
  909. rowFormatSerde
  910. @init { msgs.push("serde format specification"); }
  911. @after { msgs.pop(); }
  912. : KW_ROW KW_FORMAT KW_SERDE name=StringLiteral (KW_WITH KW_SERDEPROPERTIES serdeprops=tableProperties)?
  913. -> ^(TOK_SERDENAME $name $serdeprops?)
  914. ;
  915. rowFormatDelimited
  916. @init { msgs.push("serde properties specification"); }
  917. @after { msgs.pop(); }
  918. :
  919. KW_ROW KW_FORMAT KW_DELIMITED tableRowFormatFieldIdentifier? tableRowFormatCollItemsIdentifier? tableRowFormatMapKeysIdentifier? tableRowFormatLinesIdentifier?
  920. -> ^(TOK_SERDEPROPS tableRowFormatFieldIdentifier? tableRowFormatCollItemsIdentifier? tableRowFormatMapKeysIdentifier? tableRowFormatLinesIdentifier?)
  921. ;
  922. tableRowFormat
  923. @init { msgs.push("table row format specification"); }
  924. @after { msgs.pop(); }
  925. :
  926. rowFormatDelimited
  927. -> ^(TOK_TABLEROWFORMAT rowFormatDelimited)
  928. | rowFormatSerde
  929. -> ^(TOK_TABLESERIALIZER rowFormatSerde)
  930. ;
  931. tablePropertiesPrefixed
  932. @init { msgs.push("table properties with prefix"); }
  933. @after { msgs.pop(); }
  934. :
  935. KW_TBLPROPERTIES! tableProperties
  936. ;
  937. tableProperties
  938. @init { msgs.push("table properties"); }
  939. @after { msgs.pop(); }
  940. :
  941. LPAREN tablePropertiesList RPAREN -> ^(TOK_TABLEPROPERTIES tablePropertiesList)
  942. ;
  943. tablePropertiesList
  944. @init { msgs.push("table properties list"); }
  945. @after { msgs.pop(); }
  946. :
  947. keyValueProperty (COMMA keyValueProperty)* -> ^(TOK_TABLEPROPLIST keyValueProperty+)
  948. ;
  949. keyValueProperty
  950. @init { msgs.push("specifying key/value property"); }
  951. @after { msgs.pop(); }
  952. :
  953. key=StringLiteral EQUAL value=StringLiteral -> ^(TOK_TABLEPROPERTY $key $value)
  954. ;
  955. tableRowFormatFieldIdentifier
  956. @init { msgs.push("table row format's field separator"); }
  957. @after { msgs.pop(); }
  958. :
  959. KW_FIELDS KW_TERMINATED KW_BY fldIdnt=StringLiteral (KW_ESCAPED KW_BY fldEscape=StringLiteral)?
  960. -> ^(TOK_TABLEROWFORMATFIELD $fldIdnt $fldEscape?)
  961. ;
  962. tableRowFormatCollItemsIdentifier
  963. @init { msgs.push("table row format's column separator"); }
  964. @after { msgs.pop(); }
  965. :
  966. KW_COLLECTION KW_ITEMS KW_TERMINATED KW_BY collIdnt=StringLiteral
  967. -> ^(TOK_TABLEROWFORMATCOLLITEMS $collIdnt)
  968. ;
  969. tableRowFormatMapKeysIdentifier
  970. @init { msgs.push("table row format's map key separator"); }
  971. @after { msgs.pop(); }
  972. :
  973. KW_MAP KW_KEYS KW_TERMINATED KW_BY mapKeysIdnt=StringLiteral
  974. -> ^(TOK_TABLEROWFORMATMAPKEYS $mapKeysIdnt)
  975. ;
  976. tableRowFormatLinesIdentifier
  977. @init { msgs.push("table row format's line separator"); }
  978. @after { msgs.pop(); }
  979. :
  980. KW_LINES KW_TERMINATED KW_BY linesIdnt=StringLiteral
  981. -> ^(TOK_TABLEROWFORMATLINES $linesIdnt)
  982. ;
  983. tableFileFormat
  984. @init { msgs.push("table file format specification"); }
  985. @after { msgs.pop(); }
  986. :
  987. KW_STORED KW_AS KW_SEQUENCEFILE -> TOK_TBLSEQUENCEFILE
  988. | KW_STORED KW_AS KW_TEXTFILE -> TOK_TBLTEXTFILE
  989. | KW_STORED KW_AS KW_RCFILE -> TOK_TBLRCFILE
  990. | KW_STORED KW_AS KW_INPUTFORMAT inFmt=StringLiteral KW_OUTPUTFORMAT outFmt=StringLiteral (KW_INPUTDRIVER inDriver=StringLiteral KW_OUTPUTDRIVER outDriver=StringLiteral)?
  991. -> ^(TOK_TABLEFILEFORMAT $inFmt $outFmt $inDriver? $outDriver?)
  992. | KW_STORED KW_BY storageHandler=StringLiteral
  993. (KW_WITH KW_SERDEPROPERTIES serdeprops=tableProperties)?
  994. -> ^(TOK_STORAGEHANDLER $storageHandler $serdeprops?)
  995. | KW_STORED KW_AS genericSpec=Identifier
  996. -> ^(TOK_FILEFORMAT_GENERIC $genericSpec)
  997. ;
  998. tableLocation
  999. @init { msgs.push("table location specification"); }
  1000. @after { msgs.pop(); }
  1001. :
  1002. KW_LOCATION locn=StringLiteral -> ^(TOK_TABLELOCATION $locn)
  1003. ;
  1004. columnNameTypeList
  1005. @init { msgs.push("column name type list"); }
  1006. @after { msgs.pop(); }
  1007. : columnNameType (COMMA columnNameType)* -> ^(TOK_TABCOLLIST columnNameType+)
  1008. ;
  1009. columnNameColonTypeList
  1010. @init { msgs.push("column name type list"); }
  1011. @after { msgs.pop(); }
  1012. : columnNameColonType (COMMA columnNameColonType)* -> ^(TOK_TABCOLLIST columnNameColonType+)
  1013. ;
  1014. columnNameList
  1015. @init { msgs.push("column name list"); }
  1016. @after { msgs.pop(); }
  1017. : columnName (COMMA columnName)* -> ^(TOK_TABCOLNAME columnName+)
  1018. ;
  1019. columnName
  1020. @init { msgs.push("column name"); }
  1021. @after { msgs.pop(); }
  1022. :
  1023. Identifier
  1024. ;
  1025. columnNameOrderList
  1026. @init { msgs.push("column name order list"); }
  1027. @after { msgs.pop(); }
  1028. : columnNameOrder (COMMA columnNameOrder)* -> ^(TOK_TABCOLNAME columnNameOrder+)
  1029. ;
  1030. columnNameOrder
  1031. @init { msgs.push("column name order"); }
  1032. @after { msgs.pop(); }
  1033. : Identifier (asc=KW_ASC | desc=KW_DESC)?
  1034. -> {$desc == null}? ^(TOK_TABSORTCOLNAMEASC Identifier)
  1035. -> ^(TOK_TABSORTCOLNAMEDESC Identifier)
  1036. ;
  1037. columnNameCommentList
  1038. @init { msgs.push("column name comment list"); }
  1039. @after { msgs.pop(); }
  1040. : columnNameComment (COMMA columnNameComment)* -> ^(TOK_TABCOLNAME columnNameComment+)
  1041. ;
  1042. columnNameComment
  1043. @init { msgs.push("column name comment"); }
  1044. @after { msgs.pop(); }
  1045. : colName=Identifier (KW_COMMENT comment=StringLiteral)?
  1046. -> ^(TOK_TABCOL $colName TOK_NULL $comment?)
  1047. ;
  1048. columnRefOrder
  1049. @init { msgs.push("column order"); }
  1050. @after { msgs.pop(); }
  1051. : expression (asc=KW_ASC | desc=KW_DESC)?
  1052. -> {$desc == null}? ^(TOK_TABSORTCOLNAMEASC expression)
  1053. -> ^(TOK_TABSORTCOLNAMEDESC expression)
  1054. ;
  1055. columnNameType
  1056. @init { msgs.push("column specification"); }
  1057. @after { msgs.pop(); }
  1058. : colName=Identifier colType (KW_COMMENT comment=StringLiteral)?
  1059. -> {$comment == null}? ^(TOK_TABCOL $colName colType)
  1060. -> ^(TOK_TABCOL $colName colType $comment)
  1061. ;
  1062. columnNameColonType
  1063. @init { msgs.push("column specification"); }
  1064. @after { msgs.pop(); }
  1065. : colName=Identifier COLON colType (KW_COMMENT comment=StringLiteral)?
  1066. -> {$comment == null}? ^(TOK_TABCOL $colName colType)
  1067. -> ^(TOK_TABCOL $colName colType $comment)
  1068. ;
  1069. colType
  1070. @init { msgs.push("column type"); }
  1071. @after { msgs.pop(); }
  1072. : type
  1073. ;
  1074. colTypeList
  1075. @init { msgs.push("column type list"); }
  1076. @after { msgs.pop(); }
  1077. : colType (COMMA colType)* -> ^(TOK_COLTYPELIST colType+)
  1078. ;
  1079. type
  1080. : primitiveType
  1081. | listType
  1082. | structType
  1083. | mapType
  1084. | unionType;
  1085. primitiveType
  1086. @init { msgs.push("primitive type specification"); }
  1087. @after { msgs.pop(); }
  1088. : KW_TINYINT -> TOK_TINYINT
  1089. | KW_SMALLINT -> TOK_SMALLINT
  1090. | KW_INT -> TOK_INT
  1091. | KW_BIGINT -> TOK_BIGINT
  1092. | KW_BOOLEAN -> TOK_BOOLEAN
  1093. | KW_FLOAT -> TOK_FLOAT
  1094. | KW_DOUBLE -> TOK_DOUBLE
  1095. | KW_DATE -> TOK_DATE
  1096. | KW_DATETIME -> TOK_DATETIME
  1097. | KW_TIMESTAMP -> TOK_TIMESTAMP
  1098. | KW_STRING -> TOK_STRING
  1099. ;
  1100. listType
  1101. @init { msgs.push("list type"); }
  1102. @after { msgs.pop(); }
  1103. : KW_ARRAY LESSTHAN type GREATERTHAN -> ^(TOK_LIST type)
  1104. ;
  1105. structType
  1106. @init { msgs.push("struct type"); }
  1107. @after { msgs.pop(); }
  1108. : KW_STRUCT LESSTHAN columnNameColonTypeList GREATERTHAN -> ^(TOK_STRUCT columnNameColonTypeList)
  1109. ;
  1110. mapType
  1111. @init { msgs.push("map type"); }
  1112. @after { msgs.pop(); }
  1113. : KW_MAP LESSTHAN left=primitiveType COMMA right=type GREATERTHAN
  1114. -> ^(TOK_MAP $left $right)
  1115. ;
  1116. unionType
  1117. @init { msgs.push("uniontype type"); }
  1118. @after { msgs.pop(); }
  1119. : KW_UNIONTYPE LESSTHAN colTypeList GREATERTHAN -> ^(TOK_UNIONTYPE colTypeList)
  1120. ;
  1121. queryOperator
  1122. @init { msgs.push("query operator"); }
  1123. @after { msgs.pop(); }
  1124. : KW_UNION KW_ALL -> ^(TOK_UNION)
  1125. ;
  1126. // select statement select ... from ... where ... group by ... order by ...
  1127. queryStatementExpression
  1128. : queryStatement (queryOperator^ queryStatement)*
  1129. ;
  1130. queryStatement
  1131. :
  1132. fromClause
  1133. ( b+=body )+ -> ^(TOK_QUERY fromClause body+)
  1134. | regular_body
  1135. ;
  1136. regular_body
  1137. :
  1138. insertClause
  1139. selectClause
  1140. fromClause
  1141. whereClause?
  1142. groupByClause?
  1143. havingClause?
  1144. orderByClause?
  1145. clusterByClause?
  1146. distributeByClause?
  1147. sortByClause?
  1148. limitClause? -> ^(TOK_QUERY fromClause ^(TOK_INSERT insertClause
  1149. selectClause whereClause? groupByClause? havingClause? orderByClause? clusterByClause?
  1150. distributeByClause? sortByClause? limitClause?))
  1151. |
  1152. selectStatement
  1153. ;
  1154. selectStatement
  1155. :
  1156. selectClause
  1157. fromClause
  1158. whereClause?
  1159. groupByClause?
  1160. havingClause?
  1161. orderByClause?
  1162. clusterByClause?
  1163. distributeByClause?
  1164. sortByClause?
  1165. limitClause? -> ^(TOK_QUERY fromClause ^(TOK_INSERT ^(TOK_DESTINATION ^(TOK_DIR TOK_TMP_FILE))
  1166. selectClause whereClause? groupByClause? havingClause? orderByClause? clusterByClause?
  1167. distributeByClause? sortByClause? limitClause?))
  1168. ;
  1169. body
  1170. :
  1171. insertClause
  1172. selectClause
  1173. whereClause?
  1174. groupByClause?
  1175. havingClause?
  1176. orderByClause?
  1177. clusterByClause?
  1178. distributeByClause?
  1179. sortByClause?
  1180. limitClause? -> ^(TOK_INSERT insertClause?
  1181. selectClause whereClause? groupByClause? havingClause? orderByClause? clusterByClause?
  1182. distributeByClause? sortByClause? limitClause?)
  1183. |
  1184. selectClause
  1185. whereClause?
  1186. groupByClause?
  1187. havingClause?
  1188. orderByClause?
  1189. clusterByClause?
  1190. distributeByClause?
  1191. sortByClause?
  1192. limitClause? -> ^(TOK_INSERT ^(TOK_DESTINATION ^(TOK_DIR TOK_TMP_FILE))
  1193. selectClause whereClause? groupByClause? havingClause? orderByClause? clusterByClause?
  1194. distributeByClause? sortByClause? limitClause?)
  1195. ;
  1196. insertClause
  1197. @init { msgs.push("insert clause"); }
  1198. @after { msgs.pop(); }
  1199. :
  1200. KW_INSERT KW_OVERWRITE destination -> ^(TOK_DESTINATION destination)
  1201. ;
  1202. destination
  1203. @init { msgs.push("destination specification"); }
  1204. @after { msgs.pop(); }
  1205. :
  1206. KW_LOCAL KW_DIRECTORY StringLiteral -> ^(TOK_LOCAL_DIR StringLiteral)
  1207. | KW_DIRECTORY StringLiteral -> ^(TOK_DIR StringLiteral)
  1208. | KW_TABLE tableOrPartition -> ^(tableOrPartition)
  1209. ;
  1210. limitClause
  1211. @init { msgs.push("limit clause"); }
  1212. @after { msgs.pop(); }
  1213. :
  1214. KW_LIMIT num=Number -> ^(TOK_LIMIT $num)
  1215. ;
  1216. //----------------------- Rules for parsing selectClause -----------------------------
  1217. // select a,b,c ...
  1218. selectClause
  1219. @init { msgs.push("select clause"); }
  1220. @after { msgs.pop(); }
  1221. :
  1222. KW_SELECT hintClause? (((KW_ALL | dist=KW_DISTINCT)? selectList)
  1223. | (transform=KW_TRANSFORM selectTrfmClause))
  1224. -> {$transform == null && $dist == null}? ^(TOK_SELECT hintClause? selectList)
  1225. -> {$transform == null && $dist != null}? ^(TOK_SELECTDI hintClause? selectList)
  1226. -> ^(TOK_SELECT hintClause? ^(TOK_SELEXPR selectTrfmClause) )
  1227. |
  1228. trfmClause ->^(TOK_SELECT ^(TOK_SELEXPR trfmClause))
  1229. ;
  1230. selectList
  1231. @init { msgs.push("select list"); }
  1232. @after { msgs.pop(); }
  1233. :
  1234. selectItem ( COMMA selectItem )* -> selectItem+
  1235. ;
  1236. selectTrfmClause
  1237. @init { msgs.push("transform clause"); }
  1238. @after { msgs.pop(); }
  1239. :
  1240. LPAREN selectExpressionList RPAREN
  1241. inSerde=rowFormat inRec=recordWriter
  1242. KW_USING StringLiteral
  1243. ( KW_AS ((LPAREN (aliasList | columnNameTypeList) RPAREN) | (aliasList | columnNameTypeList)))?
  1244. outSerde=rowFormat outRec=recordReader
  1245. -> ^(TOK_TRANSFORM selectExpressionList $inSerde $inRec StringLiteral $outSerde $outRec aliasList? columnNameTypeList?)
  1246. ;
  1247. hintClause
  1248. @init { msgs.push("hint clause"); }
  1249. @after { msgs.pop(); }
  1250. :
  1251. DIVIDE STAR PLUS hintList STAR DIVIDE -> ^(TOK_HINTLIST hintList)
  1252. ;
  1253. hintList
  1254. @init { msgs.push("hint list"); }
  1255. @after { msgs.pop(); }
  1256. :
  1257. hintItem (COMMA hintItem)* -> hintItem+
  1258. ;
  1259. hintItem
  1260. @init { msgs.push("hint item"); }
  1261. @after { msgs.pop(); }
  1262. :
  1263. hintName (LPAREN hintArgs RPAREN)? -> ^(TOK_HINT hintName hintArgs?)
  1264. ;
  1265. hintName
  1266. @init { msgs.push("hint name"); }
  1267. @after { msgs.pop(); }
  1268. :
  1269. KW_MAPJOIN -> TOK_MAPJOIN
  1270. | KW_STREAMTABLE -> TOK_STREAMTABLE
  1271. | KW_HOLD_DDLTIME -> TOK_HOLD_DDLTIME
  1272. ;
  1273. hintArgs
  1274. @init { msgs.push("hint arguments"); }
  1275. @after { msgs.pop(); }
  1276. :
  1277. hintArgName (COMMA hintArgName)* -> ^(TOK_HINTARGLIST hintArgName+)
  1278. ;
  1279. hintArgName
  1280. @init { msgs.push("hint argument name"); }
  1281. @after { msgs.pop(); }
  1282. :
  1283. Identifier
  1284. ;
  1285. selectItem
  1286. @init { msgs.push("selection target"); }
  1287. @after { msgs.pop(); }
  1288. :
  1289. ( selectExpression ((KW_AS? Identifier) | (KW_AS LPAREN Identifier (COMMA Identifier)* RPAREN))?) -> ^(TOK_SELEXPR selectExpression Identifier*)
  1290. ;
  1291. trfmClause
  1292. @init { msgs.push("transform clause"); }
  1293. @after { msgs.pop(); }
  1294. :
  1295. ( KW_MAP selectExpressionList
  1296. | KW_REDUCE selectExpressionList )
  1297. inSerde=rowFormat inRec=recordWriter
  1298. KW_USING StringLiteral
  1299. ( KW_AS ((LPAREN (aliasList | columnNameTypeList) RPAREN) | (aliasList | columnNameTypeList)))?
  1300. outSerde=rowFormat outRec=recordReader
  1301. -> ^(TOK_TRANSFORM selectExpressionList $inSerde $inRec StringLiteral $outSerde $outRec aliasList? columnNameTypeList?)
  1302. ;
  1303. selectExpression
  1304. @init { msgs.push("select expression"); }
  1305. @after { msgs.pop(); }
  1306. :
  1307. expression | tableAllColumns
  1308. ;
  1309. selectExpressionList
  1310. @init { msgs.push("select expression list"); }
  1311. @after { msgs.pop(); }
  1312. :
  1313. selectExpression (COMMA selectExpression)* -> ^(TOK_EXPLIST selectExpression+)
  1314. ;
  1315. //-----------------------------------------------------------------------------------
  1316. tableAllColumns
  1317. : STAR
  1318. -> ^(TOK_ALLCOLREF)
  1319. | tableName DOT STAR
  1320. -> ^(TOK_ALLCOLREF tableName)
  1321. ;
  1322. // (table|column)
  1323. tableOrColumn
  1324. @init { msgs.push("table or column identifier"); }
  1325. @after { msgs.pop(); }
  1326. :
  1327. Identifier -> ^(TOK_TABLE_OR_COL Identifier)
  1328. ;
  1329. expressionList
  1330. @init { msgs.push("expression list"); }
  1331. @after { msgs.pop(); }
  1332. :
  1333. expression (COMMA expression)* -> ^(TOK_EXPLIST expression+)
  1334. ;
  1335. aliasList
  1336. @init { msgs.push("alias list"); }
  1337. @after { msgs.pop(); }
  1338. :
  1339. Identifier (COMMA Identifier)* -> ^(TOK_ALIASLIST Identifier+)
  1340. ;
  1341. //----------------------- Rules for parsing fromClause ------------------------------
  1342. // from [col1, col2, col3] table1, [col4, col5] table2
  1343. fromClause
  1344. @init { msgs.push("from clause"); }
  1345. @after { msgs.pop(); }
  1346. :
  1347. KW_FROM joinSource -> ^(TOK_FROM joinSource)
  1348. ;
  1349. joinSource
  1350. @init { msgs.push("join source"); }
  1351. @after { msgs.pop(); }
  1352. : fromSource ( joinToken^ fromSource (KW_ON! expression)? )*
  1353. | uniqueJoinToken^ uniqueJoinSource (COMMA! uniqueJoinSource)+
  1354. ;
  1355. uniqueJoinSource
  1356. @init { msgs.push("join source"); }
  1357. @after { msgs.pop(); }
  1358. : KW_PRESERVE? fromSource uniqueJoinExpr
  1359. ;
  1360. uniqueJoinExpr
  1361. @init { msgs.push("unique join expression list"); }
  1362. @after { msgs.pop(); }
  1363. : LPAREN e1+=expression (COMMA e1+=expression)* RPAREN
  1364. -> ^(TOK_EXPLIST $e1*)
  1365. ;
  1366. uniqueJoinToken
  1367. @init { msgs.push("unique join"); }
  1368. @after { msgs.pop(); }
  1369. : KW_UNIQUEJOIN -> TOK_UNIQUEJOIN;
  1370. joinToken
  1371. @init { msgs.push("join type specifier"); }
  1372. @after { msgs.pop(); }
  1373. :
  1374. KW_JOIN -> TOK_JOIN
  1375. | KW_LEFT KW_OUTER KW_JOIN -> TOK_LEFTOUTERJOIN
  1376. | KW_RIGHT KW_OUTER KW_JOIN -> TOK_RIGHTOUTERJOIN
  1377. | KW_FULL KW_OUTER KW_JOIN -> TOK_FULLOUTERJOIN
  1378. | KW_LEFT KW_SEMI KW_JOIN -> TOK_LEFTSEMIJOIN
  1379. ;
  1380. lateralView
  1381. @init {msgs.push("lateral view"); }
  1382. @after {msgs.pop(); }
  1383. :
  1384. KW_LATERAL KW_VIEW function tableAlias KW_AS Identifier (COMMA Identifier)* -> ^(TOK_LATERAL_VIEW ^(TOK_SELECT ^(TOK_SELEXPR function Identifier+ tableAlias)))
  1385. ;
  1386. tableAlias
  1387. @init {msgs.push("table alias"); }
  1388. @after {msgs.pop(); }
  1389. :
  1390. Identifier -> ^(TOK_TABALIAS Identifier)
  1391. ;
  1392. fromSource
  1393. @init { msgs.push("from source"); }
  1394. @after { msgs.pop(); }
  1395. :
  1396. (tableSource | subQuerySource) (lateralView^)*
  1397. ;
  1398. tableSample
  1399. @init { msgs.push("table sample specification"); }
  1400. @after { msgs.pop(); }
  1401. :
  1402. KW_TABLESAMPLE LPAREN KW_BUCKET (numerator=Number) KW_OUT KW_OF (denominator=Number) (KW_ON expr+=expression (COMMA expr+=expression)*)? RPAREN -> ^(TOK_TABLESAMPLE $numerator $denominator $expr*)
  1403. ;
  1404. tableSource
  1405. @init { msgs.push("table source"); }
  1406. @after { msgs.pop(); }
  1407. : tabname=tableName (ts=tableSample)? (alias=Identifier)?
  1408. -> ^(TOK_TABREF $tabname $ts? $alias?)
  1409. ;
  1410. tableName
  1411. @init { msgs.push("table name"); }
  1412. @after { msgs.pop(); }
  1413. : (db=Identifier DOT)? tab=Identifier
  1414. -> ^(TOK_TABNAME $db? $tab)
  1415. ;
  1416. viewName
  1417. @init { msgs.push("view name"); }
  1418. @after { msgs.pop(); }
  1419. :
  1420. (db=Identifier DOT)? view=Identifier
  1421. -> ^(TOK_TABNAME $db? $view)
  1422. ;
  1423. subQuerySource
  1424. @init { msgs.push("subquery source"); }
  1425. @after { msgs.pop(); }
  1426. :
  1427. LPAREN queryStatementExpression RPAREN Identifier -> ^(TOK_SUBQUERY queryStatementExpression Identifier)
  1428. ;
  1429. //----------------------- Rules for parsing whereClause -----------------------------
  1430. // where a=b and ...
  1431. whereClause
  1432. @init { msgs.push("where clause"); }
  1433. @after { msgs.pop(); }
  1434. :
  1435. KW_WHERE searchCondition -> ^(TOK_WHERE searchCondition)
  1436. ;
  1437. searchCondition
  1438. @init { msgs.push("search condition"); }
  1439. @after { msgs.pop(); }
  1440. :
  1441. expression
  1442. ;
  1443. //-----------------------------------------------------------------------------------
  1444. // group by a,b
  1445. groupByClause
  1446. @init { msgs.push("group by clause"); }
  1447. @after { msgs.pop(); }
  1448. :
  1449. KW_GROUP KW_BY
  1450. groupByExpression
  1451. ( COMMA groupByExpression )*
  1452. -> ^(TOK_GROUPBY groupByExpression+)
  1453. ;
  1454. groupByExpression
  1455. @init { msgs.push("group by expression"); }
  1456. @after { msgs.pop(); }
  1457. :
  1458. expression
  1459. ;
  1460. havingClause
  1461. @init { msgs.push("having clause"); }
  1462. @after { msgs.pop(); }
  1463. :
  1464. KW_HAVING havingCondition -> ^(TOK_HAVING havingCondition)
  1465. ;
  1466. havingCondition
  1467. @init { msgs.push("having condition"); }
  1468. @after { msgs.pop(); }
  1469. :
  1470. expression
  1471. ;
  1472. // order by a,b
  1473. orderByClause
  1474. @init { msgs.push("order by clause"); }
  1475. @after { msgs.pop(); }
  1476. :
  1477. KW_ORDER KW_BY
  1478. columnRefOrder
  1479. ( COMMA columnRefOrder)* -> ^(TOK_ORDERBY columnRefOrder+)
  1480. ;
  1481. clusterByClause
  1482. @init { msgs.push("cluster by clause"); }
  1483. @after { msgs.pop(); }
  1484. :
  1485. KW_CLUSTER KW_BY
  1486. expression
  1487. ( COMMA expression )* -> ^(TOK_CLUSTERBY expression+)
  1488. ;
  1489. distributeByClause
  1490. @init { msgs.push("distribute by clause"); }
  1491. @after { msgs.pop(); }
  1492. :
  1493. KW_DISTRIBUTE KW_BY
  1494. expression (COMMA expression)* -> ^(TOK_DISTRIBUTEBY expression+)
  1495. ;
  1496. sortByClause
  1497. @init { msgs.push("sort by clause"); }
  1498. @after { msgs.pop(); }
  1499. :
  1500. KW_SORT KW_BY
  1501. columnRefOrder
  1502. ( COMMA columnRefOrder)* -> ^(TOK_SORTBY columnRefOrder+)
  1503. ;
  1504. // fun(par1, par2, par3)
  1505. function
  1506. @init { msgs.push("function specification"); }
  1507. @after { msgs.pop(); }
  1508. :
  1509. functionName
  1510. LPAREN
  1511. (
  1512. (star=STAR)
  1513. | (dist=KW_DISTINCT)? (expression (COMMA expression)*)?
  1514. )
  1515. RPAREN -> {$star != null}? ^(TOK_FUNCTIONSTAR functionName)
  1516. -> {$dist == null}? ^(TOK_FUNCTION functionName (expression+)?)
  1517. -> ^(TOK_FUNCTIONDI functionName (expression+)?)
  1518. ;
  1519. functionName
  1520. @init { msgs.push("function name"); }
  1521. @after { msgs.pop(); }
  1522. : // Keyword IF is also a function name
  1523. Identifier | KW_IF | KW_ARRAY | KW_MAP | KW_STRUCT | KW_UNIONTYPE
  1524. ;
  1525. castExpression
  1526. @init { msgs.push("cast expression"); }
  1527. @after { msgs.pop(); }
  1528. :
  1529. KW_CAST
  1530. LPAREN
  1531. expression
  1532. KW_AS
  1533. primitiveType
  1534. RPAREN -> ^(TOK_FUNCTION primitiveType expression)
  1535. ;
  1536. caseExpression
  1537. @init { msgs.push("case expression"); }
  1538. @after { msgs.pop(); }
  1539. :
  1540. KW_CASE expression
  1541. (KW_WHEN expression KW_THEN expression)+
  1542. (KW_ELSE expression)?
  1543. KW_END -> ^(TOK_FUNCTION KW_CASE expression*)
  1544. ;
  1545. whenExpression
  1546. @init { msgs.push("case expression"); }
  1547. @after { msgs.pop(); }
  1548. :
  1549. KW_CASE
  1550. ( KW_WHEN expression KW_THEN expression)+
  1551. (KW_ELSE expression)?
  1552. KW_END -> ^(TOK_FUNCTION KW_WHEN expression*)
  1553. ;
  1554. constant
  1555. @init { msgs.push("constant"); }
  1556. @after { msgs.pop(); }
  1557. :
  1558. Number
  1559. | StringLiteral
  1560. | charSetStringLiteral
  1561. | booleanValue
  1562. ;
  1563. charSetStringLiteral
  1564. @init { msgs.push("character string literal"); }
  1565. @after { msgs.pop(); }
  1566. :
  1567. csName=CharSetName csLiteral=CharSetLiteral -> ^(TOK_CHARSETLITERAL $csName $csLiteral)
  1568. ;
  1569. expression
  1570. @init { msgs.push("expression specification"); }
  1571. @after { msgs.pop(); }
  1572. :
  1573. precedenceOrExpression
  1574. ;
  1575. atomExpression
  1576. :
  1577. KW_NULL -> TOK_NULL
  1578. | constant
  1579. | function
  1580. | castExpression
  1581. | caseExpression
  1582. | whenExpression
  1583. | tableOrColumn
  1584. | LPAREN! expression RPAREN!
  1585. ;
  1586. precedenceFieldExpression
  1587. :
  1588. atomExpression ((LSQUARE^ expression RSQUARE!) | (DOT^ Identifier))*
  1589. ;
  1590. precedenceUnaryOperator
  1591. :
  1592. PLUS | MINUS | TILDE
  1593. ;
  1594. nullCondition
  1595. :
  1596. KW_NULL -> ^(TOK_ISNULL)
  1597. | KW_NOT KW_NULL -> ^(TOK_ISNOTNULL)
  1598. ;
  1599. precedenceUnaryPrefixExpression
  1600. :
  1601. (precedenceUnaryOperator^)* precedenceFieldExpression
  1602. ;
  1603. precedenceUnarySuffixExpression
  1604. : precedenceUnaryPrefixExpression (a=KW_IS nullCondition)?
  1605. -> {$a != null}? ^(TOK_FUNCTION nullCondition precedenceUnaryPrefixExpression)
  1606. -> precedenceUnaryPrefixExpression
  1607. ;
  1608. precedenceBitwiseXorOperator
  1609. :
  1610. BITWISEXOR
  1611. ;
  1612. precedenceBitwiseXorExpression
  1613. :
  1614. precedenceUnarySuffixExpression (precedenceBitwiseXorOperator^ precedenceUnarySuffixExpression)*
  1615. ;
  1616. precedenceStarOperator
  1617. :
  1618. STAR | DIVIDE | MOD | DIV
  1619. ;
  1620. precedenceStarExpression
  1621. :
  1622. precedenceBitwiseXorExpression (precedenceStarOperator^ precedenceBitwiseXorExpression)*
  1623. ;
  1624. precedencePlusOperator
  1625. :
  1626. PLUS | MINUS
  1627. ;
  1628. precedencePlusExpression
  1629. :
  1630. precedenceStarExpression (precedencePlusOperator^ precedenceStarExpression)*
  1631. ;
  1632. precedenceAmpersandOperator
  1633. :
  1634. AMPERSAND
  1635. ;
  1636. precedenceAmpersandExpression
  1637. :
  1638. precedencePlusExpression (precedenceAmpersandOperator^ precedencePlusExpression)*
  1639. ;
  1640. precedenceBitwiseOrOperator
  1641. :
  1642. BITWISEOR
  1643. ;
  1644. precedenceBitwiseOrExpression
  1645. :
  1646. precedenceAmpersandExpression (precedenceBitwiseOrOperator^ precedenceAmpersandExpression)*
  1647. ;
  1648. precedenceEqualOperator
  1649. :
  1650. EQUAL | NOTEQUAL | LESSTHANOREQUALTO | LESSTHAN | GREATERTHANOREQUALTO | GREATERTHAN
  1651. | KW_LIKE | KW_RLIKE | KW_REGEXP
  1652. ;
  1653. precedenceEqualExpression
  1654. :
  1655. precedenceBitwiseOrExpression ( (precedenceEqualOperator^ precedenceBitwiseOrExpression) | (inOperator^ expressions) )*
  1656. ;
  1657. inOperator
  1658. :
  1659. KW_IN -> ^(TOK_FUNCTION KW_IN)
  1660. ;
  1661. expressions
  1662. :
  1663. LPAREN expression (COMMA expression)* RPAREN -> expression*
  1664. ;
  1665. precedenceNotOperator
  1666. :
  1667. KW_NOT
  1668. ;
  1669. precedenceNotExpression
  1670. :
  1671. (precedenceNotOperator^)* precedenceEqualExpression
  1672. ;
  1673. precedenceAndOperator
  1674. :
  1675. KW_AND
  1676. ;
  1677. precedenceAndExpression
  1678. :
  1679. precedenceNotExpression (precedenceAndOperator^ precedenceNotExpression)*
  1680. ;
  1681. precedenceOrOperator
  1682. :
  1683. KW_OR
  1684. ;
  1685. precedenceOrExpression
  1686. :
  1687. precedenceAndExpression (precedenceOrOperator^ precedenceAndExpression)*
  1688. ;
  1689. booleanValue
  1690. :
  1691. KW_TRUE^ | KW_FALSE^
  1692. ;
  1693. tableOrPartition
  1694. :
  1695. tableName partitionSpec? -> ^(TOK_TAB tableName partitionSpec?)
  1696. ;
  1697. partitionSpec
  1698. :
  1699. KW_PARTITION
  1700. LPAREN partitionVal (COMMA partitionVal )* RPAREN -> ^(TOK_PARTSPEC partitionVal +)
  1701. ;
  1702. partitionVal
  1703. :
  1704. Identifier (EQUAL constant)? -> ^(TOK_PARTVAL Identifier constant?)
  1705. ;
  1706. sysFuncNames
  1707. :
  1708. KW_AND
  1709. | KW_OR
  1710. | KW_NOT
  1711. | KW_LIKE
  1712. | KW_IF
  1713. | KW_CASE
  1714. | KW_WHEN
  1715. | KW_TINYINT
  1716. | KW_SMALLINT
  1717. | KW_INT
  1718. | KW_BIGINT
  1719. | KW_FLOAT
  1720. | KW_DOUBLE
  1721. | KW_BOOLEAN
  1722. | KW_STRING
  1723. | KW_ARRAY
  1724. | KW_MAP
  1725. | KW_STRUCT
  1726. | KW_UNIONTYPE
  1727. | EQUAL
  1728. | NOTEQUAL
  1729. | LESSTHANOREQUALTO
  1730. | LESSTHAN
  1731. | GREATERTHANOREQUALTO
  1732. | GREATERTHAN
  1733. | DIVIDE
  1734. | PLUS
  1735. | MINUS
  1736. | STAR
  1737. | MOD
  1738. | DIV
  1739. | AMPERSAND
  1740. | TILDE
  1741. | BITWISEOR
  1742. | BITWISEXOR
  1743. | KW_RLIKE
  1744. | KW_REGEXP
  1745. | KW_IN
  1746. ;
  1747. descFuncNames
  1748. :
  1749. sysFuncNames
  1750. | StringLiteral
  1751. | Identifier
  1752. ;
  1753. // Keywords
  1754. kwUser
  1755. :
  1756. {input.LT(1).getText().equalsIgnoreCase("user")}? Identifier;
  1757. kwRole
  1758. :
  1759. {input.LT(1).getText().equalsIgnoreCase("role")}? Identifier;
  1760. KW_TRUE : 'TRUE';
  1761. KW_FALSE : 'FALSE';
  1762. KW_ALL : 'ALL';
  1763. KW_AND : 'AND';
  1764. KW_OR : 'OR';
  1765. KW_NOT : 'NOT' | '!';
  1766. KW_LIKE : 'LIKE';
  1767. KW_IF : 'IF';
  1768. KW_EXISTS : 'EXISTS';
  1769. KW_ASC : 'ASC';
  1770. KW_DESC : 'DESC';
  1771. KW_ORDER : 'ORDER';
  1772. KW_GROUP : 'GROUP';
  1773. KW_BY : 'BY';
  1774. KW_HAVING : 'HAVING';
  1775. KW_WHERE : 'WHERE';
  1776. KW_FROM : 'FROM';
  1777. KW_AS : 'AS';
  1778. KW_SELECT : 'SELECT';
  1779. KW_DISTINCT : 'DISTINCT';
  1780. KW_INSERT : 'INSERT';
  1781. KW_OVERWRITE : 'OVERWRITE';
  1782. KW_OUTER : 'OUTER';
  1783. KW_UNIQUEJOIN : 'UNIQUEJOIN';
  1784. KW_PRESERVE : 'PRESERVE';
  1785. KW_JOIN : 'JOIN';
  1786. KW_LEFT : 'LEFT';
  1787. KW_RIGHT : 'RIGHT';
  1788. KW_FULL : 'FULL';
  1789. KW_ON : 'ON';
  1790. KW_PARTITION : 'PARTITION';
  1791. KW_PARTITIONS : 'PARTITIONS';
  1792. KW_TABLE: 'TABLE';
  1793. KW_TABLES: 'TABLES';
  1794. KW_INDEX: 'INDEX';
  1795. KW_INDEXES: 'INDEXES';
  1796. KW_REBUILD: 'REBUILD';
  1797. KW_FUNCTIONS: 'FUNCTIONS';
  1798. KW_SHOW: 'SHOW';
  1799. KW_MSCK: 'MSCK';
  1800. KW_REPAIR: 'REPAIR';
  1801. KW_DIRECTORY: 'DIRECTORY';
  1802. KW_LOCAL: 'LOCAL';
  1803. KW_TRANSFORM : 'TRANSFORM';
  1804. KW_USING: 'USING';
  1805. KW_CLUSTER: 'CLUSTER';
  1806. KW_DISTRIBUTE: 'DISTRIBUTE';
  1807. KW_SORT: 'SORT';
  1808. KW_UNION: 'UNION';
  1809. KW_LOAD: 'LOAD';
  1810. KW_DATA: 'DATA';
  1811. KW_INPATH: 'INPATH';
  1812. KW_IS: 'IS';
  1813. KW_NULL: 'NULL';
  1814. KW_CREATE: 'CREATE';
  1815. KW_EXTERNAL: 'EXTERNAL';
  1816. KW_ALTER: 'ALTER';
  1817. KW_CHANGE: 'CHANGE';
  1818. KW_COLUMN: 'COLUMN';
  1819. KW_FIRST: 'FIRST';
  1820. KW_AFTER: 'AFTER';
  1821. KW_DESCRIBE: 'DESCRIBE';
  1822. KW_DROP: 'DROP';
  1823. KW_RENAME: 'RENAME';
  1824. KW_TO: 'TO';
  1825. KW_COMMENT: 'COMMENT';
  1826. KW_BOOLEAN: 'BOOLEAN';
  1827. KW_TINYINT: 'TINYINT';
  1828. KW_SMALLINT: 'SMALLINT';
  1829. KW_INT: 'INT';
  1830. KW_BIGINT: 'BIGINT';
  1831. KW_FLOAT: 'FLOAT';
  1832. KW_DOUBLE: 'DOUBLE';
  1833. KW_DATE: 'DATE';
  1834. KW_DATETIME: 'DATETIME';
  1835. KW_TIMESTAMP: 'TIMESTAMP';
  1836. KW_STRING: 'STRING';
  1837. KW_ARRAY: 'ARRAY';
  1838. KW_STRUCT: 'STRUCT';
  1839. KW_MAP: 'MAP';
  1840. KW_UNIONTYPE: 'UNIONTYPE';
  1841. KW_REDUCE: 'REDUCE';
  1842. KW_PARTITIONED: 'PARTITIONED';
  1843. KW_CLUSTERED: 'CLUSTERED';
  1844. KW_SORTED: 'SORTED';
  1845. KW_INTO: 'INTO';
  1846. KW_BUCKETS: 'BUCKETS';
  1847. KW_ROW: 'ROW';
  1848. KW_FORMAT: 'FORMAT';
  1849. KW_DELIMITED: 'DELIMITED';
  1850. KW_FIELDS: 'FIELDS';
  1851. KW_TERMINATED: 'TERMINATED';
  1852. KW_ESCAPED: 'ESCAPED';
  1853. KW_COLLECTION: 'COLLECTION';
  1854. KW_ITEMS: 'ITEMS';
  1855. KW_KEYS: 'KEYS';
  1856. KW_KEY_TYPE: '$KEY$';
  1857. KW_LINES: 'LINES';
  1858. KW_STORED: 'STORED';
  1859. KW_FILEFORMAT: 'FILEFORMAT';
  1860. KW_SEQUENCEFILE: 'SEQUENCEFILE';
  1861. KW_TEXTFILE: 'TEXTFILE';
  1862. KW_RCFILE: 'RCFILE';
  1863. KW_INPUTFORMAT: 'INPUTFORMAT';
  1864. KW_OUTPUTFORMAT: 'OUTPUTFORMAT';
  1865. KW_INPUTDRIVER: 'INPUTDRIVER';
  1866. KW_OUTPUTDRIVER: 'OUTPUTDRIVER';
  1867. KW_OFFLINE: 'OFFLINE';
  1868. KW_ENABLE: 'ENABLE';
  1869. KW_DISABLE: 'DISABLE';
  1870. KW_READONLY: 'READONLY';
  1871. KW_NO_DROP: 'NO_DROP';
  1872. KW_LOCATION: 'LOCATION';
  1873. KW_TABLESAMPLE: 'TABLESAMPLE';
  1874. KW_BUCKET: 'BUCKET';
  1875. KW_OUT: 'OUT';
  1876. KW_OF: 'OF';
  1877. KW_CAST: 'CAST';
  1878. KW_ADD: 'ADD';
  1879. KW_REPLACE: 'REPLACE';
  1880. KW_COLUMNS: 'COLUMNS';
  1881. KW_RLIKE: 'RLIKE';
  1882. KW_REGEXP: 'REGEXP';
  1883. KW_TEMPORARY: 'TEMPORARY';
  1884. KW_FUNCTION: 'FUNCTION';
  1885. KW_EXPLAIN: 'EXPLAIN';
  1886. KW_EXTENDED: 'EXTENDED';
  1887. KW_FORMATTED: 'FORMATTED';
  1888. KW_SERDE: 'SERDE';
  1889. KW_WITH: 'WITH';
  1890. KW_DEFERRED: 'DEFERRED';
  1891. KW_SERDEPROPERTIES: 'SERDEPROPERTIES';
  1892. KW_DBPROPERTIES: 'DBPROPERTIES';
  1893. KW_LIMIT: 'LIMIT';
  1894. KW_SET: 'SET';
  1895. KW_TBLPROPERTIES: 'TBLPROPERTIES';
  1896. KW_IDXPROPERTIES: 'IDXPROPERTIES';
  1897. KW_VALUE_TYPE: '$VALUE$';
  1898. KW_ELEM_TYPE: '$ELEM$';
  1899. KW_CASE: 'CASE';
  1900. KW_WHEN: 'WHEN';
  1901. KW_THEN: 'THEN';
  1902. KW_ELSE: 'ELSE';
  1903. KW_END: 'END';
  1904. KW_MAPJOIN: 'MAPJOIN';
  1905. KW_STREAMTABLE: 'STREAMTABLE';
  1906. KW_HOLD_DDLTIME: 'HOLD_DDLTIME';
  1907. KW_CLUSTERSTATUS: 'CLUSTERSTATUS';
  1908. KW_UTC: 'UTC';
  1909. KW_UTCTIMESTAMP: 'UTC_TMESTAMP';
  1910. KW_LONG: 'LONG';
  1911. KW_DELETE: 'DELETE';
  1912. KW_PLUS: 'PLUS';
  1913. KW_MINUS: 'MINUS';
  1914. KW_FETCH: 'FETCH';
  1915. KW_INTERSECT: 'INTERSECT';
  1916. KW_VIEW: 'VIEW';
  1917. KW_IN: 'IN';
  1918. KW_DATABASE: 'DATABASE';
  1919. KW_DATABASES: 'DATABASES';
  1920. KW_MATERIALIZED: 'MATERIALIZED';
  1921. KW_SCHEMA: 'SCHEMA';
  1922. KW_SCHEMAS: 'SCHEMAS';
  1923. KW_GRANT: 'GRANT';
  1924. KW_REVOKE: 'REVOKE';
  1925. KW_SSL: 'SSL';
  1926. KW_UNDO: 'UNDO';
  1927. KW_LOCK: 'LOCK';
  1928. KW_LOCKS: 'LOCKS';
  1929. KW_UNLOCK: 'UNLOCK';
  1930. KW_SHARED: 'SHARED';
  1931. KW_EXCLUSIVE: 'EXCLUSIVE';
  1932. KW_PROCEDURE: 'PROCEDURE';
  1933. KW_UNSIGNED: 'UNSIGNED';
  1934. KW_WHILE: 'WHILE';
  1935. KW_READ: 'READ';
  1936. KW_READS: 'READS';
  1937. KW_PURGE: 'PURGE';
  1938. KW_RANGE: 'RANGE';
  1939. KW_ANALYZE: 'ANALYZE';
  1940. KW_BEFORE: 'BEFORE';
  1941. KW_BETWEEN: 'BETWEEN';
  1942. KW_BOTH: 'BOTH';
  1943. KW_BINARY: 'BINARY';
  1944. KW_CROSS: 'CROSS';
  1945. KW_CONTINUE: 'CONTINUE';
  1946. KW_CURSOR: 'CURSOR';
  1947. KW_TRIGGER: 'TRIGGER';
  1948. KW_RECORDREADER: 'RECORDREADER';
  1949. KW_RECORDWRITER: 'RECORDWRITER';
  1950. KW_SEMI: 'SEMI';
  1951. KW_LATERAL: 'LATERAL';
  1952. KW_TOUCH: 'TOUCH';
  1953. KW_ARCHIVE: 'ARCHIVE';
  1954. KW_UNARCHIVE: 'UNARCHIVE';
  1955. KW_COMPUTE: 'COMPUTE';
  1956. KW_STATISTICS: 'STATISTICS';
  1957. KW_USE: 'USE';
  1958. KW_OPTION: 'OPTION';
  1959. KW_SHOW_DATABASE: 'SHOW_DATABASE';
  1960. KW_UPDATE: 'UPDATE';
  1961. // Operators
  1962. // NOTE: if you add a new function/operator, add it to sysFuncNames so that describe function _FUNC_ will work.
  1963. DOT : '.'; // generated as a part of Number rule
  1964. COLON : ':' ;
  1965. COMMA : ',' ;
  1966. SEMICOLON : ';' ;
  1967. LPAREN : '(' ;
  1968. RPAREN : ')' ;
  1969. LSQUARE : '[' ;
  1970. RSQUARE : ']' ;
  1971. LCURLY : '{';
  1972. RCURLY : '}';
  1973. EQUAL : '=' | '==';
  1974. NOTEQUAL : '<>' | '!=';
  1975. LESSTHANOREQUALTO : '<=';
  1976. LESSTHAN : '<';
  1977. GREATERTHANOREQUALTO : '>=';
  1978. GREATERTHAN : '>';
  1979. DIVIDE : '/';
  1980. PLUS : '+';
  1981. MINUS : '-';
  1982. STAR : '*';
  1983. MOD : '%';
  1984. DIV : 'DIV';
  1985. AMPERSAND : '&';
  1986. TILDE : '~';
  1987. BITWISEOR : '|';
  1988. BITWISEXOR : '^';
  1989. QUESTION : '?';
  1990. DOLLAR : '$';
  1991. // LITERALS
  1992. fragment
  1993. Letter
  1994. : 'a'..'z' | 'A'..'Z'
  1995. ;
  1996. fragment
  1997. HexDigit
  1998. : 'a'..'f' | 'A'..'F'
  1999. ;
  2000. fragment
  2001. Digit
  2002. :
  2003. '0'..'9'
  2004. ;
  2005. fragment
  2006. Exponent
  2007. :
  2008. 'e' ( PLUS|MINUS )? (Digit)+
  2009. ;
  2010. fragment
  2011. RegexComponent
  2012. : 'a'..'z' | 'A'..'Z' | '0'..'9' | '_'
  2013. | PLUS | STAR | QUESTION | MINUS | DOT
  2014. | LPAREN | RPAREN | LSQUARE | RSQUARE | LCURLY | RCURLY
  2015. | BITWISEXOR | BITWISEOR | DOLLAR
  2016. ;
  2017. StringLiteral
  2018. :
  2019. ( '\'' ( ~('\''|'\\') | ('\\' .) )* '\''
  2020. | '\"' ( ~('\"'|'\\') | ('\\' .) )* '\"'
  2021. )+
  2022. ;
  2023. CharSetLiteral
  2024. :
  2025. StringLiteral
  2026. | '0' 'X' (HexDigit|Digit)+
  2027. ;
  2028. Number
  2029. :
  2030. (Digit)+ ( DOT (Digit)* (Exponent)? | Exponent)?
  2031. ;
  2032. Identifier
  2033. :
  2034. (Letter | Digit) (Letter | Digit | '_')*
  2035. | '`' RegexComponent+ '`'
  2036. ;
  2037. CharSetName
  2038. :
  2039. '_' (Letter | Digit | '_' | '-' | '.' | ':' )+
  2040. ;
  2041. WS : (' '|'\r'|'\t'|'\n') {$channel=HIDDEN;}
  2042. ;
  2043. COMMENT
  2044. : '--' (~('\n'|'\r'))*
  2045. { $channel=HIDDEN; }
  2046. ;