Skip to content

Commit 042843a

Browse files
authored
Minor: Add SQL example for date_bin (#13390)
1 parent e041b6a commit 042843a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

datafusion/functions/src/datetime/date_bin.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ Calculates time intervals and returns the start of the interval nearest to the s
181181
For example, if you "bin" or "window" data into 15 minute intervals, an input timestamp of `2023-01-01T18:18:18Z` will be updated to the start time of the 15 minute bin it is in: `2023-01-01T18:15:00Z`.
182182
"#)
183183
.with_syntax_example("date_bin(interval, expression, origin-timestamp)")
184+
.with_sql_example(r#"```sql
185+
-- Bin the timestamp into 1 day intervals
186+
> SELECT date_bin(interval '1 day', time) as bin
187+
FROM VALUES ('2023-01-01T18:18:18Z'), ('2023-01-03T19:00:03Z') t(time);
188+
+---------------------+
189+
| bin |
190+
+---------------------+
191+
| 2023-01-01T00:00:00 |
192+
| 2023-01-03T00:00:00 |
193+
+---------------------+
194+
2 row(s) fetched.
195+
196+
-- Bin the timestamp into 1 day intervals starting at 3AM on 2023-01-01
197+
> SELECT date_bin(interval '1 day', time, '2023-01-01T03:00:00') as bin
198+
FROM VALUES ('2023-01-01T18:18:18Z'), ('2023-01-03T19:00:03Z') t(time);
199+
+---------------------+
200+
| bin |
201+
+---------------------+
202+
| 2023-01-01T03:00:00 |
203+
| 2023-01-03T03:00:00 |
204+
+---------------------+
205+
2 row(s) fetched.
206+
```
207+
"#)
184208
.with_argument("interval", "Bin interval.")
185209
.with_argument("expression", "Time expression to operate on. Can be a constant, column, or function.")
186210
.with_argument("origin-timestamp", "Optional. Starting point used to determine bin boundaries. If not specified defaults 1970-01-01T00:00:00Z (the UNIX epoch in UTC).

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,32 @@ The following intervals are supported:
19541954
- years
19551955
- century
19561956

1957+
#### Example
1958+
1959+
```sql
1960+
-- Bin the timestamp into 1 day intervals
1961+
> SELECT date_bin(interval '1 day', time) as bin
1962+
FROM VALUES ('2023-01-01T18:18:18Z'), ('2023-01-03T19:00:03Z') t(time);
1963+
+---------------------+
1964+
| bin |
1965+
+---------------------+
1966+
| 2023-01-01T00:00:00 |
1967+
| 2023-01-03T00:00:00 |
1968+
+---------------------+
1969+
2 row(s) fetched.
1970+
1971+
-- Bin the timestamp into 1 day intervals starting at 3AM on 2023-01-01
1972+
> SELECT date_bin(interval '1 day', time, '2023-01-01T03:00:00') as bin
1973+
FROM VALUES ('2023-01-01T18:18:18Z'), ('2023-01-03T19:00:03Z') t(time);
1974+
+---------------------+
1975+
| bin |
1976+
+---------------------+
1977+
| 2023-01-01T03:00:00 |
1978+
| 2023-01-03T03:00:00 |
1979+
+---------------------+
1980+
2 row(s) fetched.
1981+
```
1982+
19571983
### `date_format`
19581984

19591985
_Alias of [to_char](#to_char)._

0 commit comments

Comments
 (0)