Skip to content

Commit

Permalink
[Enhancement] support iso week function (StarRocks#20511) (StarRocks#…
Browse files Browse the repository at this point in the history
…22773)

Signed-off-by: qmengss <707549024@qq.com>
  • Loading branch information
qmengss authored and abc982627271 committed Jun 5, 2023
1 parent e39051c commit 827935c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
10 changes: 10 additions & 0 deletions be/src/exprs/time_functions.cpp
Expand Up @@ -610,6 +610,16 @@ DEFINE_UNARY_FN_WITH_IMPL(week_of_year_with_default_modeImpl, t) {
}
DEFINE_TIME_UNARY_FN(week_of_year_with_default_mode, TYPE_DATETIME, TYPE_INT);

DEFINE_UNARY_FN_WITH_IMPL(week_of_year_isoImpl, t) {
auto date_value = (DateValue)t;
int year = 0, month = 0, day = 0;
date_value.to_date(&year, &month, &day);

return TimeFunctions::compute_week(year, month, day, TimeFunctions::week_mode(3));
}

DEFINE_TIME_UNARY_FN(week_of_year_iso, TYPE_DATETIME, TYPE_INT);

DEFINE_BINARY_FUNCTION_WITH_IMPL(week_of_year_with_modeImpl, t, m) {
auto date_value = (DateValue)t;
int year = 0, month = 0, day = 0;
Expand Down
8 changes: 8 additions & 0 deletions be/src/exprs/time_functions.h
Expand Up @@ -146,6 +146,14 @@ class TimeFunctions {
*/
DEFINE_VECTORIZED_FN(week_of_year_with_default_mode);

/**
* Get week of the year with iso.
* @param context
* @param column[0] [TimestampColumn] Columns that hold timestamps.
* @return IntColumn week of the year:
*/
DEFINE_VECTORIZED_FN(week_of_year_iso);

/**
* Get week of the year.
* @param context
Expand Down
26 changes: 26 additions & 0 deletions be/test/exprs/time_functions_test.cpp
Expand Up @@ -260,6 +260,32 @@ TEST_F(TimeFunctionsTest, weekOfYearTest) {
}
}

TEST_F(TimeFunctionsTest, weekOfYearIsoTest) {
auto tc = TimestampColumn::create();
tc->append(TimestampValue::create(2023, 1, 5, 0, 5, 0));
tc->append(TimestampValue::create(2023, 1, 9, 0, 9, 0));
tc->append(TimestampValue::create(2023, 1, 2, 0, 2, 0));
tc->append(TimestampValue::create(2023, 1, 6, 0, 6, 0));
tc->append(TimestampValue::create(2023, 1, 3, 0, 3, 0));
tc->append(TimestampValue::create(2023, 1, 7, 0, 7, 0));
tc->append(TimestampValue::create(2023, 1, 10, 0, 10, 0));
tc->append(TimestampValue::create(2023, 1, 1, 0, 1, 0));
tc->append(TimestampValue::create(2023, 1, 4, 0, 4, 0));
tc->append(TimestampValue::create(2023, 1, 8, 0, 8, 0));

int weeks[] = {1, 2, 1, 1, 1, 1, 2, 52, 1, 1};

Columns columns;
columns.emplace_back(tc);

ColumnPtr result = TimeFunctions::week_of_year_iso(_utils->get_fn_ctx(), columns).value();

auto year_weeks = ColumnHelper::cast_to<TYPE_INT>(result);
for (size_t i = 0; i < sizeof(weeks) / sizeof(weeks[0]); ++i) {
ASSERT_EQ(weeks[i], year_weeks->get_data()[i]);
}
}

TEST_F(TimeFunctionsTest, weekWithDefaultModeTest) {
auto tc = TimestampColumn::create();
tc->append(TimestampValue::create(2007, 1, 1, 0, 0, 0));
Expand Down
1 change: 1 addition & 0 deletions docs/TOC.md
Expand Up @@ -470,6 +470,7 @@
+ [unix_timestamp](./sql-reference/sql-functions/date-time-functions/unix_timestamp.md)
+ [utc_timestamp](./sql-reference/sql-functions/date-time-functions/utc_timestamp.md)
+ [week](./sql-reference/sql-functions/date-time-functions/week.md)
+ [week_iso](./sql-reference/sql-functions/date-time-functions/week_iso.md)
+ [weekofyear](./sql-reference/sql-functions/date-time-functions/weekofyear.md)
+ [weeks_add](./sql-reference/sql-functions/date-time-functions/weeks_add.md)
+ [weeks_diff](./sql-reference/sql-functions/date-time-functions/weeks_diff.md)
Expand Down
28 changes: 28 additions & 0 deletions docs/sql-reference/sql-functions/date-time-functions/week_iso.md
@@ -0,0 +1,28 @@
# week_iso

## Description

Returns the ISO standard week number for a given date within a year.

The date parameter must be of the DATE or DATETIME type.

## Syntax

```Haskell
INT WEEK_ISO(DATETIME date)
```

## Examples

```Plain Text
MySQL > select week_iso('2008-02-20 00:00:00');
+-----------------------------------+
| week_iso('2008-02-20 00:00:00') |
+-----------------------------------+
| 8 |
+-----------------------------------+
```

## keyword

WEEK_ISO
2 changes: 2 additions & 0 deletions gensrc/script/functions.py
Expand Up @@ -227,6 +227,7 @@
[20073, 'bit_shift_right_logical', 'BIGINT', ['BIGINT', 'BIGINT'], "BitFunctions::bitShiftRightLogical<TYPE_BIGINT>"],
[20074, 'bit_shift_right_logical', 'LARGEINT', ['LARGEINT', 'BIGINT'], "BitFunctions::bitShiftRightLogical<TYPE_LARGEINT>"],


# 30xxx: string functions
[30010, 'substr', 'VARCHAR', ['VARCHAR', 'INT'], 'StringFunctions::substring', 'StringFunctions::sub_str_prepare', 'StringFunctions::sub_str_close'],
[30011, 'substr', 'VARCHAR', ['VARCHAR', 'INT', 'INT'], 'StringFunctions::substring', 'StringFunctions::sub_str_prepare', 'StringFunctions::sub_str_close'],
Expand Down Expand Up @@ -346,6 +347,7 @@
[50063, 'weekofyear', 'INT', ['DATETIME'], 'TimeFunctions::week_of_year'],
[50064, 'week', 'INT', ['DATETIME'], 'TimeFunctions::week_of_year_with_default_mode'],
[50067, 'week', 'INT', ['DATETIME', 'INT'], 'TimeFunctions::week_of_year_with_mode'],
[50068, 'week_iso', 'INT', ['DATETIME'], 'TimeFunctions::week_of_year_iso'],

[50069, 'hour', 'TINYINT', ['DATETIME'], 'TimeFunctions::hourV2'],
[50070, 'hour', 'INT', ['DATETIME'], 'TimeFunctions::hour'],
Expand Down
24 changes: 24 additions & 0 deletions test/sql/test_time_fn/R/test_time_fn
Expand Up @@ -682,3 +682,27 @@ select d1, dow_2, previous_day(d1, dow_2) from previous_day_test a join previous
2023-04-14 Sa 2023-04-08
2023-04-15 Su 2023-04-09
-- !result

-- name: test_week_iso
select week_iso("2023-01-01");
-- result:
52
-- !result
select week_iso("2023-01-02");
-- result:
1
-- !result
select week_iso("2023-01-03");
-- result:
1
-- !result
select week_iso("");
-- result:
NULL
-- !result
select week_iso(NULL);
-- result:
NULL
-- !result


8 changes: 8 additions & 0 deletions test/sql/test_time_fn/T/test_time_fn
Expand Up @@ -181,3 +181,11 @@ select d3, dow_2, previous_day(d3, dow_2) from previous_dow order by d3;
select d3, dow_3, previous_day(d3, dow_3) from previous_dow order by d3;
select d3, dow_full, previous_day(d3, dow_full) from previous_dow order by d3;
select d1, dow_2, previous_day(d1, dow_2) from previous_day_test a join previous_dow b on a.d1 = b.d3 order by d1;


-- name: test_week_iso
select week_iso("2023-01-01");
select week_iso("2023-01-02");
select week_iso("2023-01-03");
select week_iso("");
select week_iso(NULL);

0 comments on commit 827935c

Please sign in to comment.