13
13
* permissions and limitations under the License.
14
14
*/
15
15
16
- package com .amazon .opendistroforelasticsearch .sql .intgtest ;
16
+ package com .amazon .opendistroforelasticsearch .sql .esintgtest ;
17
17
18
- import com .alibaba .druid .sql .parser .ParserException ;
19
- import com .amazon .opendistroforelasticsearch .sql .plugin .SearchDao ;
20
- import com .amazon .opendistroforelasticsearch .sql .exception .SqlParseException ;
21
- import com .amazon .opendistroforelasticsearch .sql .query .SqlElasticSearchRequestBuilder ;
22
18
import org .elasticsearch .action .search .SearchResponse ;
19
+ import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
20
+ import org .elasticsearch .common .xcontent .NamedXContentRegistry ;
21
+ import org .elasticsearch .common .xcontent .XContentFactory ;
22
+ import org .elasticsearch .common .xcontent .XContentParser ;
23
+ import org .elasticsearch .common .xcontent .XContentType ;
23
24
import org .elasticsearch .search .SearchHit ;
24
25
import org .joda .time .DateTime ;
25
26
import org .joda .time .format .DateTimeFormat ;
26
27
import org .joda .time .format .DateTimeFormatter ;
28
+ import org .json .JSONObject ;
27
29
import org .junit .Test ;
28
30
29
- import java .sql . SQLFeatureNotSupportedException ;
31
+ import java .io . IOException ;
30
32
31
- import static org .hamcrest .MatcherAssert .assertThat ;
32
33
import static org .hamcrest .Matchers .equalTo ;
33
34
import static org .hamcrest .Matchers .greaterThan ;
34
35
import static org .hamcrest .Matchers .lessThan ;
35
36
36
- public class DateFunctionsTest {
37
+ public class DateFunctionsIT extends SQLIntegTestCase {
37
38
38
39
private static final String FROM = "FROM " + TestsConstants .TEST_INDEX_ONLINE + "/online" ;
39
40
@@ -45,10 +46,15 @@ public class DateFunctionsTest {
45
46
* can be expanded on by supporting CAST and casting dates given as Strings to TIMESTAMP (SQL's date type).
46
47
*/
47
48
49
+ @ Override
50
+ protected void init () throws Exception {
51
+ loadIndex (Index .ONLINE );
52
+ }
53
+
48
54
@ Test
49
- public void year () {
55
+ public void year () throws IOException {
50
56
SearchHit [] hits = query (
51
- "SELECT YEAR(insert_time) as year"
57
+ "SELECT YEAR(insert_time) as year"
52
58
);
53
59
for (SearchHit hit : hits ) {
54
60
int year = (int ) getField (hit , "year" );
@@ -58,9 +64,9 @@ public void year() {
58
64
}
59
65
60
66
@ Test
61
- public void monthOfYear () {
67
+ public void monthOfYear () throws IOException {
62
68
SearchHit [] hits = query (
63
- "SELECT MONTH_OF_YEAR(insert_time) as month_of_year"
69
+ "SELECT MONTH_OF_YEAR(insert_time) as month_of_year"
64
70
);
65
71
for (SearchHit hit : hits ) {
66
72
int monthOfYear = (int ) getField (hit , "month_of_year" );
@@ -70,9 +76,9 @@ public void monthOfYear() {
70
76
}
71
77
72
78
@ Test
73
- public void weekOfYearInSelect () {
79
+ public void weekOfYearInSelect () throws IOException {
74
80
SearchHit [] hits = query (
75
- "SELECT WEEK_OF_YEAR(insert_time) as week_of_year"
81
+ "SELECT WEEK_OF_YEAR(insert_time) as week_of_year"
76
82
);
77
83
for (SearchHit hit : hits ) {
78
84
int weekOfYear = (int ) getField (hit , "week_of_year" );
@@ -82,12 +88,12 @@ public void weekOfYearInSelect() {
82
88
}
83
89
84
90
@ Test
85
- public void weekOfYearInWhere () {
91
+ public void weekOfYearInWhere () throws IOException {
86
92
SearchHit [] hits = query (
87
- "SELECT insert_time" ,
88
- "WHERE DATE_FORMAT(insert_time, 'YYYY-MM-dd') < '2014-08-19' AND " +
89
- "WEEK_OF_YEAR(insert_time) > 33" ,
90
- "LIMIT 2000"
93
+ "SELECT insert_time" ,
94
+ "WHERE DATE_FORMAT(insert_time, 'YYYY-MM-dd') < '2014-08-19' AND " +
95
+ "WEEK_OF_YEAR(insert_time) > 33" ,
96
+ "LIMIT 2000"
91
97
);
92
98
for (SearchHit hit : hits ) {
93
99
DateTime insertTime = getDateFromSource (hit , "insert_time" );
@@ -96,9 +102,9 @@ public void weekOfYearInWhere() {
96
102
}
97
103
98
104
@ Test
99
- public void dayOfYearInSelect () {
105
+ public void dayOfYearInSelect () throws IOException {
100
106
SearchHit [] hits = query (
101
- "SELECT DAY_OF_YEAR(insert_time) as day_of_year" , "LIMIT 2000"
107
+ "SELECT DAY_OF_YEAR(insert_time) as day_of_year" , "LIMIT 2000"
102
108
);
103
109
for (SearchHit hit : hits ) {
104
110
int dayOfYear = (int ) getField (hit , "day_of_year" );
@@ -108,9 +114,9 @@ public void dayOfYearInSelect() {
108
114
}
109
115
110
116
@ Test
111
- public void dayOfYearInWhere () {
117
+ public void dayOfYearInWhere () throws IOException {
112
118
SearchHit [] hits = query (
113
- "SELECT insert_time" , "WHERE DAY_OF_YEAR(insert_time) < 233" , "LIMIT 10000"
119
+ "SELECT insert_time" , "WHERE DAY_OF_YEAR(insert_time) < 233" , "LIMIT 10000"
114
120
);
115
121
for (SearchHit hit : hits ) {
116
122
DateTime insertTime = getDateFromSource (hit , "insert_time" );
@@ -119,9 +125,9 @@ public void dayOfYearInWhere() {
119
125
}
120
126
121
127
@ Test
122
- public void dayOfMonthInSelect () {
128
+ public void dayOfMonthInSelect () throws IOException {
123
129
SearchHit [] hits = query (
124
- "SELECT DAY_OF_MONTH(insert_time) as day_of_month" , "LIMIT 2000"
130
+ "SELECT DAY_OF_MONTH(insert_time) as day_of_month" , "LIMIT 2000"
125
131
);
126
132
for (SearchHit hit : hits ) {
127
133
int dayOfMonth = (int ) getField (hit , "day_of_month" );
@@ -131,9 +137,9 @@ public void dayOfMonthInSelect() {
131
137
}
132
138
133
139
@ Test
134
- public void dayOfMonthInWhere () {
140
+ public void dayOfMonthInWhere () throws IOException {
135
141
SearchHit [] hits = query (
136
- "SELECT insert_time" , "WHERE DAY_OF_MONTH(insert_time) < 21" , "LIMIT 10000"
142
+ "SELECT insert_time" , "WHERE DAY_OF_MONTH(insert_time) < 21" , "LIMIT 10000"
137
143
);
138
144
for (SearchHit hit : hits ) {
139
145
DateTime insertTime = getDateFromSource (hit , "insert_time" );
@@ -142,9 +148,9 @@ public void dayOfMonthInWhere() {
142
148
}
143
149
144
150
@ Test
145
- public void dayOfWeek () {
151
+ public void dayOfWeek () throws IOException {
146
152
SearchHit [] hits = query (
147
- "SELECT DAY_OF_WEEK(insert_time) as day_of_week" , "LIMIT 2000"
153
+ "SELECT DAY_OF_WEEK(insert_time) as day_of_week" , "LIMIT 2000"
148
154
);
149
155
for (SearchHit hit : hits ) {
150
156
int dayOfWeek = (int ) getField (hit , "day_of_week" );
@@ -154,9 +160,9 @@ public void dayOfWeek() {
154
160
}
155
161
156
162
@ Test
157
- public void hourOfDay () {
163
+ public void hourOfDay () throws IOException {
158
164
SearchHit [] hits = query (
159
- "SELECT HOUR_OF_DAY(insert_time) as hour_of_day" , "LIMIT 1000"
165
+ "SELECT HOUR_OF_DAY(insert_time) as hour_of_day" , "LIMIT 1000"
160
166
);
161
167
for (SearchHit hit : hits ) {
162
168
int hourOfDay = (int ) getField (hit , "hour_of_day" );
@@ -166,9 +172,9 @@ public void hourOfDay() {
166
172
}
167
173
168
174
@ Test
169
- public void minuteOfDay () {
175
+ public void minuteOfDay () throws IOException {
170
176
SearchHit [] hits = query (
171
- "SELECT MINUTE_OF_DAY(insert_time) as minute_of_day" , "LIMIT 500"
177
+ "SELECT MINUTE_OF_DAY(insert_time) as minute_of_day" , "LIMIT 500"
172
178
);
173
179
for (SearchHit hit : hits ) {
174
180
int minuteOfDay = (int ) getField (hit , "minute_of_day" );
@@ -178,9 +184,9 @@ public void minuteOfDay() {
178
184
}
179
185
180
186
@ Test
181
- public void minuteOfHour () {
187
+ public void minuteOfHour () throws IOException {
182
188
SearchHit [] hits = query (
183
- "SELECT MINUTE_OF_HOUR(insert_time) as minute_of_hour" , "LIMIT 500"
189
+ "SELECT MINUTE_OF_HOUR(insert_time) as minute_of_hour" , "LIMIT 500"
184
190
);
185
191
for (SearchHit hit : hits ) {
186
192
int minuteOfHour = (int ) getField (hit , "minute_of_hour" );
@@ -190,9 +196,9 @@ public void minuteOfHour() {
190
196
}
191
197
192
198
@ Test
193
- public void secondOfMinute () {
199
+ public void secondOfMinute () throws IOException {
194
200
SearchHit [] hits = query (
195
- "SELECT SECOND_OF_MINUTE(insert_time) as second_of_minute" , "LIMIT 500"
201
+ "SELECT SECOND_OF_MINUTE(insert_time) as second_of_minute" , "LIMIT 500"
196
202
);
197
203
for (SearchHit hit : hits ) {
198
204
int secondOfMinute = (int ) getField (hit , "second_of_minute" );
@@ -201,19 +207,19 @@ public void secondOfMinute() {
201
207
}
202
208
}
203
209
204
- private SearchHit [] query (String select , String ... statements ) {
210
+ private SearchHit [] query (String select , String ... statements ) throws IOException {
205
211
return execute (select + " " + FROM + " " + String .join (" " , statements ));
206
212
}
207
213
208
- private SearchHit [] execute (String sql ) {
209
- try {
210
- SearchDao searchDao = MainTestSuite .getSearchDao ();
211
- SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder ) searchDao .explain (sql ).explain ();
212
- return ((SearchResponse ) select .get ()).getHits ().getHits ();
213
- } catch (SQLFeatureNotSupportedException | SqlParseException e ) {
214
- throw new ParserException ("Unable to parse query: " + sql );
215
- }
214
+ // TODO: I think this code is now re-used in multiple classes, would be good to move to the base class.
215
+ private SearchHit [] execute (String sqlRequest ) throws IOException {
216
+ final JSONObject jsonObject = executeRequest (makeRequest (sqlRequest ));
216
217
218
+ final XContentParser parser = XContentFactory .xContent (XContentType .JSON ).createParser (
219
+ NamedXContentRegistry .EMPTY ,
220
+ LoggingDeprecationHandler .INSTANCE ,
221
+ jsonObject .toString ());
222
+ return SearchResponse .fromXContent (parser ).getHits ().getHits ();
217
223
}
218
224
219
225
private Object getField (SearchHit hit , String fieldName ) {
0 commit comments