Skip to content

Commit

Permalink
SQL: update datetime test for self-review
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <[email protected]>
  • Loading branch information
acarbonetto committed Jan 14, 2025
1 parent cf695df commit 1565c9b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,13 @@ public void testWeekFormats(
@Test
public void testWeekOfYearWithTimeType() {
LocalDate today = LocalDate.now(functionProperties.getQueryStartClock());
int week = getWeekOfYearBeforeSunday(today);

// week is based on the first sunday of the year
LocalDate firstSundayOfYear = today.withDayOfYear(1).with(nextOrSame(SUNDAY));
int week =
today.isBefore(firstSundayOfYear)
? 0
: (int) ChronoUnit.WEEKS.between(firstSundayOfYear, today) + 1;

assertAll(
() ->
Expand All @@ -1254,14 +1260,7 @@ public void testWeekOfYearWithTimeType() {
week));
}

private int getWeekOfYearBeforeSunday(LocalDate date) {
LocalDate firstSundayOfYear = date.withDayOfYear(1).with(nextOrSame(SUNDAY));
if (date.isBefore(firstSundayOfYear)) {
return 0;
}

return (int) ChronoUnit.WEEKS.between(firstSundayOfYear, date) + 1;
}
private int getWeekOfYearBeforeSunday(LocalDate date) {}

@Test
public void modeInUnsupportedFormat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ private void datePartWithTimeArgQuery(String part, String time, long expected) {

@Test
public void testExtractDatePartWithTimeType() {
// run this for 4 years worth to get at least one leap year:
LocalDate now = LocalDate.now(functionProperties.getQueryStartClock());

datePartWithTimeArgQuery("DAY", timeInput, now.getDayOfMonth());

// To avoid flaky test, skip the testing in December and January because the WEEK is ISO 8601
// week-of-week-based-year which is considered to start on a Monday and week 1 is the first week
// with >3 days. it is possible for early-January dates to be part of the 52nd or 53rd week of
Expand All @@ -105,7 +105,9 @@ public void testExtractDatePartWithTimeType() {
if (now.getMonthValue() != 1 && now.getMonthValue() != 12) {
datePartWithTimeArgQuery("WEEK", datetimeInput, now.get(ALIGNED_WEEK_OF_YEAR));
}

datePartWithTimeArgQuery("MONTH", timeInput, now.getMonthValue());

datePartWithTimeArgQuery("YEAR", timeInput, now.getYear());
}

Expand Down
26 changes: 13 additions & 13 deletions docs/user/ppl/functions/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2179,17 +2179,17 @@ Example::

os> source=people | eval `YEARWEEK('2020-08-26')` = YEARWEEK('2020-08-26') | eval `YEARWEEK('2019-01-05', 1)` = YEARWEEK('2019-01-05', 1) | fields `YEARWEEK('2020-08-26')`, `YEARWEEK('2019-01-05', 1)`
fetched rows / total rows = 1/1
+------------------------+---------------------------+---------------------------+
| YEARWEEK('2020-08-26') | YEARWEEK('2019-01-05', 1) | YEARWEEK('2025-01-04', 1) |
|------------------------+---------------------------+---------------------------|
| 202034 | 201901 | 202452 |
+------------------------+---------------------------+---------------------------+

os> source=people | eval `YEARWEEK('2025-01-04')` = YEARWEEK('2025-01-04') | eval `YEARWEEK('2019-01-05', 1)` = YEARWEEK('2019-01-05', 1) | fields `YEARWEEK('2020-08-26')`, `YEARWEEK('2019-01-05', 1)`
fetched rows / total rows = 1/1
+------------------------+---------------------------+---------------------------+
| YEARWEEK('2020-08-26') | YEARWEEK('2019-01-05', 1) | YEARWEEK('2025-01-04', 1) |
|------------------------+---------------------------+---------------------------|
| 202034 | 201901 | 202452 |
+------------------------+---------------------------+---------------------------+
+------------------------+---------------------------+
| YEARWEEK('2020-08-26') | YEARWEEK('2019-01-05', 1) |
|------------------------+---------------------------|
| 202034 | 201901 |
+------------------------+---------------------------+

os> source=people | eval `YEARWEEK('2025-01-04')` = YEARWEEK('2025-01-04') | fields `YEARWEEK('2025-01-04', 1)`
fetched rows / total rows = 1/1
+---------------------------+
| YEARWEEK('2025-01-04', 1) |
|---------------------------|
| 202452 |
+---------------------------+

0 comments on commit 1565c9b

Please sign in to comment.