bug: integer division `DIV` is computed through Float64: `1760000000999999999 DIV 1000000000` is `1760000001`, and `x DIV 1 ≠ x` above 2^53

Open
#20,784 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@forsaken628 is already working on this.

Since Oct 23, 2026.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust, sql
Domain
databases

Research direction

Start in src/query/functions/src/scalars/numeric_basic_arithmetic/src/numeric_basic_arithmetic.rs, at register_intdiv, and compare its float conversion with the exact modulo implementation. Reproduce the reported large integer cases, including DIV 1 above 2^53, then find or add focused tests for integer and decimal operands. Done means division truncates exactly and the reported cases return the expected values.

Written by the indexing model from the issue text.

Description

C-bug
Search before asking
  • I had searched in the issues and found no similar issues.
Version

main e9b49fd (source build, v1.2.953-nightly-e9b49fde); also v1.2.952-nightly-8862a6285b.

What's Wrong?

a DIV b (and intdiv) on BIGINT/UINT64 (and DECIMAL) operands converts both operands to f64, divides, and casts the
quotient back to an integer. Above 2^53 the dividend is rounded before the division, so the quotient can be off by one
or more. x DIV 1 returns x rounded to 53 bits. % is exact (#20693 fixed decimal % for the same reason), so
(n DIV d) * d + n % d = n is false.

This affects the usual conversion of nanosecond/microsecond epoch integers to seconds (ts_ns DIV 1000000000), which
moves the last nanoseconds of a second into the next second. INSERT … SELECT, UPDATE and stored computed columns
persist the wrong value. There is no error.

Cause: register_intdiv (src/query/functions/src/scalars/numeric_basic_arithmetic/src/numeric_basic_arithmetic.rs,
around line 485):

let lhs = F64::from(AsPrimitive::<f64>::as_(a));
output.push(AsPrimitive::<IntDivResult<L, R>>::as_(lhs / b_value));

For integer types (and decimals, which are coerced to DOUBLE for div) the quotient should be computed exactly with
checked_div on the native or widened integer type, as modulo now is.

How to Reproduce?
SELECT 1760000000999999999 DIV 1000000000,      -- 1760000001           (exact 1760000000)
       9007199254740993 DIV 1,                   -- 9007199254740992     (exact 9007199254740993)
       9223372036854775807 DIV 2,                -- 4611686018427387904  (exact 4611686018427387903)
       12345678901234567890::UINT64 DIV 10,      -- 1234567890123456768  (exact 1234567890123456789)
       CAST(12345678901234567890.5 AS DECIMAL(38,10)) DIV 7;   -- 1763668414462081024 (exact 1763668414462081127)

SELECT n DIV 1000000000 AS q, n % 1000000000 AS r,
       (n DIV 1000000000) * 1000000000 + n % 1000000000 = n AS consistent
FROM (SELECT 1760000000999999999::BIGINT AS n);
-- 1760000001 | 999999999 | 0

CREATE OR REPLACE TABLE ev (ts_ns BIGINT, sec BIGINT, sec2 BIGINT);
INSERT INTO ev (ts_ns, sec) SELECT n, n DIV 1000000000
FROM (SELECT unnest([1760000000999999999, 1760000001000000000, 1760000000500000000]) AS n);
UPDATE ev SET sec2 = ts_ns DIV 1000000000;
SELECT ts_ns, sec, sec2 FROM ev ORDER BY ts_ns;
-- 1760000000500000000 | 1760000000 | 1760000000
-- 1760000000999999999 | 1760000001 | 1760000001     <-- second …000 stored as …001
-- 1760000001000000000 | 1760000001 | 1760000001

Expected: exact integer division (truncating), e.g. 1760000000999999999 DIV 1000000000 = 1760000000.

Single node, default settings, HTTP handler.

Are you willing to submit PR?
  • Yes I am willing to submit a PR!
Dominant language
Rust
Stars
9.5k
Forks
903
Avg merge
2d 13h
Merged PRs (30d)
204

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from databendlabs/databend

All issues in databendlabs/databend

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.