Welcome to the new DOCS.vastdata.com. We've added new menus and a version selector, plus new persona and resource pages to help customers get started. Looking for a previous article? Use the docs search.

exp

Prev Next

Applies to VAST Cluster 5.5.

Functions › Numeric and math functions › Powers and logarithms. Returns e (2.718281828459045…) raised to the power x, as a DOUBLE. It's the inverse of ln.

Syntax

exp(x)
Argument Type Description
x DOUBLE (any number) The power to raise e to.

Returns: DOUBLE, whatever the argument type.

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_exp. 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 model scores, one of them very large. 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_exp"'

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}.scores"
run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}")
run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, score DOUBLE)")
run(f"INSERT INTO {TABLE} VALUES "
    "(1, 0.0), "
    "(2, 2.0), "
    "(3, -1.5), "
    "(4, 1000.0), "
    "(5, NULL)")
[]
[]
[(5,)]

What it does

exp(x) computes ex: exp(0) is 1.0, exp(1) is 2.718281828459045 and exp(-1) is 0.36787944117144233. The argument is converted to DOUBLE first, so integers, DECIMAL and FLOAT values all work and all give a DOUBLE; so does a string that holds a number.

The result grows fast. It doesn't fail when it's out of range: a result too big for a DOUBLE is infinity, and one too small is 0.

  • exp(709) is 8.218407461554972e+307; from about exp(709.8), the result is inf.
  • exp(-745) is 5e-324, the smallest DOUBLE above 0; exp(-746) is 0.0.
  • exp('inf'::DOUBLE) is inf, exp('-inf'::DOUBLE) is 0.0, and exp('nan'::DOUBLE) is nan.

exp and ln undo each other, up to rounding: ln(exp(2)) is 2.0, and exp(ln(10)) is 10.000000000000002.

NULLs and edge cases

Input Result
NULL NULL
exp(0) 1.0
exp(1) 2.718281828459045
exp(-1) 0.36787944117144233
exp(709) 8.218407461554972e+307
exp(710), exp(1000) inf
exp(-745) 5e-324
exp(-746), exp(-1000) 0.0
exp('inf'::DOUBLE), exp('-inf'::DOUBLE), exp('nan'::DOUBLE) inf, 0.0, nan
exp(ln(10)) 10.000000000000002
exp(1::FLOAT) a DOUBLE
exp('12') 162754.79141900392
exp('abc') Error: Could not convert string 'abc' to DOUBLE
exp(1, 2) Error: No function matches the given name and argument types 'exp(INTEGER_LITERAL, INTEGER_LITERAL)'
A boolean, date, interval, BLOB or list Error: No function matches the given name and argument types, for example 'exp(BOOLEAN)'

The same cases, as the cluster returns them. They use the table and helpers from Set up the examples:

run("SELECT exp(NULL), exp(0), exp(1), exp(-1), exp(709), exp(710), exp(1000)")
run("SELECT exp(-745), exp(-746), exp(-1000), exp('inf'::DOUBLE), exp('-inf'::DOUBLE), exp('nan'::DOUBLE)")
run("SELECT exp(ln(10)), typeof(exp(1::FLOAT))")
run(f"SELECT exp('12') FROM {TABLE} LIMIT 1")
run(f"SELECT exp('abc') FROM {TABLE} LIMIT 1")
run("SELECT exp(1, 2)")
run("SELECT exp(true)")
[(None, 1.0, 2.718281828459045, 0.36787944117144233, 8.218407461554972e+307, inf, inf)]
[(5e-324, 0.0, 0.0, inf, 0.0, nan)]
[(10.000000000000002, 'DOUBLE')]
[(162754.79141900392,)]
OSError: Could not convert string 'abc' to DOUBLE
InternalError: Binder Error: No function matches the given name and argument types 'exp(INTEGER_LITERAL, INTEGER_LITERAL)'. You might need to add explicit type casts.
InternalError: Binder Error: No function matches the given name and argument types 'exp(BOOLEAN)'. You might need to add explicit type casts.

Examples

e to the power of each score, and the logistic function 1 / (1 + exp(-score)), which maps any score to a probability between 0 and 1. A score of 1000 overflows exp(score) to infinity, while exp(-score) goes to 0 and the probability to 1:

run(f"SELECT id, exp(score), 1 / (1 + exp(-score)) FROM {TABLE} ORDER BY id LIMIT 10")
[(1, 1.0, 0.5), (2, 7.38905609893065, 0.8807970779778823), (3, 0.22313016014842982, 0.18242552380635635), (4, inf, 1.0), (5, None, None)]

Keep only the scores whose exponential is above 1:

run(f"SELECT id, score FROM {TABLE} WHERE exp(score) > 1 ORDER BY id LIMIT 10")
[(2, 2.0), (4, 1000.0)]

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 exp() of a column is always computed by the Query Engine:

  • In the SELECT list, the scan reads the column and the Query Engine computes exp() for each row it returns.
  • In WHERE, the scan sends the column for every row, and a FILTER step in the Query Engine computes exp() 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 exp(score) > 1")
┌───────────────────────────┐
│         PROJECTION        │
│    ────────────────────   │
│             #1            │
│                           │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│           FILTER          │
│    ────────────────────   │
│     (exp(score) > 1.0)    │
│                           │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         VAST_SCAN         │
│    ────────────────────   │
│    Function: VAST_SCAN    │
│                           │
│        Projections:       │
│           score           │
│             id            │
│                           │
└───────────────────────────┘

Make it faster

  • Take the logarithm of the constant instead. A larger number always has a larger exponential, so exp(score) > 1 is the same as score > 0, and exp(score) > c is the same as score > ln(c) for a positive c. A comparison of the raw column with a constant is pushed down to VAST DataBase, so rows that don't match never leave storage. Its plan has no FILTER step:
plan(f"SELECT id FROM {TABLE} WHERE score > 0")
┌───────────────────────────┐
│         VAST_SCAN         │
│    ────────────────────   │
│    Function: VAST_SCAN    │
│      Projections: id      │
│                           │
└───────────────────────────┘
  • Store the result if you filter on it often. Compute it when you write the rows and keep it in its own column.
  • Combine it with a pushed-down condition. In id > 100 AND exp(score) > 1, id > 100 is still evaluated in VAST DataBase, so exp() runs only on the rows that pass it.

Limitations and errors

  • Numbers only, one argument. A boolean, date, interval, BLOB or list fails with No function matches the given name and argument types; the error lists the one form exp accepts, exp(DOUBLE) -> DOUBLE. For a power of another base, use pow. A string is converted when the query runs, and one that isn't a number fails with Could not convert string 'abc' to DOUBLE.
  • Overflow gives infinity, not an error. Test for it with isinf, or keep the input below about 709.
  • No e(), exp2 or expm1. They fail with Scalar Function with name ... does not exist!. Use exp(1) for e, pow(2, x) for 2x, and exp(x) - 1 for expm1(x).
run(f"SELECT id, isinf(exp(score)) FROM {TABLE} ORDER BY id LIMIT 10")
run("SELECT e()")
run("SELECT exp(1), pow(2, 3), exp(1) - 1")
[(1, False), (2, False), (3, False), (4, True), (5, None)]
InternalError: Catalog Error: Scalar Function with name e does not exist!
[(2.718281828459045, 8.0, 1.718281828459045)]

Similar functions

Function Computes Result
exp(2) e2 7.38905609893065
pow (exp(1), 2) the same, through a power; rounded differently 7.3890560989306495
ln the inverse of exp: ln(exp(2)) 2.0
exp(y * ln(x)) x to the power y, like pow: exp(10 * ln(2)) 1024.0
sinh (exp(x) - exp(-x)) / 2 sinh(1) is 1.1752011936438014
run("SELECT exp(1), pow(exp(1), 2), exp(2), ln(exp(2))")
run("SELECT exp(10 * ln(2)), pow(2, 10)")
run("SELECT sinh(1), (exp(1) - exp(-1)) / 2")
[(2.718281828459045, 7.3890560989306495, 7.38905609893065, 2.0)]
[(1024.0, 1024.0)]
[(1.1752011936438014, 1.1752011936438014)]

Clean up:

run(f"DROP TABLE IF EXISTS {TABLE}")
run(f"DROP SCHEMA IF EXISTS {SCHEMA}")
conn.close()
[]
[]

See also