module SQLPP
select := 'SELECT' optional_distinct
optional_projections optional_froms optional_wheres optional_groups optional_orders optional_limit optional_offset
optional_distinct := ''
| 'DISTINCT'
optional_projections := ''
| list
optional_froms := ''
| 'FROM' froms
optional_wheres := ''
| 'WHERE' expr1
optional_groups := ''
| 'GROUP' 'BY' list
optional_orders := ''
| 'ORDER' 'BY' sort_keys
optional_limit := ''
| 'LIMIT' expr4
optional_offset := ''
| 'OFFSET' expr4
sort_keys := sort_key
| sort_key ',' sort_keys
sort_key := expr1
| expr1 sort_options
sort_options := sort_option
| sort_option ' ' sort_options
sort_option := 'ASC' | 'DESC' | 'NULLS FIRST' | 'NULLS LAST'
froms := from
| from ',' froms
from := entity
| entity optional_join_expr
optional_join_expr := ''
| 'LEFT' 'JOIN' from 'ON' expr | 'INNER' 'JOIN' from 'ON' expr | 'OUTER' 'JOIN' from 'ON' expr | 'FULL' 'OUTER' 'JOIN' from 'ON' expr
entity := '(' from ')'
| id | select_stmt
expr1 := expr2
| expr2 op expr1
op := 'AND' | 'OR' | 'IS' | 'IS NOT'
expr2 := expr3 optional_op
optional_op := ''
| 'NOT' optional_op | 'BETWEEN' expr3 AND expr3 | 'NOT IN' '(' list ')' | 'IN' '(' list ')' | bop expr3
bop := '<' | '<=' | '<>' | '=' | '>=' | '>'
expr3 := expr4
| expr4 op2 expr3 | unary expr3
op2 := '+' | '-' | '*' | '/'
unary := '+' | '-' | 'NOT' | 'DISTINCT'
expr4 := lit
| id | id '.' id | id '(' args ')' | 'CASE' case_stmt 'END' | '(' expr1 ')' | expr4 '[' expr1 ']' | expr4 '::' expr4
list := expr1
| expr1 ',' list