SQLiteParser.g4 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014 by Bart Kiers
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  7. * associated documentation files (the "Software"), to deal in the Software without restriction,
  8. * including without limitation the rights to use, copy, modify, merge, publish, distribute,
  9. * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all copies or
  13. * substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  16. * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * Project : sqlite-parser; an ANTLR4 grammar for SQLite https://github.com/bkiers/sqlite-parser
  22. * Developed by:
  23. * Bart Kiers, bart@big-o.nl
  24. * Martin Mirchev, marti_2203@abv.bg
  25. * Mike Lische, mike@lischke-online.de
  26. */
  27. // $antlr-format alignTrailingComments on, columnLimit 130, minEmptyLines 1, maxEmptyLinesToKeep 1, reflowComments off
  28. // $antlr-format useTab off, allowShortRulesOnASingleLine off, allowShortBlocksOnASingleLine on, alignSemicolons ownLine
  29. parser grammar SQLiteParser;
  30. options {
  31. tokenVocab = SQLiteLexer;
  32. }
  33. parse: (sql_stmt_list)* EOF
  34. ;
  35. sql_stmt_list:
  36. SCOL* sql_stmt (SCOL+ sql_stmt)* SCOL*
  37. ;
  38. sql_stmt: (EXPLAIN_ (QUERY_ PLAN_)?)? (
  39. alter_table_stmt
  40. | analyze_stmt
  41. | attach_stmt
  42. | begin_stmt
  43. | commit_stmt
  44. | create_index_stmt
  45. | create_table_stmt
  46. | create_trigger_stmt
  47. | create_view_stmt
  48. | create_virtual_table_stmt
  49. | delete_stmt
  50. | delete_stmt_limited
  51. | detach_stmt
  52. | drop_stmt
  53. | insert_stmt
  54. | pragma_stmt
  55. | reindex_stmt
  56. | release_stmt
  57. | rollback_stmt
  58. | savepoint_stmt
  59. | select_stmt
  60. | update_stmt
  61. | update_stmt_limited
  62. | vacuum_stmt
  63. )
  64. ;
  65. alter_table_stmt:
  66. ALTER_ TABLE_ (schema_name DOT)? table_name (
  67. RENAME_ (
  68. TO_ new_table_name
  69. | COLUMN_? old_column_name = column_name TO_ new_column_name = column_name
  70. )
  71. | ADD_ COLUMN_? column_def
  72. | DROP_ COLUMN_? column_name
  73. )
  74. ;
  75. analyze_stmt:
  76. ANALYZE_ (schema_name | (schema_name DOT)? table_or_index_name)?
  77. ;
  78. attach_stmt:
  79. ATTACH_ DATABASE_? expr AS_ schema_name
  80. ;
  81. begin_stmt:
  82. BEGIN_ (DEFERRED_ | IMMEDIATE_ | EXCLUSIVE_)? (
  83. TRANSACTION_ transaction_name?
  84. )?
  85. ;
  86. commit_stmt: (COMMIT_ | END_) TRANSACTION_?
  87. ;
  88. rollback_stmt:
  89. ROLLBACK_ TRANSACTION_? (TO_ SAVEPOINT_? savepoint_name)?
  90. ;
  91. savepoint_stmt:
  92. SAVEPOINT_ savepoint_name
  93. ;
  94. release_stmt:
  95. RELEASE_ SAVEPOINT_? savepoint_name
  96. ;
  97. create_index_stmt:
  98. CREATE_ UNIQUE_? INDEX_ (IF_ NOT_ EXISTS_)? (schema_name DOT)? index_name ON_ table_name OPEN_PAR
  99. indexed_column (COMMA indexed_column)* CLOSE_PAR (WHERE_ expr)?
  100. ;
  101. indexed_column: (column_name | expr) (COLLATE_ collation_name)? asc_desc?
  102. ;
  103. table_option:
  104. WITHOUT_ row_ROW_ID = IDENTIFIER
  105. | STRICT_
  106. ;
  107. create_table_stmt:
  108. CREATE_ (TEMP_ | TEMPORARY_)? TABLE_ (IF_ NOT_ EXISTS_)? (
  109. schema_name DOT
  110. )? table_name (
  111. OPEN_PAR column_def (COMMA column_def)*? (COMMA table_constraint)* CLOSE_PAR (
  112. table_option (COMMA table_option)*
  113. )?
  114. | AS_ select_stmt
  115. )
  116. ;
  117. column_def:
  118. column_name type_name? column_constraint*
  119. ;
  120. type_name:
  121. name+? (
  122. OPEN_PAR signed_number CLOSE_PAR
  123. | OPEN_PAR signed_number COMMA signed_number CLOSE_PAR
  124. )?
  125. ;
  126. column_constraint: (CONSTRAINT_ name)? (
  127. (PRIMARY_ KEY_ asc_desc? conflict_clause? AUTOINCREMENT_?)
  128. | (NOT_ NULL_ | UNIQUE_) conflict_clause?
  129. | CHECK_ OPEN_PAR expr CLOSE_PAR
  130. | DEFAULT_ (signed_number | literal_value | OPEN_PAR expr CLOSE_PAR)
  131. | COLLATE_ collation_name
  132. | foreign_key_clause
  133. | (GENERATED_ ALWAYS_)? AS_ OPEN_PAR expr CLOSE_PAR (
  134. STORED_
  135. | VIRTUAL_
  136. )?
  137. )
  138. ;
  139. signed_number: (PLUS | MINUS)? NUMERIC_LITERAL
  140. ;
  141. table_constraint: (CONSTRAINT_ name)? (
  142. (PRIMARY_ KEY_ | UNIQUE_) OPEN_PAR indexed_column (
  143. COMMA indexed_column
  144. )* CLOSE_PAR conflict_clause?
  145. | CHECK_ OPEN_PAR expr CLOSE_PAR
  146. | FOREIGN_ KEY_ OPEN_PAR column_name (COMMA column_name)* CLOSE_PAR foreign_key_clause
  147. )
  148. ;
  149. foreign_key_clause:
  150. REFERENCES_ foreign_table (
  151. OPEN_PAR column_name (COMMA column_name)* CLOSE_PAR
  152. )? (
  153. ON_ (DELETE_ | UPDATE_) (
  154. SET_ (NULL_ | DEFAULT_)
  155. | CASCADE_
  156. | RESTRICT_
  157. | NO_ ACTION_
  158. )
  159. | MATCH_ name
  160. )* (NOT_? DEFERRABLE_ (INITIALLY_ (DEFERRED_ | IMMEDIATE_))?)?
  161. ;
  162. conflict_clause:
  163. ON_ CONFLICT_ (
  164. ROLLBACK_
  165. | ABORT_
  166. | FAIL_
  167. | IGNORE_
  168. | REPLACE_
  169. )
  170. ;
  171. create_trigger_stmt:
  172. CREATE_ (TEMP_ | TEMPORARY_)? TRIGGER_ (IF_ NOT_ EXISTS_)? (
  173. schema_name DOT
  174. )? trigger_name (BEFORE_ | AFTER_ | INSTEAD_ OF_)? (
  175. DELETE_
  176. | INSERT_
  177. | UPDATE_ (OF_ column_name ( COMMA column_name)*)?
  178. ) ON_ table_name (FOR_ EACH_ ROW_)? (WHEN_ expr)? BEGIN_ (
  179. (update_stmt | insert_stmt | delete_stmt | select_stmt) SCOL
  180. )+ END_
  181. ;
  182. create_view_stmt:
  183. CREATE_ (TEMP_ | TEMPORARY_)? VIEW_ (IF_ NOT_ EXISTS_)? (
  184. schema_name DOT
  185. )? view_name (OPEN_PAR column_name (COMMA column_name)* CLOSE_PAR)? AS_ select_stmt
  186. ;
  187. create_virtual_table_stmt:
  188. CREATE_ VIRTUAL_ TABLE_ (IF_ NOT_ EXISTS_)? (schema_name DOT)? table_name USING_ module_name (
  189. OPEN_PAR module_argument (COMMA module_argument)* CLOSE_PAR
  190. )?
  191. ;
  192. with_clause:
  193. WITH_ RECURSIVE_? cte_table_name AS_ OPEN_PAR select_stmt CLOSE_PAR (
  194. COMMA cte_table_name AS_ OPEN_PAR select_stmt CLOSE_PAR
  195. )*
  196. ;
  197. cte_table_name:
  198. table_name (OPEN_PAR column_name ( COMMA column_name)* CLOSE_PAR)?
  199. ;
  200. recursive_cte:
  201. cte_table_name AS_ OPEN_PAR initial_select UNION_ ALL_? recursive__select CLOSE_PAR
  202. ;
  203. common_table_expression:
  204. table_name (OPEN_PAR column_name ( COMMA column_name)* CLOSE_PAR)? AS_ OPEN_PAR select_stmt CLOSE_PAR
  205. ;
  206. returning_clause:
  207. RETURNING_ (
  208. (STAR | expr ( AS_? column_alias)?) (
  209. COMMA (STAR | expr ( AS_? column_alias)?)
  210. )*
  211. )
  212. ;
  213. delete_stmt:
  214. with_clause? DELETE_ FROM_ qualified_table_name (WHERE_ expr)? returning_clause?
  215. ;
  216. delete_stmt_limited:
  217. with_clause? DELETE_ FROM_ qualified_table_name (WHERE_ expr)? (
  218. order_by_stmt? limit_stmt
  219. )? returning_clause?
  220. ;
  221. detach_stmt:
  222. DETACH_ DATABASE_? schema_name
  223. ;
  224. drop_stmt:
  225. DROP_ object = (INDEX_ | TABLE_ | TRIGGER_ | VIEW_) (
  226. IF_ EXISTS_
  227. )? (schema_name DOT)? any_name
  228. ;
  229. /*
  230. SQLite understands the following binary operators, in order from highest to lowest precedence:
  231. ||
  232. * / %
  233. + -
  234. << >> & |
  235. < <= > >=
  236. = == != <> IS IS NOT IN LIKE GLOB MATCH REGEXP
  237. AND
  238. OR
  239. */
  240. expr:
  241. literal_value #expr_literal
  242. | NUMBERED_BIND_PARAMETER #expr_bind
  243. | NAMED_BIND_PARAMETER #expr_bind
  244. | ((schema_name DOT)? table_name DOT)? column_name #expr_qualified_column_name
  245. | unary_operator expr #expr_unary
  246. | expr PIPE2 expr #expr_binary
  247. | expr ( PTR | PTR2 ) expr #expr_binary
  248. | expr ( STAR | DIV | MOD) expr #expr_binary
  249. | expr ( PLUS | MINUS) expr #expr_binary
  250. | expr ( LT2 | GT2 | AMP | PIPE) expr #expr_comparison
  251. | expr ( LT | LT_EQ | GT | GT_EQ) expr #expr_comparison
  252. | expr (
  253. ASSIGN
  254. | EQ
  255. | NOT_EQ1
  256. | NOT_EQ2
  257. | IS_
  258. | IS_ NOT_
  259. | NOT_? IN_
  260. | LIKE_
  261. | GLOB_
  262. | MATCH_
  263. | REGEXP_
  264. ) expr #expr_comparison
  265. | expr NOT_? IN_ (
  266. OPEN_PAR (select_stmt | expr ( COMMA expr)*)? CLOSE_PAR
  267. | ( schema_name DOT)? table_name
  268. | (schema_name DOT)? table_function_name OPEN_PAR (expr (COMMA expr)*)? CLOSE_PAR
  269. ) #expr_in_select
  270. | expr AND_ expr #expr_bool
  271. | expr OR_ expr #expr_bool
  272. | qualified_function_name OPEN_PAR ((DISTINCT_? expr ( COMMA expr)*) | STAR)? CLOSE_PAR filter_clause? over_clause? #expr_function
  273. | OPEN_PAR expr (COMMA expr)* CLOSE_PAR #expr_list
  274. | CAST_ OPEN_PAR expr AS_ type_name CLOSE_PAR #expr_cast
  275. | expr COLLATE_ collation_name #expr_collate
  276. | expr NOT_? (LIKE_ | GLOB_ | REGEXP_ | MATCH_) expr (
  277. ESCAPE_ expr
  278. )? #expr_comparison
  279. | expr ( ISNULL_ | NOTNULL_ | NOT_ NULL_) #expr_null_comp
  280. | expr NOT_? BETWEEN_ expr AND_ expr #expr_between
  281. | ((NOT_)? EXISTS_)? OPEN_PAR select_stmt CLOSE_PAR #expr_in_select
  282. | CASE_ expr? (WHEN_ expr THEN_ expr)+ (ELSE_ expr)? END_ #expr_case
  283. | raise_function #expr_raise
  284. ;
  285. raise_function:
  286. RAISE_ OPEN_PAR (
  287. IGNORE_
  288. | (ROLLBACK_ | ABORT_ | FAIL_) COMMA error_message
  289. ) CLOSE_PAR
  290. ;
  291. literal_value:
  292. NUMERIC_LITERAL
  293. | STRING_LITERAL
  294. | BLOB_LITERAL
  295. | NULL_
  296. | TRUE_
  297. | FALSE_
  298. | CURRENT_TIME_
  299. | CURRENT_DATE_
  300. | CURRENT_TIMESTAMP_
  301. ;
  302. insert_stmt:
  303. with_clause? (
  304. INSERT_
  305. | REPLACE_
  306. | INSERT_ OR_ (
  307. REPLACE_
  308. | ROLLBACK_
  309. | ABORT_
  310. | FAIL_
  311. | IGNORE_
  312. )
  313. ) INTO_ (schema_name DOT)? table_name (AS_ table_alias)? (
  314. OPEN_PAR column_name ( COMMA column_name)* CLOSE_PAR
  315. )? (
  316. (
  317. VALUES_ OPEN_PAR expr (COMMA expr)* CLOSE_PAR (
  318. COMMA OPEN_PAR expr ( COMMA expr)* CLOSE_PAR
  319. )*
  320. | select_stmt
  321. ) upsert_clause? returning_clause?
  322. )
  323. | DEFAULT_ VALUES_
  324. ;
  325. upsert_clause:
  326. ON_ CONFLICT_ (
  327. OPEN_PAR indexed_column (COMMA indexed_column)* CLOSE_PAR (WHERE_ expr)?
  328. )? DO_ (
  329. NOTHING_
  330. | UPDATE_ SET_ (
  331. (column_name | column_name_list) ASSIGN expr (
  332. COMMA (column_name | column_name_list) ASSIGN expr
  333. )* (WHERE_ expr)?
  334. )
  335. )
  336. ;
  337. pragma_stmt:
  338. PRAGMA_ (schema_name DOT)? pragma_name (
  339. ASSIGN pragma_value
  340. | OPEN_PAR pragma_value CLOSE_PAR
  341. )?
  342. ;
  343. pragma_value:
  344. signed_number
  345. | name
  346. | STRING_LITERAL
  347. ;
  348. reindex_stmt:
  349. REINDEX_ (collation_name | (schema_name DOT)? (table_name | index_name))?
  350. ;
  351. select_stmt:
  352. common_table_stmt? select_core (compound_operator select_core)* order_by_stmt? limit_stmt?
  353. ;
  354. join_clause:
  355. table_or_subquery (join_operator table_or_subquery join_constraint)*
  356. ;
  357. select_core:
  358. (
  359. SELECT_ (DISTINCT_ | ALL_)? result_column (COMMA result_column)* (
  360. FROM_ (table_or_subquery (COMMA table_or_subquery)* | join_clause)
  361. )? (WHERE_ expr)? (GROUP_ BY_ expr (COMMA expr)* (HAVING_ expr)?)? (
  362. WINDOW_ window_name AS_ window_defn (
  363. COMMA window_name AS_ window_defn
  364. )*
  365. )?
  366. )
  367. | VALUES_ OPEN_PAR expr (COMMA expr)* CLOSE_PAR (
  368. COMMA OPEN_PAR expr ( COMMA expr)* CLOSE_PAR
  369. )*
  370. ;
  371. factored_select_stmt:
  372. select_stmt
  373. ;
  374. simple_select_stmt:
  375. common_table_stmt? select_core order_by_stmt? limit_stmt?
  376. ;
  377. compound_select_stmt:
  378. common_table_stmt? select_core (
  379. (UNION_ ALL_? | INTERSECT_ | EXCEPT_) select_core
  380. )+ order_by_stmt? limit_stmt?
  381. ;
  382. table_or_subquery:
  383. (schema_name DOT)? table_name (AS_? table_alias)? (INDEXED_ BY_ index_name | NOT_ INDEXED_)?
  384. | (schema_name DOT)? table_function_name OPEN_PAR expr (COMMA expr)* CLOSE_PAR (AS_? table_alias)?
  385. | OPEN_PAR (table_or_subquery (COMMA table_or_subquery)* | join_clause) CLOSE_PAR
  386. | OPEN_PAR select_stmt CLOSE_PAR (AS_? table_alias)?
  387. | (schema_name DOT)? table_name (AS_? table_alias_fallback)? (INDEXED_ BY_ index_name | NOT_ INDEXED_)?
  388. | (schema_name DOT)? table_function_name OPEN_PAR expr (COMMA expr)* CLOSE_PAR (AS_? table_alias_fallback)?
  389. | OPEN_PAR (table_or_subquery (COMMA table_or_subquery)* | join_clause) CLOSE_PAR
  390. | OPEN_PAR select_stmt CLOSE_PAR (AS_? table_alias_fallback)?
  391. ;
  392. result_column:
  393. STAR
  394. | table_name DOT STAR
  395. | expr ( AS_? column_alias)?
  396. ;
  397. join_operator:
  398. COMMA
  399. | NATURAL_? (((LEFT_ | RIGHT_ | FULL_) OUTER_?) | INNER_)? JOIN_
  400. | CROSS_ JOIN_
  401. ;
  402. join_constraint:
  403. (ON_ expr
  404. | USING_ OPEN_PAR column_name ( COMMA column_name)* CLOSE_PAR)?
  405. ;
  406. compound_operator:
  407. UNION_ ALL_?
  408. | INTERSECT_
  409. | EXCEPT_
  410. ;
  411. update_stmt:
  412. with_clause? UPDATE_ (
  413. OR_ (ROLLBACK_ | ABORT_ | REPLACE_ | FAIL_ | IGNORE_)
  414. )? qualified_table_name SET_ (column_name | column_name_list) ASSIGN expr (
  415. COMMA (column_name | column_name_list) ASSIGN expr
  416. )* (WHERE_ expr)? returning_clause?
  417. ;
  418. column_name_list:
  419. OPEN_PAR column_name (COMMA column_name)* CLOSE_PAR
  420. ;
  421. update_stmt_limited:
  422. with_clause? UPDATE_ (
  423. OR_ (ROLLBACK_ | ABORT_ | REPLACE_ | FAIL_ | IGNORE_)
  424. )? qualified_table_name SET_ (column_name | column_name_list) ASSIGN expr (
  425. COMMA (column_name | column_name_list) ASSIGN expr
  426. )* (WHERE_ expr)? (order_by_stmt? limit_stmt)?
  427. ;
  428. qualified_table_name: (schema_name DOT)? table_name (AS_ alias)? (
  429. INDEXED_ BY_ index_name
  430. | NOT_ INDEXED_
  431. )?
  432. ;
  433. vacuum_stmt:
  434. VACUUM_ schema_name? (INTO_ filename)?
  435. ;
  436. filter_clause:
  437. FILTER_ OPEN_PAR WHERE_ expr CLOSE_PAR
  438. ;
  439. window_defn:
  440. OPEN_PAR base_window_name? (PARTITION_ BY_ expr (COMMA expr)*)? (
  441. ORDER_ BY_ ordering_term (COMMA ordering_term)*
  442. ) frame_spec? CLOSE_PAR
  443. ;
  444. over_clause:
  445. OVER_ (
  446. window_name
  447. | OPEN_PAR base_window_name? (PARTITION_ BY_ expr (COMMA expr)*)? (
  448. ORDER_ BY_ ordering_term (COMMA ordering_term)*
  449. )? frame_spec? CLOSE_PAR
  450. )
  451. ;
  452. frame_spec:
  453. frame_clause (
  454. EXCLUDE_ (NO_ OTHERS_)
  455. | CURRENT_ ROW_
  456. | GROUP_
  457. | TIES_
  458. )?
  459. ;
  460. frame_clause: (RANGE_ | ROWS_ | GROUPS_) (
  461. frame_single
  462. | BETWEEN_ frame_left AND_ frame_right
  463. )
  464. ;
  465. simple_function_invocation:
  466. simple_func OPEN_PAR (expr (COMMA expr)* | STAR) CLOSE_PAR
  467. ;
  468. aggregate_function_invocation:
  469. aggregate_func OPEN_PAR (DISTINCT_? expr (COMMA expr)* | STAR)? CLOSE_PAR filter_clause?
  470. ;
  471. window_function_invocation:
  472. window_function OPEN_PAR (expr (COMMA expr)* | STAR)? CLOSE_PAR filter_clause? OVER_ (
  473. window_defn
  474. | window_name
  475. )
  476. ;
  477. common_table_stmt: //additional structures
  478. WITH_ RECURSIVE_? common_table_expression (COMMA common_table_expression)*
  479. ;
  480. order_by_stmt:
  481. ORDER_ BY_ ordering_term (COMMA ordering_term)*
  482. ;
  483. limit_stmt:
  484. LIMIT_ expr ((OFFSET_ | COMMA) expr)?
  485. ;
  486. ordering_term:
  487. expr (COLLATE_ collation_name)? asc_desc? (NULLS_ (FIRST_ | LAST_))?
  488. ;
  489. asc_desc:
  490. ASC_
  491. | DESC_
  492. ;
  493. frame_left:
  494. expr PRECEDING_
  495. | expr FOLLOWING_
  496. | CURRENT_ ROW_
  497. | UNBOUNDED_ PRECEDING_
  498. ;
  499. frame_right:
  500. expr PRECEDING_
  501. | expr FOLLOWING_
  502. | CURRENT_ ROW_
  503. | UNBOUNDED_ FOLLOWING_
  504. ;
  505. frame_single:
  506. expr PRECEDING_
  507. | UNBOUNDED_ PRECEDING_
  508. | CURRENT_ ROW_
  509. ;
  510. // unknown
  511. window_function:
  512. (FIRST_VALUE_ | LAST_VALUE_) OPEN_PAR expr CLOSE_PAR OVER_ OPEN_PAR partition_by? order_by_expr_asc_desc frame_clause
  513. ? CLOSE_PAR
  514. | (CUME_DIST_ | PERCENT_RANK_) OPEN_PAR CLOSE_PAR OVER_ OPEN_PAR partition_by? order_by_expr? CLOSE_PAR
  515. | (DENSE_RANK_ | RANK_ | ROW_NUMBER_) OPEN_PAR CLOSE_PAR OVER_ OPEN_PAR partition_by? order_by_expr_asc_desc
  516. CLOSE_PAR
  517. | (LAG_ | LEAD_) OPEN_PAR expr of_OF_fset? default_DEFAULT__value? CLOSE_PAR OVER_ OPEN_PAR partition_by?
  518. order_by_expr_asc_desc CLOSE_PAR
  519. | NTH_VALUE_ OPEN_PAR expr COMMA signed_number CLOSE_PAR OVER_ OPEN_PAR partition_by? order_by_expr_asc_desc
  520. frame_clause? CLOSE_PAR
  521. | NTILE_ OPEN_PAR expr CLOSE_PAR OVER_ OPEN_PAR partition_by? order_by_expr_asc_desc CLOSE_PAR
  522. ;
  523. of_OF_fset:
  524. COMMA signed_number
  525. ;
  526. default_DEFAULT__value:
  527. COMMA signed_number
  528. ;
  529. partition_by:
  530. PARTITION_ BY_ expr+
  531. ;
  532. order_by_expr:
  533. ORDER_ BY_ expr+
  534. ;
  535. order_by_expr_asc_desc:
  536. ORDER_ BY_ order_by_expr_asc_desc
  537. ;
  538. expr_asc_desc:
  539. expr asc_desc? (COMMA expr asc_desc?)*
  540. ;
  541. //TODO BOTH OF THESE HAVE TO BE REWORKED TO FOLLOW THE SPEC
  542. initial_select:
  543. select_stmt
  544. ;
  545. recursive__select:
  546. select_stmt
  547. ;
  548. unary_operator:
  549. MINUS
  550. | PLUS
  551. | TILDE
  552. | NOT_
  553. ;
  554. error_message:
  555. STRING_LITERAL
  556. ;
  557. module_argument: // TODO check what exactly is permitted here
  558. expr
  559. | column_def
  560. ;
  561. column_alias:
  562. IDENTIFIER
  563. | STRING_LITERAL
  564. ;
  565. keyword:
  566. ABORT_
  567. | ACTION_
  568. | ADD_
  569. | AFTER_
  570. | ALL_
  571. | ALTER_
  572. | ANALYZE_
  573. | AND_
  574. | AS_
  575. | ASC_
  576. | ATTACH_
  577. | AUTOINCREMENT_
  578. | BEFORE_
  579. | BEGIN_
  580. | BETWEEN_
  581. | BY_
  582. | CASCADE_
  583. | CASE_
  584. | CAST_
  585. | CHECK_
  586. | COLLATE_
  587. | COLUMN_
  588. | COMMIT_
  589. | CONFLICT_
  590. | CONSTRAINT_
  591. | CREATE_
  592. | CROSS_
  593. | CURRENT_DATE_
  594. | CURRENT_TIME_
  595. | CURRENT_TIMESTAMP_
  596. | DATABASE_
  597. | DEFAULT_
  598. | DEFERRABLE_
  599. | DEFERRED_
  600. | DELETE_
  601. | DESC_
  602. | DETACH_
  603. | DISTINCT_
  604. | DROP_
  605. | EACH_
  606. | ELSE_
  607. | END_
  608. | ESCAPE_
  609. | EXCEPT_
  610. | EXCLUSIVE_
  611. | EXISTS_
  612. | EXPLAIN_
  613. | FAIL_
  614. | FOR_
  615. | FOREIGN_
  616. | FROM_
  617. | FULL_
  618. | GLOB_
  619. | GROUP_
  620. | HAVING_
  621. | IF_
  622. | IGNORE_
  623. | IMMEDIATE_
  624. | IN_
  625. | INDEX_
  626. | INDEXED_
  627. | INITIALLY_
  628. | INNER_
  629. | INSERT_
  630. | INSTEAD_
  631. | INTERSECT_
  632. | INTO_
  633. | IS_
  634. | ISNULL_
  635. | JOIN_
  636. | KEY_
  637. | LEFT_
  638. | LIKE_
  639. | LIMIT_
  640. | MATCH_
  641. | NATURAL_
  642. | NO_
  643. | NOT_
  644. | NOTNULL_
  645. | NULL_
  646. | OF_
  647. | OFFSET_
  648. | ON_
  649. | OR_
  650. | ORDER_
  651. | OUTER_
  652. | PLAN_
  653. | PRAGMA_
  654. | PRIMARY_
  655. | QUERY_
  656. | RAISE_
  657. | RECURSIVE_
  658. | REFERENCES_
  659. | REGEXP_
  660. | REINDEX_
  661. | RELEASE_
  662. | RENAME_
  663. | REPLACE_
  664. | RESTRICT_
  665. | RETURNING_
  666. | RIGHT_
  667. | ROLLBACK_
  668. | ROW_
  669. | ROWS_
  670. | SAVEPOINT_
  671. | SELECT_
  672. | SET_
  673. | STRICT_
  674. | TABLE_
  675. | TEMP_
  676. | TEMPORARY_
  677. | THEN_
  678. | TO_
  679. | TRANSACTION_
  680. | TRIGGER_
  681. | UNION_
  682. | UNIQUE_
  683. | UPDATE_
  684. | USING_
  685. | VACUUM_
  686. | VALUES_
  687. | VIEW_
  688. | VIRTUAL_
  689. | WHEN_
  690. | WHERE_
  691. | WITH_
  692. | WITHOUT_
  693. | FIRST_VALUE_
  694. | OVER_
  695. | PARTITION_
  696. | RANGE_
  697. | PRECEDING_
  698. | UNBOUNDED_
  699. | CURRENT_
  700. | FOLLOWING_
  701. | CUME_DIST_
  702. | DENSE_RANK_
  703. | LAG_
  704. | LAST_VALUE_
  705. | LEAD_
  706. | NTH_VALUE_
  707. | NTILE_
  708. | PERCENT_RANK_
  709. | RANK_
  710. | ROW_NUMBER_
  711. | GENERATED_
  712. | ALWAYS_
  713. | STORED_
  714. | TRUE_
  715. | FALSE_
  716. | WINDOW_
  717. | NULLS_
  718. | FIRST_
  719. | LAST_
  720. | FILTER_
  721. | GROUPS_
  722. | EXCLUDE_
  723. ;
  724. // TODO: check all names below
  725. name:
  726. any_name
  727. ;
  728. function_name:
  729. any_name
  730. ;
  731. qualified_function_name:
  732. (schema_name DOT)? function_name
  733. ;
  734. schema_name:
  735. any_name
  736. ;
  737. table_name:
  738. any_name
  739. ;
  740. table_or_index_name:
  741. any_name
  742. ;
  743. new_table_name:
  744. any_name
  745. ;
  746. column_name:
  747. any_name
  748. ;
  749. collation_name:
  750. any_name
  751. ;
  752. foreign_table:
  753. any_name
  754. ;
  755. index_name:
  756. any_name
  757. ;
  758. trigger_name:
  759. any_name
  760. ;
  761. view_name:
  762. any_name
  763. ;
  764. module_name:
  765. any_name
  766. ;
  767. pragma_name:
  768. any_name
  769. ;
  770. savepoint_name:
  771. any_name
  772. ;
  773. table_alias: IDENTIFIER | STRING_LITERAL;
  774. table_alias_fallback: any_name;
  775. transaction_name:
  776. any_name
  777. ;
  778. window_name:
  779. any_name
  780. ;
  781. alias:
  782. any_name
  783. ;
  784. filename:
  785. any_name
  786. ;
  787. base_window_name:
  788. any_name
  789. ;
  790. simple_func:
  791. any_name
  792. ;
  793. aggregate_func:
  794. any_name
  795. ;
  796. table_function_name:
  797. any_name
  798. ;
  799. any_name:
  800. IDENTIFIER
  801. | keyword
  802. | STRING_LITERAL
  803. | OPEN_PAR any_name CLOSE_PAR
  804. ;