Skip to content

Commit

Permalink
SQL: Allow whitespaces in escape patterns (#47577)
Browse files Browse the repository at this point in the history
Previously, we supported only the format `{fn <FUNCTION_NAME>()}`
but other DBs like MSSQL, DB2, MariaDB/MySQL alos allow whitespaces
between `{` and `fn`. Furhermore, also some applications - like PowerBI -
generate escape sequences with spaces: `select { fn name(params) } etc.`

Add support for white spaces between `{` and the escape pattern definition
like `fn`, `ts`, `d`, `guid` etc.

Closes: #47401

(cherry picked from commit 08a22d0)
  • Loading branch information
matriv committed Oct 7, 2019
1 parent b6d1d2e commit e698e68
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 618 deletions.
19 changes: 10 additions & 9 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pattern

patternEscape
: ESCAPE escape=string
| ESCAPE_ESC escape=string '}'
| ESCAPE_ESC escape=string ESC_END
;

valueExpression
Expand Down Expand Up @@ -274,7 +274,7 @@ extractTemplate

functionExpression
: functionTemplate
| FUNCTION_ESC functionTemplate '}'
| FUNCTION_ESC functionTemplate ESC_END
;

functionTemplate
Expand Down Expand Up @@ -480,15 +480,16 @@ YEAR: 'YEAR';
YEARS: 'YEARS';

// Escaped Sequence
ESCAPE_ESC: '{ESCAPE';
FUNCTION_ESC: '{FN';
LIMIT_ESC:'{LIMIT';
DATE_ESC: '{D';
TIME_ESC: '{T';
TIMESTAMP_ESC: '{TS';
ESCAPE_ESC: ESC_START 'ESCAPE';
FUNCTION_ESC: ESC_START 'FN';
LIMIT_ESC: ESC_START 'LIMIT';
DATE_ESC: ESC_START 'D';
TIME_ESC: ESC_START 'T';
TIMESTAMP_ESC: ESC_START 'TS';
// mapped to string literal
GUID_ESC: '{GUID';
GUID_ESC: ESC_START 'GUID';

ESC_START: '{' (WS)*;
ESC_END: '}';

// Operators
Expand Down
100 changes: 47 additions & 53 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,37 @@ DATE_ESC=105
TIME_ESC=106
TIMESTAMP_ESC=107
GUID_ESC=108
ESC_END=109
EQ=110
NULLEQ=111
NEQ=112
LT=113
LTE=114
GT=115
GTE=116
PLUS=117
MINUS=118
ASTERISK=119
SLASH=120
PERCENT=121
CAST_OP=122
CONCAT=123
DOT=124
PARAM=125
STRING=126
INTEGER_VALUE=127
DECIMAL_VALUE=128
IDENTIFIER=129
DIGIT_IDENTIFIER=130
TABLE_IDENTIFIER=131
QUOTED_IDENTIFIER=132
BACKQUOTED_IDENTIFIER=133
SIMPLE_COMMENT=134
BRACKETED_COMMENT=135
WS=136
UNRECOGNIZED=137
DELIMITER=138
ESC_START=109
ESC_END=110
EQ=111
NULLEQ=112
NEQ=113
LT=114
LTE=115
GT=116
GTE=117
PLUS=118
MINUS=119
ASTERISK=120
SLASH=121
PERCENT=122
CAST_OP=123
CONCAT=124
DOT=125
PARAM=126
STRING=127
INTEGER_VALUE=128
DECIMAL_VALUE=129
IDENTIFIER=130
DIGIT_IDENTIFIER=131
TABLE_IDENTIFIER=132
QUOTED_IDENTIFIER=133
BACKQUOTED_IDENTIFIER=134
SIMPLE_COMMENT=135
BRACKETED_COMMENT=136
WS=137
UNRECOGNIZED=138
DELIMITER=139
'('=1
')'=2
','=3
Expand Down Expand Up @@ -237,26 +238,19 @@ DELIMITER=138
'WITH'=99
'YEAR'=100
'YEARS'=101
'{ESCAPE'=102
'{FN'=103
'{LIMIT'=104
'{D'=105
'{T'=106
'{TS'=107
'{GUID'=108
'}'=109
'='=110
'<=>'=111
'<'=113
'<='=114
'>'=115
'>='=116
'+'=117
'-'=118
'*'=119
'/'=120
'%'=121
'::'=122
'||'=123
'.'=124
'?'=125
'}'=110
'='=111
'<=>'=112
'<'=114
'<='=115
'>'=116
'>='=117
'+'=118
'-'=119
'*'=120
'/'=121
'%'=122
'::'=123
'||'=124
'.'=125
'?'=126
98 changes: 46 additions & 52 deletions x-pack/plugin/sql/src/main/antlr/SqlBaseLexer.tokens
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,36 @@ DATE_ESC=105
TIME_ESC=106
TIMESTAMP_ESC=107
GUID_ESC=108
ESC_END=109
EQ=110
NULLEQ=111
NEQ=112
LT=113
LTE=114
GT=115
GTE=116
PLUS=117
MINUS=118
ASTERISK=119
SLASH=120
PERCENT=121
CAST_OP=122
CONCAT=123
DOT=124
PARAM=125
STRING=126
INTEGER_VALUE=127
DECIMAL_VALUE=128
IDENTIFIER=129
DIGIT_IDENTIFIER=130
TABLE_IDENTIFIER=131
QUOTED_IDENTIFIER=132
BACKQUOTED_IDENTIFIER=133
SIMPLE_COMMENT=134
BRACKETED_COMMENT=135
WS=136
UNRECOGNIZED=137
ESC_START=109
ESC_END=110
EQ=111
NULLEQ=112
NEQ=113
LT=114
LTE=115
GT=116
GTE=117
PLUS=118
MINUS=119
ASTERISK=120
SLASH=121
PERCENT=122
CAST_OP=123
CONCAT=124
DOT=125
PARAM=126
STRING=127
INTEGER_VALUE=128
DECIMAL_VALUE=129
IDENTIFIER=130
DIGIT_IDENTIFIER=131
TABLE_IDENTIFIER=132
QUOTED_IDENTIFIER=133
BACKQUOTED_IDENTIFIER=134
SIMPLE_COMMENT=135
BRACKETED_COMMENT=136
WS=137
UNRECOGNIZED=138
'('=1
')'=2
','=3
Expand Down Expand Up @@ -236,26 +237,19 @@ UNRECOGNIZED=137
'WITH'=99
'YEAR'=100
'YEARS'=101
'{ESCAPE'=102
'{FN'=103
'{LIMIT'=104
'{D'=105
'{T'=106
'{TS'=107
'{GUID'=108
'}'=109
'='=110
'<=>'=111
'<'=113
'<='=114
'>'=115
'>='=116
'+'=117
'-'=118
'*'=119
'/'=120
'%'=121
'::'=122
'||'=123
'.'=124
'?'=125
'}'=110
'='=111
'<=>'=112
'<'=114
'<='=115
'>'=116
'>='=117
'+'=118
'-'=119
'*'=120
'/'=121
'%'=122
'::'=123
'||'=124
'.'=125
'?'=126
Loading

0 comments on commit e698e68

Please sign in to comment.