Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: non ISO 8601 versions of DAY_OF_WEEK and WEEK_OF_YEAR functions #36358

Merged
merged 12 commits into from Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
351 changes: 328 additions & 23 deletions docs/reference/sql/functions/date-time.asciidoc

Large diffs are not rendered by default.

Expand Up @@ -78,6 +78,8 @@ public void testShowFunctionsLikeInfix() throws IOException {
assertThat(readLine(), RegexMatcher.matches("\\s*DAY_OF_WEEK\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*DAY_OF_YEAR\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*HOUR_OF_DAY\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*ISODAYOFWEEK\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*ISO_DAY_OF_WEEK\\s*\\|\\s*SCALAR\\s*"));
assertThat(readLine(), RegexMatcher.matches("\\s*MINUTE_OF_DAY\\s*\\|\\s*SCALAR\\s*"));
assertEquals("", readLine());
}
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/command.csv-spec
Expand Up @@ -40,6 +40,15 @@ DOW |SCALAR
DOY |SCALAR
HOUR |SCALAR
HOUR_OF_DAY |SCALAR
IDOW |SCALAR
ISODAYOFWEEK |SCALAR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to add these shortcuts:
isodayofweek/isodow ->idow
isoweek -> iw
isoweekofyear -> iwoy

ISODOW |SCALAR
ISOWEEK |SCALAR
ISOWEEKOFYEAR |SCALAR
ISO_DAY_OF_WEEK |SCALAR
ISO_WEEK_OF_YEAR|SCALAR
IW |SCALAR
IWOY |SCALAR
MINUTE |SCALAR
MINUTE_OF_DAY |SCALAR
MINUTE_OF_HOUR |SCALAR
Expand Down Expand Up @@ -156,6 +165,8 @@ DAY_OF_MONTH |SCALAR
DAY_OF_WEEK |SCALAR
DAY_OF_YEAR |SCALAR
HOUR_OF_DAY |SCALAR
ISODAYOFWEEK |SCALAR
ISO_DAY_OF_WEEK|SCALAR
MINUTE_OF_DAY |SCALAR
;

Expand Down
218 changes: 107 additions & 111 deletions x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec
Expand Up @@ -54,10 +54,10 @@ d:i | l:s
;

//
// Date (in H2 these start at 0 instead of 1...)
// Date
//
dateTimeDayOfWeek
SELECT DAY_OF_WEEK(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY DAY_OF_WEEK(birth_date);
dateTimeIsoDayOfWeek
SELECT ISO_DAY_OF_WEEK(birth_date) d, last_name l FROM "test_emp" WHERE emp_no < 10010 ORDER BY ISO_DAY_OF_WEEK(birth_date);

d:i | l:s
1 | Preusig
Expand Down Expand Up @@ -86,10 +86,96 @@ d:i | l:s
110 | Peac
;

weekOfYear
SELECT WEEK(birth_date) week, birth_date FROM test_emp ORDER BY WEEK(birth_date) DESC, birth_date DESC LIMIT 15;

week:i | birth_date:ts
---------------+------------------------
52 |1962-12-29T00:00:00.000Z
52 |1959-12-25T00:00:00.000Z
52 |1952-12-24T00:00:00.000Z
51 |1960-12-17T00:00:00.000Z
50 |1956-12-13T00:00:00.000Z
49 |1959-12-03T00:00:00.000Z
49 |1957-12-03T00:00:00.000Z
48 |1963-11-26T00:00:00.000Z
48 |1962-11-26T00:00:00.000Z
47 |1962-11-19T00:00:00.000Z
46 |1956-11-14T00:00:00.000Z
46 |1952-11-13T00:00:00.000Z
45 |1962-11-07T00:00:00.000Z
45 |1953-11-07T00:00:00.000Z
44 |1961-11-02T00:00:00.000Z
;

weekOfYearWithFilter
SELECT WEEK(birth_date) week, birth_date FROM test_emp WHERE WEEK(birth_date) > 50 OR WEEK(birth_date) < 4 ORDER BY WEEK(birth_date) DESC, birth_date DESC;

week:i | birth_date:ts
---------------+------------------------
52 |1962-12-29T00:00:00.000Z
52 |1959-12-25T00:00:00.000Z
52 |1952-12-24T00:00:00.000Z
51 |1960-12-17T00:00:00.000Z
2 |1965-01-03T00:00:00.000Z
2 |1953-01-07T00:00:00.000Z
;

//
// Aggregate
//

dateTimeAggByIsoDayOfWeekWithFilter
SELECT IDOW(birth_date) day, DAY_NAME(birth_date) name, COUNT(*) c FROM test_emp WHERE IDOW(birth_date) < 6 GROUP BY day, name ORDER BY day desc;

day:i | name:s | c:l
---------------+---------------+---------------
5 |Friday |12
4 |Thursday |15
3 |Wednesday |14
2 |Tuesday |18
1 |Monday |8
;

dateTimeAggByIsoDayOfWeek
SELECT IDOW(birth_date) day, DAY_NAME(birth_date) name, COUNT(*) c FROM test_emp GROUP BY day, name ORDER BY day desc;

day:i | name:s | c:l
---------------+---------------+---------------
7 |Sunday |10
6 |Saturday |13
5 |Friday |12
4 |Thursday |15
3 |Wednesday |14
2 |Tuesday |18
1 |Monday |8
null |null |10
;

dateTimeAggByIsoWeekOfYear
SELECT IW(birth_date) iso_week, WEEK(birth_date) week FROM test_emp WHERE IW(birth_date) < 20 GROUP BY iso_week, week ORDER BY iso_week;

iso_week:i | week:i
---------------+---------------
1 |2
3 |4
4 |4
4 |5
6 |7
7 |7
8 |8
8 |9
9 |9
10 |11
12 |12
14 |14
14 |15
15 |16
16 |16
16 |17
18 |18
;

dateTimeAggByYear
SELECT YEAR(birth_date) AS d, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY YEAR(birth_date) ORDER BY YEAR(birth_date) LIMIT 13;

Expand Down Expand Up @@ -166,112 +252,22 @@ d:i | c:l | s:i
null |10 |100445
;

constantYear
// tag::year
SELECT YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS year;

year
---------------
2018
// end::year
;

constantMonthOfYear
// tag::monthOfYear
SELECT MONTH_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS month;

month
---------------
2
// end::monthOfYear
;

constantWeekOfYear
// tag::weekOfYear
SELECT WEEK_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS week;

week
---------------
8
// end::weekOfYear
;

constantDayOfYear
// tag::dayOfYear
SELECT DAY_OF_YEAR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

day
---------------
50
// end::dayOfYear
;

extractDayOfYear
// tag::extractDayOfYear
SELECT EXTRACT(DAY_OF_YEAR FROM CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

day
---------------
50
// end::extractDayOfYear
;

constantDayOfMonth
// tag::dayOfMonth
SELECT DAY_OF_MONTH(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

day
---------------
19
// end::dayOfMonth
;

constantDayOfWeek
// tag::dayOfWeek
SELECT DAY_OF_WEEK(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS day;

day
---------------
1
// end::dayOfWeek
;

constantHourOfDay
// tag::hourOfDay
SELECT HOUR_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS hour;

hour
---------------
10
// end::hourOfDay
;

constantMinuteOfDay
// tag::minuteOfDay
SELECT MINUTE_OF_DAY(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;

minute
---------------
623
// end::minuteOfDay
;

constantMinuteOfHour
// tag::minuteOfHour
SELECT MINUTE_OF_HOUR(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS minute;

minute
---------------
23
// end::minuteOfHour
;

constantSecondOfMinute
// tag::secondOfMinute
SELECT SECOND_OF_MINUTE(CAST('2018-02-19T10:23:27Z' AS TIMESTAMP)) AS second;

second
---------------
27
// end::secondOfMinute
weekOfYearGroupBy
SELECT WEEK(birth_date) week, COUNT(*) c FROM test_emp WHERE MOD(WEEK(birth_date), 4) = 0 GROUP BY week ORDER BY WEEK(birth_date);

week:i | c:l
---------------+---------------
4 |3
8 |2
12 |1
16 |3
20 |1
24 |2
28 |3
32 |1
36 |3
40 |4
44 |2
48 |2
52 |3
;
14 changes: 13 additions & 1 deletion x-pack/plugin/sql/qa/src/main/resources/datetime.sql-spec
Expand Up @@ -6,6 +6,9 @@
// Time NOT IMPLEMENTED in H2 on TIMESTAMP WITH TIME ZONE - hence why these are moved to CSV
//

// WEEK_OF_YEAR moved to CSV tests, because H2 builds its Calendar with the local Locale, we consider ROOT as the default Locale
// This has implications on the results, which could change given specific locales where the rules for determining the start of a year are different.

//
// Date
//
Expand All @@ -31,6 +34,9 @@ SELECT DAYNAME(CAST('2018-09-03' AS TIMESTAMP)) day FROM "test_emp" limit 1;
quarterSelect
SELECT QUARTER(hire_date) q, hire_date FROM test_emp ORDER BY hire_date LIMIT 15;

dayOfWeek
SELECT DAY_OF_WEEK(birth_date) day, birth_date FROM test_emp ORDER BY DAY_OF_WEEK(birth_date);

//
// Filter
//
Expand Down Expand Up @@ -59,6 +65,9 @@ SELECT first_name, last_name FROM "test_emp" WHERE DAYNAME(hire_date) = 'Sunday'
quarterWithFilter
SELECT QUARTER(hire_date) quarter, hire_date FROM test_emp WHERE QUARTER(hire_date) > 2 ORDER BY hire_date LIMIT 15;

dayOfWeekWithFilter
SELECT DAY_OF_WEEK(birth_date) day, birth_date FROM test_emp WHERE DAY_OF_WEEK(birth_date) IN (6,7) ORDER BY DAY_OF_WEEK(birth_date);

//
// Aggregate
//
Expand Down Expand Up @@ -101,4 +110,7 @@ dayNameWithGroupByOrderByAndHaving
SELECT CAST(MAX(salary) AS DOUBLE) max_salary, DAYNAME(hire_date) day_name FROM test_emp GROUP BY DAYNAME(hire_date) HAVING MAX(salary) > 50000 ORDER BY DAYNAME("hire_date");

quarterWithGroupByAndOrderBy
SELECT QUARTER(hire_date) quarter, COUNT(*) hires FROM test_emp GROUP BY QUARTER(hire_date) ORDER BY QUARTER(hire_date);
SELECT QUARTER(hire_date) quarter, COUNT(*) hires FROM test_emp GROUP BY QUARTER(hire_date) ORDER BY QUARTER(hire_date);

dayOfWeekGroupBy
SELECT DAY_OF_WEEK(birth_date) day, COUNT(*) c FROM test_emp WHERE DAY_OF_WEEK(birth_date) < 6 GROUP BY day ORDER BY DAY_OF_WEEK(birth_date);