Applies to VAST Cluster 5.5.
Functions › Numeric and math functions › Floating-point values. Returns true if x is NaN (not a number), the floating-point value that results from operations such as 0 / 0, and false for any other number.
Syntax
isnan(x)
| Argument | Type | Description |
|---|---|---|
x |
DOUBLE or FLOAT |
The value to test. An integer or DECIMAL literal is accepted, and is never NaN. |
Returns: BOOLEAN.
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_numeric_isnan. 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 seven sensor readings: ordinary values, NaN, both infinities, negative zero and, in row 6, no value:
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_numeric_isnan"'
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}.readings"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, x DOUBLE)")
run(f"INSERT INTO {TABLE} VALUES "
"(1, 2.5), (2, 'nan'::DOUBLE), (3, 'inf'::DOUBLE), (4, '-inf'::DOUBLE), "
"(5, '-0.0'::DOUBLE), (6, NULL), (7, 1.0)")
[]
[]
[(7,)]
What it does
NaN is a special floating-point value, separate from NULL. You get it from operations with no numeric answer, such as 0 / 0, fmod(1, 0) or pow(-27, 1.0 / 3), and you can write it as 'nan'::DOUBLE (or 'NaN'::DOUBLE). isnan(x) is true only for NaN: infinity, negative zero and every ordinary number give false, and NULL gives NULL.
In the VAST Query Engine, NaN compares as a value of its own, larger than any other number:
| Comparison | Result |
|---|---|
'nan'::DOUBLE = 'nan'::DOUBLE |
true |
'nan'::DOUBLE <> 'nan'::DOUBLE |
false |
'nan'::DOUBLE > 1e308 |
true |
'nan'::DOUBLE > 'inf'::DOUBLE |
true |
So max() of a column that holds a NaN is NaN, and a sum that includes one is NaN. Exclude NaN with NOT isnan(x) when you aggregate (see Examples).
run("SELECT 'nan'::DOUBLE = 'nan'::DOUBLE, 'nan'::DOUBLE <> 'nan'::DOUBLE, "
"'nan'::DOUBLE > 1e308, 'nan'::DOUBLE > 'inf'::DOUBLE")
[(True, False, True, True)]
NULLs and edge cases
| Input | Result |
|---|---|
NULL, NULL::FLOAT |
NULL: NULL isn't NaN |
'nan'::DOUBLE, 'nan'::FLOAT |
true |
0 / 0 |
true |
'inf'::DOUBLE, '-inf'::DOUBLE, 1 / 0, 1e308 * 10 |
false |
-0.0::DOUBLE, 1.0::DOUBLE, 1::FLOAT |
false |
1, 2.45, 9223372036854775807 (integer and DECIMAL literals) |
false |
'nan' or any other string literal |
Error: Could not choose a best candidate function for the function call "isnan(STRING_LITERAL)"; cast it: 'nan'::DOUBLE |
A date, timestamp, BLOB, boolean, list or interval |
Error: No function matches the given name and argument types, for example 'isnan(DATE)' |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run("SELECT isnan(NULL), isnan(NULL::FLOAT), isnan('nan'::DOUBLE), isnan('nan'::FLOAT), isnan(0 / 0)")
run("SELECT isnan('inf'::DOUBLE), isnan('-inf'::DOUBLE), isnan(1 / 0), isnan(1e308 * 10)")
run("SELECT isnan(-0.0::DOUBLE), isnan(1.0::DOUBLE), isnan(1::FLOAT), isnan(1), isnan(2.45), "
"isnan(9223372036854775807), typeof(isnan(1))")
run("SELECT isnan('nan')")
run("SELECT isnan(DATE '2026-10-16')")
[(None, None, True, True, True)]
[(False, False, False, False)]
[(False, False, False, False, False, False, 'BOOLEAN')]
InternalError: Binder Error: Could not choose a best candidate function for the function call "isnan(STRING_LITERAL)". In order to select one, please add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'isnan(DATE)'. You might need to add explicit type casts.
Examples
Test each reading. Row 6 has no value, so its result is NULL:
run(f"SELECT id, x, isnan(x) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 2.5, False), (2, nan, True), (3, inf, False), (4, -inf, False), (5, -0.0, False), (6, None, None), (7, 1.0, False)]
Find the NaN readings:
run(f"SELECT id FROM {TABLE} WHERE isnan(x) ORDER BY id LIMIT 10")
[(2,)]
Aggregate with and without the NaN reading. With it, max() and sum() are NaN. Without it, max() is inf, but sum() is still NaN, because the column also holds inf and -inf, and their sum is NaN; to leave out the infinities too, use isfinite:
run(f"SELECT min(x), max(x), sum(x), count(x) FROM {TABLE}")
run(f"SELECT min(x), max(x), sum(x), count(x) FROM {TABLE} WHERE NOT isnan(x)")
[(-inf, nan, nan, 6)]
[(-inf, inf, nan, 5)]
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 isnan() is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesisnan()for each row it returns. - In
WHERE,isnan(x)andNOT isnan(x)alike, the scan sends the column for every row, and aFILTERstep in the Query Engine computesisnan()and keeps the rows that match.
The plan of the WHERE example shows the FILTER step above the scan:
plan(f"SELECT id FROM {TABLE} WHERE isnan(x)")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ isnan(x) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ x │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Compare with NaN directly.
x = 'nan'::DOUBLEis a comparison of the raw column with a constant, so it's pushed down to VAST DataBase and the plan has noFILTERstep. The second line shows the rows it returns:
plan(f"SELECT id FROM {TABLE} WHERE x = 'nan'::DOUBLE")
run(f"SELECT id FROM {TABLE} WHERE x = 'nan'::DOUBLE ORDER BY id LIMIT 10")
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
[(2,)]
- Store NULL for a missing value, not NaN. Aggregates skip NULLs (
count(x)above doesn't count row 6) but not NaN, so a NULL keepsmax()andsum()meaningful without a filter.
Limitations and errors
- Floating-point values only. The function takes
DOUBLEandFLOAT. A date, timestamp,BLOB, boolean, list or interval fails withNo function matches the given name and argument types. For the special date values'infinity'and'-infinity', use isinf or isfinite. - A string literal must be cast.
isnan('nan')fails withCould not choose a best candidate function for the function call "isnan(STRING_LITERAL)", because the literal could be aDOUBLEor aFLOAT. Writeisnan('nan'::DOUBLE). - Other names aren't available.
is_nan()and anan()constant don't exist (Scalar Function with name is_nan does not exist!); useisnan()and'nan'::DOUBLE:
run("SELECT is_nan(1.0)")
run("SELECT nan()")
run("SELECT 'nan'::DOUBLE, 'NaN'::DOUBLE")
InternalError: Catalog Error: Scalar Function with name is_nan does not exist!
InternalError: Catalog Error: Scalar Function with name nan does not exist!
[(nan, nan)]
Similar functions
| Function | nan |
inf |
-inf |
-0.0 |
1.0 |
NULL |
|---|---|---|---|---|---|---|
isnan(x) |
true |
false |
false |
false |
false |
NULL |
isinf(x) |
false |
true |
true |
false |
false |
NULL |
isfinite(x) |
false |
false |
false |
true |
true |
NULL |
signbit(x) |
false |
false |
true |
true |
false |
NULL |
run("SELECT x, isnan(x), isinf(x), isfinite(x), signbit(x) FROM (VALUES ('nan'::DOUBLE), ('inf'::DOUBLE), "
"('-inf'::DOUBLE), (-0.0::DOUBLE), (1.0::DOUBLE), (NULL::DOUBLE)) v(x)")
[(nan, True, False, False, False), (inf, False, True, False, False), (-inf, False, True, False, True), (-0.0, False, False, True, True), (1.0, False, False, True, False), (None, None, None, None, None)]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- Numeric and math functions: isnan, isinf and isfinite.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.