--- title: "fdiv" slug: "dev-database-reference-sql-functions-numeric-fdiv" updated: 2026-10-30T18:18:51Z published: 2026-10-30T18:18:51Z canonical: "docs.vastdata.com/dev-database-reference-sql-functions-numeric-fdiv" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.vastdata.com/llms.txt > Use this file to discover all available pages before exploring further. # fdiv *Applies to VAST Cluster 5.5.* *[Functions](/develop/docs/dev-database-reference-sql-functions-index) › [Numeric and math functions](/develop/docs/dev-database-reference-sql-functions-numeric) › Arithmetic.* Divides `a` by `b` and rounds the quotient down (towards minus infinity) to a whole number, returned as a `DOUBLE`: `fdiv(-5.5, 2)` is `-3.0`. It goes with [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod): for ordinary values, `a = fdiv(a, b) * b + fmod(a, b)`. ## Syntax ``` fdiv(a, b) ``` | Argument | Type | Description | | --- | --- | --- | | `a` | Any number | The dividend. | | `b` | Any number | The divisor. | **Returns:** `DOUBLE`, for every numeric input type. ## Set up the examples The examples connect with the ADBC driver, as in [VAST Query Engine (ADBC)](/develop/docs/dev-database-integrations-adbc), and work in a schema called `docs_sql_fn_numeric_fdiv`. Set `QE_ENDPOINT`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `VASTDB_BUCKET` ([SQL reference](/develop/docs/dev-database-reference-sql-index#example-connect-and-use-a-transaction)), 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 six events with an offset in seconds from a reference time; negative offsets are before it. 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_fdiv"' 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, "") 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, offset_s BIGINT)") run(f"INSERT INTO {TABLE} VALUES " "(1, 0), " "(2, 59), " "(3, 60), " "(4, 125), " "(5, -1), " "(6, NULL)") ``` ``` [] [] [(6,)] ``` ## What it does `fdiv(a, b)` is `floor(a / b)`: the quotient, rounded down to a whole number. For a negative quotient, rounding down moves away from zero: | Call | `a / b` | Result | | --- | --- | --- | | `fdiv(5.5, 2)` | `2.75` | `2.0` | | `fdiv(-5.5, 2)` | `-2.75` | `-3.0` | | `fdiv(5.5, -2)` | `-2.75` | `-3.0` | | `fdiv(5, 3)` | `1.67` | `1.0` | | `fdiv(-5, 3)` | `-1.67` | `-2.0` | This differs from integer division with `//`, which truncates towards zero: `-7 // 2` is `-3`, while `fdiv(-7, 2)` is `-4.0`. Use `fdiv` to put values into buckets of equal width, such as seconds into minutes, when some values are negative: `fdiv(-1, 60)` is `-1.0`, the minute before the reference time. ## NULLs and edge cases | Input | Result | | --- | --- | | `a` or `b` is `NULL` | `NULL` | | `b` = 0 | `inf` or `-inf`, by the sign of `a` | | `0` by `0` | `nan` | | `a` infinite | `inf` | | `b` infinite | `0.0` | | `a` is `NaN` | `nan` | | `fdiv(1e308, 1e-308)` | `inf` | | `fdiv(7::DECIMAL(4,1), 2)` | `3.0`, a `DOUBLE` | | `fdiv(9223372036854775807, 2)` | `4.611686018427388e+18`: a `DOUBLE` holds about 16 significant digits | | `'7'` (a string) | Error: `No function matches the given name and argument types 'floor(INTERVAL)'` | The same cases, as the cluster returns them. They use the table and helpers from [Set up the examples](/develop/docs/dev-database-reference-sql-functions-numeric-fdiv#set-up-the-examples): ``` run("SELECT fdiv(NULL, 2), fdiv(1, NULL), fdiv(5, 0), fdiv(5.0, 0.0), fdiv(0, 0)") run("SELECT fdiv('inf'::DOUBLE, 1), fdiv(1, 'inf'::DOUBLE), fdiv('NaN'::DOUBLE, 1), fdiv(1e308, 1e-308)") run("SELECT fdiv(7::DECIMAL(4,1), 2), fdiv(9223372036854775807, 2), typeof(fdiv(5, 2))") run("SELECT fdiv('7', 2)") ``` ``` [(None, None, inf, inf, nan)] [(inf, 0.0, nan, inf)] [(3.0, 4.611686018427388e+18, 'DOUBLE')] InternalError: Binder Error: No function matches the given name and argument types 'floor(INTERVAL)'. You might need to add explicit type casts. ``` ## Examples The minute each event falls in, and the second within that minute from [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod). Compare with `//`, which puts the event at -1 seconds into minute 0: ``` run(f"SELECT id, offset_s, fdiv(offset_s, 60), fmod(offset_s, 60), offset_s // 60 FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, 0, 0.0, 0.0, 0), (2, 59, 0.0, 59.0, 0), (3, 60, 1.0, 0.0, 1), (4, 125, 2.0, 5.0, 2), (5, -1, -1.0, 59.0, 0), (6, None, None, None, None)] ``` The events in the first minute: ``` run(f"SELECT id, offset_s FROM {TABLE} WHERE fdiv(offset_s, 60) = 0 ORDER BY id LIMIT 10") ``` ``` [(1, 0), (2, 59)] ``` ## 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 `fdiv()` is always computed by the Query Engine: - **In the `SELECT` list,** the scan reads the columns and the Query Engine computes `fdiv()` for each row it returns. - **In `WHERE`,** the scan sends the columns for every row, and a `FILTER` step in the Query Engine computes `fdiv()` 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 fdiv(offset_s, 60) = 0") ``` ``` ┌───────────────────────────┐ │ PROJECTION │ │ ──────────────────── │ │ #1 │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ──────────────────── │ │ (floor((CAST(offset_s AS │ │ DOUBLE) / 60.0)) = 0.0) │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ │ │ Projections: │ │ offset_s │ │ id │ │ │ └───────────────────────────┘ ``` ## Make it faster - **Filter on a range of the raw column instead.** `fdiv(offset_s, 60) = 0` keeps the offsets from 0 to 59; `offset_s >= 0 AND offset_s < 60` returns the same rows, and 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 offset_s >= 0 AND offset_s < 60") ``` ``` ┌───────────────────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ Projections: id │ │ │ └───────────────────────────┘ ``` - **Compute the bucket in the `SELECT` list** of the rows you return, and filter on the raw column. ## Limitations and errors - **The result is always a `DOUBLE`,** also for integers: `fdiv(7, 2)` is `3.0`. Cast it if you need an integer: `CAST(fdiv(7, 2) AS BIGINT)`. Integers beyond about 16 significant digits lose precision in the `DOUBLE`. - **Division by zero returns `inf`, `-inf` or `nan`, not an error and not `NULL`.** Test for it with `isinf()` and `isnan()`, or guard the divisor: `fdiv(a, NULLIF(b, 0))` returns `NULL` instead. - **Numbers only.** A date, boolean, list or `BLOB` fails with an error that names the division `fdiv` performs, for example `No function matches the given name and argument types '/(DATE, INTEGER_LITERAL)'`. A string, even a numeric one, fails with `'floor(INTERVAL)'`. Cast strings to a number first. ``` run("SELECT CAST(fdiv(7, 2) AS BIGINT), isinf(fdiv(1, 0)), fdiv(1, NULLIF(0, 0))") run("SELECT fdiv(DATE '2026-10-16', 2)") run("SELECT fdiv(CAST('7' AS DOUBLE), 2)") ``` ``` [(3, True, None)] InternalError: Binder Error: No function matches the given name and argument types '/(DATE, INTEGER_LITERAL)'. You might need to add explicit type casts. [(3.0,)] ``` ## Similar functions | Function | Does | `-7` by `2` | `7` by `2` | | --- | --- | --- | --- | | `fdiv` | the quotient rounded down, as a `DOUBLE` | `-4.0` | `3.0` | | [// (integer division)](/develop/docs/dev-database-reference-sql-functions-operators-integer-division) | the quotient truncated towards zero | `-3` | `3` | | `floor(a / b)` with [floor](/develop/docs/dev-database-reference-sql-functions-numeric-floor) | the same as `fdiv` | `-4.0` | | | [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod) | the remainder that goes with `fdiv` | | | | [divide](/develop/docs/dev-database-reference-sql-functions-numeric-divide) | gives the same results as `//` here | `-3` | `3` | ``` run("SELECT fdiv(-7, 2), -7 // 2, floor(-7 / 2), fmod(-7, 2), divide(-7, 2)") run("SELECT fdiv(7, 2), 7 // 2, floor(7 / 2), fmod(7, 2), divide(7, 2)") ``` ``` [(-4.0, -3, -4.0, 1.0, -3)] [(3.0, 3, 3.0, 1.0, 3)] ``` Clean up: ``` run(f"DROP TABLE IF EXISTS {TABLE}") run(f"DROP SCHEMA IF EXISTS {SCHEMA}") conn.close() ``` ``` [] [] ``` ## See also - [Numeric and math functions](/develop/docs/dev-database-reference-sql-functions-numeric): all the numeric functions. - [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod), [// (integer division)](/develop/docs/dev-database-reference-sql-functions-operators-integer-division) and [floor](/develop/docs/dev-database-reference-sql-functions-numeric-floor). - [Where filters run](/develop/docs/dev-database-reference-sql-select#where-filters-run) and [pushdowns](/develop/docs/dev-database-reference-pushdowns). - [EXPLAIN](/develop/docs/dev-database-reference-sql-explain): how to read a plan.