2023-03-19 07:58:00 +03:00
|
|
|
// This is the grammar for SLQ, the query language used by sq (https://sq.io).
|
|
|
|
// The grammar is not yet finalized; it is subject to change in any new sq release.
|
2016-10-17 07:14:01 +03:00
|
|
|
grammar SLQ;
|
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
stmtList: ';'* query ( ';'+ query)* ';'*;
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
query: segment ('|' segment)*;
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
segment: (element) (',' element)*;
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
element
|
2023-06-17 07:54:25 +03:00
|
|
|
: handleTable
|
2023-03-22 09:17:34 +03:00
|
|
|
| handle
|
|
|
|
| selectorElement
|
2022-12-24 19:43:21 +03:00
|
|
|
| join
|
2023-03-27 05:03:40 +03:00
|
|
|
| groupBy
|
2023-11-22 20:56:19 +03:00
|
|
|
| having
|
2023-03-26 04:20:53 +03:00
|
|
|
| orderBy
|
2022-12-24 19:43:21 +03:00
|
|
|
| rowRange
|
2023-03-28 09:48:24 +03:00
|
|
|
| uniqueFunc
|
|
|
|
| countFunc
|
2023-06-17 07:54:25 +03:00
|
|
|
| where
|
2023-03-27 05:03:40 +03:00
|
|
|
| funcElement
|
2023-06-17 07:54:25 +03:00
|
|
|
| exprElement;
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
funcElement: func (alias)?;
|
|
|
|
func: funcName '(' ( expr ( ',' expr)* | '*')? ')';
|
2023-05-27 06:11:38 +03:00
|
|
|
funcName
|
|
|
|
: 'sum'
|
|
|
|
| 'avg'
|
|
|
|
| 'max'
|
|
|
|
| 'min'
|
2023-11-19 03:05:48 +03:00
|
|
|
| 'schema'
|
|
|
|
| 'catalog'
|
2023-11-20 09:44:36 +03:00
|
|
|
| 'rownum'
|
2023-05-27 06:11:38 +03:00
|
|
|
| PROPRIETARY_FUNC_NAME
|
|
|
|
;
|
|
|
|
|
|
|
|
// PROPRIETARY_FUNC_NAME is a DB-native func, which is invoked by prefixing
|
|
|
|
// an underscore to the func name, e.g. _date(xyz).
|
|
|
|
PROPRIETARY_FUNC_NAME: '_' ID;
|
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
/*
|
|
|
|
join
|
|
|
|
----
|
|
|
|
|
|
|
|
join implements SQL's JOIN mechanism.
|
|
|
|
|
|
|
|
@sakila_pg | .actor | join(.film_actor, .actor_id)
|
|
|
|
@sakila_pg | .actor | join(@sakila_my.film_actor, .actor_id)
|
|
|
|
@sakila_pg | .actor | join(.film_actor, .actor.actor_id == .film_actor.actor_id)
|
|
|
|
@sakila_pg | .actor:a | join(.film_actor:fa, .a.actor_id == .fa.actor_id)
|
|
|
|
@sakila_pg.actor:a | join(@sakila_my.film_actor:fa, .a.actor_id == .fa.actor_id)
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-07-03 18:34:19 +03:00
|
|
|
See:
|
|
|
|
- https://www.sqlite.org/syntax/join-clause.html
|
|
|
|
- https://www.sqlite.org/syntax/join-operator.html
|
|
|
|
*/
|
|
|
|
join: JOIN_TYPE '(' joinTable (',' expr)? ')';
|
|
|
|
joinTable: (HANDLE)? NAME (alias)?;
|
|
|
|
// JOIN_TYPE is the set of join types, and their aliases.
|
|
|
|
// Note that not every database may support every join type, but
|
|
|
|
// this is not the concern of the grammar.
|
|
|
|
//
|
|
|
|
// Note that NATURAL JOIN is not supported, as its implementation
|
|
|
|
// is spotty in various DBs, and it's often considered an anti-pattern.
|
|
|
|
JOIN_TYPE
|
|
|
|
: 'join'
|
|
|
|
| 'inner_join'
|
|
|
|
| 'left_join'
|
|
|
|
| 'ljoin'
|
|
|
|
| 'left_outer_join'
|
|
|
|
| 'lojoin'
|
|
|
|
| 'right_join'
|
|
|
|
| 'rjoin'
|
|
|
|
| 'right_outer_join'
|
|
|
|
| 'rojoin'
|
|
|
|
| 'full_outer_join'
|
|
|
|
| 'fojoin'
|
|
|
|
| 'cross_join'
|
|
|
|
| 'xjoin'
|
|
|
|
;
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
/*
|
|
|
|
uniqueFunc
|
|
|
|
----------
|
|
|
|
|
|
|
|
uniqueFunc implements SQL's DISTINCT mechanism.
|
|
|
|
|
|
|
|
.actor | .first_name | unique
|
|
|
|
.actor | unique
|
|
|
|
|
|
|
|
The func takes zero args.
|
|
|
|
*/
|
2023-11-19 03:05:48 +03:00
|
|
|
uniqueFunc: 'unique' | 'uniq';
|
2023-03-28 09:48:24 +03:00
|
|
|
|
|
|
|
/*
|
2023-07-04 20:31:47 +03:00
|
|
|
|
2023-03-28 09:48:24 +03:00
|
|
|
countFunc
|
|
|
|
---------
|
|
|
|
|
|
|
|
This implements SQL's COUNT function. It has special handling vs other
|
|
|
|
funcs because of the several forms it can take.
|
|
|
|
|
|
|
|
.actor | count
|
|
|
|
.actor | count:quantity # alias
|
|
|
|
.actor | count()
|
|
|
|
.actor | count(*)
|
|
|
|
.actor | count(.first_name):quanity # alias
|
|
|
|
|
|
|
|
*/
|
2023-04-01 11:38:32 +03:00
|
|
|
countFunc: 'count' (LPAR (selector)? RPAR)? (alias)?;
|
2023-03-28 09:48:24 +03:00
|
|
|
|
|
|
|
|
2023-06-17 07:54:25 +03:00
|
|
|
/*
|
|
|
|
where
|
|
|
|
-----
|
|
|
|
The "where" mechanism implements SQL's WHERE clause.
|
|
|
|
|
|
|
|
.actor | where(.actor_id > 10 && .first_name == "TOM")
|
|
|
|
|
|
|
|
From a SQL perspective, "where" is the natural name to use. However,
|
|
|
|
and inconveniently, jq uses "select" for this purpose.
|
|
|
|
|
|
|
|
https://jqlang.github.io/jq/manual/v1.6/#select(boolean_expression)
|
|
|
|
|
|
|
|
One of sq's design principles is to adhere to jq syntax whenver possible.
|
|
|
|
Alas, for SQL users, "select" means "SELECT these columns", not "select
|
|
|
|
the matching rows".
|
|
|
|
|
|
|
|
It's unclear what the best approach is here, so for now, we will allow
|
|
|
|
both "where" and "select", and plan to deprecate one of these after user
|
|
|
|
feedback.
|
|
|
|
*/
|
|
|
|
|
|
|
|
WHERE: 'where' | 'select';
|
|
|
|
where: WHERE LPAR (expr)? RPAR;
|
|
|
|
|
|
|
|
|
2023-03-26 11:01:41 +03:00
|
|
|
/*
|
2023-03-27 05:03:40 +03:00
|
|
|
group_by
|
2023-03-26 11:01:41 +03:00
|
|
|
-------
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
The 'group_by' construct implments the SQL "GROUP BY" clause.
|
2023-03-26 11:01:41 +03:00
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
.payment | .customer_id, sum(.amount) | group_by(.customer_id)
|
2023-03-26 11:01:41 +03:00
|
|
|
|
|
|
|
Syonyms:
|
|
|
|
- 'group_by' for jq interoperability.
|
|
|
|
https://stedolan.github.io/jq/manual/v1.6/#group_by(path_expression)
|
|
|
|
*/
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
GROUP_BY: 'group_by';
|
|
|
|
groupByTerm: selector | func;
|
|
|
|
groupBy: GROUP_BY '(' groupByTerm (',' groupByTerm)* ')';
|
2023-03-22 09:17:34 +03:00
|
|
|
|
2023-11-22 20:56:19 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
having
|
|
|
|
------
|
|
|
|
|
|
|
|
The 'having' construct implements the SQL "HAVING" clause.
|
|
|
|
It is a top-level segment clause, and must be preceded by a 'group_by' clause.
|
|
|
|
|
|
|
|
.payment | .customer_id, sum(.amount) |
|
|
|
|
group_by(.customer_id) | having(sum(.amount) > 100)
|
|
|
|
*/
|
|
|
|
|
|
|
|
HAVING: 'having';
|
|
|
|
having: HAVING '(' expr ')';
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
/*
|
2023-03-27 05:03:40 +03:00
|
|
|
order_by
|
2023-03-26 11:01:41 +03:00
|
|
|
------
|
2023-03-26 04:20:53 +03:00
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
The 'order_by' construct implements the SQL "ORDER BY" clause.
|
2023-03-26 04:20:53 +03:00
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
.actor | order_by(.first_name, .last_name)
|
|
|
|
.actor | order_by(.first_name+)
|
|
|
|
.actor | order_by(.actor.first_name-)
|
2023-03-26 04:20:53 +03:00
|
|
|
|
|
|
|
The optional plus/minus tokens specify ASC or DESC order.
|
|
|
|
|
2023-03-26 11:01:41 +03:00
|
|
|
Synonyms:
|
|
|
|
|
|
|
|
- 'sort_by' for jq interoperability.
|
|
|
|
https://stedolan.github.io/jq/manual/v1.6/#sort,sort_by(path_expression)
|
2023-03-26 04:20:53 +03:00
|
|
|
|
2023-03-26 11:01:41 +03:00
|
|
|
We do not implement a 'sort' synonym for the jq 'sort' function, because SQL
|
2023-03-26 04:20:53 +03:00
|
|
|
results are inherently sorted. Although perhaps it should be implemented
|
|
|
|
as a no-op.
|
|
|
|
*/
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
ORDER_BY: 'order_by' | 'sort_by';
|
2023-11-20 09:44:36 +03:00
|
|
|
orderByTerm: selector ('+' | '-')?;
|
2023-03-26 04:20:53 +03:00
|
|
|
orderBy: ORDER_BY '(' orderByTerm (',' orderByTerm)* ')';
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// selector specfies a table name, a column name, or table.column.
|
|
|
|
// - .first_name
|
|
|
|
// - ."first name"
|
|
|
|
// - .actor
|
|
|
|
// - ."actor"
|
|
|
|
// - .actor.first_name
|
|
|
|
selector: NAME (NAME)?;
|
|
|
|
|
|
|
|
// selector is a selector element.
|
|
|
|
// - .first_name
|
|
|
|
// - ."first name"
|
|
|
|
// - .first_name:given_name
|
|
|
|
// - ."first name":given_name
|
|
|
|
// - .actor.first_name
|
|
|
|
// - .actor.first_name:given_name
|
|
|
|
// - ."actor".first_name
|
2023-04-01 11:38:32 +03:00
|
|
|
selectorElement: (selector) (alias)?;
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2023-06-18 09:05:09 +03:00
|
|
|
alias: ALIAS_RESERVED | ':' (ARG | ID | STRING);
|
2023-03-28 09:48:24 +03:00
|
|
|
// The grammar has problems dealing with "reserved" lexer tokens.
|
|
|
|
// Basically, there's a problem with using "column:KEYWORD".
|
|
|
|
// ALIAS_RESERVED is a hack to deal with those cases.
|
|
|
|
// The grammar could probably be refactored to not need this.
|
|
|
|
ALIAS_RESERVED
|
|
|
|
// TODO: Update ALIAS_RESERVED with all "keywords"
|
|
|
|
: ':count'
|
|
|
|
| ':count_unique'
|
|
|
|
| ':avg'
|
|
|
|
| ':group_by'
|
|
|
|
| ':max'
|
|
|
|
| ':min'
|
|
|
|
| ':order_by'
|
|
|
|
| ':unique'
|
|
|
|
;
|
2023-03-22 09:17:34 +03:00
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
ARG: '$' ID;
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
arg : ARG;
|
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// handleTable is a handle.table pair.
|
|
|
|
// - @my1.user
|
|
|
|
handleTable: HANDLE NAME;
|
|
|
|
|
|
|
|
// handle is a source handle.
|
|
|
|
// - @sakila
|
2023-04-16 01:28:51 +03:00
|
|
|
// - @work/acme/sakila
|
|
|
|
// - @home/csv/actor
|
2023-03-22 09:17:34 +03:00
|
|
|
handle: HANDLE;
|
|
|
|
|
|
|
|
// rowRange specifies a range of rows. It gets turned into
|
|
|
|
// a SQL "LIMIT x OFFSET y".
|
|
|
|
// - [] select all rows
|
|
|
|
// - [10] select row 10
|
|
|
|
// - [10:15] select rows 10 thru 15
|
|
|
|
// - [0:15] select rows 0 thru 15
|
|
|
|
// - [:15] same as above (0 thru 15) [10:] select all rows from 10 onwards
|
2022-12-24 19:43:21 +03:00
|
|
|
rowRange:
|
|
|
|
'.[' (
|
|
|
|
NN COLON NN // [10:15]
|
|
|
|
| NN COLON // [10:]
|
|
|
|
| COLON NN // [:15]
|
|
|
|
| NN // [10]
|
|
|
|
)? ']';
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
|
2023-06-17 07:54:25 +03:00
|
|
|
exprElement: expr (alias)?;
|
2022-12-24 19:43:21 +03:00
|
|
|
|
|
|
|
expr:
|
2023-06-17 07:54:25 +03:00
|
|
|
'(' expr ')'
|
|
|
|
| selector
|
2022-12-24 19:43:21 +03:00
|
|
|
| literal
|
2023-04-07 11:00:49 +03:00
|
|
|
| arg
|
2022-12-24 19:43:21 +03:00
|
|
|
| unaryOperator expr
|
|
|
|
| expr '||' expr
|
|
|
|
| expr ( '*' | '/' | '%') expr
|
|
|
|
| expr ( '+' | '-') expr
|
|
|
|
| expr ( '<<' | '>>' | '&') expr
|
|
|
|
| expr ( '<' | '<=' | '>' | '>=') expr
|
|
|
|
| expr ( '==' | '!=' |) expr
|
|
|
|
| expr '&&' expr
|
2023-03-27 05:03:40 +03:00
|
|
|
| func
|
2023-03-22 09:17:34 +03:00
|
|
|
;
|
2022-12-24 19:43:21 +03:00
|
|
|
|
|
|
|
literal: NN | NUMBER | STRING | NULL;
|
|
|
|
|
|
|
|
unaryOperator: '-' | '+' | '~' | '!';
|
|
|
|
|
2023-04-07 11:00:49 +03:00
|
|
|
NULL: 'null';
|
2022-12-24 19:43:21 +03:00
|
|
|
ID: [a-zA-Z_][a-zA-Z0-9_]*;
|
|
|
|
WS: [ \t\r\n]+ -> skip;
|
|
|
|
LPAR: '(';
|
2016-10-17 07:14:01 +03:00
|
|
|
RPAR: ')';
|
2022-12-24 19:43:21 +03:00
|
|
|
LBRA: '[';
|
2016-10-17 07:14:01 +03:00
|
|
|
RBRA: ']';
|
|
|
|
COMMA: ',';
|
2022-12-24 19:43:21 +03:00
|
|
|
PIPE: '|';
|
2016-10-17 07:14:01 +03:00
|
|
|
COLON: ':';
|
2023-04-07 11:00:49 +03:00
|
|
|
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// NN: Natural Number {0,1,2,3, ...}
|
|
|
|
NN: INTF;
|
2016-10-17 07:14:01 +03:00
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
NUMBER:
|
|
|
|
NN
|
|
|
|
| '-'? INTF '.' [0-9]+ EXP? // 1.35, 1.35E-9, 0.3, -4.5
|
|
|
|
| '-'? INTF EXP // 1e10 -3e4
|
|
|
|
| '-'? INTF ; // -3, 45
|
2023-03-22 09:17:34 +03:00
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
fragment INTF: '0' | [1-9] [0-9]*; // no leading zeros
|
2023-03-22 09:17:34 +03:00
|
|
|
|
2022-12-24 19:43:21 +03:00
|
|
|
fragment EXP:
|
2023-03-22 09:17:34 +03:00
|
|
|
[Ee] [+\-]? INTF; // \- since "-" means "range" inside [...]
|
2022-12-24 19:43:21 +03:00
|
|
|
|
|
|
|
LT_EQ: '<=';
|
|
|
|
LT: '<';
|
|
|
|
GT_EQ: '>=';
|
|
|
|
GT: '>';
|
|
|
|
NEQ: '!=';
|
|
|
|
EQ: '==';
|
|
|
|
|
2023-03-26 04:20:53 +03:00
|
|
|
|
2023-04-01 11:38:32 +03:00
|
|
|
NAME: '.' (ARG | ID | STRING);
|
2023-03-19 07:58:00 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// SEL can be .THING or .THING.OTHERTHING.
|
|
|
|
// It can also be ."some name".OTHERTHING, etc.
|
|
|
|
//SEL: '.' (ID | STRING) ('.' (ID | STRING))*;
|
2023-03-19 07:58:00 +03:00
|
|
|
|
2023-03-22 09:17:34 +03:00
|
|
|
// HANDLE: @mydb1 or @postgres_db2 etc.
|
2023-04-16 01:28:51 +03:00
|
|
|
|
|
|
|
HANDLE: '@' ID ('/' ID)*;
|
2022-12-24 19:43:21 +03:00
|
|
|
|
|
|
|
STRING: '"' (ESC | ~["\\])* '"';
|
|
|
|
fragment ESC: '\\' (["\\/bfnrt] | UNICODE);
|
|
|
|
fragment UNICODE: 'u' HEX HEX HEX HEX;
|
|
|
|
fragment HEX: [0-9a-fA-F];
|
2020-08-06 20:58:47 +03:00
|
|
|
|
|
|
|
//NUMERIC_LITERAL
|
2022-12-24 19:43:21 +03:00
|
|
|
// : DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )? | '.' DIGIT+ ( E [-+]? DIGIT+ )? ;
|
|
|
|
|
|
|
|
fragment DIGIT: [0-9];
|
|
|
|
|
|
|
|
fragment A: [aA];
|
|
|
|
fragment B: [bB];
|
|
|
|
fragment C: [cC];
|
|
|
|
fragment D: [dD];
|
|
|
|
fragment E: [eE];
|
|
|
|
fragment F: [fF];
|
|
|
|
fragment G: [gG];
|
|
|
|
fragment H: [hH];
|
|
|
|
fragment I: [iI];
|
|
|
|
fragment J: [jJ];
|
|
|
|
fragment K: [kK];
|
|
|
|
fragment L: [lL];
|
|
|
|
fragment M: [mM];
|
|
|
|
fragment N: [nN];
|
|
|
|
fragment O: [oO];
|
|
|
|
fragment P: [pP];
|
|
|
|
fragment Q: [qQ];
|
|
|
|
fragment R: [rR];
|
|
|
|
fragment S: [sS];
|
|
|
|
fragment T: [tT];
|
|
|
|
fragment U: [uU];
|
|
|
|
fragment V: [vV];
|
|
|
|
fragment W: [wW];
|
|
|
|
fragment X: [xX];
|
|
|
|
fragment Y: [yY];
|
|
|
|
fragment Z: [zZ];
|
|
|
|
|
2023-03-27 05:03:40 +03:00
|
|
|
LINECOMMENT: '#' .*? '\n' -> skip;
|
2023-03-22 09:17:34 +03:00
|
|
|
|
|
|
|
//// From https://github.com/antlr/grammars-v4/blob/master/sql/sqlite/SQLiteLexer.g4
|
|
|
|
//IDENTIFIER:
|
|
|
|
// '"' (~'"' | '""')* '"'
|
|
|
|
// | '`' (~'`' | '``')* '`'
|
|
|
|
// | '[' ~']'* ']'
|
|
|
|
// | [A-Z_] [A-Z_0-9]*
|
|
|
|
//; // TODO check: needs more chars in set
|
2023-03-28 09:48:24 +03:00
|
|
|
|
|
|
|
|