Skip to content

Commit ab813d9

Browse files
committed
Simplify CalciteTimechartCommandIT
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent 1579846 commit ab813d9

File tree

2 files changed

+23
-73
lines changed

2 files changed

+23
-73
lines changed

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalciteTimechartCommandIT.java

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.opensearch.sql.calcite.remote;
77

88
import static org.opensearch.sql.legacy.TestUtils.*;
9+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
910
import static org.opensearch.sql.util.MatcherUtils.*;
1011

1112
import java.io.IOException;
@@ -23,6 +24,7 @@ public void init() throws Exception {
2324
disallowCalciteFallback();
2425

2526
// Create events index with timestamp data
27+
loadIndex(Index.BANK);
2628
loadIndex(Index.EVENTS);
2729
loadIndex(Index.EVENTS_NULL);
2830
createEventsManyHostsIndex();
@@ -41,7 +43,6 @@ public void testTimechartWithHourSpanAndGroupBy() throws IOException {
4143
rows("2024-07-01 00:00:00", "db-01", 1),
4244
rows("2024-07-01 00:00:00", "web-01", 2),
4345
rows("2024-07-01 00:00:00", "web-02", 2));
44-
assertEquals(3, result.getInt("total"));
4546
}
4647

4748
@Test
@@ -53,9 +54,6 @@ public void testTimechartWithMinuteSpanAndGroupBy() throws IOException {
5354
schema("host", "string"),
5455
schema("count()", "bigint"));
5556

56-
// Actual result shows 5 rows, not zero-filled results
57-
assertEquals(5, result.getInt("total"));
58-
5957
verifyDataRows(
6058
result,
6159
rows("2024-07-01 00:00:00", "web-01", 1),
@@ -67,25 +65,13 @@ public void testTimechartWithMinuteSpanAndGroupBy() throws IOException {
6765

6866
@Test
6967
public void testTimechartWithoutTimestampField() throws IOException {
70-
// Create index without @timestamp field
71-
String noTimestampMapping =
72-
"{\"mappings\":{\"properties\":{\"name\":{\"type\":\"keyword\"},\"occupation\":{\"type\":\"keyword\"},\"country\":{\"type\":\"keyword\"},\"salary\":{\"type\":\"integer\"},\"year\":{\"type\":\"integer\"},\"month\":{\"type\":\"integer\"}}}}";
73-
if (!isIndexExist(client(), "no_timestamp")) {
74-
createIndexByRestClient(client(), "no_timestamp", noTimestampMapping);
75-
loadDataByRestClient(client(), "no_timestamp", "src/test/resources/occupation.json");
76-
}
77-
78-
// Test should throw exception for missing @timestamp field
7968
Throwable exception =
80-
assertThrowsWithReplace(
69+
assertThrows(
8170
ResponseException.class,
8271
() -> {
83-
executeQuery("source=no_timestamp | timechart count()");
72+
executeQuery(String.format("source=%s | timechart count()", TEST_INDEX_BANK));
8473
});
85-
assertTrue(
86-
"Error message should mention missing @timestamp field",
87-
exception.getMessage().contains("@timestamp")
88-
|| exception.getMessage().contains("timestamp"));
74+
verifyErrorMessageContains(exception, "Field [@timestamp] not found.");
8975
}
9076

9177
@Test
@@ -131,19 +117,8 @@ public void testTimechartWithOtherCategory() throws IOException {
131117
schema("avg(cpu_usage)", "double"));
132118

133119
// Verify we have 11 data rows (10 hosts + OTHER)
134-
assertEquals(11, result.getJSONArray("datarows").length());
135-
136-
// Verify the OTHER row exists with the correct value
137-
boolean foundOther = false;
138-
for (int i = 0; i < result.getJSONArray("datarows").length(); i++) {
139-
Object[] row = result.getJSONArray("datarows").getJSONArray(i).toList().toArray();
140-
if ("OTHER".equals(row[1])) {
141-
foundOther = true;
142-
assertEquals(35.9, ((Number) row[2]).doubleValue(), 0.01);
143-
break;
144-
}
145-
}
146-
assertTrue("OTHER category not found in results", foundOther);
120+
verifyNumOfRows(result, 11);
121+
verifyDataRowsSome(result, rows("2024-07-01 00:00:00", "OTHER", 35.9));
147122
}
148123

149124
@Test
@@ -176,9 +151,6 @@ public void testTimechartWithLimitCountGroupBy() throws IOException {
176151
schema("host", "string"),
177152
schema("count()", "bigint"));
178153

179-
// Actual result shows 5 rows, not zero-filled results
180-
assertEquals(5, result.getInt("total"));
181-
182154
verifyDataRows(
183155
result,
184156
rows("2024-07-01 00:00:00", "web-01", 1),
@@ -202,7 +174,7 @@ public void testTimechartWithLimitZeroAndAvg() throws IOException {
202174
schema("avg(cpu_usage)", "double"));
203175

204176
// Verify we have 11 data rows (all 11 hosts, no OTHER)
205-
assertEquals(11, result.getJSONArray("datarows").length());
177+
verifyNumOfRows(result, 11);
206178

207179
// Verify no OTHER category
208180
boolean foundOther = false;
@@ -228,8 +200,7 @@ public void testTimechartWithLimitZeroAndCount() throws IOException {
228200
schema("host", "string"),
229201
schema("count()", "bigint"));
230202

231-
// For count with limit=0, should show zero-filled results: 11 hosts × 1 time span = 11 rows
232-
assertEquals(11, result.getInt("total"));
203+
verifyNumOfRows(result, 11);
233204
}
234205

235206
@Test
@@ -245,7 +216,7 @@ public void testTimechartWithUseOtherFalseAndAvg() throws IOException {
245216
schema("avg(cpu_usage)", "double"));
246217

247218
// Verify we have 10 data rows (top 10 hosts, no OTHER)
248-
assertEquals(10, result.getJSONArray("datarows").length());
219+
verifyNumOfRows(result, 10);
249220

250221
// Verify no OTHER category
251222
boolean foundOther = false;
@@ -270,9 +241,7 @@ public void testTimechartWithUseOtherFalseAndCount() throws IOException {
270241
schema("host", "string"),
271242
schema("count()", "bigint"));
272243

273-
// For count with useother=false, should show zero-filled results: 10 hosts × 1 time span = 10
274-
// rows
275-
assertEquals(10, result.getInt("total"));
244+
verifyNumOfRows(result, 10);
276245
}
277246

278247
@Test
@@ -285,9 +254,6 @@ public void testTimechartWithCountNoLimitByHostShowZero() throws IOException {
285254
schema("host", "string"),
286255
schema("count()", "bigint"));
287256

288-
// The actual result shows 5 rows, not 15 as zero-filling doesn't happen as expected
289-
assertEquals(5, result.getInt("total"));
290-
291257
verifyDataRows(
292258
result,
293259
rows("2024-07-01 00:00:00", "web-01", 1),
@@ -310,34 +276,12 @@ public void testTimechartWithLimitAndUseOther() throws IOException {
310276
schema("host", "string"),
311277
schema("avg(cpu_usage)", "double"));
312278

313-
// Verify we have 4 data rows (3 hosts + OTHER)
314-
assertEquals(4, result.getJSONArray("datarows").length());
315-
316-
// Verify specific values with tolerance for floating point precision
317-
boolean foundOther = false, foundWeb03 = false, foundWeb07 = false, foundWeb09 = false;
318-
for (int i = 0; i < result.getJSONArray("datarows").length(); i++) {
319-
Object[] row = result.getJSONArray("datarows").getJSONArray(i).toList().toArray();
320-
String host = (String) row[1];
321-
double cpuUsage = ((Number) row[2]).doubleValue();
322-
323-
if ("OTHER".equals(host)) {
324-
foundOther = true;
325-
assertEquals(41.3, cpuUsage, 0.1);
326-
} else if ("web-03".equals(host)) {
327-
foundWeb03 = true;
328-
assertEquals(55.3, cpuUsage, 0.1);
329-
} else if ("web-07".equals(host)) {
330-
foundWeb07 = true;
331-
assertEquals(48.6, cpuUsage, 0.1);
332-
} else if ("web-09".equals(host)) {
333-
foundWeb09 = true;
334-
assertEquals(67.8, cpuUsage, 0.1);
335-
}
336-
}
337-
assertTrue("OTHER not found", foundOther);
338-
assertTrue("web-03 not found", foundWeb03);
339-
assertTrue("web-07 not found", foundWeb07);
340-
assertTrue("web-09 not found", foundWeb09);
279+
verifyDataRows(
280+
result,
281+
closeTo("2024-07-01 00:00:00", "OTHER", 41.300000000000004),
282+
closeTo("2024-07-01 00:00:00", "web-03", 55.3),
283+
closeTo("2024-07-01 00:00:00", "web-07", 48.6),
284+
closeTo("2024-07-01 00:00:00", "web-09", 67.8));
341285
}
342286

343287
@Test

integ-test/src/test/java/org/opensearch/sql/util/MatcherUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public static void verifyDataRowsInOrder(JSONObject response, Matcher<JSONArray>
184184
verifyInOrder(response.getJSONArray("datarows"), matchers);
185185
}
186186

187+
@SafeVarargs
188+
@SuppressWarnings("unchecked")
189+
public static void verifyDataRowsSome(JSONObject response, Matcher<JSONArray>... matchers) {
190+
verifySome(response.getJSONArray("datarows"), matchers);
191+
}
192+
187193
public static void verifyNumOfRows(JSONObject response, int numOfRow) {
188194
assertEquals(numOfRow, response.getJSONArray("datarows").length());
189195
}

0 commit comments

Comments
 (0)