Skip to content

Commit 84e464e

Browse files
Fix flaky tests of week/yearweek in CalcitePPLDateTimeBuiltinFunctionIT (opensearch-project#3815) (opensearch-project#3824)
(cherry picked from commit 30f2508) Signed-off-by: Yuanchun Shen <yuanchu@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 3e08047 commit 84e464e

1 file changed

Lines changed: 40 additions & 13 deletions

File tree

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
@@ -1005,6 +1026,8 @@ public void testDateFormatAndDatetimeAndFromDays() throws IOException {
10051026
offsetUTC.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
10061027
String expectedDatetimeAtPlus8 =
10071028
offsetPlus8.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
1029+
int week19840412 = getYearWeek(LocalDate.of(1984, 4, 12), 0) % 100;
1030+
int week19840412Mode1 = getYearWeek(LocalDate.of(1984, 4, 12), 1) % 100;
10081031

10091032
verifyDataRows(
10101033
actual,
@@ -1017,8 +1040,8 @@ public void testDateFormatAndDatetimeAndFromDays() throws IOException {
10171040
"2017-11-02",
10181041
expectedDatetimeAtPlus8,
10191042
expectedDatetimeAtUTC,
1020-
"15 1984 15",
1021-
"15 15 1984",
1043+
String.format("%d 1984 %d", week19840412, week19840412),
1044+
String.format("%d %d 1984", week19840412Mode1, week19840412Mode1),
10221045
"09:07:42.000123"));
10231046
}
10241047

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

0 commit comments

Comments
 (0)