-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ES|QL][DOCS] Add docs for date_period and time_duration #116368
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
Changes from 8 commits
a02e9b0
b3dabf6
f22f43b
c73583c
5a0d525
4f33792
4eeb682
96fe148
e9bc792
da25762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||||||
[[esql-time-spans]] | ||||||||||
=== {esql} time spans | ||||||||||
|
||||||||||
++++ | ||||||||||
<titleabbrev>Time spans</titleabbrev> | ||||||||||
++++ | ||||||||||
|
||||||||||
Time spans provide a way of describing a specific period of time that separates two datetime values. There are currently two supported types of time spans, `DATE_PERIOD` specifies intervals in years, quarters, months, weeks and days, and `TIME_DURATION` specifies intervals in hours, minutes, seconds and milliseconds. | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
The time span contains two elements, an integer value and a temporal unit, both of them are required when specifying a time span. | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
Time spans are often used in datetime related grouping functions like <<esql-bucket, BUCKET>>, scalar functions like <<esql-date_trunc, DATE_TRUNC>> and arithmetic operators like `+` and `-`. There are conversions functions that can convert string literals to time spans, <<esql-to_dateperiod, TO_DATEPERIOD>>, <<esql-to_timeduration, TO_TIMEDURATION>> and their corresponding cast operators `::DATE_PERIOD` and `::TIME_DURATION`. | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
==== Examples of using time spans in {esql} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. certainly did! fixed suggestion :) |
||||||||||
|
||||||||||
|
||||||||||
With `BUCKET` | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
[source.merge.styled,esql] | ||||||||||
---- | ||||||||||
include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan] | ||||||||||
---- | ||||||||||
[%header.monospaced.styled,format=dsv,separator=|] | ||||||||||
|=== | ||||||||||
include::{esql-specs}/bucket.csv-spec[tag=docsBucketWeeklyHistogramWithSpan-result] | ||||||||||
|=== | ||||||||||
|
||||||||||
|
||||||||||
With `DATE_TRUNC` | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
[source.merge.styled,esql] | ||||||||||
---- | ||||||||||
include::{esql-specs}/date.csv-spec[tag=docsDateTrunc] | ||||||||||
---- | ||||||||||
[%header.monospaced.styled,format=dsv,separator=|] | ||||||||||
|=== | ||||||||||
include::{esql-specs}/date.csv-spec[tag=docsDateTrunc-result] | ||||||||||
|=== | ||||||||||
|
||||||||||
|
||||||||||
With `+` and/or `-` | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
[source.merge.styled,esql] | ||||||||||
---- | ||||||||||
include::{esql-specs}/date.csv-spec[tag=docsNowWhere] | ||||||||||
---- | ||||||||||
[%header.monospaced.styled,format=dsv,separator=|] | ||||||||||
|=== | ||||||||||
include::{esql-specs}/date.csv-spec[tag=docsNowWhere-result] | ||||||||||
|=== | ||||||||||
|
||||||||||
|
||||||||||
When a time span is provided as a name parameter in string format, `TO_DATEPERIOD`, `::DATE_PERIOD`, `TO_TIMEDURATION` or `::TIME_DURATION` can be used to convert it to its corresponding time span value in arithmetic operations like `+` and/or `-`. | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
[source, esql] | ||||||||||
---- | ||||||||||
POST /_query | ||||||||||
{ | ||||||||||
"query": """ | ||||||||||
FROM employees | ||||||||||
| EVAL x = hire_date + ?timespan::DATE_PERIOD, y = hire_date - TO_DATEPERIOD(?timespan) | ||||||||||
""", | ||||||||||
"params": [{"timespan" : "1 day"}] | ||||||||||
} | ||||||||||
---- | ||||||||||
|
||||||||||
When a time span is provided as a name parameter in string format, it can be converted automatically to its corresponding time span value in grouping functions and scalar functions, like `BUCKET` and `DATE_TRUNC`. | ||||||||||
fang-xing-esql marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
[source, esql] | ||||||||||
---- | ||||||||||
POST /_query | ||||||||||
{ | ||||||||||
"query": """ | ||||||||||
FROM employees | ||||||||||
| WHERE hire_date >= "1985-01-01T00:00:00Z" AND hire_date < "1986-01-01T00:00:00Z" | ||||||||||
| STATS hires_per_week = COUNT(*) BY week = BUCKET(hire_date, ?timespan) | ||||||||||
| SORT week | ||||||||||
""", | ||||||||||
"params": [{"timespan" : "1 week"}] | ||||||||||
} | ||||||||||
---- | ||||||||||
|
||||||||||
[source, esql] | ||||||||||
---- | ||||||||||
POST /_query | ||||||||||
{ | ||||||||||
"query": """ | ||||||||||
FROM employees | ||||||||||
| KEEP first_name, last_name, hire_date | ||||||||||
| EVAL year_hired = DATE_TRUNC(?timespan, hire_date) | ||||||||||
""", | ||||||||||
"params": [{"timespan" : "1 year"}] | ||||||||||
} | ||||||||||
---- | ||||||||||
|
||||||||||
|
||||||||||
[[esql-time-spans-table]] | ||||||||||
==== Supported temporal units | ||||||||||
[%header.monospaced.styled,format=dsv,separator=|] | ||||||||||
|=== | ||||||||||
Temporal Units|Valid Abbreviations | ||||||||||
year|y, yr, years | ||||||||||
quarter|q, quarters | ||||||||||
month|mo, months | ||||||||||
week|w, weeks | ||||||||||
day|d, days | ||||||||||
hour|h, hours, | ||||||||||
minute|min, minutes | ||||||||||
second|s, sec, seconds | ||||||||||
millisecond|ms, milliseconds | ||||||||||
|=== |
Uh oh!
There was an error while loading. Please reload this page.