33
33
import java .util .stream .Collectors ;
34
34
import java .util .stream .IntStream ;
35
35
36
+ import static com .amazon .opendistroforelasticsearch .sql .util .MatcherUtils .rows ;
37
+ import static com .amazon .opendistroforelasticsearch .sql .util .MatcherUtils .schema ;
38
+ import static com .amazon .opendistroforelasticsearch .sql .util .MatcherUtils .verifyDataRows ;
39
+ import static com .amazon .opendistroforelasticsearch .sql .util .MatcherUtils .verifySchema ;
36
40
import static org .hamcrest .Matchers .contains ;
37
41
import static org .hamcrest .Matchers .is ;
38
42
@@ -112,6 +116,17 @@ public void and() throws SqlParseException{
112
116
);
113
117
}
114
118
119
+ @ Test
120
+ public void andWithDefaultTimeZone () throws SqlParseException {
121
+ assertThat (
122
+ dateQuery (SELECT_FROM +
123
+ "WHERE date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') >= '2014-08-17 16:13:12' " +
124
+ "AND date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') <= '2014-08-17 16:13:13'" ,
125
+ "yyyy-MM-dd HH:mm:ss" ),
126
+ contains ("2014-08-17 16:13:12" )
127
+ );
128
+ }
129
+
115
130
@ Test
116
131
public void or () throws SqlParseException {
117
132
assertThat (
@@ -152,6 +167,17 @@ public void sortByAliasedDateFormat() throws IOException {
152
167
is (new DateTime ("2014-08-24T00:00:41.221Z" , DateTimeZone .UTC )));
153
168
}
154
169
170
+ @ Test
171
+ public void selectDateTimeWithDefaultTimeZone () throws SqlParseException {
172
+ JSONObject response = executeJdbcRequest ("SELECT date_format(insert_time, 'yyyy-MM-dd') as date " +
173
+ " FROM " + TestsConstants .TEST_INDEX_ONLINE +
174
+ " WHERE date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') >= '2014-08-17 16:13:12' " +
175
+ " AND date_format(insert_time, 'yyyy-MM-dd HH:mm:ss') <= '2014-08-17 16:13:13'" );
176
+
177
+ verifySchema (response , schema ("date" , "" , "text" ));
178
+ verifyDataRows (response , rows ("2014-08-17" ));
179
+ }
180
+
155
181
@ Test
156
182
public void groupByAndSort () throws IOException {
157
183
JSONObject aggregations = executeQuery (
@@ -203,17 +229,19 @@ private void checkAggregations(JSONObject aggregations, String key, Ordering<Com
203
229
}
204
230
205
231
private Set <Object > dateQuery (String sql ) throws SqlParseException {
232
+ return dateQuery (sql , TestsConstants .SIMPLE_DATE_FORMAT );
233
+ }
234
+
235
+ private Set <Object > dateQuery (String sql , String format ) throws SqlParseException {
206
236
try {
207
237
JSONObject response = executeQuery (sql );
208
- return getResult (response , "insert_time" );
238
+ return getResult (response , "insert_time" , DateTimeFormat . forPattern ( format ) );
209
239
} catch (IOException e ) {
210
240
throw new SqlParseException (String .format ("Unable to process query '%s'" , sql ));
211
241
}
212
242
}
213
243
214
- private Set <Object > getResult (JSONObject response , String fieldName ) {
215
- DateTimeFormatter formatter = DateTimeFormat .forPattern (TestsConstants .SIMPLE_DATE_FORMAT );
216
-
244
+ private Set <Object > getResult (JSONObject response , String fieldName , DateTimeFormatter formatter ) {
217
245
JSONArray hits = getHits (response );
218
246
Set <Object > result = new TreeSet <>(); // Using TreeSet so order is maintained
219
247
for (int i = 0 ; i < hits .length (); i ++) {
@@ -227,4 +255,8 @@ private Set<Object> getResult(JSONObject response, String fieldName) {
227
255
228
256
return result ;
229
257
}
258
+
259
+ private JSONObject executeJdbcRequest (String query ) {
260
+ return new JSONObject (executeQuery (query , "jdbc" ));
261
+ }
230
262
}
0 commit comments