Applies to VAST Cluster 5.5.
Functions › Date and time functions › Epoch values. Returns the number of whole microseconds since 1970-01-01 00:00:00 of a date or timestamp, as a BIGINT. On a time of day it returns the microseconds since midnight, and on an interval its length in microseconds.
Syntax
epoch_us(value)
| Argument | Type | Description |
|---|---|---|
value |
DATE, TIMESTAMP (also TIMESTAMP_NS, TIMESTAMP_MS, TIMESTAMP_S), TIMESTAMP WITH TIME ZONE, TIME, TIME WITH TIME ZONE or INTERVAL |
The value to convert to microseconds. |
Returns: BIGINT.
Set up the examples
The examples connect with the ADBC driver, as in VAST Query Engine (ADBC), and work in a schema called docs_sql_fn_date_time_epoch_us. Set QE_ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and VASTDB_BUCKET (SQL reference), and run the blocks in order in one Python session. Below each block is the output you should see.
This block connects, defines the run() and plan() helpers, and creates a table of five events. Each has a date (d), a timestamp (ts), a time of day (t) and a timestamp with nanoseconds (tns). Row 4 has no values, and row 5 is the last moment of 1969:
import os
import re
import adbc_driver_manager
import adbc_driver_manager.dbapi as dbapi
import adbc_driver_vastdb
BUCKET = os.environ["VASTDB_BUCKET"]
SCHEMA = f'"{BUCKET}/docs_sql_fn_date_time_epoch_us"'
conn = dbapi.connect(
driver=adbc_driver_vastdb.get_driver_path(),
db_kwargs={
"vast.db.endpoint": os.environ["QE_ENDPOINT"],
"vast.db.access_key": os.environ["AWS_ACCESS_KEY_ID"],
"vast.db.secret_key": os.environ["AWS_SECRET_ACCESS_KEY"],
},
autocommit=True,
)
def server_message(e):
"""The server's message in a driver error, without the request ID or the echoed statement."""
text = re.sub(r"\[req=0x[0-9a-f]+\]: ", "", str(e))
found = (re.search(r'message: "(.*?)", request_id', text, re.S)
or re.search(r"description: (.*)\)", text, re.S))
message = found.group(1) if found else text.strip().splitlines()[-1]
message = re.sub(r"^Plan query: '.*?' failed with: ", "", message, flags=re.S)
message = re.split(r"\\n|\n", message)[0].replace('\\"', '"')
return message.replace(BUCKET, "<bucket>")
def run(sql):
"""Run one statement. Print the rows it returns, or the error."""
try:
with conn.cursor() as cur:
cur.execute(sql)
rows = cur.fetchall() if cur.description else []
except (adbc_driver_manager.Error, OSError) as e:
print(f"{type(e).__name__}: {server_message(e)}")
else:
print(rows)
def plan(sql):
"""Print the plan without its ~N Rows lines: the table's row count, which can still be 0 just after an insert."""
with conn.cursor() as cur:
cur.execute("EXPLAIN " + sql)
steps = cur.fetchall()[0][1]
print("\n".join(line for line in steps.splitlines() if "Rows" not in line))
TABLE = f"{SCHEMA}.events"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, d DATE, ts TIMESTAMP, t TIME, tns TIMESTAMP_NS)")
run(f"INSERT INTO {TABLE} VALUES "
"(1, DATE '2026-02-28', TIMESTAMP '2026-02-28 23:15:00', TIME '23:15:00', "
"CAST('2026-02-28 23:15:00.123456789' AS TIMESTAMP_NS)), "
"(2, DATE '2026-03-01', TIMESTAMP '2026-03-01 08:30:45.123456', TIME '08:30:45.123456', "
"CAST('2026-03-01 08:30:45.123456' AS TIMESTAMP_NS)), "
"(3, DATE '2024-12-30', TIMESTAMP '2024-12-30 12:00:00', TIME '12:00:00', "
"CAST('2024-12-30 12:00:00' AS TIMESTAMP_NS)), "
"(4, NULL, NULL, NULL, NULL), "
"(5, DATE '1969-12-31', TIMESTAMP '1969-12-31 23:59:59.999999', TIME '23:59:59.999999', "
"CAST('1969-12-31 23:59:59.999999999' AS TIMESTAMP_NS))")
[]
[]
[(5,)]
What it does
epoch_us converts a value to whole microseconds, as a BIGINT:
| Argument type | What it returns | Example | Result |
|---|---|---|---|
TIMESTAMP |
Microseconds since 1970-01-01 00:00:00 | epoch_us(TIMESTAMP '2024-02-29 13:45:30.123456') |
1709214330123456 |
DATE |
Microseconds since 1970-01-01 at the date's midnight | epoch_us(DATE '2024-02-29') |
1709164800000000 |
TIME |
Microseconds since midnight | epoch_us(TIME '13:45:30.123456') |
49530123456 |
INTERVAL |
The interval's length in microseconds | epoch_us(INTERVAL '1 year') |
31104000000000 |
A TIMESTAMP holds microseconds, so epoch_us is exact for it, including before 1970: one microsecond before 1970 (row 5) is -1. On a TIMESTAMP_NS, the nanoseconds are dropped: epoch_us(TIMESTAMP_NS '2024-02-29 13:45:30.123456789') is 1709214330123456.
On a TIMESTAMP WITH TIME ZONE, the microseconds count from 1970-01-01 00:00:00 UTC, so the offset is applied: epoch_us(TIMESTAMPTZ '2024-02-29 13:45:30.123456+02') is 1709207130123456. On a TIME WITH TIME ZONE, the offset isn't applied: epoch_us(TIMETZ '13:45:30.123456+02:30') is 49530123456.
On an INTERVAL, epoch_us counts a month as 30 days and a year as 12 months (360 days). epoch counts a year as 365.25 days instead.
NULLs and edge cases
| Input | Result |
|---|---|
| A NULL value (row 4) | NULL |
DATE 'infinity', TIMESTAMP '-infinity' |
NULL |
DATE '0001-01-01' |
-62135596800000000 |
DATE '9999-12-31' |
253402214400000000 |
INTERVAL '1 year 2 months 3 days 04:05:06.789' |
36561906789000 |
INTERVAL '-1 year -2 months -3 days -04:05:06.789' |
-36561906789000 |
An untyped NULL: epoch_us(NULL) |
Error: Could not choose a best candidate function for the function call "epoch_us("NULL")" |
| A string | Error: Could not choose a best candidate function; cast it to TIMESTAMP first |
A number, boolean, BLOB or list |
Error: No function matches the given name and argument types, for example 'epoch_us(INTEGER_LITERAL)' |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run(f"SELECT id, epoch_us(ts) FROM {TABLE} WHERE id = 4")
run("SELECT epoch_us(DATE 'infinity'), epoch_us(TIMESTAMP '-infinity'), epoch_us(DATE '0001-01-01'), epoch_us(DATE '9999-12-31')")
run("SELECT epoch_us(INTERVAL '1 year 2 months 3 days 04:05:06.789'), epoch_us(INTERVAL '-1 year -2 months -3 days -04:05:06.789')")
run("SELECT epoch_us(NULL)")
run("SELECT epoch_us('2024-02-29 13:45:30')")
run("SELECT epoch_us(123)")
[(4, None)]
[(None, None, -62135596800000000, 253402214400000000)]
[(36561906789000, -36561906789000)]
InternalError: Binder Error: Could not choose a best candidate function for the function call "epoch_us("NULL")". In order to select one, please add explicit type casts.
InternalError: Binder Error: Could not choose a best candidate function for the function call "epoch_us(STRING_LITERAL)". In order to select one, please add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'epoch_us(INTEGER_LITERAL)'. You might need to add explicit type casts.
Examples
The microseconds since 1970 of each event's date and timestamp, and the microseconds since midnight of its time:
run(f"SELECT id, epoch_us(d), epoch_us(ts), epoch_us(t) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 1772236800000000, 1772320500000000, 83700000000), (2, 1772323200000000, 1772353845123456, 30645123456), (3, 1735516800000000, 1735560000000000, 43200000000), (4, None, None, None), (5, -86400000000, -1, 86399999999)]
The microseconds between two timestamps, as a whole number:
run("SELECT epoch_us(TIMESTAMP '2026-03-01 08:30:45.123456') - epoch_us(TIMESTAMP '2026-02-28 23:15:00')")
[(33345123456,)]
Keep only the events after a Unix time in microseconds. This works, but Make it faster has a faster way to write it:
run(f"SELECT id FROM {TABLE} WHERE epoch_us(ts) > 1772300000000000 ORDER BY id LIMIT 10")
[(1,), (2,)]
How it runs in VAST
The Query Engine runs on the cluster's CNodes, and a query on a table starts with a scan in VAST DataBase (VAST_SCAN). VAST DataBase can evaluate a comparison of a column with a constant, but not a function of a column, so epoch_us() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesepoch_us()for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesepoch_us()and keeps the rows that match. No rows are skipped in storage.
The plan of the WHERE example shows the FILTER step above the scan:
plan(f"SELECT id FROM {TABLE} WHERE epoch_us(ts) > 1772300000000000")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (epoch_us(ts) > │
│ 1772300000000000) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ ts │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Turn the number into a timestamp, not the column into a number.
ts > make_timestamp(1772300000000000)returns the same rows asepoch_us(ts) > 1772300000000000: make_timestamp with one argument is the reverse ofepoch_us. The Query Engine computes the constant once, so the comparison of the raw column is pushed down to VAST DataBase, and the plan has noFILTERstep:
plan(f"SELECT id FROM {TABLE} WHERE ts > make_timestamp(1772300000000000)")
run(f"SELECT id FROM {TABLE} WHERE ts > make_timestamp(1772300000000000) ORDER BY id LIMIT 10")
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
[(1,), (2,)]
- Use
epoch_us()in theSELECTlist, where it doesn't stop a filter from being pushed down.
Limitations and errors
- No reverse form.
epoch_us(1772353845123456)fails withNo function matches the given name and argument types 'epoch_us(INTEGER_LITERAL)'. To turn microseconds into a timestamp, use make_timestamp. - Microseconds at most. On a
TIMESTAMP_NS, the result has microsecond precision: row 5's1969-12-31 23:59:59.999999999gives0, whileepoch_nsgives-1. Use epoch_ns for nanoseconds. - One argument.
epoch_us(ts, ts)fails withNo function matches the given name and argument types 'epoch_us(TIMESTAMP, TIMESTAMP)'.
run("SELECT epoch_us(1772353845123456)")
run("SELECT make_timestamp(1772353845123456)")
run(f"SELECT id, epoch_us(tns), epoch_ns(tns) FROM {TABLE} ORDER BY id LIMIT 10")
run(f"SELECT epoch_us(ts, ts) FROM {TABLE} LIMIT 1")
InternalError: Binder Error: No function matches the given name and argument types 'epoch_us(INTEGER_LITERAL)'. You might need to add explicit type casts.
[(datetime.datetime(2026, 3, 1, 8, 30, 45, 123456),)]
[(1, 1772320500123456, 1772320500123456789), (2, 1772353845123456, 1772353845123456000), (3, 1735560000000000, 1735560000000000000), (4, None, None), (5, 0, -1)]
InternalError: Binder Error: No function matches the given name and argument types 'epoch_us(TIMESTAMP, TIMESTAMP)'. You might need to add explicit type casts.
Similar functions
| Function | Returns | Result for TIME '13:45:30.5' (epoch_us gives 49530500000) |
Result for INTERVAL '1 year 1 month 1 day' (epoch_us gives 33782400000000) |
|---|---|---|---|
| epoch | Seconds, as a DOUBLE with the fraction; a year is 365.25 days |
49530.5 |
34236000.0 |
| epoch_ms | Whole milliseconds | 49530500 |
33782400000 |
| epoch_ns | Whole nanoseconds | 49530500000000 |
(not compared) |
| make_timestamp with one argument | From microseconds to a TIMESTAMP |
(the reverse of epoch_us) |
(the reverse of epoch_us) |
run("SELECT epoch_us(TIME '13:45:30.5'), epoch(TIME '13:45:30.5'), epoch_ms(TIME '13:45:30.5'), epoch_ns(TIME '13:45:30.5')")
run("SELECT epoch_us(INTERVAL '1 year 1 month 1 day'), epoch(INTERVAL '1 year 1 month 1 day'), "
"epoch_ms(INTERVAL '1 year 1 month 1 day')")
[(49530500000, 49530.5, 49530500, 49530500000000)]
[(33782400000000, 34236000.0, 33782400000)]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- epoch, epoch_ms and epoch_ns: seconds, milliseconds and nanoseconds.
- make_timestamp: from microseconds back to a timestamp.
- Date and time functions: all the date and time functions.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.