forked from opendistro-for-elasticsearch/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AggregationExpressionIT.java
271 lines (235 loc) · 8.51 KB
/
AggregationExpressionIT.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
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
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2020 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.legacy;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.rows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.schema;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifyDataRows;
import static com.amazon.opendistroforelasticsearch.sql.util.MatcherUtils.verifySchema;
import org.json.JSONObject;
import org.junit.Ignore;
import org.junit.Test;
public class AggregationExpressionIT extends SQLIntegTestCase {
@Override
protected void init() throws Exception {
loadIndex(Index.ACCOUNT);
loadIndex(Index.BANK);
}
@Test
public void noGroupKeySingleFuncOverAggWithoutAliasShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT abs(MAX(age)) " +
"FROM %s",
Index.ACCOUNT.getName()));
verifySchema(response, schema("abs(MAX(age))", null, "long"));
verifyDataRows(response, rows(40));
}
@Test
public void noGroupKeyMaxAddMinShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT MAX(age) + MIN(age) as add " +
"FROM %s",
Index.ACCOUNT.getName()));
verifySchema(response, schema("add", "add", "long"));
verifyDataRows(response, rows(60));
}
// todo age field should has long type instead of integer type.
@Ignore
@Test
public void noGroupKeyMaxAddLiteralShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT MAX(age) + 1 as add " +
"FROM %s",
Index.ACCOUNT.getName()));
verifySchema(response, schema("add", "add", "long"));
verifyDataRows(response, rows(41));
}
@Ignore("skip this test because the old engine returns an integer instead of a double type")
@Test
public void noGroupKeyAvgOnIntegerShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT AVG(age) as avg " +
"FROM %s",
Index.BANK.getName()));
verifySchema(response, schema("avg", "avg", "double"));
verifyDataRows(response, rows(34));
}
@Test
public void hasGroupKeyAvgOnIntegerShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, AVG(age) as avg " +
"FROM %s " +
"GROUP BY gender",
Index.BANK.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("avg", "avg", "double"));
verifyDataRows(response,
rows("m", 34.25),
rows("f", 33.666666666666664d));
}
@Test
public void hasGroupKeyMaxAddMinShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, MAX(age) + MIN(age) as add " +
"FROM %s " +
"GROUP BY gender",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("add", "add", "long"));
verifyDataRows(response,
rows("m", 60),
rows("f", 60));
}
// todo age field should has long type instead of integer type.
@Ignore
@Test
public void hasGroupKeyMaxAddLiteralShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, MAX(age) + 1 as add " +
"FROM %s " +
"GROUP BY gender",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("add", "add", "long"));
verifyDataRows(response,
rows("m", 1),
rows("f", 1));
}
@Test
public void noGroupKeyLogMaxAddMinShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT Log(MAX(age) + MIN(age)) as log " +
"FROM %s",
Index.ACCOUNT.getName()));
verifySchema(response, schema("log", "log", "double"));
verifyDataRows(response, rows(4.0943445622221d));
}
@Test
public void hasGroupKeyLogMaxAddMinShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, Log(MAX(age) + MIN(age)) as log " +
"FROM %s " +
"GROUP BY gender",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("log", "log", "double"));
verifyDataRows(response,
rows("m", 4.0943445622221d),
rows("f", 4.0943445622221d));
}
// todo age field should has long type instead of integer type.
@Ignore
@Test
public void AddLiteralOnGroupKeyShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, age+10, max(balance) as max " +
"FROM %s " +
"WHERE gender = 'm' and age < 22 " +
"GROUP BY gender, age " +
"ORDER BY age",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("age", "age", "long"),
schema("max", "max", "long"));
verifyDataRows(response,
rows("m", 30, 49568),
rows("m", 31, 49433));
}
@Test
public void logWithAddLiteralOnGroupKeyShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, Log(age+10) as logAge, max(balance) as max " +
"FROM %s " +
"WHERE gender = 'm' and age < 22 " +
"GROUP BY gender, age " +
"ORDER BY age",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("logAge", "logAge", "double"),
schema("max", "max", "long"));
verifyDataRows(response,
rows("m", 3.4011973816621555d, 49568),
rows("m", 3.4339872044851463d, 49433));
}
// todo max field should has long as type instead of integer type.
@Ignore
@Test
public void logWithAddLiteralOnGroupKeyAndMaxSubtractLiteralShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT gender, Log(age+10) as logAge, max(balance) - 100 as max " +
"FROM %s " +
"WHERE gender = 'm' and age < 22 " +
"GROUP BY gender, age " +
"ORDER BY age",
Index.ACCOUNT.getName()));
verifySchema(response,
schema("gender", null, "text"),
schema("logAge", "logAge", "double"),
schema("max", "max", "long"));
verifyDataRows(response,
rows("m", 3.4011973816621555d, 49468),
rows("m", 3.4339872044851463d, 49333));
}
/**
* The date is in JDBC format.
*/
@Ignore("skip this test due to inconsistency in type in new engine")
@Test
public void groupByDateShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT birthdate, count(*) as count " +
"FROM %s " +
"WHERE age < 30 " +
"GROUP BY birthdate ",
Index.BANK.getName()));
verifySchema(response,
schema("birthdate", null, "date"),
schema("count", "count", "integer"));
verifyDataRows(response,
rows("2018-06-23 00:00:00.000", 1));
}
@Ignore("skip this test due to inconsistency in type in new engine")
@Test
public void groupByDateWithAliasShouldPass() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT birthdate as birth, count(*) as count " +
"FROM %s " +
"WHERE age < 30 " +
"GROUP BY birthdate ",
Index.BANK.getName()));
verifySchema(response,
schema("birth", "birth", "date"),
schema("count", "count", "integer"));
verifyDataRows(response,
rows("2018-06-23 00:00:00.000", 1));
}
@Test
public void aggregateCastStatementShouldNotReturnZero() {
JSONObject response = executeJdbcRequest(String.format(
"SELECT SUM(CAST(male AS INT)) AS male_sum FROM %s",
Index.BANK.getName()));
verifySchema(response, schema("male_sum", "male_sum", "double"));
verifyDataRows(response, rows(4));
}
private JSONObject executeJdbcRequest(String query) {
return new JSONObject(executeQuery(query, "jdbc"));
}
}