This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
SyntaxAnalysisTest.java
149 lines (126 loc) Β· 4.95 KB
/
SyntaxAnalysisTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.opendistroforelasticsearch.sql.antlr;
import com.amazon.opendistroforelasticsearch.sql.antlr.syntax.SyntaxAnalysisException;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.Matchers.allOf;
/**
* Test cases focused on illegal syntax testing (blacklist) along with a few normal cases not covered previously.
* All other normal cases should be covered in existing unit test and IT.
*/
public class SyntaxAnalysisTest {
/** public accessor is required by @Rule annotation */
@Rule
public ExpectedException exception = ExpectedException.none();
private OpenDistroSqlAnalyzer analyzer = new OpenDistroSqlAnalyzer(new SqlAnalysisConfig(true, true, 1000));
/** In reality exception occurs before reaching new parser for now */
@Test
public void unsupportedKeywordShouldThrowException() {
expectValidationFailWithErrorMessage(
"INSERT INTO accounts VALUES ('a')",
"offending symbol [INSERT]"
);
}
/**
* Why we need to let it go and verify in semantic analyzer?
* Parser treats LOG123 a valid column and stops at '(' which gives wrong location and expected token
* In this case it's hard for parser to figure out if this is a wrong function name indeed or not.
* So we let it pass as an UDF and fail in semantic analyzer with meaningful message.
*/
@Test //(expected = SyntaxAnalysisException.class)
public void unsupportedFunctionShouldThrowException() {
validate("SELECT * FROM accounts WHERE LOG123(balance) = 1");
}
@Test
public void unsupportedOperatorShouldPassSyntaxCheck() {
expectValidationFailWithErrorMessage(
"SELECT * FROM accounts WHERE age <=> 1",
"offending symbol [>]"
);
}
@Test
public void missingFromClauseShouldThrowException() {
expectValidationFailWithErrorMessage(
"SELECT 1",
"offending symbol [<EOF>]" // parsing was unable to terminate normally
);
}
@Test
public void missingWhereKeywordShouldThrowException() {
expectValidationFailWithErrorMessage(
"SELECT * FROM accounts age = 1",
"offending symbol [=]", // parser thought 'age' is alias of 'accounts' and failed at '='
"Expecting", ";" // "Expecting tokens in {<EOF>, ';'}"
);
}
@Test
public void someKeywordsShouldBeAbleToUseAsIdentifier() {
validate("SELECT AVG(balance) AS avg FROM accounts");
}
@Test
public void specialIndexNameShouldPass() {
validate("SELECT * FROM accounts/temp");
validate("SELECT * FROM account*");
validate("SELECT * FROM es-accounts");
validate("SELECT * FROM es-account*");
}
@Test
public void typeNamePatternShouldThrowException() {
expectValidationFailWithErrorMessage(
"SELECT * FROM accounts/tem*",
"offending symbol [*]"
);
}
@Test
public void systemIndexNameShouldPass() {
validate("SELECT * FROM .kibana");
}
@Test
public void useMetadataFieldShouldPass() {
validate("SELECT @timestamp FROM accounts");
}
@Test
public void leftJoinOnNestedFieldWithoutOnClauseShouldPass() {
validate("SELECT * FROM accounts a LEFT JOIN a.projects p");
}
@Test
public void useDeepNestedFieldShouldPass() {
validate("SELECT a.projects.name FROM accounts a");
}
/** As the translation is not supported for now, check this in semantic analyzer */
@Test
public void arithmeticExpressionInWhereClauseShouldPass() {
validate("SELECT * FROM accounts WHERE age + 1 = 10");
}
@Test
public void queryEndWithSemiColonShouldPass() {
validate("SELECT * FROM accounts;");
}
private void expectValidationFailWithErrorMessage(String query, String... messages) {
exception.expect(SyntaxAnalysisException.class);
exception.expectMessage(allOf(Arrays.stream(messages).
map(Matchers::containsString).
collect(toList())));
validate(query);
}
private void validate(String sql) {
analyzer.analyzeSyntax(sql);
}
}