--- title: "% (modulo)" slug: "dev-database-reference-sql-functions-operators-modulo" updated: 2026-10-30T18:18:56Z published: 2026-10-30T18:18:56Z canonical: "docs.vastdata.com/dev-database-reference-sql-functions-operators-modulo" --- > ## 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. # % (modulo) *Applies to VAST Cluster 5.5.* *[Functions](/develop/docs/dev-database-reference-sql-functions-index) › [Operators and predicates](/develop/docs/dev-database-reference-sql-functions-operators-and-predicates) › Arithmetic operators.* `a % b` is the remainder of `a` divided by `b`, and `mod(a, b)` is the same function. The remainder has the sign of `a`: `7 % 3` is 1 and `-7 % 3` is -1. It works on integers, `DECIMAL` values (exactly) and floating-point values. ## Syntax ``` a % b mod(a, b) ``` | Argument | Type | Description | | --- | --- | --- | | `a` | any integer type, `DECIMAL`, `FLOAT` or `DOUBLE` | The dividend. | | `b` | any integer type, `DECIMAL`, `FLOAT` or `DOUBLE` | The divisor. | **Returns:** for two integers of the same type, that type: `INTEGER % INTEGER` is an `INTEGER`. `DECIMAL` arguments give an exact `DECIMAL`; `FLOAT` gives a `FLOAT` and `DOUBLE` a `DOUBLE`. **Aliases:** `mod(a, b)`. ## 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_operators_modulo`. 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 three tickets: a ticket number (`INTEGER`, negative for a refund), an amount (`DECIMAL(10,2)`) and a compass bearing in degrees (`DOUBLE`). 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_operators_modulo"' 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}.tickets" run(f"CREATE SCHEMA IF NOT EXISTS {SCHEMA}") run(f"CREATE TABLE IF NOT EXISTS {TABLE} (id BIGINT, num INTEGER, amount DECIMAL(10,2), bearing DOUBLE)") run(f"INSERT INTO {TABLE} VALUES " "(1, 17, 7.50, 370.5), " "(2, -17, -7.50, -30.0), " "(3, 0, 0.00, 0.0), " "(4, NULL, NULL, NULL)") ``` ``` [] [] [(4,)] ``` ## What it does `a % b` is what's left of `a` after taking out as many whole `b` as fit, counting toward zero. It goes with [// (integer division)](/develop/docs/dev-database-reference-sql-functions-operators-integer-division): `a = (a // b) * b + a % b`. **The remainder has the sign of `a`,** whatever the sign of `b`: | Call | Result | | --- | --- | | `7 % 3` | `1` | | `-7 % 3` | `-1` | | `7 % -3` | `1` | | `-7 % -3` | `-1` | So `n % 2 = 1` is true only for positive odd numbers: `-17 % 2` is -1. Test for odd numbers with `n % 2 <> 0`. For a remainder with the sign of `b` instead, use [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod): `fmod(-7, 2)` is 1.0. **`DECIMAL` remainders are exact:** `2.45 % 0.7` is `Decimal('0.35')`, while the same on `DOUBLE` values is 0.3500000000000003. `7.5 % 2` is `Decimal('1.5')`. **By zero, the result depends on the type:** an integer or `DECIMAL` remainder by 0 is NULL; a `FLOAT` or `DOUBLE` remainder by 0 is `NaN`. Neither is an error. ## NULLs and edge cases | Input | Result | | --- | --- | | `NULL % 2`, `2 % NULL` | `NULL` | | `7 % 3`, `-7 % 3`, `7 % -3`, `-7 % -3` | `1`, `-1`, `1`, `-1` | | `mod(-7, 3)`, `mod(7, -3)` | `-1`, `1` | | `7.5 % 2`, `typeof(7.5 % 2)` | `Decimal('1.5')`, `DECIMAL(11,1)` | | `2.45 % 0.7`, `2.45::DOUBLE % 0.7` | `Decimal('0.35')`, `0.3500000000000003` | | `1 % 0`, `2.45 % 0` | `NULL`, `NULL` | | `1.0::DOUBLE % 0` | `nan` | | `'inf'::DOUBLE % 1`, `1 % 'inf'::DOUBLE`, `'nan'::DOUBLE % 1` | `nan`, `1.0`, `nan` | | The smallest value of a signed integer type `% -1` | Error: `Overflow in division of -128 / -1` (for a `TINYINT`) | | `'abc' % num` | Error: `Could not convert string 'abc' to INT32` | | `7 MOD 2` | Error: a syntax error; write `7 % 2` or `mod(7, 2)` | | A boolean, date, `INTERVAL`, `BLOB` or list | 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](/develop/docs/dev-database-reference-sql-functions-operators-modulo#set-up-the-examples): ``` run("SELECT NULL % 2, 2 % NULL, 7 % 3, -7 % 3, 7 % -3, -7 % -3, mod(-7, 3), mod(7, -3)") run("SELECT 7.5 % 2, typeof(7.5 % 2), 2.45 % 0.7, 2.45::DOUBLE % 0.7") run("SELECT 1 % 0, 2.45 % 0, 1.0::DOUBLE % 0") run("SELECT 'inf'::DOUBLE % 1, 1 % 'inf'::DOUBLE, 'nan'::DOUBLE % 1") run("SELECT (-128)::TINYINT % (-1)::TINYINT") run(f"SELECT 'abc' % num FROM {TABLE} WHERE id = 1") run("SELECT 7 MOD 2") run("SELECT true % 2") ``` ``` [(None, None, 1, -1, 1, -1, -1, 1)] [(Decimal('1.5'), 'DECIMAL(11,1)', Decimal('0.35'), 0.3500000000000003)] [(None, None, nan)] [(nan, 1.0, nan)] OSError: Overflow in division of -128 / -1 OSError: Could not convert string 'abc' to INT32 InternalError: Parse query: 'SELECT 7 MOD 2' failed with: Parser Error: syntax error at or near "2" InternalError: Binder Error: No function matches the given name and argument types '%(BOOLEAN, INTEGER_LITERAL)'. You might need to add explicit type casts. ``` ## Examples The remainder by 2 and by 5, with `%` and `mod()`, and the bearing reduced to one turn. Ticket 2 is negative, so its remainders are negative too: ``` run(f"SELECT id, num % 2, mod(num, 5), amount % 2, bearing % 360 FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, 1, 2, Decimal('1.50'), 10.5), (2, -1, -2, Decimal('-1.50'), -30.0), (3, 0, 0, Decimal('0.00'), 0.0), (4, None, None, None, None)] ``` A remainder from 0 to 4 for every ticket, negative or not: add the divisor and take the remainder again, or use `fmod`, which returns a `DOUBLE`: ``` run(f"SELECT id, ((num % 5) + 5) % 5, fmod(num, 5) FROM {TABLE} ORDER BY id LIMIT 10") ``` ``` [(1, 2, 2.0), (2, 3, 3.0), (3, 0, 0.0), (4, None, None)] ``` Keep only the odd ticket numbers. `num % 2 = 1` misses ticket 2, because `-17 % 2` is -1; `num % 2 <> 0` finds both: ``` run(f"SELECT id, num FROM {TABLE} WHERE num % 2 = 1 ORDER BY id LIMIT 10") run(f"SELECT id, num FROM {TABLE} WHERE num % 2 <> 0 ORDER BY id LIMIT 10") ``` ``` [(1, 17)] [(1, 17), (2, -17)] ``` ## 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 remainder of a column, so `num % 2` is computed by the Query Engine: - **In the `SELECT` list,** the scan reads the column and the Query Engine computes the remainder for each row it returns. - **In `WHERE`,** the scan sends the column for every row, and a `FILTER` step in the Query Engine computes the remainder and keeps the rows that match. No rows are skipped in storage. `mod(num, 2)` runs the same way. The plan of the `WHERE` example shows the `FILTER` step above the scan: ``` plan(f"SELECT id FROM {TABLE} WHERE num % 2 <> 0") ``` ``` ┌───────────────────────────┐ │ PROJECTION │ │ ──────────────────── │ │ #1 │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ FILTER │ │ ──────────────────── │ │ ((num % 2) != 0) │ │ │ └─────────────┬─────────────┘ ┌─────────────┴─────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ │ │ Projections: │ │ num │ │ id │ │ │ └───────────────────────────┘ ``` ## Make it faster - **Store the bucket you filter on.** A remainder is often a bucket: odd or even, a shard number, a day of a cycle. If you filter on one often, compute it when you write the rows and keep it in its own column: `shard = 3` is pushed down to VAST DataBase, `num % 8 = 3` isn't. The plan for a raw column compared with a constant has no `FILTER` step: ``` plan(f"SELECT id FROM {TABLE} WHERE num = 17") ``` ``` ┌───────────────────────────┐ │ VAST_SCAN │ │ ──────────────────── │ │ Function: VAST_SCAN │ │ Projections: id │ │ │ └───────────────────────────┘ ``` - **Combine it with a pushed-down condition.** In `id > 100 AND num % 2 <> 0`, `id > 100` is still evaluated in VAST DataBase, so the remainder is computed only on the rows that pass it. ## Limitations and errors - **The smallest value of a signed integer type `% -1` stops the query** with `Overflow in division of -128 / -1` (for a `TINYINT`), although the remainder would be 0. Cast to a wider type first. - **No `MOD` operator.** `7 MOD 2` is a syntax error. Use `%` or `mod()`. - **A string that isn't a number fails** with `Could not convert string '…' to INT32` (or the type of the other side). A numeric string, such as `'12'`, is converted. - **Booleans, dates, `INTERVAL` values, `BLOB` values and lists** fail with `No function matches the given name and argument types`. ``` run("SELECT CAST((-128)::TINYINT AS INTEGER) % -1") run(f"SELECT '12' % num FROM {TABLE} WHERE id = 1") ``` ``` [(0,)] [(12,)] ``` ## Similar functions | Function | Does | `-7` by `2` | `7` by `-2` | `1` by `0` | | --- | --- | --- | --- | --- | | `a % b`, `mod(a, b)` | the remainder, with the sign of `a` | `-1` | `1` | `NULL` | | [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod) | the remainder of rounding down, with the sign of `b`, as a `DOUBLE` | `1.0` | `-1.0` | `nan` | | [// (integer division)](/develop/docs/dev-database-reference-sql-functions-operators-integer-division) | the quotient that goes with `%` | `-3` | `-3` | `NULL` | | [fdiv](/develop/docs/dev-database-reference-sql-functions-numeric-fdiv) | the quotient that goes with `fmod` | `-4.0` | `-4.0` | `inf` | | [gcd](/develop/docs/dev-database-reference-sql-functions-numeric-gcd) | the largest number that leaves no remainder for both | `1` | `1` | `1` | ``` run("SELECT -7 % 2, mod(-7, 2), fmod(-7, 2), -7 // 2, fdiv(-7, 2), gcd(-7, 2)") run("SELECT 7 % -2, mod(7, -2), fmod(7, -2), 7 // -2, fdiv(7, -2), gcd(7, -2)") run("SELECT 1 % 0, mod(1, 0), fmod(1, 0), 1 // 0, fdiv(1, 0), gcd(1, 0)") ``` ``` [(-1, -1, 1.0, -3, -4.0, 1)] [(1, 1, -1.0, -3, -4.0, 1)] [(None, None, nan, None, inf, 1)] ``` Clean up: ``` run(f"DROP TABLE IF EXISTS {TABLE}") run(f"DROP SCHEMA IF EXISTS {SCHEMA}") conn.close() ``` ``` [] [] ``` ## See also - [Operators and predicates](/develop/docs/dev-database-reference-sql-functions-operators-and-predicates#arithmetic): all the arithmetic operators. - [// (integer division)](/develop/docs/dev-database-reference-sql-functions-operators-integer-division), [fmod](/develop/docs/dev-database-reference-sql-functions-numeric-fmod) and [fdiv](/develop/docs/dev-database-reference-sql-functions-numeric-fdiv). - [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.