apache / apache/datafusion

Casting existing timestamp to timestamp again strips timezone information

Open
#12,218 23 comments 0 reactions 1 assignee Claimed by @devanbenz View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

Using the `::timestamp` (or CAST()) function strips timezone information.

### To Reproduce

```
DataFusion CLI v40.0.0
> select arrow_typeof(now()) as tz;
+---------------------------------------+
| tz |
+---------------------------------------+
| Timestamp(Nanosecond, Some("+00:00")) |
+---------------------------------------+

> select arrow_typeof(now()::timestamp) as tz;
+-----------------------------+
| tz |
+-----------------------------+
| Timestamp(Nanosecond, None) |
+-----------------------------+

> select arrow_typeof((now() AT TIME ZONE 'America/Denver')::timestamp) as tz;
+-----------------------------+
| tz |
+-----------------------------+
| Timestamp(Nanosecond, None) |
+-----------------------------+
```

Or as seen with actual values:
```
DataFusion CLI v40.0.0
> select now();
+-----------------------------+
| now() |
+-----------------------------+
| 2024-08-28T17:08:08.698528Z |
+-----------------------------+

> select (now() AT TIME ZONE 'America/Denver');
+----------------------------------+
| now() |
+----------------------------------+
| 2024-08-28T11:08:13.578025-06:00 |
+----------------------------------+

> select now()::timestamp;
+----------------------------+
| now() |
+----------------------------+
| 2024-08-28T17:08:17.228985 |
+----------------------------+

> select (now() AT TIME ZONE 'America/Denver')::timestamp;
+----------------------------+
| now() |
+----------------------------+
| 2024-08-28T17:08:20.042920 |
+----------------------------+
```

### Expected behavior

I would expect that using the `::timestamp` cast on a timestamp would not impact its timezone. This is how Postgres behaves.
```
postgres@localhost:postgres> select now()::timestamp
+----------------------------+
| now |
|----------------------------|
| 2024-08-28 17:05:07.779323 |
+----------------------------+

postgres@localhost:postgres> select (now() at time zone 'America/Denver')::timestamp
+----------------------------+
| timezone |
|----------------------------|
| 2024-08-28 11:05:09.275359 |
+----------------------------+
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.