55
66package org .opensearch .sql .calcite .remote ;
77
8+ import static org .opensearch .sql .legacy .TestsConstants .TEST_INDEX_ALIAS ;
89import static org .opensearch .sql .util .MatcherUtils .rows ;
910import static org .opensearch .sql .util .MatcherUtils .schema ;
1011import static org .opensearch .sql .util .MatcherUtils .verifyDataRows ;
2526 */
2627public class CalciteAliasFieldAggregationIT extends PPLIntegTestCase {
2728
28- private static final String TEST_INDEX_ALIAS = "test_alias_bug" ;
29+ private static final String TEST_ALIAS_BUG = "test_alias_bug" ;
2930
3031 @ Override
3132 public void init () throws Exception {
3233 super .init ();
3334 enableCalcite ();
3435 createTestIndexWithAliasFields ();
36+ loadIndex (Index .DATA_TYPE_ALIAS );
3537 }
3638
3739 /**
@@ -41,14 +43,14 @@ public void init() throws Exception {
4143 private void createTestIndexWithAliasFields () throws IOException {
4244 // Delete the index if it exists (for test isolation)
4345 try {
44- Request deleteIndex = new Request ("DELETE" , "/" + TEST_INDEX_ALIAS );
46+ Request deleteIndex = new Request ("DELETE" , "/" + TEST_ALIAS_BUG );
4547 client ().performRequest (deleteIndex );
4648 } catch (ResponseException e ) {
4749 // Index doesn't exist, which is fine
4850 }
4951
5052 // Create index with alias fields
51- Request createIndex = new Request ("PUT" , "/" + TEST_INDEX_ALIAS );
53+ Request createIndex = new Request ("PUT" , "/" + TEST_ALIAS_BUG );
5254 createIndex .setJsonEntity (
5355 "{\n "
5456 + " \" mappings\" : {\n "
@@ -63,7 +65,7 @@ private void createTestIndexWithAliasFields() throws IOException {
6365 client ().performRequest (createIndex );
6466
6567 // Insert test documents
66- Request bulkRequest = new Request ("POST" , "/" + TEST_INDEX_ALIAS + "/_bulk?refresh=true" );
68+ Request bulkRequest = new Request ("POST" , "/" + TEST_ALIAS_BUG + "/_bulk?refresh=true" );
6769 bulkRequest .setJsonEntity (
6870 "{\" index\" :{}}\n "
6971 + "{\" created_at\" : \" 2024-01-01T10:00:00Z\" , \" value\" : 100}\n "
@@ -77,15 +79,15 @@ private void createTestIndexWithAliasFields() throws IOException {
7779 @ Test
7880 public void testMinWithDateAliasField () throws IOException {
7981 JSONObject actual =
80- executeQuery (String .format ("source=%s | stats MIN(@timestamp)" , TEST_INDEX_ALIAS ));
82+ executeQuery (String .format ("source=%s | stats MIN(@timestamp)" , TEST_ALIAS_BUG ));
8183 verifySchema (actual , schema ("MIN(@timestamp)" , "timestamp" ));
8284 verifyDataRows (actual , rows ("2024-01-01 10:00:00" ));
8385 }
8486
8587 @ Test
8688 public void testMaxWithDateAliasField () throws IOException {
8789 JSONObject actual =
88- executeQuery (String .format ("source=%s | stats MAX(@timestamp)" , TEST_INDEX_ALIAS ));
90+ executeQuery (String .format ("source=%s | stats MAX(@timestamp)" , TEST_ALIAS_BUG ));
8991 verifySchema (actual , schema ("MAX(@timestamp)" , "timestamp" ));
9092 verifyDataRows (actual , rows ("2024-01-03 10:00:00" ));
9193 }
@@ -94,8 +96,7 @@ public void testMaxWithDateAliasField() throws IOException {
9496 public void testMinMaxWithNumericAliasField () throws IOException {
9597 JSONObject actual =
9698 executeQuery (
97- String .format (
98- "source=%s | stats MIN(value_alias), MAX(value_alias)" , TEST_INDEX_ALIAS ));
99+ String .format ("source=%s | stats MIN(value_alias), MAX(value_alias)" , TEST_ALIAS_BUG ));
99100 verifySchemaInOrder (
100101 actual , schema ("MIN(value_alias)" , "int" ), schema ("MAX(value_alias)" , "int" ));
101102 verifyDataRows (actual , rows (100 , 300 ));
@@ -105,8 +106,7 @@ public void testMinMaxWithNumericAliasField() throws IOException {
105106 public void testFirstWithAliasField () throws IOException {
106107 JSONObject actual =
107108 executeQuery (
108- String .format (
109- "source=%s | sort @timestamp | stats FIRST(@timestamp)" , TEST_INDEX_ALIAS ));
109+ String .format ("source=%s | sort @timestamp | stats FIRST(@timestamp)" , TEST_ALIAS_BUG ));
110110 verifySchema (actual , schema ("FIRST(@timestamp)" , "timestamp" ));
111111 verifyDataRows (actual , rows ("2024-01-01 10:00:00" ));
112112 }
@@ -115,8 +115,7 @@ public void testFirstWithAliasField() throws IOException {
115115 public void testLastWithAliasField () throws IOException {
116116 JSONObject actual =
117117 executeQuery (
118- String .format (
119- "source=%s | sort @timestamp | stats LAST(@timestamp)" , TEST_INDEX_ALIAS ));
118+ String .format ("source=%s | sort @timestamp | stats LAST(@timestamp)" , TEST_ALIAS_BUG ));
120119 verifySchema (actual , schema ("LAST(@timestamp)" , "timestamp" ));
121120 verifyDataRows (actual , rows ("2024-01-03 10:00:00" ));
122121 }
@@ -126,7 +125,7 @@ public void testTakeWithAliasField() throws IOException {
126125 JSONObject actual =
127126 executeQuery (
128127 String .format (
129- "source=%s | sort @timestamp | stats TAKE(@timestamp, 2)" , TEST_INDEX_ALIAS ));
128+ "source=%s | sort @timestamp | stats TAKE(@timestamp, 2)" , TEST_ALIAS_BUG ));
130129 verifySchema (actual , schema ("TAKE(@timestamp, 2)" , "array" ));
131130 verifyDataRows (actual , rows (List .of ("2024-01-01T10:00:00.000Z" , "2024-01-02T10:00:00.000Z" )));
132131 }
@@ -135,7 +134,7 @@ public void testTakeWithAliasField() throws IOException {
135134 public void testAggregationsWithOriginalFieldsStillWork () throws IOException {
136135 JSONObject actual =
137136 executeQuery (
138- String .format ("source=%s | stats MIN(created_at), MAX(value)" , TEST_INDEX_ALIAS ));
137+ String .format ("source=%s | stats MIN(created_at), MAX(value)" , TEST_ALIAS_BUG ));
139138 verifySchemaInOrder (
140139 actual , schema ("MIN(created_at)" , "timestamp" ), schema ("MAX(value)" , "int" ));
141140 verifyDataRows (actual , rows ("2024-01-01 10:00:00" , 300 ));
@@ -147,12 +146,50 @@ public void testUnaffectedAggregationsWithAliasFields() throws IOException {
147146 executeQuery (
148147 String .format (
149148 "source=%s | stats SUM(value_alias), AVG(value_alias), COUNT(value_alias)" ,
150- TEST_INDEX_ALIAS ));
149+ TEST_ALIAS_BUG ));
151150 verifySchemaInOrder (
152151 actual ,
153152 schema ("SUM(value_alias)" , "bigint" ),
154153 schema ("AVG(value_alias)" , "double" ),
155154 schema ("COUNT(value_alias)" , "bigint" ));
156155 verifyDataRows (actual , rows (600 , 200.0 , 3 ));
157156 }
157+
158+ @ Test
159+ public void testAliasTypeWithLastFirstTakeLatestEarliestAggregation () throws IOException {
160+ JSONObject actual =
161+ executeQuery (
162+ String .format (
163+ "source=%s | stats take(original_text, 2), last(original_text),"
164+ + " first(original_text), take(alias_text, 2), last(alias_text),"
165+ + " first(alias_text), take(original_col, 2), last(original_col),"
166+ + " first(original_col), take(alias_col, 2), last(alias_col), first(alias_col),"
167+ + " latest(original_col), earliest(original_col), latest(alias_col),"
168+ + " earliest(alias_col),latest(original_text), earliest(original_text),"
169+ + " latest(alias_text), earliest(alias_text)" ,
170+ TEST_INDEX_ALIAS ));
171+ verifyDataRows (
172+ actual ,
173+ rows (
174+ List .of ("a b c" , "d e f" ),
175+ "x y z" ,
176+ "a b c" ,
177+ List .of ("a b c" , "d e f" ),
178+ "x y z" ,
179+ "a b c" ,
180+ List .of (1 , 2 ),
181+ 3 ,
182+ 1 ,
183+ List .of (1 , 2 ),
184+ 3 ,
185+ 1 ,
186+ 3 ,
187+ 1 ,
188+ 3 ,
189+ 1 ,
190+ "x y z" ,
191+ "a b c" ,
192+ "x y z" ,
193+ "a b c" ));
194+ }
158195}
0 commit comments