Skip to content

Commit

Permalink
[Enhancement]implement dayofweek in FE (#51453)
Browse files Browse the repository at this point in the history
Signed-off-by: evenhuang <[email protected]>
  • Loading branch information
even986025158 authored Oct 11, 2024
1 parent 1992f75 commit fb41039
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ public static ConstantOperator to_days(ConstantOperator first) {
first.getDatetime().truncatedTo(ChronoUnit.DAYS)).toDays());
}

@ConstantFunction.List(list = {
@ConstantFunction(name = "dayofweek", argTypes = {DATETIME}, returnType = INT),
@ConstantFunction(name = "dayofweek", argTypes = {DATE}, returnType = INT),
@ConstantFunction(name = "dayofweek", argTypes = {INT}, returnType = INT)
})
public static ConstantOperator dayofweek(ConstantOperator date) {
// LocalDateTime.getDayOfWeek is return day of the week, such as monday is 1 and sunday is 7.
// function of dayofweek in starrocks monday is 2 and sunday is 1, so need mod 7 and plus 1.
return ConstantOperator.createInt((date.getDatetime().getDayOfWeek().getValue()) % 7 + 1);
}

@ConstantFunction.List(list = {
@ConstantFunction(name = "years_add", argTypes = {DATETIME,
INT}, returnType = DATETIME, isMonotonic = true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ public void to_days() {
assertEquals(734443, ScalarOperatorFunctions.to_days(O_DT_20101102_183010).getInt());
}

@Test
public void dayofweek() {
ConstantOperator testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 3, 13, 4, 5));
assertEquals(7,
ScalarOperatorFunctions.dayofweek(testDate).getInt());

testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 4, 13, 4, 5));
assertEquals(1,
ScalarOperatorFunctions.dayofweek(testDate).getInt());

testDate = ConstantOperator.createDatetime(LocalDateTime.of(2024, 2, 5, 13, 4, 5));
assertEquals(2,
ScalarOperatorFunctions.dayofweek(testDate).getInt());
}

@Test
public void yearsAdd() {
assertEquals("2025-03-23T09:23:55",
Expand Down

0 comments on commit fb41039

Please sign in to comment.