Skip to content

Commit

Permalink
SQL: Support for escape sequences (#31884)
Browse files Browse the repository at this point in the history
Enhance grammar to allow JDBC/ODBC escape sequences, namely
- date, time and timestamp {d ''}, {t ''} and {ts ''}
- guid {guid ''}
- LIKE escape {escape ''}
- scalar function {fn }

Fix #31883

(cherry picked from commit dc633e0)
  • Loading branch information
costin committed Jul 11, 2018
1 parent 5440fb4 commit 4214375
Show file tree
Hide file tree
Showing 13 changed files with 2,921 additions and 1,375 deletions.
62 changes: 57 additions & 5 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ queryNoWith
: queryTerm
/** we could add sort by - sort per partition */
(ORDER BY orderBy (',' orderBy)*)?
(LIMIT limit=(INTEGER_VALUE | ALL))?
limitClause?
;

limitClause
: LIMIT limit=(INTEGER_VALUE | ALL)
| LIMIT_ESC limit=(INTEGER_VALUE | ALL) ESC_END
;

queryTerm
: querySpecification #queryPrimaryDefault
| '(' queryNoWith ')' #subquery
Expand Down Expand Up @@ -185,7 +190,12 @@ predicate
;

pattern
: value=string (ESCAPE escape=string)?
: value=string patternEscape?
;

patternEscape
: ESCAPE escape=string
| ESCAPE_ESC escape=string '}'
;
valueExpression
Expand All @@ -197,25 +207,55 @@ valueExpression
;
primaryExpression
: CAST '(' expression AS dataType ')' #cast
| EXTRACT '(' field=identifier FROM valueExpression ')' #extract
: castExpression #cast
| extractExpression #extract
| constant #constantDefault
| ASTERISK #star
| (qualifiedName DOT)? ASTERISK #star
| identifier '(' (setQuantifier? expression (',' expression)*)? ')' #functionCall
| functionExpression #function
| '(' query ')' #subqueryExpression
| identifier #columnReference
| qualifiedName #dereference
| '(' expression ')' #parenthesizedExpression
;
castExpression
: castTemplate
| FUNCTION_ESC castTemplate ESC_END
;
castTemplate
: CAST '(' expression AS dataType ')'
;
extractExpression
: extractTemplate
| FUNCTION_ESC extractTemplate ESC_END
;
extractTemplate
: EXTRACT '(' field=identifier FROM valueExpression ')'
;
functionExpression
: functionTemplate
| FUNCTION_ESC functionTemplate '}'
;
functionTemplate
: identifier '(' (setQuantifier? expression (',' expression)*)? ')'
;
constant
: NULL #nullLiteral
| number #numericLiteral
| booleanValue #booleanLiteral
| STRING+ #stringLiteral
| PARAM #paramLiteral
| DATE_ESC string ESC_END #dateEscapedLiteral
| TIME_ESC string ESC_END #timeEscapedLiteral
| TIMESTAMP_ESC string ESC_END #timestampEscapedLiteral
| GUID_ESC string ESC_END #guidEscapedLiteral
;
comparisonOperator
Expand Down Expand Up @@ -351,6 +391,18 @@ VERIFY: 'VERIFY';
WHERE: 'WHERE';
WITH: 'WITH';
// Escaped Sequence
ESCAPE_ESC: '{ESCAPE';
FUNCTION_ESC: '{FN';
LIMIT_ESC:'{LIMIT';
DATE_ESC: '{D';
TIME_ESC: '{T';
TIMESTAMP_ESC: '{TS';
// mapped to string literal
GUID_ESC: '{GUID';
ESC_END: '}';
EQ : '=';
NEQ : '<>' | '!=' | '<=>';
LT : '<';
Expand Down
96 changes: 56 additions & 40 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,41 @@ USING=68
VERIFY=69
WHERE=70
WITH=71
EQ=72
NEQ=73
LT=74
LTE=75
GT=76
GTE=77
PLUS=78
MINUS=79
ASTERISK=80
SLASH=81
PERCENT=82
CONCAT=83
DOT=84
PARAM=85
STRING=86
INTEGER_VALUE=87
DECIMAL_VALUE=88
IDENTIFIER=89
DIGIT_IDENTIFIER=90
TABLE_IDENTIFIER=91
QUOTED_IDENTIFIER=92
BACKQUOTED_IDENTIFIER=93
SIMPLE_COMMENT=94
BRACKETED_COMMENT=95
WS=96
UNRECOGNIZED=97
DELIMITER=98
ESCAPE_ESC=72
FUNCTION_ESC=73
LIMIT_ESC=74
DATE_ESC=75
TIME_ESC=76
TIMESTAMP_ESC=77
GUID_ESC=78
ESC_END=79
EQ=80
NEQ=81
LT=82
LTE=83
GT=84
GTE=85
PLUS=86
MINUS=87
ASTERISK=88
SLASH=89
PERCENT=90
CONCAT=91
DOT=92
PARAM=93
STRING=94
INTEGER_VALUE=95
DECIMAL_VALUE=96
IDENTIFIER=97
DIGIT_IDENTIFIER=98
TABLE_IDENTIFIER=99
QUOTED_IDENTIFIER=100
BACKQUOTED_IDENTIFIER=101
SIMPLE_COMMENT=102
BRACKETED_COMMENT=103
WS=104
UNRECOGNIZED=105
DELIMITER=106
'('=1
')'=2
','=3
Expand Down Expand Up @@ -167,16 +175,24 @@ DELIMITER=98
'VERIFY'=69
'WHERE'=70
'WITH'=71
'='=72
'<'=74
'<='=75
'>'=76
'>='=77
'+'=78
'-'=79
'*'=80
'/'=81
'%'=82
'||'=83
'.'=84
'?'=85
'{ESCAPE'=72
'{FN'=73
'{LIMIT'=74
'{D'=75
'{T'=76
'{TS'=77
'{GUID'=78
'}'=79
'='=80
'<'=82
'<='=83
'>'=84
'>='=85
'+'=86
'-'=87
'*'=88
'/'=89
'%'=90
'||'=91
'.'=92
'?'=93
94 changes: 55 additions & 39 deletions x-pack/plugin/sql/src/main/antlr/SqlBaseLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,40 @@ USING=68
VERIFY=69
WHERE=70
WITH=71
EQ=72
NEQ=73
LT=74
LTE=75
GT=76
GTE=77
PLUS=78
MINUS=79
ASTERISK=80
SLASH=81
PERCENT=82
CONCAT=83
DOT=84
PARAM=85
STRING=86
INTEGER_VALUE=87
DECIMAL_VALUE=88
IDENTIFIER=89
DIGIT_IDENTIFIER=90
TABLE_IDENTIFIER=91
QUOTED_IDENTIFIER=92
BACKQUOTED_IDENTIFIER=93
SIMPLE_COMMENT=94
BRACKETED_COMMENT=95
WS=96
UNRECOGNIZED=97
ESCAPE_ESC=72
FUNCTION_ESC=73
LIMIT_ESC=74
DATE_ESC=75
TIME_ESC=76
TIMESTAMP_ESC=77
GUID_ESC=78
ESC_END=79
EQ=80
NEQ=81
LT=82
LTE=83
GT=84
GTE=85
PLUS=86
MINUS=87
ASTERISK=88
SLASH=89
PERCENT=90
CONCAT=91
DOT=92
PARAM=93
STRING=94
INTEGER_VALUE=95
DECIMAL_VALUE=96
IDENTIFIER=97
DIGIT_IDENTIFIER=98
TABLE_IDENTIFIER=99
QUOTED_IDENTIFIER=100
BACKQUOTED_IDENTIFIER=101
SIMPLE_COMMENT=102
BRACKETED_COMMENT=103
WS=104
UNRECOGNIZED=105
'('=1
')'=2
','=3
Expand Down Expand Up @@ -166,16 +174,24 @@ UNRECOGNIZED=97
'VERIFY'=69
'WHERE'=70
'WITH'=71
'='=72
'<'=74
'<='=75
'>'=76
'>='=77
'+'=78
'-'=79
'*'=80
'/'=81
'%'=82
'||'=83
'.'=84
'?'=85
'{ESCAPE'=72
'{FN'=73
'{LIMIT'=74
'{D'=75
'{T'=76
'{TS'=77
'{GUID'=78
'}'=79
'='=80
'<'=82
'<='=83
'>'=84
'>='=85
'+'=86
'-'=87
'*'=88
'/'=89
'%'=90
'||'=91
'.'=92
'?'=93
Loading

0 comments on commit 4214375

Please sign in to comment.