bug: integer division `DIV` is computed through Float64: `1760000000999999999 DIV 1000000000` is `1760000001`, and `x DIV 1 ≠ x` above 2^53
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
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
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
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from databendlabs/databend
-
bug: segment pruning skips rows when only some blocks have statistics of an added columnPossibly taken @zhyass claimed this today. OpenC-bug
databendlabs/databend#21244 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: ORDER BY ... NULLS LAST LIMIT returns wrong rows when an inverted index answers the filter and the sort column has NULLsPossibly taken @b41sh claimed this today. OpenC-bug
databendlabs/databend#21243 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Reduce role-graph work for explicit secondary rolesPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21226 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Key role-dependent query results by authorization contextPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21225 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: filters folded to false on block statistics drop matching rows: cast-eliminated equality under NOT, float arithmetic over NaN and infinities, variant IS DISTINCT FROMPossibly taken @forsaken628 claimed this 1 day ago. OpenC-bug
databendlabs/databend#21216 · 1 assignee ·
Maintainers usually reply within 1 day
All issues in databendlabs/databend
Similar issues
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
eliasstravik/herdr-projects#1605 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
jlcodes99/cockpit-tools#3332 ·
Maintainers usually reply within 3 days
-
Cognito: the email MFA code (EMAIL_OTP challenge after a password flow) has 8 digits; AWS delivers 6Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-emulator#692 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 82/100
pamburus/hl#1779 · 1 comment ·
Maintainers usually reply within 1 day