Applies to VAST Cluster 5.5.
Functions › Numeric and math functions › Angles and constants. Converts an angle from radians to degrees: degrees(x) is x * 180 / pi(). radians does the opposite.
Syntax
degrees(x)
| Argument | Type | Description |
|---|---|---|
x |
DOUBLE (any number) |
An angle in radians. Integers, DECIMAL and FLOAT values are converted to DOUBLE. |
Returns: DOUBLE.
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_degrees. 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 headings in radians. Row 3 has no heading:
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_degrees"'
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}.headings"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, rad DOUBLE)")
run(f"INSERT INTO {TABLE} VALUES (1, 2.5), (2, -1.25), (3, NULL), (4, 0.0), (5, 1.0)")
[]
[]
[(5,)]
What it does
degrees(x) multiplies an angle in radians by 180/π. It doesn't wrap the result into a range: degrees(-pi()) is -180.0. Use it to show the result of the inverse trigonometric functions, such as asin or atan2, in degrees.
The result is rounded to the nearest DOUBLE, so it can be off in the last digit: degrees(asin(0.5)) is 30.000000000000004, not 30.0. Round it if you display it.
NULLs and edge cases
| Input | Result |
|---|---|
NULL |
NULL |
pi(), pi() / 2, -pi() |
180.0, 90.0, -180.0 |
1 |
57.29577951308232 |
radians(45) |
45.0 |
-0.0::DOUBLE |
-0.0 |
5e-324::DOUBLE (the smallest DOUBLE above 0) |
2.8e-322 |
1e308 |
inf: the result is too large for a DOUBLE, and there's no error |
'inf'::DOUBLE, '-inf'::DOUBLE, 'nan'::DOUBLE |
inf, -inf, nan |
'12' (a string) |
Converted to DOUBLE: 687.5493541569879 |
'abc' |
Error: Could not convert string 'abc' to DOUBLE |
A BLOB, date, boolean, list or interval |
Error: No function matches the given name and argument types, for example 'degrees(DATE)' |
An INTEGER, FLOAT or DECIMAL value |
Converted; the result is DOUBLE |
The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:
run("SELECT degrees(NULL), degrees(pi()), degrees(pi() / 2), degrees(-pi()), degrees(1), degrees(radians(45))")
run("SELECT degrees(-0.0::DOUBLE), degrees(5e-324::DOUBLE), degrees(1e308)")
run("SELECT degrees('inf'::DOUBLE), degrees('-inf'::DOUBLE), degrees('nan'::DOUBLE), degrees('12')")
run("SELECT typeof(degrees(1::INTEGER)), typeof(degrees(0.5::FLOAT)), typeof(degrees(0.5))")
run("SELECT degrees('abc')")
run("SELECT degrees(DATE '2026-10-16')")
[(None, 180.0, 90.0, -180.0, 57.29577951308232, 45.0)]
[(-0.0, 2.8e-322, inf)]
[(inf, -inf, nan, 687.5493541569879)]
[('DOUBLE', 'DOUBLE', 'DOUBLE')]
OSError: Could not convert string 'abc' to DOUBLE
InternalError: Binder Error: No function matches the given name and argument types 'degrees(DATE)'. You might need to add explicit type casts.
Examples
Each heading in degrees, and rounded to one decimal place:
run(f"SELECT id, rad, degrees(rad), round(degrees(rad), 1) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 2.5, 143.2394487827058, 143.2), (2, -1.25, -71.6197243913529, -71.6), (3, None, None, None), (4, 0.0, 0.0, 0.0), (5, 1.0, 57.29577951308232, 57.3)]
Keep the headings of more than 90 degrees:
run(f"SELECT id FROM {TABLE} WHERE degrees(rad) > 90 ORDER BY id LIMIT 10")
[(1,)]
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 degrees() of a column is always computed by the Query Engine:
- In the
SELECTlist, the scan reads the column and the Query Engine computesdegrees()for each row it returns. - In
WHERE, the scan sends the column for every row, and aFILTERstep in the Query Engine computesdegrees()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 degrees(rad) > 90")
┌───────────────────────────┐
│ PROJECTION │
│ ──────────────────── │
│ #1 │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ FILTER │
│ ──────────────────── │
│ (degrees(rad) > 90.0) │
│ │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ │
│ Projections: │
│ rad │
│ id │
│ │
└───────────────────────────┘
Make it faster
- Convert the constant, not the column.
rad > radians(90)keeps the same rows asdegrees(rad) > 90.radians(90)is computed once, before the scan, so the condition is pushed down to VAST DataBase and the plan has noFILTERstep:
plan(f"SELECT id FROM {TABLE} WHERE rad > radians(90)")
run(f"SELECT id FROM {TABLE} WHERE rad > radians(90) ORDER BY id LIMIT 10")
┌───────────────────────────┐
│ VAST_SCAN │
│ ──────────────────── │
│ Function: VAST_SCAN │
│ Projections: id │
│ │
└───────────────────────────┘
[(1,)]
- Store angles in the unit you filter by. If you query in degrees most of the time, store degrees and convert in the
SELECTlist.
Limitations and errors
- No range check. A result too large for a
DOUBLEisinf, with no error. Infinity and NaN pass through unchanged. - One argument, numbers only. A second argument, a date, a boolean, a
BLOB, a list or an interval fails withNo function matches the given name and argument types. A string is converted toDOUBLEfirst, and fails withCould not convert string ... to DOUBLEif it isn't a number. - Rounding. Results are
DOUBLEvalues, so conversions such asdegrees(asin(0.5))can be off in the last digit. Use round to display them.
Similar functions
| Function | Converts | Result for its argument |
|---|---|---|
degrees(x) |
radians to degrees | degrees(pi()) is 180.0 |
radians(x) |
degrees to radians | radians(180) is 3.141592653589793 |
x * 180 / pi() |
the same formula as degrees, written out |
|
atan2(y, x) |
a point to its angle in radians; wrap it in degrees() for degrees |
run("SELECT degrees(pi()), radians(180), 1 * 180 / pi(), degrees(1), radians(degrees(1)), degrees(atan2(1, 1))")
[(180.0, 3.141592653589793, 57.29577951308232, 57.29577951308232, 1.0, 45.0)]
Clean up:
run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]
See also
- Numeric and math functions: pi, degrees and radians.
- Where filters run and pushdowns.
- EXPLAIN: how to read a plan.