Skip to content

Commit b446c28

Browse files
committed
chore: added sql example to datetime docs
1 parent 6ef4cef commit b446c28

6 files changed

Lines changed: 178 additions & 11 deletions

File tree

datafusion/functions/src/datetime/current_date.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,24 @@ The `current_date()` return value is determined at query time and will return th
3939
"#,
4040
syntax_example = r#"current_date()
4141
(optional) SET datafusion.execution.time_zone = '+00:00';
42-
SELECT current_date();"#
42+
SELECT current_date();"#,
43+
sql_example = r#"```sql
44+
> SELECT current_date();
45+
+----------------+
46+
| current_date() |
47+
+----------------+
48+
| 2024-12-23 |
49+
+----------------+
50+
51+
-- The current date is based on the session time zone (UTC by default)
52+
> SET datafusion.execution.time_zone = 'Asia/Tokyo';
53+
> SELECT current_date();
54+
+----------------+
55+
| current_date() |
56+
+----------------+
57+
| 2024-12-24 |
58+
+----------------+
59+
```"#
4360
)]
4461
#[derive(Debug, PartialEq, Eq, Hash)]
4562
pub struct CurrentDateFunc {

datafusion/functions/src/datetime/current_time.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,24 @@ The `current_time()` return value is determined at query time and will return th
3939
4040
The session time zone can be set using the statement 'SET datafusion.execution.time_zone = desired time zone'. The time zone can be a value like +00:00, 'Europe/London' etc.
4141
"#,
42-
syntax_example = r#"current_time()
43-
(optional) SET datafusion.execution.time_zone = '+00:00';
44-
SELECT current_time();"#
42+
syntax_example = "current_time()",
43+
sql_example = r#"```sql
44+
> SELECT current_time();
45+
+--------------------+
46+
| current_time() |
47+
+--------------------+
48+
| 06:30:00.123456789 |
49+
+--------------------+
50+
51+
-- The current time is based on the session time zone (UTC by default)
52+
> SET datafusion.execution.time_zone = 'Europe/London';
53+
> SELECT current_time();
54+
+--------------------+
55+
| current_time() |
56+
+--------------------+
57+
| 07:30:00.123456789 |
58+
+--------------------+
59+
```"#
4560
)]
4661
#[derive(Debug, PartialEq, Eq, Hash)]
4762
pub struct CurrentTimeFunc {

datafusion/functions/src/datetime/date_part.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,21 @@ use datafusion_macros::user_doc;
8787
argument(
8888
name = "expression",
8989
description = "Time expression to operate on. Can be a constant, column, or function."
90-
)
90+
),
91+
sql_example = r#"```sql
92+
> SELECT date_part('year', '2024-05-01T00:00:00');
93+
+---------------------------------------------+
94+
| date_part(Utf8("year"),Utf8("2024-05-01T00:00:00")) |
95+
+---------------------------------------------+
96+
| 2024.0 |
97+
+---------------------------------------------+
98+
> SELECT extract(day FROM timestamp '2024-05-01T00:00:00');
99+
+---------------------------------------------+
100+
| date_part(Utf8("day"),Utf8("2024-05-01T00:00:00")) |
101+
+---------------------------------------------+
102+
| 1.0 |
103+
+---------------------------------------------+
104+
```"#
91105
)]
92106
#[derive(Debug, PartialEq, Eq, Hash)]
93107
pub struct DatePartFunc {

datafusion/functions/src/datetime/date_trunc.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,21 @@ impl DateTruncGranularity {
167167
argument(
168168
name = "expression",
169169
description = "Timestamp or time expression to operate on. Can be a constant, column, or function."
170-
)
170+
),
171+
sql_example = r#"```sql
172+
> SELECT date_trunc('month', '2024-05-15T10:30:00');
173+
+-----------------------------------------------+
174+
| date_trunc(Utf8("month"),Utf8("2024-05-15T10:30:00")) |
175+
+-----------------------------------------------+
176+
| 2024-05-01T00:00:00 |
177+
+-----------------------------------------------+
178+
> SELECT date_trunc('hour', '2024-05-15T10:30:00');
179+
+----------------------------------------------+
180+
| date_trunc(Utf8("hour"),Utf8("2024-05-15T10:30:00")) |
181+
+----------------------------------------------+
182+
| 2024-05-15T10:00:00 |
183+
+----------------------------------------------+
184+
```"#
171185
)]
172186
#[derive(Debug, PartialEq, Eq, Hash)]
173187
pub struct DateTruncFunc {

datafusion/functions/src/datetime/now.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,24 @@ Returns the current timestamp in the system configured timezone (None by default
3737
3838
The `now()` return value is determined at query time and will return the same timestamp, no matter when in the query plan the function executes.
3939
"#,
40-
syntax_example = "now()"
40+
syntax_example = "now()",
41+
sql_example = r#"```sql
42+
> SELECT now();
43+
+----------------------------------+
44+
| now() |
45+
+----------------------------------+
46+
| 2024-12-23T06:30:00.123456789Z |
47+
+----------------------------------+
48+
49+
-- The timezone of the returned timestamp depends on the session time zone
50+
> SET datafusion.execution.time_zone = 'America/New_York';
51+
> SELECT now();
52+
+----------------------------------+
53+
| now() |
54+
+----------------------------------+
55+
| 2024-12-23T01:30:00.123456789Z |
56+
+----------------------------------+
57+
```"#
4158
)]
4259
#[derive(Debug, PartialEq, Eq, Hash)]
4360
pub struct NowFunc {

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

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,8 +2411,26 @@ The `current_date()` return value is determined at query time and will return th
24112411

24122412
```sql
24132413
current_date()
2414-
(optional) SET datafusion.execution.time_zone = '+00:00';
2415-
SELECT current_date();
2414+
```
2415+
2416+
#### Example
2417+
2418+
```sql
2419+
> SELECT current_date();
2420+
+----------------+
2421+
| current_date() |
2422+
+----------------+
2423+
| 2024-12-23 |
2424+
+----------------+
2425+
2426+
-- The current date is based on the session time zone (UTC by default)
2427+
> SET datafusion.execution.time_zone = 'Asia/Tokyo';
2428+
> SELECT current_date();
2429+
+----------------+
2430+
| current_date() |
2431+
+----------------+
2432+
| 2024-12-24 |
2433+
+----------------+
24162434
```
24172435

24182436
#### Aliases
@@ -2429,8 +2447,26 @@ The session time zone can be set using the statement 'SET datafusion.execution.t
24292447

24302448
```sql
24312449
current_time()
2432-
(optional) SET datafusion.execution.time_zone = '+00:00';
2433-
SELECT current_time();
2450+
```
2451+
2452+
#### Example
2453+
2454+
```sql
2455+
> SELECT current_time();
2456+
+--------------------+
2457+
| current_time() |
2458+
+--------------------+
2459+
| 06:30:00.123456789 |
2460+
+--------------------+
2461+
2462+
-- The current time is based on the session time zone (UTC by default)
2463+
> SET datafusion.execution.time_zone = 'Europe/London';
2464+
> SELECT current_time();
2465+
+--------------------+
2466+
| current_time() |
2467+
+--------------------+
2468+
| 07:30:00.123456789 |
2469+
+--------------------+
24342470
```
24352471

24362472
### `current_timestamp`
@@ -2537,6 +2573,23 @@ date_part(part, expression)
25372573

25382574
- **expression**: Time expression to operate on. Can be a constant, column, or function.
25392575

2576+
#### Example
2577+
2578+
```sql
2579+
> SELECT date_part('year', '2024-05-01T00:00:00');
2580+
+---------------------------------------------+
2581+
| date_part(Utf8("year"),Utf8("2024-05-01T00:00:00")) |
2582+
+---------------------------------------------+
2583+
| 2024.0 |
2584+
+---------------------------------------------+
2585+
> SELECT extract(day FROM timestamp '2024-05-01T00:00:00');
2586+
+---------------------------------------------+
2587+
| date_part(Utf8("day"),Utf8("2024-05-01T00:00:00")) |
2588+
+---------------------------------------------+
2589+
| 1.0 |
2590+
+---------------------------------------------+
2591+
```
2592+
25402593
#### Alternative Syntax
25412594

25422595
```sql
@@ -2582,6 +2635,23 @@ date_trunc(precision, expression)
25822635

25832636
- **expression**: Timestamp or time expression to operate on. Can be a constant, column, or function.
25842637

2638+
#### Example
2639+
2640+
```sql
2641+
> SELECT date_trunc('month', '2024-05-15T10:30:00');
2642+
+-----------------------------------------------+
2643+
| date_trunc(Utf8("month"),Utf8("2024-05-15T10:30:00")) |
2644+
+-----------------------------------------------+
2645+
| 2024-05-01T00:00:00 |
2646+
+-----------------------------------------------+
2647+
> SELECT date_trunc('hour', '2024-05-15T10:30:00');
2648+
+----------------------------------------------+
2649+
| date_trunc(Utf8("hour"),Utf8("2024-05-15T10:30:00")) |
2650+
+----------------------------------------------+
2651+
| 2024-05-15T10:00:00 |
2652+
+----------------------------------------------+
2653+
```
2654+
25852655
#### Aliases
25862656

25872657
- datetrunc
@@ -2694,6 +2764,26 @@ The `now()` return value is determined at query time and will return the same ti
26942764
now()
26952765
```
26962766

2767+
#### Example
2768+
2769+
```sql
2770+
> SELECT now();
2771+
+----------------------------------+
2772+
| now() |
2773+
+----------------------------------+
2774+
| 2024-12-23T06:30:00.123456789Z |
2775+
+----------------------------------+
2776+
2777+
-- The timezone of the returned timestamp depends on the session time zone
2778+
> SET datafusion.execution.time_zone = 'America/New_York';
2779+
> SELECT now();
2780+
+----------------------------------+
2781+
| now() |
2782+
+----------------------------------+
2783+
| 2024-12-23T01:30:00.123456789Z |
2784+
+----------------------------------+
2785+
```
2786+
26972787
#### Aliases
26982788

26992789
- current_timestamp

0 commit comments

Comments
 (0)