Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql grammar/lexer/parser/listener #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ require (
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/getsentry/sentry-go v0.13.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down Expand Up @@ -309,6 +308,7 @@ require (
github.com/hashicorp/go-version v1.5.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/memberlist v0.3.1 // indirect
github.com/hashicorp/serf v0.9.7 // indirect
github.com/hetznercloud/hcloud-go v1.33.2 // indirect
github.com/honeycombio/libhoney-go v1.15.8 // indirect
Expand Down Expand Up @@ -362,7 +362,6 @@ require (
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/hashstructure v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
Expand Down Expand Up @@ -433,7 +432,6 @@ require (
github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.9 // indirect
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921 // indirect
github.com/shirou/gopsutil v3.21.10+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.22.5 // indirect
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.3 // indirect
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083 // indirect
Expand Down Expand Up @@ -537,6 +535,13 @@ require (
skywalking.apache.org/repo/goapi v0.0.0-20220121092418-9c455d0dda3f // indirect
)

require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20220626175859-9abda183db8e
github.com/go-kit/kit v0.12.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/shirou/gopsutil v3.21.10+incompatible // indirect
)

// Replace references to modules that are in this repository with their relateive paths
// so that we always build with current (latest) version of the source code.

Expand Down
282 changes: 281 additions & 1 deletion go.sum

Large diffs are not rendered by default.

141 changes: 141 additions & 0 deletions processor/stream_processor/Sql.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
grammar Sql;



sqlQuery
: selectQuery EOF;

selectQuery
: K_SELECT (resultColumns) ( whereStatement )? EOQ
;


resultColumns
: column (COMMA column)+
| avg (COMMA avg)+
| STAR
;


column
: IDENTIFIER
;

whereStatement
: ( K_WHERE expr )?
| (tumblingWindow) ( K_WHERE expr )? (groupBy)
;

tumblingWindow
: K_WINDOW_TUMBLING NUMERIC_LITERAL( K_WHERE expr )?
;

groupBy
: K_GROUP_BY column
;


avg
: (K_MIN | K_MAX | K_COUNT | K_AVG) L_BRACKET column R_BRACKET;

SPACE
: [ \u000B\t\r\n] -> channel(HIDDEN)
;

COMMA : ',' ;
L_BRACKET : '(' ;
R_BRACKET : ')' ;

EOQ: ';';

K_SELECT : S E L E C T;
K_WHERE : W H E R E;
K_WINDOW_TUMBLING : W I N D O W SPACE T U M B L I N G;
K_GROUP_BY : G R O U P SPACE B Y;
K_AND : A N D;
K_OR : O R;
K_IS : I S;
K_LIKE : L I K E;
K_EQUAL : '=';
K_GREATER : '>';
K_LESS : '<';
K_LESS_EQUAL : (K_LESS K_EQUAL);
K_GREATER_EQUAL : (K_GREATER K_EQUAL);
K_NOT_EQUAL : ('!' K_EQUAL);
K_NULL : N U L L;
K_IS_NULL : (K_IS SPACE K_NULL);
K_IS_NOT_NULL : (K_IS SPACE K_NOT SPACE K_NULL);
K_NOT : N O T;
K_NOT_IN : (K_NOT SPACE I N);
K_IN : I N;

K_COUNT : C O U N T;
K_MIN : M I N;
K_MAX : M A X;
K_AVG : A V G;

IDENTIFIER
: '"' (~'"' | '""')* '"'
| '`' (~'`' | '``')* '`'
| '[' ~']'* ']'
| [a-zA-Z_] [a-zA-Z_0-9]*
;

expr
: IDENTIFIER comparisonOperator literalValue
| expr ( K_AND | K_OR ) expr
| IDENTIFIER (K_IS_NULL | K_IS_NOT_NULL)
;



comparisonOperator
: K_EQUAL | K_GREATER | K_LESS | K_LESS_EQUAL | K_GREATER_EQUAL | K_NOT_EQUAL | K_LIKE | K_IN | K_IS | K_NOT_IN
;


literalValue
: NUMERIC_LITERAL
| STRING_LITERAL
;


NUMERIC_LITERAL
: DIGIT+ ( '.' DIGIT* )? ( E [-+]? DIGIT+ )?
| '.' DIGIT+ ( E [-+]? DIGIT+ )?
;

STRING_LITERAL
: '\'' ( ~'\'' | '\'\'' )* '\''
;


STAR : '*';

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];
88 changes: 88 additions & 0 deletions processor/stream_processor/parser/Sql.interp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
token literal names:
null
null
','
'('
')'
';'
null
null
null
null
null
null
null
null
'='
'>'
'<'
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
'*'

token symbolic names:
null
SPACE
COMMA
L_BRACKET
R_BRACKET
EOQ
K_SELECT
K_WHERE
K_WINDOW_TUMBLING
K_GROUP_BY
K_AND
K_OR
K_IS
K_LIKE
K_EQUAL
K_GREATER
K_LESS
K_LESS_EQUAL
K_GREATER_EQUAL
K_NOT_EQUAL
K_NULL
K_IS_NULL
K_IS_NOT_NULL
K_NOT
K_NOT_IN
K_IN
K_COUNT
K_MIN
K_MAX
K_AVG
IDENTIFIER
NUMERIC_LITERAL
STRING_LITERAL
STAR

rule names:
sqlQuery
selectQuery
resultColumns
column
whereStatement
tumblingWindow
groupBy
avg
expr
comparisonOperator
literalValue


atn:
[4, 1, 33, 100, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 3, 1, 29, 8, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 4, 2, 36, 8, 2, 11, 2, 12, 2, 37, 1, 2, 1, 2, 1, 2, 4, 2, 43, 8, 2, 11, 2, 12, 2, 44, 1, 2, 3, 2, 48, 8, 2, 1, 3, 1, 3, 1, 4, 1, 4, 3, 4, 54, 8, 4, 1, 4, 1, 4, 1, 4, 3, 4, 59, 8, 4, 1, 4, 1, 4, 3, 4, 63, 8, 4, 1, 5, 1, 5, 1, 5, 1, 5, 3, 5, 69, 8, 5, 1, 6, 1, 6, 1, 6, 1, 7, 1, 7, 1, 7, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 1, 8, 3, 8, 86, 8, 8, 1, 8, 1, 8, 1, 8, 5, 8, 91, 8, 8, 10, 8, 12, 8, 94, 9, 8, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 0, 1, 16, 11, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 0, 5, 1, 0, 26, 29, 1, 0, 21, 22, 1, 0, 10, 11, 2, 0, 12, 19, 24, 25, 1, 0, 31, 32, 99, 0, 22, 1, 0, 0, 0, 2, 25, 1, 0, 0, 0, 4, 47, 1, 0, 0, 0, 6, 49, 1, 0, 0, 0, 8, 62, 1, 0, 0, 0, 10, 64, 1, 0, 0, 0, 12, 70, 1, 0, 0, 0, 14, 73, 1, 0, 0, 0, 16, 85, 1, 0, 0, 0, 18, 95, 1, 0, 0, 0, 20, 97, 1, 0, 0, 0, 22, 23, 3, 2, 1, 0, 23, 24, 5, 0, 0, 1, 24, 1, 1, 0, 0, 0, 25, 26, 5, 6, 0, 0, 26, 28, 3, 4, 2, 0, 27, 29, 3, 8, 4, 0, 28, 27, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 5, 5, 0, 0, 31, 3, 1, 0, 0, 0, 32, 35, 3, 6, 3, 0, 33, 34, 5, 2, 0, 0, 34, 36, 3, 6, 3, 0, 35, 33, 1, 0, 0, 0, 36, 37, 1, 0, 0, 0, 37, 35, 1, 0, 0, 0, 37, 38, 1, 0, 0, 0, 38, 48, 1, 0, 0, 0, 39, 42, 3, 14, 7, 0, 40, 41, 5, 2, 0, 0, 41, 43, 3, 14, 7, 0, 42, 40, 1, 0, 0, 0, 43, 44, 1, 0, 0, 0, 44, 42, 1, 0, 0, 0, 44, 45, 1, 0, 0, 0, 45, 48, 1, 0, 0, 0, 46, 48, 5, 33, 0, 0, 47, 32, 1, 0, 0, 0, 47, 39, 1, 0, 0, 0, 47, 46, 1, 0, 0, 0, 48, 5, 1, 0, 0, 0, 49, 50, 5, 30, 0, 0, 50, 7, 1, 0, 0, 0, 51, 52, 5, 7, 0, 0, 52, 54, 3, 16, 8, 0, 53, 51, 1, 0, 0, 0, 53, 54, 1, 0, 0, 0, 54, 63, 1, 0, 0, 0, 55, 58, 3, 10, 5, 0, 56, 57, 5, 7, 0, 0, 57, 59, 3, 16, 8, 0, 58, 56, 1, 0, 0, 0, 58, 59, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 61, 3, 12, 6, 0, 61, 63, 1, 0, 0, 0, 62, 53, 1, 0, 0, 0, 62, 55, 1, 0, 0, 0, 63, 9, 1, 0, 0, 0, 64, 65, 5, 8, 0, 0, 65, 68, 5, 31, 0, 0, 66, 67, 5, 7, 0, 0, 67, 69, 3, 16, 8, 0, 68, 66, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 11, 1, 0, 0, 0, 70, 71, 5, 9, 0, 0, 71, 72, 3, 6, 3, 0, 72, 13, 1, 0, 0, 0, 73, 74, 7, 0, 0, 0, 74, 75, 5, 3, 0, 0, 75, 76, 3, 6, 3, 0, 76, 77, 5, 4, 0, 0, 77, 15, 1, 0, 0, 0, 78, 79, 6, 8, -1, 0, 79, 80, 5, 30, 0, 0, 80, 81, 3, 18, 9, 0, 81, 82, 3, 20, 10, 0, 82, 86, 1, 0, 0, 0, 83, 84, 5, 30, 0, 0, 84, 86, 7, 1, 0, 0, 85, 78, 1, 0, 0, 0, 85, 83, 1, 0, 0, 0, 86, 92, 1, 0, 0, 0, 87, 88, 10, 2, 0, 0, 88, 89, 7, 2, 0, 0, 89, 91, 3, 16, 8, 3, 90, 87, 1, 0, 0, 0, 91, 94, 1, 0, 0, 0, 92, 90, 1, 0, 0, 0, 92, 93, 1, 0, 0, 0, 93, 17, 1, 0, 0, 0, 94, 92, 1, 0, 0, 0, 95, 96, 7, 3, 0, 0, 96, 19, 1, 0, 0, 0, 97, 98, 7, 4, 0, 0, 98, 21, 1, 0, 0, 0, 10, 28, 37, 44, 47, 53, 58, 62, 68, 85, 92]
41 changes: 41 additions & 0 deletions processor/stream_processor/parser/Sql.tokens
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
SPACE=1
COMMA=2
L_BRACKET=3
R_BRACKET=4
EOQ=5
K_SELECT=6
K_WHERE=7
K_WINDOW_TUMBLING=8
K_GROUP_BY=9
K_AND=10
K_OR=11
K_IS=12
K_LIKE=13
K_EQUAL=14
K_GREATER=15
K_LESS=16
K_LESS_EQUAL=17
K_GREATER_EQUAL=18
K_NOT_EQUAL=19
K_NULL=20
K_IS_NULL=21
K_IS_NOT_NULL=22
K_NOT=23
K_NOT_IN=24
K_IN=25
K_COUNT=26
K_MIN=27
K_MAX=28
K_AVG=29
IDENTIFIER=30
NUMERIC_LITERAL=31
STRING_LITERAL=32
STAR=33
','=2
'('=3
')'=4
';'=5
'='=14
'>'=15
'<'=16
'*'=33
Loading