1212import static org .opensearch .sql .util .MatcherUtils .rows ;
1313import static org .opensearch .sql .util .MatcherUtils .schema ;
1414import static org .opensearch .sql .util .MatcherUtils .verifyDataRows ;
15+ import static org .opensearch .sql .util .MatcherUtils .verifyErrorMessageContains ;
1516import static org .opensearch .sql .util .MatcherUtils .verifySchema ;
1617
18+ import org .hamcrest .Matcher ;
1719import org .json .JSONArray ;
1820import org .json .JSONObject ;
1921import org .junit .jupiter .api .Test ;
@@ -36,6 +38,8 @@ public void testFlattenNestedStruct() throws Exception {
3638 verifySchema (
3739 result ,
3840 // Nested fields are retrieved as array of nested structs
41+ // This is because such fields can store either a struct like {"dayOfWeek":1}
42+ // or an array of structs like [{"dayOfWeek":1}, {"dayOfWeek":2}]
3943 schema ("comment" , "array" ),
4044 schema ("myNum" , "bigint" ),
4145 schema ("someField" , "string" ),
@@ -44,52 +48,92 @@ public void testFlattenNestedStruct() throws Exception {
4448 schema ("author" , "string" ),
4549 schema ("dayOfWeek" , "bigint" ),
4650 schema ("info" , "string" ));
47- verifyDataRows (
51+ verifyDataRows (result , getExpectedRows ());
52+ }
53+
54+ @ Test
55+ public void testFlattenWithAliases () throws Exception {
56+ JSONObject result =
57+ executeQuery (
58+ String .format (
59+ "source=%s | flatten message as (creator, dow, information)" ,
60+ TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS ));
61+ verifySchema (
4862 result ,
49- rows (
50- new JSONArray ().put (new JSONObject ().put ("data" , "ab" ).put ("likes" , 3 )),
51- 1 ,
52- "b" ,
53- new JSONArray ()
54- .put (new JSONObject ().put ("info" , "a" ).put ("author" , "e" ).put ("dayOfWeek" , 1 )),
55- "e" ,
56- 1 ,
57- "a" ),
58- rows (
59- new JSONArray ().put (new JSONObject ().put ("data" , "aa" ).put ("likes" , 2 )),
60- 2 ,
61- "a" ,
62- new JSONArray ()
63- .put (new JSONObject ().put ("info" , "b" ).put ("author" , "f" ).put ("dayOfWeek" , 2 )),
64- "f" ,
65- 2 ,
66- "b" ),
67- rows (
68- new JSONArray ().put (new JSONObject ().put ("data" , "aa" ).put ("likes" , 3 )),
69- 3 ,
70- "a" ,
71- new JSONArray ()
72- .put (new JSONObject ().put ("info" , "c" ).put ("author" , "g" ).put ("dayOfWeek" , 1 )),
73- "g" ,
74- 1 ,
75- "c" ),
76- rows (
77- new JSONArray ().put (new JSONObject ().put ("data" , "ab" ).put ("likes" , 1 )),
78- 4 ,
79- "b" ,
80- new JSONArray ()
81- .put (new JSONObject ().put ("info" , "c" ).put ("author" , "h" ).put ("dayOfWeek" , 4 )),
82- "h" ,
83- 4 ,
84- "c" ),
85- rows (
86- new JSONArray ().put (new JSONObject ().put ("data" , "bb" ).put ("likes" , 10 )),
87- 3 ,
88- "a" ,
89- new JSONArray ()
90- .put (new JSONObject ().put ("info" , "zz" ).put ("author" , "zz" ).put ("dayOfWeek" , 6 )),
91- "zz" ,
92- 6 ,
93- "zz" ));
63+ schema ("comment" , "array" ),
64+ schema ("myNum" , "bigint" ),
65+ schema ("someField" , "string" ),
66+ schema ("message" , "array" ),
67+ schema ("creator" , "string" ),
68+ schema ("dow" , "bigint" ),
69+ schema ("information" , "string" ));
70+ verifyDataRows (result , getExpectedRows ());
71+ }
72+
73+ @ Test
74+ public void testFlattenWithMismatchedNumberOfAliasesShouldThrow () throws Exception {
75+ Throwable t =
76+ expectThrows (
77+ Exception .class ,
78+ () ->
79+ executeQuery (
80+ String .format (
81+ "source=%s | flatten message as a, b, c, d" ,
82+ TEST_INDEX_NESTED_TYPE_WITHOUT_ARRAYS )));
83+ verifyErrorMessageContains (
84+ t ,
85+ "The number of aliases has to match the number of flattened fields. Expected 3"
86+ + " (message.author, message.dayOfWeek, message.info), got 4 (a, b, c, d)" );
87+ }
88+
89+ @ SuppressWarnings ("unchecked" )
90+ private static Matcher <JSONArray >[] getExpectedRows () {
91+ return new org .hamcrest .TypeSafeMatcher [] {
92+ rows (
93+ new JSONArray ().put (new JSONObject ().put ("data" , "ab" ).put ("likes" , 3 )),
94+ 1 ,
95+ "b" ,
96+ new JSONArray ()
97+ .put (new JSONObject ().put ("info" , "a" ).put ("author" , "e" ).put ("dayOfWeek" , 1 )),
98+ "e" ,
99+ 1 ,
100+ "a" ),
101+ rows (
102+ new JSONArray ().put (new JSONObject ().put ("data" , "aa" ).put ("likes" , 2 )),
103+ 2 ,
104+ "a" ,
105+ new JSONArray ()
106+ .put (new JSONObject ().put ("info" , "b" ).put ("author" , "f" ).put ("dayOfWeek" , 2 )),
107+ "f" ,
108+ 2 ,
109+ "b" ),
110+ rows (
111+ new JSONArray ().put (new JSONObject ().put ("data" , "aa" ).put ("likes" , 3 )),
112+ 3 ,
113+ "a" ,
114+ new JSONArray ()
115+ .put (new JSONObject ().put ("info" , "c" ).put ("author" , "g" ).put ("dayOfWeek" , 1 )),
116+ "g" ,
117+ 1 ,
118+ "c" ),
119+ rows (
120+ new JSONArray ().put (new JSONObject ().put ("data" , "ab" ).put ("likes" , 1 )),
121+ 4 ,
122+ "b" ,
123+ new JSONArray ()
124+ .put (new JSONObject ().put ("info" , "c" ).put ("author" , "h" ).put ("dayOfWeek" , 4 )),
125+ "h" ,
126+ 4 ,
127+ "c" ),
128+ rows (
129+ new JSONArray ().put (new JSONObject ().put ("data" , "bb" ).put ("likes" , 10 )),
130+ 3 ,
131+ "a" ,
132+ new JSONArray ()
133+ .put (new JSONObject ().put ("info" , "zz" ).put ("author" , "zz" ).put ("dayOfWeek" , 6 )),
134+ "zz" ,
135+ 6 ,
136+ "zz" )
137+ };
94138 }
95139}
0 commit comments