Skip to content

Can not use extract <part> on the value of now() #3980

@alamb

Description

@alamb

Describe the bug
I would like to be able to find the current date, or the current day of week, etc

DataFusion supports now() and extract (hour from <timestamp>) syntax ✅

However, you can't use extract with the value of now() 😢

To Reproduce

❯ select extract (day  from now());
Plan("Coercion from [Utf8, Timestamp(Nanosecond, Some(\"UTC\"))] to the signature OneOf([Exact([Utf8, Date32]), Exact([Utf8, Date64]), Exact([Utf8, Timestamp(Second, None)]), Exact([Utf8, Timestamp(Microsecond, None)]), Exact([Utf8, Timestamp(Millisecond, None)]), Exact([Utf8, Timestamp(Nanosecond, None)])]) failed.")

Expected behavior
I expect the day part of now() to be extracted as in postgres:

postgres=# select extract(day from now());
 extract 
---------
      27
(1 row)

Note that the error seems to be that DataFusion can't coerce a Timestamp(Nanosecond, "UTC") to Timestamp(Nanosecond, None) -- aka it is related to timezones

You can work around the problem by explicitly casting the output of now() to timestamp (not timestamptz):

select extract (day  from cast(now() as timestamp));
+-----------------------------+
| datepart(Utf8("DAY"),now()) |
+-----------------------------+
| 27                          |
+-----------------------------+

Additional context
This was reported by early (internal) users of IOx

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions