SQL: Fix grammar for * in arithm expressions ()

Previously, parsing an arithmetic expression with `*` and no spaces,
e.g.: `2*i` threw a parsing exception as the grammar rule for
tableIdentifier was clashing with the rule for arithmetic operator `*`.

This issue comes already in the lexer and the left part of the
expression (in our example `2*`) was recognised as a
TABLE_IDENTIFIER token.

The solution adopted is to allow the `*` wildcard in the table name
only if it's surrounded with double quotes, e.g.: `"my*index"`

Closes: 
This commit is contained in:
Marios Trivyzas 2018-10-02 11:47:42 +03:00 committed by GitHub
parent aba4a59d0d
commit a8a2a83d45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 66 deletions
docs/reference/sql/language
x-pack
plugin/sql/src
main
antlr
java/org/elasticsearch/xpack/sql/parser
test/java/org/elasticsearch/xpack/sql
qa/sql/src/main/resources

@ -22,6 +22,17 @@ it excludes (due to `-`) all indices that start with `l`.
This notation is very convenient and powerful as it allows both inclusion and exclusion, depending on This notation is very convenient and powerful as it allows both inclusion and exclusion, depending on
the target naming convention. the target naming convention.
The same kind of patterns can also be used to query multiple indices or tables.
For example:
["source","sql",subs="attributes,callouts,macros"]
----
include-tagged::{sql-specs}/docs.csv-spec[fromTablePatternQuoted]
----
NOTE: There is the restriction that all resolved concrete tables have the exact same mapping.
* SQL `LIKE` notation * SQL `LIKE` notation
The common `LIKE` statement (including escaping if needed) to match a wildcard pattern, based on one `_` The common `LIKE` statement (including escaping if needed) to match a wildcard pattern, based on one `_`

@ -88,7 +88,7 @@ where:
Represents the name (optionally qualified) of an existing table, either a concrete or base one (actual index) or alias. Represents the name (optionally qualified) of an existing table, either a concrete or base one (actual index) or alias.
If the table name contains special SQL characters (such as `.`,`-`,etc...) use double quotes to escape them: If the table name contains special SQL characters (such as `.`,`-`,`*`,etc...) use double quotes to escape them:
["source","sql",subs="attributes,callouts,macros"] ["source","sql",subs="attributes,callouts,macros"]
---- ----

@ -454,7 +454,7 @@ DIGIT_IDENTIFIER
; ;
TABLE_IDENTIFIER TABLE_IDENTIFIER
: (LETTER | DIGIT | '_' | '@' | ASTERISK)+ : (LETTER | DIGIT | '_')+
; ;
QUOTED_IDENTIFIER QUOTED_IDENTIFIER

@ -142,7 +142,7 @@ class SqlBaseLexer extends Lexer {
public ATN getATN() { return _ATN; } public ATN getATN() { return _ATN; }
public static final String _serializedATN = public static final String _serializedATN =
"\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2k\u0370\b\1\4\2\t"+ "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2k\u036f\b\1\4\2\t"+
"\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+ "\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13"+
"\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+ "\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22"+
"\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+ "\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31\t\31"+
@ -187,13 +187,13 @@ class SqlBaseLexer extends Lexer {
"\16a\u02ec\3a\6a\u02f0\na\ra\16a\u02f1\3a\3a\7a\u02f6\na\fa\16a\u02f9"+ "\16a\u02ec\3a\6a\u02f0\na\ra\16a\u02f1\3a\3a\7a\u02f6\na\fa\16a\u02f9"+
"\13a\5a\u02fb\na\3a\3a\3a\3a\6a\u0301\na\ra\16a\u0302\3a\3a\5a\u0307\n"+ "\13a\5a\u02fb\na\3a\3a\3a\3a\6a\u0301\na\ra\16a\u0302\3a\3a\5a\u0307\n"+
"a\3b\3b\5b\u030b\nb\3b\3b\3b\7b\u0310\nb\fb\16b\u0313\13b\3c\3c\3c\3c"+ "a\3b\3b\5b\u030b\nb\3b\3b\3b\7b\u0310\nb\fb\16b\u0313\13b\3c\3c\3c\3c"+
"\6c\u0319\nc\rc\16c\u031a\3d\3d\3d\3d\6d\u0321\nd\rd\16d\u0322\3e\3e\3"+ "\6c\u0319\nc\rc\16c\u031a\3d\3d\3d\6d\u0320\nd\rd\16d\u0321\3e\3e\3e\3"+
"e\3e\7e\u0329\ne\fe\16e\u032c\13e\3e\3e\3f\3f\3f\3f\7f\u0334\nf\ff\16"+ "e\7e\u0328\ne\fe\16e\u032b\13e\3e\3e\3f\3f\3f\3f\7f\u0333\nf\ff\16f\u0336"+
"f\u0337\13f\3f\3f\3g\3g\5g\u033d\ng\3g\6g\u0340\ng\rg\16g\u0341\3h\3h"+ "\13f\3f\3f\3g\3g\5g\u033c\ng\3g\6g\u033f\ng\rg\16g\u0340\3h\3h\3i\3i\3"+
"\3i\3i\3j\3j\3j\3j\7j\u034c\nj\fj\16j\u034f\13j\3j\5j\u0352\nj\3j\5j\u0355"+ "j\3j\3j\3j\7j\u034b\nj\fj\16j\u034e\13j\3j\5j\u0351\nj\3j\5j\u0354\nj"+
"\nj\3j\3j\3k\3k\3k\3k\3k\7k\u035e\nk\fk\16k\u0361\13k\3k\3k\3k\3k\3k\3"+ "\3j\3j\3k\3k\3k\3k\3k\7k\u035d\nk\fk\16k\u0360\13k\3k\3k\3k\3k\3k\3l\6"+
"l\6l\u0369\nl\rl\16l\u036a\3l\3l\3m\3m\3\u035f\2n\3\3\5\4\7\5\t\6\13\7"+ "l\u0368\nl\rl\16l\u0369\3l\3l\3m\3m\3\u035e\2n\3\3\5\4\7\5\t\6\13\7\r"+
"\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25"+ "\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25"+
")\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O"+ ")\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!A\"C#E$G%I&K\'M(O"+
")Q*S+U,W-Y.[/]\60_\61a\62c\63e\64g\65i\66k\67m8o9q:s;u<w=y>{?}@\177A\u0081"+ ")Q*S+U,W-Y.[/]\60_\61a\62c\63e\64g\65i\66k\67m8o9q:s;u<w=y>{?}@\177A\u0081"+
"B\u0083C\u0085D\u0087E\u0089F\u008bG\u008dH\u008fI\u0091J\u0093K\u0095"+ "B\u0083C\u0085D\u0087E\u0089F\u008bG\u008dH\u008fI\u0091J\u0093K\u0095"+
@ -201,7 +201,7 @@ class SqlBaseLexer extends Lexer {
"V\u00abW\u00adX\u00afY\u00b1Z\u00b3[\u00b5\\\u00b7]\u00b9^\u00bb_\u00bd"+ "V\u00abW\u00adX\u00afY\u00b1Z\u00b3[\u00b5\\\u00b7]\u00b9^\u00bb_\u00bd"+
"`\u00bfa\u00c1b\u00c3c\u00c5d\u00c7e\u00c9f\u00cbg\u00cd\2\u00cf\2\u00d1"+ "`\u00bfa\u00c1b\u00c3c\u00c5d\u00c7e\u00c9f\u00cbg\u00cd\2\u00cf\2\u00d1"+
"\2\u00d3h\u00d5i\u00d7j\u00d9k\3\2\f\3\2))\4\2BBaa\5\2<<BBaa\3\2$$\3\2"+ "\2\u00d3h\u00d5i\u00d7j\u00d9k\3\2\f\3\2))\4\2BBaa\5\2<<BBaa\3\2$$\3\2"+
"bb\4\2--//\3\2\62;\3\2C\\\4\2\f\f\17\17\5\2\13\f\17\17\"\"\u0392\2\3\3"+ "bb\4\2--//\3\2\62;\3\2C\\\4\2\f\f\17\17\5\2\13\f\17\17\"\"\u0390\2\3\3"+
"\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2"+ "\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2"+
"\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3"+ "\17\3\2\2\2\2\21\3\2\2\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3"+
"\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2"+ "\2\2\2\2\33\3\2\2\2\2\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2"+
@ -245,10 +245,10 @@ class SqlBaseLexer extends Lexer {
"\2\2\u00ad\u02bb\3\2\2\2\u00af\u02bd\3\2\2\2\u00b1\u02bf\3\2\2\2\u00b3"+ "\2\2\u00ad\u02bb\3\2\2\2\u00af\u02bd\3\2\2\2\u00b1\u02bf\3\2\2\2\u00b3"+
"\u02c1\3\2\2\2\u00b5\u02c3\3\2\2\2\u00b7\u02c5\3\2\2\2\u00b9\u02c8\3\2"+ "\u02c1\3\2\2\2\u00b5\u02c3\3\2\2\2\u00b7\u02c5\3\2\2\2\u00b9\u02c8\3\2"+
"\2\2\u00bb\u02ca\3\2\2\2\u00bd\u02cc\3\2\2\2\u00bf\u02d8\3\2\2\2\u00c1"+ "\2\2\u00bb\u02ca\3\2\2\2\u00bd\u02cc\3\2\2\2\u00bf\u02d8\3\2\2\2\u00c1"+
"\u0306\3\2\2\2\u00c3\u030a\3\2\2\2\u00c5\u0314\3\2\2\2\u00c7\u0320\3\2"+ "\u0306\3\2\2\2\u00c3\u030a\3\2\2\2\u00c5\u0314\3\2\2\2\u00c7\u031f\3\2"+
"\2\2\u00c9\u0324\3\2\2\2\u00cb\u032f\3\2\2\2\u00cd\u033a\3\2\2\2\u00cf"+ "\2\2\u00c9\u0323\3\2\2\2\u00cb\u032e\3\2\2\2\u00cd\u0339\3\2\2\2\u00cf"+
"\u0343\3\2\2\2\u00d1\u0345\3\2\2\2\u00d3\u0347\3\2\2\2\u00d5\u0358\3\2"+ "\u0342\3\2\2\2\u00d1\u0344\3\2\2\2\u00d3\u0346\3\2\2\2\u00d5\u0357\3\2"+
"\2\2\u00d7\u0368\3\2\2\2\u00d9\u036e\3\2\2\2\u00db\u00dc\7*\2\2\u00dc"+ "\2\2\u00d7\u0367\3\2\2\2\u00d9\u036d\3\2\2\2\u00db\u00dc\7*\2\2\u00dc"+
"\4\3\2\2\2\u00dd\u00de\7+\2\2\u00de\6\3\2\2\2\u00df\u00e0\7.\2\2\u00e0"+ "\4\3\2\2\2\u00dd\u00de\7+\2\2\u00de\6\3\2\2\2\u00df\u00e0\7.\2\2\u00e0"+
"\b\3\2\2\2\u00e1\u00e2\7<\2\2\u00e2\n\3\2\2\2\u00e3\u00e4\7C\2\2\u00e4"+ "\b\3\2\2\2\u00e1\u00e2\7<\2\2\u00e2\n\3\2\2\2\u00e3\u00e4\7C\2\2\u00e4"+
"\u00e5\7N\2\2\u00e5\u00e6\7N\2\2\u00e6\f\3\2\2\2\u00e7\u00e8\7C\2\2\u00e8"+ "\u00e5\7N\2\2\u00e5\u00e6\7N\2\2\u00e6\f\3\2\2\2\u00e7\u00e8\7C\2\2\u00e8"+
@ -408,38 +408,37 @@ class SqlBaseLexer extends Lexer {
"\2\2\u0313\u0311\3\2\2\2\u0314\u0318\5\u00cfh\2\u0315\u0319\5\u00d1i\2"+ "\2\2\u0313\u0311\3\2\2\2\u0314\u0318\5\u00cfh\2\u0315\u0319\5\u00d1i\2"+
"\u0316\u0319\5\u00cfh\2\u0317\u0319\t\4\2\2\u0318\u0315\3\2\2\2\u0318"+ "\u0316\u0319\5\u00cfh\2\u0317\u0319\t\4\2\2\u0318\u0315\3\2\2\2\u0318"+
"\u0316\3\2\2\2\u0318\u0317\3\2\2\2\u0319\u031a\3\2\2\2\u031a\u0318\3\2"+ "\u0316\3\2\2\2\u0318\u0317\3\2\2\2\u0319\u031a\3\2\2\2\u031a\u0318\3\2"+
"\2\2\u031a\u031b\3\2\2\2\u031b\u00c6\3\2\2\2\u031c\u0321\5\u00d1i\2\u031d"+ "\2\2\u031a\u031b\3\2\2\2\u031b\u00c6\3\2\2\2\u031c\u0320\5\u00d1i\2\u031d"+
"\u0321\5\u00cfh\2\u031e\u0321\t\3\2\2\u031f\u0321\5\u00b1Y\2\u0320\u031c"+ "\u0320\5\u00cfh\2\u031e\u0320\t\3\2\2\u031f\u031c\3\2\2\2\u031f\u031d"+
"\3\2\2\2\u0320\u031d\3\2\2\2\u0320\u031e\3\2\2\2\u0320\u031f\3\2\2\2\u0321"+ "\3\2\2\2\u031f\u031e\3\2\2\2\u0320\u0321\3\2\2\2\u0321\u031f\3\2\2\2\u0321"+
"\u0322\3\2\2\2\u0322\u0320\3\2\2\2\u0322\u0323\3\2\2\2\u0323\u00c8\3\2"+ "\u0322\3\2\2\2\u0322\u00c8\3\2\2\2\u0323\u0329\7$\2\2\u0324\u0328\n\5"+
"\2\2\u0324\u032a\7$\2\2\u0325\u0329\n\5\2\2\u0326\u0327\7$\2\2\u0327\u0329"+ "\2\2\u0325\u0326\7$\2\2\u0326\u0328\7$\2\2\u0327\u0324\3\2\2\2\u0327\u0325"+
"\7$\2\2\u0328\u0325\3\2\2\2\u0328\u0326\3\2\2\2\u0329\u032c\3\2\2\2\u032a"+ "\3\2\2\2\u0328\u032b\3\2\2\2\u0329\u0327\3\2\2\2\u0329\u032a\3\2\2\2\u032a"+
"\u0328\3\2\2\2\u032a\u032b\3\2\2\2\u032b\u032d\3\2\2\2\u032c\u032a\3\2"+ "\u032c\3\2\2\2\u032b\u0329\3\2\2\2\u032c\u032d\7$\2\2\u032d\u00ca\3\2"+
"\2\2\u032d\u032e\7$\2\2\u032e\u00ca\3\2\2\2\u032f\u0335\7b\2\2\u0330\u0334"+ "\2\2\u032e\u0334\7b\2\2\u032f\u0333\n\6\2\2\u0330\u0331\7b\2\2\u0331\u0333"+
"\n\6\2\2\u0331\u0332\7b\2\2\u0332\u0334\7b\2\2\u0333\u0330\3\2\2\2\u0333"+ "\7b\2\2\u0332\u032f\3\2\2\2\u0332\u0330\3\2\2\2\u0333\u0336\3\2\2\2\u0334"+
"\u0331\3\2\2\2\u0334\u0337\3\2\2\2\u0335\u0333\3\2\2\2\u0335\u0336\3\2"+ "\u0332\3\2\2\2\u0334\u0335\3\2\2\2\u0335\u0337\3\2\2\2\u0336\u0334\3\2"+
"\2\2\u0336\u0338\3\2\2\2\u0337\u0335\3\2\2\2\u0338\u0339\7b\2\2\u0339"+ "\2\2\u0337\u0338\7b\2\2\u0338\u00cc\3\2\2\2\u0339\u033b\7G\2\2\u033a\u033c"+
"\u00cc\3\2\2\2\u033a\u033c\7G\2\2\u033b\u033d\t\7\2\2\u033c\u033b\3\2"+ "\t\7\2\2\u033b\u033a\3\2\2\2\u033b\u033c\3\2\2\2\u033c\u033e\3\2\2\2\u033d"+
"\2\2\u033c\u033d\3\2\2\2\u033d\u033f\3\2\2\2\u033e\u0340\5\u00cfh\2\u033f"+ "\u033f\5\u00cfh\2\u033e\u033d\3\2\2\2\u033f\u0340\3\2\2\2\u0340\u033e"+
"\u033e\3\2\2\2\u0340\u0341\3\2\2\2\u0341\u033f\3\2\2\2\u0341\u0342\3\2"+ "\3\2\2\2\u0340\u0341\3\2\2\2\u0341\u00ce\3\2\2\2\u0342\u0343\t\b\2\2\u0343"+
"\2\2\u0342\u00ce\3\2\2\2\u0343\u0344\t\b\2\2\u0344\u00d0\3\2\2\2\u0345"+ "\u00d0\3\2\2\2\u0344\u0345\t\t\2\2\u0345\u00d2\3\2\2\2\u0346\u0347\7/"+
"\u0346\t\t\2\2\u0346\u00d2\3\2\2\2\u0347\u0348\7/\2\2\u0348\u0349\7/\2"+ "\2\2\u0347\u0348\7/\2\2\u0348\u034c\3\2\2\2\u0349\u034b\n\n\2\2\u034a"+
"\2\u0349\u034d\3\2\2\2\u034a\u034c\n\n\2\2\u034b\u034a\3\2\2\2\u034c\u034f"+ "\u0349\3\2\2\2\u034b\u034e\3\2\2\2\u034c\u034a\3\2\2\2\u034c\u034d\3\2"+
"\3\2\2\2\u034d\u034b\3\2\2\2\u034d\u034e\3\2\2\2\u034e\u0351\3\2\2\2\u034f"+ "\2\2\u034d\u0350\3\2\2\2\u034e\u034c\3\2\2\2\u034f\u0351\7\17\2\2\u0350"+
"\u034d\3\2\2\2\u0350\u0352\7\17\2\2\u0351\u0350\3\2\2\2\u0351\u0352\3"+ "\u034f\3\2\2\2\u0350\u0351\3\2\2\2\u0351\u0353\3\2\2\2\u0352\u0354\7\f"+
"\2\2\2\u0352\u0354\3\2\2\2\u0353\u0355\7\f\2\2\u0354\u0353\3\2\2\2\u0354"+ "\2\2\u0353\u0352\3\2\2\2\u0353\u0354\3\2\2\2\u0354\u0355\3\2\2\2\u0355"+
"\u0355\3\2\2\2\u0355\u0356\3\2\2\2\u0356\u0357\bj\2\2\u0357\u00d4\3\2"+ "\u0356\bj\2\2\u0356\u00d4\3\2\2\2\u0357\u0358\7\61\2\2\u0358\u0359\7,"+
"\2\2\u0358\u0359\7\61\2\2\u0359\u035a\7,\2\2\u035a\u035f\3\2\2\2\u035b"+ "\2\2\u0359\u035e\3\2\2\2\u035a\u035d\5\u00d5k\2\u035b\u035d\13\2\2\2\u035c"+
"\u035e\5\u00d5k\2\u035c\u035e\13\2\2\2\u035d\u035b\3\2\2\2\u035d\u035c"+ "\u035a\3\2\2\2\u035c\u035b\3\2\2\2\u035d\u0360\3\2\2\2\u035e\u035f\3\2"+
"\3\2\2\2\u035e\u0361\3\2\2\2\u035f\u0360\3\2\2\2\u035f\u035d\3\2\2\2\u0360"+ "\2\2\u035e\u035c\3\2\2\2\u035f\u0361\3\2\2\2\u0360\u035e\3\2\2\2\u0361"+
"\u0362\3\2\2\2\u0361\u035f\3\2\2\2\u0362\u0363\7,\2\2\u0363\u0364\7\61"+ "\u0362\7,\2\2\u0362\u0363\7\61\2\2\u0363\u0364\3\2\2\2\u0364\u0365\bk"+
"\2\2\u0364\u0365\3\2\2\2\u0365\u0366\bk\2\2\u0366\u00d6\3\2\2\2\u0367"+ "\2\2\u0365\u00d6\3\2\2\2\u0366\u0368\t\13\2\2\u0367\u0366\3\2\2\2\u0368"+
"\u0369\t\13\2\2\u0368\u0367\3\2\2\2\u0369\u036a\3\2\2\2\u036a\u0368\3"+ "\u0369\3\2\2\2\u0369\u0367\3\2\2\2\u0369\u036a\3\2\2\2\u036a\u036b\3\2"+
"\2\2\2\u036a\u036b\3\2\2\2\u036b\u036c\3\2\2\2\u036c\u036d\bl\2\2\u036d"+ "\2\2\u036b\u036c\bl\2\2\u036c\u00d8\3\2\2\2\u036d\u036e\13\2\2\2\u036e"+
"\u00d8\3\2\2\2\u036e\u036f\13\2\2\2\u036f\u00da\3\2\2\2\"\2\u02af\u02d0"+ "\u00da\3\2\2\2\"\2\u02af\u02d0\u02d2\u02da\u02df\u02e5\u02ec\u02f1\u02f7"+
"\u02d2\u02da\u02df\u02e5\u02ec\u02f1\u02f7\u02fa\u0302\u0306\u030a\u030f"+ "\u02fa\u0302\u0306\u030a\u030f\u0311\u0318\u031a\u031f\u0321\u0327\u0329"+
"\u0311\u0318\u031a\u0320\u0322\u0328\u032a\u0333\u0335\u033c\u0341\u034d"+ "\u0332\u0334\u033b\u0340\u034c\u0350\u0353\u035c\u035e\u0369\3\2\3\2";
"\u0351\u0354\u035d\u035f\u036a\3\2\3\2";
public static final ATN _ATN = public static final ATN _ATN =
new ATNDeserializer().deserialize(_serializedATN.toCharArray()); new ATNDeserializer().deserialize(_serializedATN.toCharArray());
static { static {

@ -38,7 +38,7 @@ public class PreAnalyzerTests extends ESTestCase {
} }
public void testWildIndexWithCatalog() { public void testWildIndexWithCatalog() {
LogicalPlan plan = parser.createStatement("SELECT * FROM elastic:index*"); LogicalPlan plan = parser.createStatement("SELECT * FROM elastic:\"index*\"");
PreAnalysis result = preAnalyzer.preAnalyze(plan); PreAnalysis result = preAnalyzer.preAnalyze(plan);
assertThat(plan.preAnalyzed(), is(true)); assertThat(plan.preAnalyzed(), is(true));
assertThat(result.indices, hasSize(1)); assertThat(result.indices, hasSize(1));

@ -9,13 +9,14 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.expression.Expression; import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Literal; import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction; import org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction;
import org.elasticsearch.xpack.sql.expression.predicate.operator.arithmetic.Mul;
import org.elasticsearch.xpack.sql.type.DataType; import org.elasticsearch.xpack.sql.type.DataType;
public class ExpressionTests extends ESTestCase { public class ExpressionTests extends ESTestCase {
private final SqlParser parser = new SqlParser(); private final SqlParser parser = new SqlParser();
public void testTokenFunctionName() throws Exception { public void testTokenFunctionName() {
Expression lt = parser.createExpression("LEFT()"); Expression lt = parser.createExpression("LEFT()");
assertEquals(UnresolvedFunction.class, lt.getClass()); assertEquals(UnresolvedFunction.class, lt.getClass());
UnresolvedFunction uf = (UnresolvedFunction) lt; UnresolvedFunction uf = (UnresolvedFunction) lt;
@ -23,7 +24,7 @@ public class ExpressionTests extends ESTestCase {
} }
public void testLiteralBoolean() throws Exception { public void testLiteralBoolean() {
Expression lt = parser.createExpression("TRUE"); Expression lt = parser.createExpression("TRUE");
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -31,7 +32,7 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.BOOLEAN, l.dataType()); assertEquals(DataType.BOOLEAN, l.dataType());
} }
public void testLiteralDouble() throws Exception { public void testLiteralDouble() {
Expression lt = parser.createExpression(String.valueOf(Double.MAX_VALUE)); Expression lt = parser.createExpression(String.valueOf(Double.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -39,7 +40,7 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.DOUBLE, l.dataType()); assertEquals(DataType.DOUBLE, l.dataType());
} }
public void testLiteralDoubleNegative() throws Exception { public void testLiteralDoubleNegative() {
Expression lt = parser.createExpression(String.valueOf(Double.MIN_VALUE)); Expression lt = parser.createExpression(String.valueOf(Double.MIN_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -47,7 +48,7 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.DOUBLE, l.dataType()); assertEquals(DataType.DOUBLE, l.dataType());
} }
public void testLiteralDoublePositive() throws Exception { public void testLiteralDoublePositive() {
Expression lt = parser.createExpression("+" + Double.MAX_VALUE); Expression lt = parser.createExpression("+" + Double.MAX_VALUE);
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -55,7 +56,7 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.DOUBLE, l.dataType()); assertEquals(DataType.DOUBLE, l.dataType());
} }
public void testLiteralLong() throws Exception { public void testLiteralLong() {
Expression lt = parser.createExpression(String.valueOf(Long.MAX_VALUE)); Expression lt = parser.createExpression(String.valueOf(Long.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -63,14 +64,14 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.LONG, l.dataType()); assertEquals(DataType.LONG, l.dataType());
} }
public void testLiteralLongNegative() throws Exception { public void testLiteralLongNegative() {
Expression lt = parser.createExpression(String.valueOf(Long.MIN_VALUE)); Expression lt = parser.createExpression(String.valueOf(Long.MIN_VALUE));
assertTrue(lt.foldable()); assertTrue(lt.foldable());
assertEquals(Long.MIN_VALUE, lt.fold()); assertEquals(Long.MIN_VALUE, lt.fold());
assertEquals(DataType.LONG, lt.dataType()); assertEquals(DataType.LONG, lt.dataType());
} }
public void testLiteralLongPositive() throws Exception { public void testLiteralLongPositive() {
Expression lt = parser.createExpression("+" + String.valueOf(Long.MAX_VALUE)); Expression lt = parser.createExpression("+" + String.valueOf(Long.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -78,7 +79,7 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.LONG, l.dataType()); assertEquals(DataType.LONG, l.dataType());
} }
public void testLiteralInteger() throws Exception { public void testLiteralInteger() {
Expression lt = parser.createExpression(String.valueOf(Integer.MAX_VALUE)); Expression lt = parser.createExpression(String.valueOf(Integer.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
@ -86,29 +87,44 @@ public class ExpressionTests extends ESTestCase {
assertEquals(DataType.INTEGER, l.dataType()); assertEquals(DataType.INTEGER, l.dataType());
} }
public void testLiteralIntegerWithShortValue() throws Exception { public void testLiteralIntegerWithShortValue() {
Expression lt = parser.createExpression(String.valueOf(Short.MAX_VALUE)); Expression lt = parser.createExpression(String.valueOf(Short.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
assertEquals(Integer.valueOf(Short.MAX_VALUE), l.value()); assertEquals((int) Short.MAX_VALUE, l.value());
assertEquals(DataType.INTEGER, l.dataType()); assertEquals(DataType.INTEGER, l.dataType());
} }
public void testLiteralIntegerWithByteValue() throws Exception { public void testLiteralIntegerWithByteValue() {
Expression lt = parser.createExpression(String.valueOf(Byte.MAX_VALUE)); Expression lt = parser.createExpression(String.valueOf(Byte.MAX_VALUE));
assertEquals(Literal.class, lt.getClass()); assertEquals(Literal.class, lt.getClass());
Literal l = (Literal) lt; Literal l = (Literal) lt;
assertEquals(Integer.valueOf(Byte.MAX_VALUE), l.value()); assertEquals((int) Byte.MAX_VALUE, l.value());
assertEquals(DataType.INTEGER, l.dataType()); assertEquals(DataType.INTEGER, l.dataType());
} }
public void testLiteralIntegerInvalid() throws Exception { public void testLiteralIntegerInvalid() {
ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("123456789098765432101")); ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("123456789098765432101"));
assertEquals("Number [123456789098765432101] is too large", ex.getErrorMessage()); assertEquals("Number [123456789098765432101] is too large", ex.getErrorMessage());
} }
public void testLiteralDecimalTooBig() throws Exception { public void testLiteralDecimalTooBig() {
ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("1.9976931348623157e+308")); ParsingException ex = expectThrows(ParsingException.class, () -> parser.createExpression("1.9976931348623157e+308"));
assertEquals("Number [1.9976931348623157e+308] is too large", ex.getErrorMessage()); assertEquals("Number [1.9976931348623157e+308] is too large", ex.getErrorMessage());
} }
}
public void testLiteralTimesLiteral() {
Expression expr = parser.createExpression("10*2");
assertEquals(Mul.class, expr.getClass());
Mul mul = (Mul) expr;
assertEquals("10 * 2", mul.name());
assertEquals(DataType.INTEGER, mul.dataType());
}
public void testFunctionTimesLiteral() {
Expression expr = parser.createExpression("PI()*2");
assertEquals(Mul.class, expr.getClass());
Mul mul = (Mul) expr;
assertEquals("(PI) * 2", mul.name());
}
}

@ -47,7 +47,7 @@ salary | INTEGER
; ;
describePattern describePattern
DESCRIBE test_*; DESCRIBE "test_*";
column:s | type:s column:s | type:s
@ -99,7 +99,7 @@ F | 10099.28
; ;
testGroupByOnPattern testGroupByOnPattern
SELECT gender, PERCENTILE(emp_no, 97) p1 FROM test_* WHERE gender is NOT NULL GROUP BY gender; SELECT gender, PERCENTILE(emp_no, 97) p1 FROM "test_*" WHERE gender is NOT NULL GROUP BY gender;
gender:s | p1:d gender:s | p1:d