Skip to content

Commit d81c82d

Browse files
authored
Improve to_timestamp docs (#8981)
* Updated documentation concerning to_timestamp scalar function including adding missing link in the datafusion-examples/README file. Resolves #8980 * Fix example url. * Applied prettier.
1 parent 90e61c7 commit d81c82d

File tree

3 files changed

+140
-5
lines changed

3 files changed

+140
-5
lines changed

datafusion-examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cargo run --example csv_sql
6464
- [`simple_udaf.rs`](examples/simple_udaf.rs): Define and invoke a User Defined Aggregate Function (UDAF)
6565
- [`advanced_udaf.rs`](examples/advanced_udaf.rs): Define and invoke a more complicated User Defined Aggregate Function (UDAF)
6666
- [`simple_udfw.rs`](examples/simple_udwf.rs): Define and invoke a User Defined Window Function (UDWF)
67+
- [`to_timestamp.rs`](examples/to_timestamp.rs): Examples of using the to_timestamp functions
6768
- [`advanced_udwf.rs`](examples/advanced_udwf.rs): Define and invoke a more complicated User Defined Window Function (UDWF)
6869

6970
## Distributed

datafusion-examples/examples/dataframe_to_timestamp.rs renamed to datafusion-examples/examples/to_timestamp.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use datafusion::error::Result;
2424
use datafusion::prelude::*;
2525
use datafusion_common::assert_contains;
2626

27-
/// This example demonstrates how to use the to_timestamp function in the DataFrame API as well as via sql.
27+
/// This example demonstrates how to use the to_timestamp series
28+
/// of functions in the DataFrame API as well as via sql.
2829
#[tokio::main]
2930
async fn main() -> Result<()> {
3031
// define a schema.
@@ -61,7 +62,8 @@ async fn main() -> Result<()> {
6162

6263
// use to_timestamp function to convert col 'a' to timestamp type using the default parsing
6364
let df = df.with_column("a", to_timestamp(vec![col("a")]))?;
64-
// use to_timestamp_seconds function to convert col 'b' to timestamp(Seconds) type using a list of chrono formats to try
65+
// use to_timestamp_seconds function to convert col 'b' to timestamp(Seconds) type using a list
66+
// of chrono formats (https://docs.rs/chrono/latest/chrono/format/strftime/index.html) to try
6567
let df = df.with_column(
6668
"b",
6769
to_timestamp_seconds(vec![
@@ -90,20 +92,47 @@ async fn main() -> Result<()> {
9092
df.show().await?;
9193

9294
// use sql to convert a static string to a timestamp using a list of chrono formats to try
93-
let df = ctx.sql("select to_timestamp('01-14-2023 01:01:30+05:30', '%+', '%d-%m-%Y %H/%M/%S', '%m-%d-%Y %H:%M:%S%#z')").await?;
95+
// note that one of the formats is invalid ('%q') but since DataFusion will try all the
96+
// formats until it encounters one that parses the timestamp expression successfully
97+
// no error will be returned
98+
let df = ctx.sql("select to_timestamp_micros('01-14-2023 01:01:30+05:30', '%q', '%d-%m-%Y %H/%M/%S', '%+', '%m-%d-%Y %H:%M:%S%#z')").await?;
99+
100+
// print the results
101+
df.show().await?;
102+
103+
// casting a string to TIMESTAMP will also work for RFC3339 timestamps
104+
let df = ctx
105+
.sql("select to_timestamp_millis(TIMESTAMP '2022-08-03T14:38:50Z')")
106+
.await?;
107+
108+
// print the results
109+
df.show().await?;
110+
111+
// unix timestamps (in seconds) are also supported
112+
let df = ctx.sql("select to_timestamp(1926632005)").await?;
94113

95114
// print the results
96115
df.show().await?;
97116

98117
// use sql to convert a static string to a timestamp using a non-matching chrono format to try
99118
let result = ctx
100-
.sql("select to_timestamp('01-14-2023 01/01/30', '%d-%m-%Y %H:%M:%S')")
119+
.sql("select to_timestamp_nanos('01-14-2023 01/01/30', '%d-%m-%Y %H:%M:%S')")
101120
.await?
102121
.collect()
103122
.await;
104123

105124
let expected = "Error parsing timestamp from '01-14-2023 01/01/30' using format '%d-%m-%Y %H:%M:%S': input contains invalid characters";
106125
assert_contains!(result.unwrap_err().to_string(), expected);
107126

127+
// note that using arrays for the chrono formats is not supported
128+
let result = ctx
129+
.sql("SELECT to_timestamp('2022-08-03T14:38:50+05:30', make_array('%s', '%q', '%d-%m-%Y %H:%M:%S%#z', '%+'))")
130+
.await?
131+
.collect()
132+
.await;
133+
134+
let expected = "to_timestamp function unsupported data type at index 1: List";
135+
assert_contains!(result.unwrap_err().to_string(), expected);
136+
108137
Ok(())
109138
}

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,8 +1311,8 @@ regexp_replace(str, regexp, replacement, flags)
13111311
- [date_part](#date_part)
13121312
- [datepart](#datepart)
13131313
- [extract](#extract)
1314-
- [to_timestamp](#to_timestamp)
13151314
- [today](#today)
1315+
- [to_timestamp](#to_timestamp)
13161316
- [to_timestamp_millis](#to_timestamp_millis)
13171317
- [to_timestamp_micros](#to_timestamp_micros)
13181318
- [to_timestamp_seconds](#to_timestamp_seconds)
@@ -1526,6 +1526,27 @@ to_timestamp(expression[, ..., format_n])
15261526

15271527
[chrono format]: https://docs.rs/chrono/latest/chrono/format/strftime/index.html
15281528

1529+
#### Example
1530+
1531+
```
1532+
❯ select to_timestamp('2023-01-31T09:26:56.123456789-05:00');
1533+
+-----------------------------------------------------------+
1534+
| to_timestamp(Utf8("2023-01-31T09:26:56.123456789-05:00")) |
1535+
+-----------------------------------------------------------+
1536+
| 2023-01-31T14:26:56.123456789 |
1537+
+-----------------------------------------------------------+
1538+
❯ select to_timestamp('03:59:00.123456789 05-17-2023', '%c', '%+', '%H:%M:%S%.f %m-%d-%Y');
1539+
+--------------------------------------------------------------------------------------------------------+
1540+
| to_timestamp(Utf8("03:59:00.123456789 05-17-2023"),Utf8("%c"),Utf8("%+"),Utf8("%H:%M:%S%.f %m-%d-%Y")) |
1541+
+--------------------------------------------------------------------------------------------------------+
1542+
| 2023-05-17T03:59:00.123456789 |
1543+
+--------------------------------------------------------------------------------------------------------+
1544+
```
1545+
1546+
Additional examples can be found [here]
1547+
1548+
[here]: https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples/to_timestamp.rs
1549+
15291550
### `to_timestamp_millis`
15301551

15311552
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000Z`).
@@ -1546,6 +1567,27 @@ to_timestamp_millis(expression[, ..., format_n])
15461567
they appear with the first successful one being returned. If none of the formats successfully parse the expression
15471568
an error will be returned.
15481569

1570+
#### Example
1571+
1572+
```
1573+
❯ select to_timestamp_millis('2023-01-31T09:26:56.123456789-05:00');
1574+
+------------------------------------------------------------------+
1575+
| to_timestamp_millis(Utf8("2023-01-31T09:26:56.123456789-05:00")) |
1576+
+------------------------------------------------------------------+
1577+
| 2023-01-31T14:26:56.123 |
1578+
+------------------------------------------------------------------+
1579+
❯ select to_timestamp_millis('03:59:00.123456789 05-17-2023', '%c', '%+', '%H:%M:%S%.f %m-%d-%Y');
1580+
+---------------------------------------------------------------------------------------------------------------+
1581+
| to_timestamp_millis(Utf8("03:59:00.123456789 05-17-2023"),Utf8("%c"),Utf8("%+"),Utf8("%H:%M:%S%.f %m-%d-%Y")) |
1582+
+---------------------------------------------------------------------------------------------------------------+
1583+
| 2023-05-17T03:59:00.123 |
1584+
+---------------------------------------------------------------------------------------------------------------+
1585+
```
1586+
1587+
Additional examples can be found [here]
1588+
1589+
[here]: https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples/to_timestamp.rs
1590+
15491591
### `to_timestamp_micros`
15501592

15511593
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000Z`).
@@ -1566,6 +1608,27 @@ to_timestamp_micros(expression[, ..., format_n])
15661608
they appear with the first successful one being returned. If none of the formats successfully parse the expression
15671609
an error will be returned.
15681610

1611+
#### Example
1612+
1613+
```
1614+
❯ select to_timestamp_micros('2023-01-31T09:26:56.123456789-05:00');
1615+
+------------------------------------------------------------------+
1616+
| to_timestamp_micros(Utf8("2023-01-31T09:26:56.123456789-05:00")) |
1617+
+------------------------------------------------------------------+
1618+
| 2023-01-31T14:26:56.123456 |
1619+
+------------------------------------------------------------------+
1620+
❯ select to_timestamp_micros('03:59:00.123456789 05-17-2023', '%c', '%+', '%H:%M:%S%.f %m-%d-%Y');
1621+
+---------------------------------------------------------------------------------------------------------------+
1622+
| to_timestamp_micros(Utf8("03:59:00.123456789 05-17-2023"),Utf8("%c"),Utf8("%+"),Utf8("%H:%M:%S%.f %m-%d-%Y")) |
1623+
+---------------------------------------------------------------------------------------------------------------+
1624+
| 2023-05-17T03:59:00.123456 |
1625+
+---------------------------------------------------------------------------------------------------------------+
1626+
```
1627+
1628+
Additional examples can be found [here]
1629+
1630+
[here]: https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples/to_timestamp.rs
1631+
15691632
### `to_timestamp_nanos`
15701633

15711634
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000000000Z`).
@@ -1586,6 +1649,27 @@ to_timestamp_nanos(expression[, ..., format_n])
15861649
they appear with the first successful one being returned. If none of the formats successfully parse the expression
15871650
an error will be returned.
15881651

1652+
#### Example
1653+
1654+
```
1655+
❯ select to_timestamp_nanos('2023-01-31T09:26:56.123456789-05:00');
1656+
+-----------------------------------------------------------------+
1657+
| to_timestamp_nanos(Utf8("2023-01-31T09:26:56.123456789-05:00")) |
1658+
+-----------------------------------------------------------------+
1659+
| 2023-01-31T14:26:56.123456789 |
1660+
+-----------------------------------------------------------------+
1661+
❯ select to_timestamp_nanos('03:59:00.123456789 05-17-2023', '%c', '%+', '%H:%M:%S%.f %m-%d-%Y');
1662+
+--------------------------------------------------------------------------------------------------------------+
1663+
| to_timestamp_nanos(Utf8("03:59:00.123456789 05-17-2023"),Utf8("%c"),Utf8("%+"),Utf8("%H:%M:%S%.f %m-%d-%Y")) |
1664+
+--------------------------------------------------------------------------------------------------------------+
1665+
| 2023-05-17T03:59:00.123456789 |
1666+
+---------------------------------------------------------------------------------------------------------------+
1667+
```
1668+
1669+
Additional examples can be found [here]
1670+
1671+
[here]: https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples/to_timestamp.rs
1672+
15891673
### `to_timestamp_seconds`
15901674

15911675
Converts a value to a timestamp (`YYYY-MM-DDT00:00:00.000Z`).
@@ -1606,6 +1690,27 @@ to_timestamp_seconds(expression[, ..., format_n])
16061690
they appear with the first successful one being returned. If none of the formats successfully parse the expression
16071691
an error will be returned.
16081692

1693+
#### Example
1694+
1695+
```
1696+
❯ select to_timestamp_seconds('2023-01-31T09:26:56.123456789-05:00');
1697+
+-------------------------------------------------------------------+
1698+
| to_timestamp_seconds(Utf8("2023-01-31T09:26:56.123456789-05:00")) |
1699+
+-------------------------------------------------------------------+
1700+
| 2023-01-31T14:26:56 |
1701+
+-------------------------------------------------------------------+
1702+
❯ select to_timestamp_seconds('03:59:00.123456789 05-17-2023', '%c', '%+', '%H:%M:%S%.f %m-%d-%Y');
1703+
+----------------------------------------------------------------------------------------------------------------+
1704+
| to_timestamp_seconds(Utf8("03:59:00.123456789 05-17-2023"),Utf8("%c"),Utf8("%+"),Utf8("%H:%M:%S%.f %m-%d-%Y")) |
1705+
+----------------------------------------------------------------------------------------------------------------+
1706+
| 2023-05-17T03:59:00 |
1707+
+----------------------------------------------------------------------------------------------------------------+
1708+
```
1709+
1710+
Additional examples can be found [here]
1711+
1712+
[here]: https://github.com/apache/arrow-datafusion/blob/main/datafusion-examples/examples/to_timestamp.rs
1713+
16091714
### `from_unixtime`
16101715

16111716
Converts an integer to RFC3339 timestamp format (`YYYY-MM-DDT00:00:00.000000000Z`).

0 commit comments

Comments
 (0)