Skip to content

Commit 30f2508

Browse files
authored
Fix flaky tests of week/yearweek in CalcitePPLDateTimeBuiltinFunctionIT (#3815)
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent 35518dc commit 30f2508

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

integ-test/src/test/java/org/opensearch/sql/calcite/standalone/CalcitePPLDateTimeBuiltinFunctionIT.java

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -418,20 +418,33 @@ public void testWeekAndWeekOfYear() throws IOException {
418418
schema("WEEK(DATE('2008-02-20'))", "int"),
419419
schema("WEEK(DATE('2008-02-20'), 1)", "int"));
420420

421-
verifyDataRows(actual, rows(15, 15, 15, 15, 7, 8));
421+
int week19840412 = getYearWeek(LocalDate.of(1984, 4, 12), 0) % 100;
422+
int week19840412Mode1 = getYearWeek(LocalDate.of(1984, 4, 12), 1) % 100;
423+
int week20080220 = getYearWeek(LocalDate.of(2008, 2, 20), 0) % 100;
424+
int week20080220Mode1 = getYearWeek(LocalDate.of(2008, 2, 20), 1) % 100;
425+
verifyDataRows(
426+
actual,
427+
rows(
428+
week19840412,
429+
week19840412,
430+
week19840412Mode1,
431+
week19840412Mode1,
432+
week20080220,
433+
week20080220Mode1));
422434
}
423435

424436
@Test
425-
public void testWeekAndWeekOfYearWithFilter() throws IOException {
437+
public void testWeekAndWeekOfYearWithFilter() {
438+
int week19840412 = getYearWeek(LocalDate.of(1984, 4, 12), 0) % 100;
426439
JSONObject actual =
427440
executeQuery(
428441
String.format(
429442
"source=%s | fields strict_date_optional_time"
430443
+ "| where YEAR(strict_date_optional_time) < 2000"
431-
+ "| where WEEK(DATE(strict_date_optional_time)) = 15"
444+
+ "| where WEEK(DATE(strict_date_optional_time)) = %d"
432445
+ "| stats COUNT() AS CNT "
433446
+ "| head 1 ",
434-
TEST_INDEX_DATE_FORMATS));
447+
TEST_INDEX_DATE_FORMATS, week19840412));
435448

436449
verifySchema(actual, schema("CNT", "bigint"));
437450

@@ -464,13 +477,14 @@ public void testWeekDay() throws IOException {
464477
}
465478

466479
@Test
467-
public void testYearWeek() throws IOException {
480+
public void testYearWeek() {
468481
int currentYearWeek =
469-
exprYearweek(
470-
new ExprDateValue(
471-
LocalDateTime.now(new FunctionProperties().getQueryStartClock()).toLocalDate()),
472-
new ExprIntegerValue(0))
473-
.integerValue();
482+
getYearWeek(
483+
LocalDateTime.now(new FunctionProperties().getQueryStartClock()).toLocalDate(), 0);
484+
int yearWeek19840412 = getYearWeek(LocalDate.of(1984, 4, 12), 0);
485+
int yearWeek20200826 = getYearWeek(LocalDate.of(2020, 8, 26), 0);
486+
int yearWeek20190105 = getYearWeek(LocalDate.of(2019, 1, 5), 1);
487+
// Write to a tmp file
474488
JSONObject actual =
475489
executeQuery(
476490
String.format(
@@ -491,7 +505,14 @@ public void testYearWeek() throws IOException {
491505
schema("YEARWEEK('2020-08-26')", "int"),
492506
schema("YEARWEEK('2019-01-05', 1)", "int"));
493507

494-
verifyDataRows(actual, rows(198415, currentYearWeek, 198415, 202034, 201901));
508+
verifyDataRows(
509+
actual,
510+
rows(
511+
yearWeek19840412,
512+
currentYearWeek,
513+
yearWeek19840412,
514+
yearWeek20200826,
515+
yearWeek20190105));
495516
}
496517

497518
@Test
@@ -1004,6 +1025,8 @@ public void testDateFormatAndDatetimeAndFromDays() throws IOException {
10041025
offsetUTC.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
10051026
String expectedDatetimeAtPlus8 =
10061027
offsetPlus8.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
1028+
int week19840412 = getYearWeek(LocalDate.of(1984, 4, 12), 0) % 100;
1029+
int week19840412Mode1 = getYearWeek(LocalDate.of(1984, 4, 12), 1) % 100;
10071030

10081031
verifyDataRows(
10091032
actual,
@@ -1016,8 +1039,8 @@ public void testDateFormatAndDatetimeAndFromDays() throws IOException {
10161039
"2017-11-02",
10171040
expectedDatetimeAtPlus8,
10181041
expectedDatetimeAtUTC,
1019-
"15 1984 15",
1020-
"15 15 1984",
1042+
String.format("%d 1984 %d", week19840412, week19840412),
1043+
String.format("%d %d 1984", week19840412Mode1, week19840412Mode1),
10211044
"09:07:42.000123"));
10221045
}
10231046

@@ -1452,4 +1475,8 @@ public void testMicrosecond() throws IOException {
14521475
schema("m5", "int"));
14531476
verifyDataRows(actual, rows(0, 0, 0, 123456, 123456));
14541477
}
1478+
1479+
private static int getYearWeek(LocalDate date, int mode) {
1480+
return exprYearweek(new ExprDateValue(date), new ExprIntegerValue(mode)).integerValue();
1481+
}
14551482
}

0 commit comments

Comments
 (0)