99
1010import static org .opensearch .sql .legacy .TestsConstants .TEST_INDEX_ARRAY ;
1111import static org .opensearch .sql .legacy .TestsConstants .TEST_INDEX_NESTED_SIMPLE ;
12+ import static org .opensearch .sql .util .MatcherUtils .rows ;
1213import static org .opensearch .sql .util .MatcherUtils .schema ;
14+ import static org .opensearch .sql .util .MatcherUtils .verifyDataRows ;
1315import static org .opensearch .sql .util .MatcherUtils .verifyNumOfRows ;
1416import static org .opensearch .sql .util .MatcherUtils .verifySchema ;
1517
18+ import org .json .JSONArray ;
1619import org .json .JSONObject ;
1720import org .junit .Ignore ;
1821import org .junit .jupiter .api .Test ;
@@ -39,11 +42,108 @@ public void testExpandOnNested() throws Exception {
3942 schema ("id" , "bigint" ),
4043 schema ("address" , "struct" ));
4144 verifyNumOfRows (response , 11 );
45+ verifyDataRows (
46+ response ,
47+ rows (
48+ "abbas" ,
49+ null ,
50+ 24 ,
51+ new JSONObject ()
52+ .put ("city" , "New york city" )
53+ .put ("state" , "NY" )
54+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1984-04-12 09:07:42" ))),
55+ rows (
56+ "abbas" ,
57+ null ,
58+ 24 ,
59+ new JSONObject ()
60+ .put ("city" , "bellevue" )
61+ .put ("state" , "WA" )
62+ .put (
63+ "moveInDate" ,
64+ new JSONArray ()
65+ .put (new JSONObject ().put ("dateAndTime" , "2023-05-03 08:07:42" ))
66+ .put (new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" )))),
67+ rows (
68+ "abbas" ,
69+ null ,
70+ 24 ,
71+ new JSONObject ()
72+ .put ("city" , "seattle" )
73+ .put ("state" , "WA" )
74+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1966-03-19 03:04:55" ))),
75+ rows (
76+ "abbas" ,
77+ null ,
78+ 24 ,
79+ new JSONObject ()
80+ .put ("city" , "chicago" )
81+ .put ("state" , "IL" )
82+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2011-06-01 01:01:42" ))),
83+ rows (
84+ "chen" ,
85+ null ,
86+ 32 ,
87+ new JSONObject ()
88+ .put ("city" , "Miami" )
89+ .put ("state" , "Florida" )
90+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1901-08-11 04:03:33" ))),
91+ rows (
92+ "chen" ,
93+ null ,
94+ 32 ,
95+ new JSONObject ()
96+ .put ("city" , "los angeles" )
97+ .put ("state" , "CA" )
98+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2023-05-03 08:07:42" ))),
99+ rows (
100+ "peng" ,
101+ null ,
102+ 26 ,
103+ new JSONObject ()
104+ .put ("city" , "san diego" )
105+ .put ("state" , "CA" )
106+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" ))),
107+ rows (
108+ "peng" ,
109+ null ,
110+ 26 ,
111+ new JSONObject ()
112+ .put ("city" , "austin" )
113+ .put ("state" , "TX" )
114+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1977-07-13 09:04:41" ))),
115+ rows (
116+ "andy" ,
117+ 4 ,
118+ 19 ,
119+ new JSONObject ()
120+ .put ("city" , "houston" )
121+ .put ("state" , "TX" )
122+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1933-12-12 05:05:45" ))),
123+ rows (
124+ "david" ,
125+ null ,
126+ 25 ,
127+ new JSONObject ()
128+ .put ("city" , "raleigh" )
129+ .put ("state" , "NC" )
130+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1909-06-17 01:04:21" ))),
131+ rows (
132+ "david" ,
133+ null ,
134+ 25 ,
135+ new JSONObject ()
136+ .put ("city" , "charlotte" )
137+ .put ("state" , "SC" )
138+ .put (
139+ "moveInDate" ,
140+ new JSONArray ()
141+ .put (new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" )))));
42142 }
43143
44- // TODO: confirm if expand on array (instead of nested) will be supported.
144+ // To consider in future releases: will expand on array (instead of nested) be supported.
45145 // In Opensearch, a string field can store either a single string or an array of strings.
46- // This makes it difficult to implement expand on array .
146+ // This makes it difficult to implement expand on arries .
47147 @ Ignore
48148 @ Test
49149 public void testExpandOnArray () throws Exception {
@@ -63,6 +163,117 @@ public void testExpandWithAlias() throws Exception {
63163 schema ("age" , "bigint" ),
64164 schema ("id" , "bigint" ),
65165 schema ("addr" , "struct" ));
166+ verifyDataRows (
167+ response ,
168+ rows (
169+ "abbas" ,
170+ null ,
171+ 24 ,
172+ new JSONObject ()
173+ .put ("city" , "New york city" )
174+ .put ("state" , "NY" )
175+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1984-04-12 09:07:42" ))),
176+ rows (
177+ "abbas" ,
178+ null ,
179+ 24 ,
180+ new JSONObject ()
181+ .put ("city" , "bellevue" )
182+ .put ("state" , "WA" )
183+ .put (
184+ "moveInDate" ,
185+ new JSONArray ()
186+ .put (new JSONObject ().put ("dateAndTime" , "2023-05-03 08:07:42" ))
187+ .put (new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" )))),
188+ rows (
189+ "abbas" ,
190+ null ,
191+ 24 ,
192+ new JSONObject ()
193+ .put ("city" , "seattle" )
194+ .put ("state" , "WA" )
195+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1966-03-19 03:04:55" ))),
196+ rows (
197+ "abbas" ,
198+ null ,
199+ 24 ,
200+ new JSONObject ()
201+ .put ("city" , "chicago" )
202+ .put ("state" , "IL" )
203+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2011-06-01 01:01:42" ))),
204+ rows (
205+ "chen" ,
206+ null ,
207+ 32 ,
208+ new JSONObject ()
209+ .put ("city" , "Miami" )
210+ .put ("state" , "Florida" )
211+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1901-08-11 04:03:33" ))),
212+ rows (
213+ "chen" ,
214+ null ,
215+ 32 ,
216+ new JSONObject ()
217+ .put ("city" , "los angeles" )
218+ .put ("state" , "CA" )
219+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2023-05-03 08:07:42" ))),
220+ rows (
221+ "peng" ,
222+ null ,
223+ 26 ,
224+ new JSONObject ()
225+ .put ("city" , "san diego" )
226+ .put ("state" , "CA" )
227+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" ))),
228+ rows (
229+ "peng" ,
230+ null ,
231+ 26 ,
232+ new JSONObject ()
233+ .put ("city" , "austin" )
234+ .put ("state" , "TX" )
235+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1977-07-13 09:04:41" ))),
236+ rows (
237+ "andy" ,
238+ 4 ,
239+ 19 ,
240+ new JSONObject ()
241+ .put ("city" , "houston" )
242+ .put ("state" , "TX" )
243+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1933-12-12 05:05:45" ))),
244+ rows (
245+ "david" ,
246+ null ,
247+ 25 ,
248+ new JSONObject ()
249+ .put ("city" , "raleigh" )
250+ .put ("state" , "NC" )
251+ .put ("moveInDate" , new JSONObject ().put ("dateAndTime" , "1909-06-17 01:04:21" ))),
252+ rows (
253+ "david" ,
254+ null ,
255+ 25 ,
256+ new JSONObject ()
257+ .put ("city" , "charlotte" )
258+ .put ("state" , "SC" )
259+ .put (
260+ "moveInDate" ,
261+ new JSONArray ()
262+ .put (new JSONObject ().put ("dateAndTime" , "2001-11-11 04:07:44" )))));
263+ }
264+
265+ @ Test
266+ public void testExpandWithEval () throws Exception {
267+ JSONObject response =
268+ executeQuery (
269+ String .format ("source=%s | eval addr=address | expand addr" , TEST_INDEX_NESTED_SIMPLE ));
270+ verifySchema (
271+ response ,
272+ schema ("name" , "string" ),
273+ schema ("age" , "bigint" ),
274+ schema ("address" , "array" ),
275+ schema ("id" , "bigint" ),
276+ schema ("addr" , "struct" ));
66277 verifyNumOfRows (response , 11 );
67278 }
68279}
0 commit comments