Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3a3e978
Bring EXTRACT into alignment with PostgreSQL v14
jkosh44 Jan 10, 2022
d155340
Clean up generics with DecimalLike
jkosh44 Jan 14, 2022
fbce708
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 14, 2022
c4cced3
Remove unnecessary nanoseconds from DateLike epoch
jkosh44 Jan 15, 2022
1d9cfb2
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 19, 2022
2158f75
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 21, 2022
7d5c5c1
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 22, 2022
a43a0dd
Fix user docs
jkosh44 Jan 24, 2022
92b9213
Remove mention of Ingres and date_part from extract docs
jkosh44 Jan 25, 2022
d6b93f0
Hoist generics up a level
jkosh44 Jan 25, 2022
e5f4345
Pushed unwrapping Datums down where possible
jkosh44 Jan 25, 2022
70317bd
Remove Ingres from and SQL standard from date-part docs
jkosh44 Jan 25, 2022
8733cf1
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 25, 2022
3fc700d
Remove alias from docs
jkosh44 Jan 25, 2022
b4c9906
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 25, 2022
bd496cd
Fix release notes
jkosh44 Jan 25, 2022
de66b42
Merge branch 'main' of github.com:MaterializeInc/materialize into jko…
jkosh44 Jan 27, 2022
1ea3baa
Fix lint issues
jkosh44 Jan 31, 2022
bcbfec7
Add date-part diagram
jkosh44 Jan 31, 2022
7faaf6f
Fix date-part test
jkosh44 Jan 31, 2022
b9ad09a
Fix test error msg
jkosh44 Jan 31, 2022
45b95cc
Fix release notes link
jkosh44 Jan 31, 2022
010d994
Fix testdrive
jkosh44 Jan 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/user/content/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ changes that have not yet been documented.

- Add the `array_cat` function.

- **Breaking change.** Return an error when [`extract`](/sql/functions/extract/)
is called with a [`date`] value but a time-related field (e.g., `SECOND`).

Previous versions of Materialize would incorrectly return `0` in these cases.
The new behavior matches PostgreSQL.

[`date_part`](/sql/functions/date-part/) still returns a `0` in these cases,
which matches the PostgreSQL behavior.

- **Breaking change.** Change the return type of [`extract`](/sql/functions/extract/)
from [`float`](/sql/types/float/) to [`numeric`](/sql/types/numeric/).

This new behavior matches PostgreSQL v14.

{{< comment >}}
Only add new release notes above this line.

Expand Down
72 changes: 72 additions & 0 deletions doc/user/content/sql/functions/date-part.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: "date_part Function"
description: "Returns a specified time component from a time-based value"
menu:
main:
parent: 'sql-functions'
---

`date_part` returns some time component from a time-based value, such as the year from a Timestamp.
It is mostly functionally equivalent to the function [`EXTRACT`](../extract), except to maintain
PostgreSQL compatibility, `date_part` returns values of type [`float`](../../types/float). This can
result in a loss of precision in certain uses. Using [`EXTRACT`](../extract) is recommended instead.

## Signatures

{{< diagram "func-date-part.svg" >}}

Parameter | Type | Description
----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|------------
_val_ | [`time`](../../types/time), [`timestamp`](../../types/timestamp), [`timestamp with time zone`](../../types/timestamptz), [`interval`](../../types/interval), [`date`](../../types/date) | The value from which you want to extract a component. vals of type [`date`](../../types/date) are first cast to type [`timestamp`](../../types/timestamp).

### Arguments

`date_part` supports multiple synonyms for most time periods.

Time period | Synonyms
------------|---------
epoch | `EPOCH`
millennium | `MIL`, `MILLENNIUM`, `MILLENNIA`
century | `C`, `CENT`, `CENTURY`, `CENTURIES`
decade | `DEC`, `DECS`, `DECADE`, `DECADES`
year | `Y`, `YEAR`, `YEARS`, `YR`, `YRS`
quarter | `QTR`, `QUARTER`
month | `MON`, `MONS`, `MONTH`, `MONTHS`
week | `W`, `WEEK`, `WEEKS`
day | `D`, `DAY`, `DAYS`
hour |`H`, `HR`, `HRS`, `HOUR`, `HOURS`
minute | `M`, `MIN`, `MINS`, `MINUTE`, `MINUTES`
second | `S`, `SEC`, `SECS`, `SECOND`, `SECONDS`
microsecond | `US`, `USEC`, `USECS`, `USECONDS`, `MICROSECOND`, `MICROSECONDS`
millisecond | `MS`, `MSEC`, `MSECS`, `MSECONDS`, `MILLISECOND`, `MILLISECONDS`
day of week |`DOW`
ISO day of week | `ISODOW`
day of year | `DOY`

### Return value

`date_part` returns a [`float`](../../types/float) value.

## Examples

### Extract second from timestamptz

```sql
SELECT date_part('S', TIMESTAMP '2006-01-02 15:04:05.06');
```
```nofmt
date_part
-----------
5.06
```

### Extract century from date

```sql
SELECT date_part('CENTURIES', DATE '2006-01-02');
```
```nofmt
date_part
-----------
21
```
24 changes: 11 additions & 13 deletions doc/user/content/sql/functions/extract.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ menu:

{{< diagram "func-extract.svg" >}}

Parameter | Type | Description
----------|-----------------------------------------------------------------------------------------------------------------------------------------------------|------------
_val_ | [`date`](../../types/date), [`time`](../../types/time), [`timestamp`](../../types/timestamp), [`timestamp with time zone`](../../types/timestamptz) | The value from which you want to extract a component.
Parameter | Type | Description
----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------
_val_ | [`date`](../../types/date), [`time`](../../types/time), [`timestamp`](../../types/timestamp), [`timestamp with time zone`](../../types/timestamptz), [`interval`](../../types/interval) | The value from which you want to extract a component.

### Arguments

Expand Down Expand Up @@ -42,30 +42,28 @@ decade | `DEC`, `DECS`, `DECADE`, `DECADES`

### Return value

`EXTRACT` returns a [`float`](../../types/float) value.
`EXTRACT` returns a [`numeric`](../../types/numeric) value.

## Examples

### Extract second from timestamptz

```sql
SELECT EXTRACT(S FROM TIMESTAMP '2006-01-02 15:04:05.06')
AS sec_extr;
SELECT EXTRACT(S FROM TIMESTAMP '2006-01-02 15:04:05.06');
```
```nofmt
sec_extr
----------
5.06
extract
---------
5.06
```

### Extract century from date

```sql
SELECT EXTRACT(CENTURIES FROM DATE '2006-01-02')
AS sec_extr;
SELECT EXTRACT(CENTURIES FROM DATE '2006-01-02');
```
```nofmt
sec_extr
----------
extract
---------
21
```
6 changes: 5 additions & 1 deletion doc/user/data/sql_funcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,14 @@
description: Largest `time_component` <= `val`
url: date-trunc

- signature: EXTRACT(extract_expr) -> float
- signature: EXTRACT(extract_expr) -> numeric
description: Specified time component from value
url: extract

- signature: 'date_part(time_component: str, val: timestamp) -> float'
description: Specified time component from value
url: date-part

- signature: mz_logical_timestamp() -> numeric
description: 'The logical time at which a query executes. Used for temporal filters and internal debugging.'
url: now_and_mz_logical_timestamp
Expand Down
195 changes: 195 additions & 0 deletions doc/user/layouts/partials/sql-grammar/func-date-part.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions doc/user/sql-grammar/sql-grammar.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ func_date_trunc ::=
'date_trunc' '(' "'" ( 'microseconds' | 'milliseconds' | 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year' | 'decade' | 'century' | 'millenium' ) "'" ',' ts_val ')'
func_extract ::=
'EXTRACT' '(' ( 'EPOCH' | 'MILLENNIUM' | 'CENTURY' | 'DECADE' | 'YEAR' | 'QUARTER' | 'MONTH' | 'WEEK' | 'DAY' | 'HOUR' | 'MINUTE' | 'SECOND' | 'MICROSECOND' | 'MILLISECOND' | 'DOW' | 'ISODOW' | 'DOY' ) 'FROM' val ')'
func_date_part ::=
'date_part' '(' "'" ( 'epoch' | 'millennium' | 'century' | 'decade' | 'year' | 'quarter' | 'month' | 'week' | 'dat' | 'hour' | 'minute' | 'second' | 'microsecond' | 'millisecond' | 'dow' | 'isodow' | 'doy' ) "'" ',' val ')'
func_length ::=
'length' '(' str (',' encoding_name)? ')'
func_substring ::=
Expand Down
Loading