Applies to VAST Cluster 5.5.
Functions › Numeric and math functions › Sign and rounding. Returns the absolute value of a number: the number without its sign, in the same type. The prefix operator @ is the same function.
Syntax
abs(x)
@ x
| Argument | Type | Description |
|---|---|---|
x |
any number: TINYINT, SMALLINT, INTEGER, BIGINT, HUGEINT, the unsigned integer types, DECIMAL, FLOAT or DOUBLE |
The value to take the absolute value of. |
Returns: the type of x.
Aliases: the prefix operator @.
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_abs. 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 four stock changes: a change in price (DOUBLE), a change in quantity (INTEGER) and an amount (DECIMAL). The last row has no values:
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_abs"'
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}.changes"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, delta DOUBLE, qty INTEGER, amount DECIMAL(10, 2))")
run(f"INSERT INTO {TABLE} VALUES "
"(1, 2.5, 12, 2.45), "
"(2, -1.25, -18, -1.35), "
"(3, 0.0, 0, 0.00), "
"(4, NULL, NULL, NULL)")
[]
[]
[(4,)]
What it does
abs(x) returns x for a positive value or zero, and -x for a negative value. The result has the type of x: abs(-5) is the INTEGER 5, abs(-2.45) is the DECIMAL(3,2) 2.45, and the absolute value of a TINYINT column is a TINYINT. On the unsigned integer types, which have no negative values, it returns the value unchanged.
On DOUBLE and FLOAT values, abs clears the sign of a negative zero (abs(-0.0::DOUBLE) is 0.0, and signbit() of it is false), turns -inf into inf, and leaves nan as nan.
@ x is the same function. @ applies to the whole expression after it: @ -5 + 1 is @ (-5 + 1), which is 4. Write abs(-5) + 1 when you mean the absolute value plus one.
NULLs and edge cases
| Input | Result |
|---|---|
x is NULL |
NULL |
abs(-0.0::DOUBLE) |
0.0, without the sign |
abs('-inf'::DOUBLE) |
inf |
abs('nan'::DOUBLE) |
nan |
abs(-2.45) |
2.45, a DECIMAL(3,2) |
abs(-5) |
5, an INTEGER |
@ -5 + 1 |
4: @ applies to -5 + 1 |
The smallest value of a signed integer type: abs((-128)::TINYINT) |
Error: Overflow on abs(-128) |
abs(-3::UTINYINT) |
253: the cast comes first, and -(3::UTINYINT) wraps around to 253 |
A string, even '12' |
Error: Could not choose a best candidate function for the function call "abs(STRING_LITERAL)" |
A date, boolean, BLOB, list or interval |
Error: No function matches the given name and argument types |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run("SELECT abs(NULL), abs(-0.0::DOUBLE), signbit(abs(-0.0::DOUBLE)), abs('-inf'::DOUBLE), abs('nan'::DOUBLE)")
run("SELECT abs(-2.45), typeof(abs(-2.45)), abs(-5), typeof(abs(-5)), @ -5, @ -5 + 1")
run("SELECT abs((-128)::TINYINT)")
run("SELECT abs(-3::UTINYINT)")
run("SELECT abs('12')")
run("SELECT abs(DATE '2026-10-16')")
[(None, 0.0, False, inf, nan)]
[(Decimal('2.45'), 'DECIMAL(3,2)', 5, 'INTEGER', 5, 4)]
OSError: Overflow on abs(-128)
[(253,)]
InternalError: Binder Error: Could not choose a best candidate function for the function call "abs(STRING_LITERAL)". In order to select one, please add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'abs(DATE)'. You might need to add explicit type casts.
Examples
The size of each change, for each column type. Each result keeps the type of its column:
run(f"SELECT id, abs(delta), @ delta, abs(qty), abs(amount) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 2.5, 2.5, 12, Decimal('2.45')), (2, 1.25, 1.25, 18, Decimal('1.35')), (3, 0.0, 0.0, 0, Decimal('0.00')), (4, None, None, None, None)]
Keep the changes of more than 1 in either direction:
run(f"SELECT id, delta FROM {TABLE} WHERE abs(delta) > 1 ORDER BY id LIMIT 10")
[(1, 2.5), (2, -1.25)]
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 abs() and @ are always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesabs()for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesabs()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 abs(delta) > 1")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (abs(delta) > 1.0) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ delta │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Write the condition on the raw column.
abs(delta) > 1is the same asdelta > 1 OR delta < -1, and that form is pushed down to VAST DataBase, so rows that don't match never leave storage. Its plan has noFILTERstep:
plan(f"SELECT id FROM {TABLE} WHERE delta > 1 OR delta < -1")
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
- Use
BETWEENfor "close to zero".abs(qty) <= 1runs in aFILTERstep;qty BETWEEN -1 AND 1is pushed down. - Combine it with a pushed-down condition. In
id > 1 AND abs(delta) > 1, theFILTERstep holds onlyabs(delta) > 1:id > 1is evaluated in VAST DataBase, soabs()runs only on the rows that pass it.
Limitations and errors
- The smallest value of a signed integer type overflows. Its absolute value doesn't fit the type:
abs((-128)::TINYINT)fails withOverflow on abs(-128), and the same happens for the smallestSMALLINT,INTEGER,BIGINTandHUGEINT. One such row stops the whole query. Cast the value to a wider type first. - A cast binds more tightly than the minus sign. In
-3::UTINYINTthe cast comes first, and in-9223372036854775808::BIGINTthe number 9223372036854775808 is cast toBIGINTbefore the minus, which fails withType INT128 with value 9223372036854775808 can't be cast because the value is out of range for the destination type INT64. Put the negative number in parentheses:(-128)::TINYINT. - Strings aren't converted.
abs('12')fails withCould not choose a best candidate function. Cast the string to a number first. - Numbers only. A date, a boolean, a
BLOB, a list or an interval fails withNo function matches the given name and argument types.
run("SELECT abs((-128)::TINYINT)")
run("SELECT abs(CAST((-128)::TINYINT AS SMALLINT)), abs(CAST('12' AS INTEGER))")
OSError: Overflow on abs(-128)
[(128, 12)]
Similar functions
| Function | Returns | Result for -5 |
Result for -0.0::DOUBLE |
|---|---|---|---|
abs(x), @ x |
the value without its sign, in the type of x |
5 |
0.0 |
| sign | -1, 0 or 1, as a TINYINT |
-1 |
0 |
| signbit | whether the sign bit is set, as a BOOLEAN |
true |
true |
run("SELECT abs(-5), @ -5, sign(-5), signbit(-5), abs(-0.0::DOUBLE), sign(-0.0::DOUBLE), signbit(-0.0::DOUBLE)")
run("SELECT abs(-5) + 1, @ -5 + 1")
[(5, 5, -1, True, 0.0, 0, True)]
[(6, 4)]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- Numeric and math functions: all the numeric functions.
- sign and signbit.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.