-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathJoinAliasWriterRuleIT.java
More file actions
261 lines (240 loc) · 10.7 KB
/
Copy pathJoinAliasWriterRuleIT.java
File metadata and controls
261 lines (240 loc) · 10.7 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.legacy;
import static org.hamcrest.Matchers.equalTo;
import static org.opensearch.sql.util.Capability.EXPLAIN_FORMAT;
import static org.opensearch.sql.util.Capability.QUERY_ERROR_MESSAGE;
import java.io.IOException;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.client.ResponseException;
import org.opensearch.sql.util.RequiresCapability;
/** Test cases for writing missing join table aliases. */
public class JoinAliasWriterRuleIT extends SQLIntegTestCase {
@Rule public final ExpectedException exception = ExpectedException.none();
protected void init() throws Exception {
loadIndex(Index.ORDER); // opensearch-sql_test_index_order
loadIndex(Index.BANK); // opensearch-sql_test_index_bank
loadIndex(Index.BANK_TWO); // opensearch-sql_test_index_bank_two
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void noTableAliasNoCommonColumns() throws IOException {
sameExplain(
query(
"SELECT id, firstname",
"FROM opensearch-sql_test_index_order",
"INNER JOIN opensearch-sql_test_index_bank ",
"ON name = firstname WHERE state = 'WA' OR id < 7"),
query(
"SELECT opensearch-sql_test_index_order_0.id,"
+ " opensearch-sql_test_index_bank_1.firstname ",
"FROM opensearch-sql_test_index_order opensearch-sql_test_index_order_0 ",
"INNER JOIN opensearch-sql_test_index_bank opensearch-sql_test_index_bank_1 ",
"ON opensearch-sql_test_index_order_0.name = opensearch-sql_test_index_bank_1.firstname"
+ " ",
"WHERE opensearch-sql_test_index_bank_1.state = 'WA' OR"
+ " opensearch-sql_test_index_order_0.id < 7"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void oneTableAliasNoCommonColumns() throws IOException {
sameExplain(
query(
"SELECT id, firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank ",
"ON name = firstname WHERE state = 'WA' OR id < 7"),
query(
"SELECT a.id, opensearch-sql_test_index_bank_0.firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank opensearch-sql_test_index_bank_0 ",
"ON a.name = opensearch-sql_test_index_bank_0.firstname ",
"WHERE opensearch-sql_test_index_bank_0.state = 'WA' OR a.id < 7"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void bothTableAliasNoCommonColumns() throws IOException {
sameExplain(
query(
"SELECT id, firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON name = firstname WHERE state = 'WA' OR id < 7 "),
query(
"SELECT a.id, b.firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON a.name = b.firstname ",
"WHERE b.state = 'WA' OR a.id < 7 "));
}
@Test
@Ignore
@RequiresCapability(EXPLAIN_FORMAT)
public void tableNamesWithTypeName() throws IOException {
sameExplain(
query(
"SELECT id, firstname ",
"FROM opensearch-sql_test_index_order/_doc ",
"INNER JOIN opensearch-sql_test_index_bank/account ",
"ON name = firstname WHERE state = 'WA' OR id < 7"),
query(
"SELECT opensearch-sql_test_index_order_0.id,"
+ " opensearch-sql_test_index_bank_1.firstname ",
"FROM opensearch-sql_test_index_order/_doc opensearch-sql_test_index_order_0 ",
"INNER JOIN opensearch-sql_test_index_bank/_account opensearch-sql_test_index_bank_1 ",
"ON opensearch-sql_test_index_order_0.name = opensearch-sql_test_index_bank_1.firstname"
+ " ",
"WHERE opensearch-sql_test_index_bank_1.state = 'WA' OR"
+ " opensearch-sql_test_index_order_0.id < 7"));
}
@Ignore
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void tableNamesWithTypeNameExplicitTableAlias() throws IOException {
sameExplain(
query(
"SELECT id, firstname ",
"FROM opensearch-sql_test_index_order/_doc a ",
"INNER JOIN opensearch-sql_test_index_bank/account b ",
"ON name = firstname WHERE state = 'WA' OR id < 7"),
query(
"SELECT a.id, b.firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON a.name = b.firstname ",
"WHERE b.state = 'WA' OR a.id < 7"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void actualTableNameAsAliasOnColumnFields() throws IOException {
sameExplain(
query(
"SELECT opensearch-sql_test_index_order.id, b.firstname ",
"FROM opensearch-sql_test_index_order ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON opensearch-sql_test_index_order.name = firstname WHERE state = 'WA' OR id < 7"),
query(
"SELECT opensearch-sql_test_index_order_0.id, b.firstname ",
"FROM opensearch-sql_test_index_order opensearch-sql_test_index_order_0 ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON opensearch-sql_test_index_order_0.name = b.firstname ",
"WHERE b.state = 'WA' OR opensearch-sql_test_index_order_0.id < 7"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void actualTableNameAsAliasOnColumnFieldsTwo() throws IOException {
sameExplain(
query(
"SELECT opensearch-sql_test_index_order.id, opensearch-sql_test_index_bank.firstname ",
"FROM opensearch-sql_test_index_order ",
"INNER JOIN opensearch-sql_test_index_bank ",
"ON opensearch-sql_test_index_order.name = firstname ",
"WHERE opensearch-sql_test_index_bank.state = 'WA' OR id < 7"),
query(
"SELECT opensearch-sql_test_index_order_0.id,"
+ " opensearch-sql_test_index_bank_1.firstname ",
"FROM opensearch-sql_test_index_order opensearch-sql_test_index_order_0 ",
"INNER JOIN opensearch-sql_test_index_bank opensearch-sql_test_index_bank_1",
"ON opensearch-sql_test_index_order_0.name = opensearch-sql_test_index_bank_1.firstname"
+ " ",
"WHERE opensearch-sql_test_index_bank_1.state = 'WA' OR"
+ " opensearch-sql_test_index_order_0.id < 7"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void columnsWithTableAliasNotAffected() throws IOException {
sameExplain(
query(
"SELECT a.id, firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON name = b.firstname WHERE state = 'WA' OR a.id < 7"),
query(
"SELECT a.id, b.firstname ",
"FROM opensearch-sql_test_index_order a ",
"INNER JOIN opensearch-sql_test_index_bank b ",
"ON a.name = b.firstname ",
"WHERE b.state = 'WA' OR a.id < 7"));
}
@Test
@RequiresCapability(QUERY_ERROR_MESSAGE)
public void commonColumnWithoutTableAliasDifferentTables() throws IOException {
exception.expect(ResponseException.class);
exception.expectMessage("Field name [firstname] is ambiguous");
String explain =
explainQuery(
query(
"SELECT firstname, lastname ",
"FROM opensearch-sql_test_index_bank ",
"LEFT JOIN opensearch-sql_test_index_bank_two ",
"ON firstname = lastname WHERE state = 'VA' "));
}
@Test
@RequiresCapability(QUERY_ERROR_MESSAGE)
public void sameTablesNoAliasAndNoAliasOnColumns() throws IOException {
exception.expect(ResponseException.class);
exception.expectMessage("Not unique table/alias: [opensearch-sql_test_index_bank]");
String explain =
explainQuery(
query(
"SELECT firstname, lastname ",
"FROM opensearch-sql_test_index_bank ",
"LEFT JOIN opensearch-sql_test_index_bank ",
"ON firstname = lastname WHERE state = 'VA' "));
}
@Test
@RequiresCapability(QUERY_ERROR_MESSAGE)
public void sameTablesNoAliasWithTableNameAsAliasOnColumns() throws IOException {
exception.expect(ResponseException.class);
exception.expectMessage("Not unique table/alias: [opensearch-sql_test_index_bank]");
String explain =
explainQuery(
query(
"SELECT opensearch-sql_test_index_bank.firstname",
"FROM opensearch-sql_test_index_bank ",
"JOIN opensearch-sql_test_index_bank ",
"ON opensearch-sql_test_index_bank.firstname ="
+ " opensearch-sql_test_index_bank.lastname"));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void sameTablesWithExplicitAliasOnFirst() throws IOException {
sameExplain(
query(
"SELECT opensearch-sql_test_index_bank.firstname, a.lastname ",
"FROM opensearch-sql_test_index_bank a",
"JOIN opensearch-sql_test_index_bank ",
"ON opensearch-sql_test_index_bank.firstname = a.lastname "),
query(
"SELECT opensearch-sql_test_index_bank_0.firstname, a.lastname ",
"FROM opensearch-sql_test_index_bank a",
"JOIN opensearch-sql_test_index_bank opensearch-sql_test_index_bank_0",
"ON opensearch-sql_test_index_bank_0.firstname = a.lastname "));
}
@Test
@RequiresCapability(EXPLAIN_FORMAT)
public void sameTablesWithExplicitAliasOnSecond() throws IOException {
sameExplain(
query(
"SELECT opensearch-sql_test_index_bank.firstname, a.lastname ",
"FROM opensearch-sql_test_index_bank ",
"JOIN opensearch-sql_test_index_bank a",
"ON opensearch-sql_test_index_bank.firstname = a.lastname "),
query(
"SELECT opensearch-sql_test_index_bank_0.firstname, a.lastname ",
"FROM opensearch-sql_test_index_bank opensearch-sql_test_index_bank_0",
"JOIN opensearch-sql_test_index_bank a",
"ON opensearch-sql_test_index_bank_0.firstname = a.lastname "));
}
private void sameExplain(String actualQuery, String expectedQuery) throws IOException {
assertThat(explainQuery(actualQuery), equalTo(explainQuery(expectedQuery)));
}
private String query(String... statements) {
return String.join(" ", statements);
}
}