Applies to VAST Cluster 5.5.
Functions › Date and time functions › Epoch values. Returns the number of whole nanoseconds since 1970-01-01 00:00:00 of a date or timestamp, as a BIGINT. On a time of day it returns the nanoseconds since midnight, and on an interval its length in nanoseconds. It's the only epoch function that keeps the nanoseconds of a TIMESTAMP_NS.
Syntax
epoch_ns(value)
| Argument | Type | Description |
|---|---|---|
value |
DATE, TIMESTAMP_NS, TIMESTAMP (also TIMESTAMP_MS, TIMESTAMP_S), TIMESTAMP WITH TIME ZONE, TIME, TIME WITH TIME ZONE or INTERVAL |
The value to convert to nanoseconds. |
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_ns. 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_ns"'
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_ns converts a value to whole nanoseconds, as a BIGINT:
| Argument type | What it returns | Example | Result |
|---|---|---|---|
TIMESTAMP_NS |
Nanoseconds since 1970-01-01 00:00:00, exactly | epoch_ns(TIMESTAMP_NS '2024-02-29 13:45:30.123456789') |
1709214330123456789 |
TIMESTAMP |
Nanoseconds since 1970-01-01 00:00:00; the last three digits are 000 |
epoch_ns(TIMESTAMP '2024-02-29 13:45:30.123456') |
1709214330123456000 |
DATE |
Nanoseconds since 1970-01-01 at the date's midnight | epoch_ns(DATE '2024-02-29') |
1709164800000000000 |
TIME |
Nanoseconds since midnight | epoch_ns(TIME '13:45:30.123456') |
49530123456000 |
INTERVAL |
The interval's length in nanoseconds | epoch_ns(INTERVAL '1 year') |
31104000000000000 |
Values before 1970 are negative: row 5's tns, one nanosecond before 1970, gives -1, and its ts, one microsecond before 1970, gives -1000.
A BIGINT of nanoseconds covers a limited span around 1970: epoch_ns works on 2262-04-11 23:47:16 and on 1677-09-21 00:12:44, but fails on 2262-04-12 00:00:00 and on 1677-09-21 00:00:00 (see Limitations and errors).
On a TIMESTAMP WITH TIME ZONE, the nanoseconds count from 1970-01-01 00:00:00 UTC, so the offset is applied: epoch_ns(TIMESTAMPTZ '2024-02-29 13:45:30.123456+02') is 1709207130123456000. On a TIME WITH TIME ZONE, the offset isn't applied. On an INTERVAL, a month is 30 days and a year is 12 months (360 days).
NULLs and edge cases
| Input | Result |
|---|---|
| A NULL value (row 4) | NULL |
DATE 'infinity', TIMESTAMP '-infinity' |
NULL |
TIMESTAMP '2262-04-11 23:47:16' |
9223372036000000000 |
TIMESTAMP '1677-09-21 00:12:44' |
-9223372036000000000 |
INTERVAL '-1 year -2 months -3 days -04:05:06.789' |
-36561906789000000 |
TIMESTAMP '2262-04-12 00:00:00' (past the range) |
Error: a Conversion Error that starts Could not convert Timestamp(US) to Timestamp(NS |
DATE '0001-01-01' (outside the range) |
Error: Could not convert DATE (0001-01-01) to nanoseconds |
An untyped NULL: epoch_ns(NULL) |
Error: Could not choose a best candidate function for the function call "epoch_ns("NULL")" |
| A string | Error: Could not choose a best candidate function; cast it to TIMESTAMP_NS or TIMESTAMP first |
A number, boolean, BLOB or list |
Error: No function matches the given name and argument types, for example 'epoch_ns(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_ns(ts), epoch_ns(tns) FROM {TABLE} WHERE id = 4")
run("SELECT epoch_ns(DATE 'infinity'), epoch_ns(TIMESTAMP '-infinity'), epoch_ns(TIMESTAMP '2262-04-11 23:47:16'), "
"epoch_ns(TIMESTAMP '1677-09-21 00:12:44'), epoch_ns(INTERVAL '-1 year -2 months -3 days -04:05:06.789')")
run("SELECT epoch_ns(TIMESTAMP '2262-04-12 00:00:00')")
run("SELECT epoch_ns(DATE '0001-01-01')")
run("SELECT epoch_ns(NULL)")
run("SELECT epoch_ns('2024-02-29 13:45:30')")
run("SELECT epoch_ns(123)")
[(4, None, None)]
[(None, None, 9223372036000000000, -9223372036000000000, -36561906789000000)]
InternalError: Conversion Error: Could not convert Timestamp(US) to Timestamp(NS)
InternalError: Conversion Error: Could not convert DATE (0001-01-01) to nanoseconds
InternalError: Binder Error: Could not choose a best candidate function for the function call "epoch_ns("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_ns(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_ns(INTEGER_LITERAL)'. You might need to add explicit type casts.
Examples
The nanoseconds since 1970 of each event's timestamp and nanosecond timestamp. Only tns keeps the nanoseconds of row 1:
run(f"SELECT id, epoch_ns(ts), epoch_ns(tns) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 1772320500000000000, 1772320500123456789), (2, 1772353845123456000, 1772353845123456000), (3, 1735560000000000000, 1735560000000000000), (4, None, None), (5, -1000, -1)]
The nanoseconds since midnight of each event's time, and since 1970 of its date:
run(f"SELECT id, epoch_ns(t), epoch_ns(d) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 83700000000000, 1772236800000000000), (2, 30645123456000, 1772323200000000000), (3, 43200000000000, 1735516800000000000), (4, None, None), (5, 86399999999000, -86400000000000)]
Keep only the events after a Unix time in nanoseconds. This works, but Make it faster has a faster way to write it:
run(f"SELECT id FROM {TABLE} WHERE epoch_ns(ts) > 1772300000000000000 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_ns() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesepoch_ns()for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesepoch_ns()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_ns(ts) > 1772300000000000000")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (epoch_ns(ts) > │
│ 1772300000000000000) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ ts │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Turn the number into a timestamp, not the column into a number. On a
TIMESTAMPcolumn,ts > make_timestamp(1772300000000000)(the same time in microseconds) returns the same rows asepoch_ns(ts) > 1772300000000000000. 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_ns()in theSELECTlist, where it doesn't stop a filter from being pushed down.
Limitations and errors
- About 1677 to 2262 only. A timestamp or date outside that range fails with a
Conversion Error:Could not convert Timestamp(US) to Timestamp(NSfor a timestamp,Could not convert DATE (...) to nanosecondsfor a date. Use epoch_us for values outside it. - No reverse form.
epoch_ns(1772353845123456789)fails withNo function matches the given name and argument types 'epoch_ns(INTEGER_LITERAL)'. To turn nanoseconds into a timestamp, use make_timestamp_ns. - One argument.
epoch_ns(ts, ts)fails withNo function matches the given name and argument types.
run("SELECT epoch_ns(DATE '2300-01-01')")
run("SELECT epoch_us(DATE '2300-01-01')")
run("SELECT epoch_ns(1772353845123456789)")
run("SELECT CAST(make_timestamp_ns(1772353845123456789) AS VARCHAR)")
run(f"SELECT epoch_ns(ts, ts) FROM {TABLE} LIMIT 1")
InternalError: Conversion Error: Could not convert DATE (2300-01-01) to nanoseconds
[(10413792000000000,)]
InternalError: Binder Error: No function matches the given name and argument types 'epoch_ns(INTEGER_LITERAL)'. You might need to add explicit type casts.
[('2026-03-01 08:30:45.123456789',)]
InternalError: Binder Error: No function matches the given name and argument types 'epoch_ns(TIMESTAMP, TIMESTAMP)'. You might need to add explicit type casts.
Similar functions
| Function | Returns | Result for row 1's tns, 2026-02-28 23:15:00.123456789 (epoch_ns gives 1772320500123456789) |
|---|---|---|
| epoch_us | Whole microseconds; the nanoseconds are dropped | 1772320500123456 |
| epoch_ms | Whole milliseconds | 1772320500123 |
| epoch | Seconds, as a DOUBLE with the fraction to the microsecond |
1772320500.123456 |
| make_timestamp_ns | From nanoseconds to a TIMESTAMP_NS: the reverse of epoch_ns |
(the reverse) |
run(f"SELECT epoch_ns(tns), epoch_us(tns), epoch_ms(tns), epoch(tns) FROM {TABLE} WHERE id = 1")
[(1772320500123456789, 1772320500123456, 1772320500123, 1772320500.123456)]
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_us: seconds, milliseconds and microseconds.
- make_timestamp_ns: from nanoseconds 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.