Skip to content

fix(core): refuse a zero whose exponent is outside the number range - #1572

Open
navareamol wants to merge 12 commits into
mainfrom
fix/1526-zero-exponent
Open

navareamol wants to merge 12 commits into
mainfrom
fix/1526-zero-exponent

Conversation

@navareamol

Copy link
Copy Markdown
Collaborator

What

A zero is range checked on its exponent less its fraction digits, as any other number is. 0e999, 0e126 and 0.0e200 are refused as Number overflow, and 0e-999 as Number underflow. Those four are the values in the issue. The tests also refuse -0E126 (overflow) and 0E-131 and 0.0E-130 (underflow). Those three come from the measurements the old code comment cites, not from the issue and not re-measured here. 0E125, 0.0E126 and 0E-130 are still stored as 0.

The change is in validate_and_normalize_number (crates/core/src/validation/number.rs). It range checked a zero only when the exponent did not fit in an i32. It now checks every zero with the rule it already applied to those. A refused zero keeps its raw spelling in AttributeValue, so the checks that already refuse 1E126 on a path refuse it too. The messages are the existing NumberFault overflow and underflow texts.

Docs: docs/differences-from-dynamodb.md no longer lists the ImportTable zero as a difference. Two code comments that said a zero with any 32-bit exponent is 0 are corrected (crates/core/src/backup_format/key_order.rs, crates/core/src/expression/update_evaluator.rs). docs/manuals/07-upgrade-manual.md gets a ### v1.2.0: entry.

Why

Refs #1526. The issue measured on the live service that these zeros are refused where ExtendDB answered 200. With the fix removed, 119 of the new wire tests fail and 5 core unit tests fail (see Testing done). The issue's table was not re-measured on the live service here.

This is Refs, not Fixes. The issue also names QueryFilter. The legacy QueryFilter, ScanFilter and Scan ExpressionAttributeValues check no number on main, for any value (1E126 and abc are accepted there, run on this branch), so a zero is not refused there either. #562 covers the Scan values and #618 tracks the rest.

Zeros already stored

No released version could store a zero whose exponent fits in an i32 in its raw spelling, so no read, update or restore path needs to accept one. Every request value, PartiQL value, imported row and backup line reaches storage through AttributeValue's deserializer, and in every tag that deserializer turned such a zero into 0:

  • v0.1.0: crates/core/src/types/attribute_value.rs:103 calls validate_and_normalize_number(n).unwrap_or_else(|_| n.to_owned()), and crates/core/src/validation/number.rs:55 returns "0" for any zero. The exponent is read as an i32 (parse::<i32>), so a zero whose exponent does not fit is refused and kept raw. The normalization came in 19b868a, which every tag contains.
  • v0.1.14: the same, attribute_value.rs:107 and number.rs:65.
  • v0.1.15 to v1.1.0: attribute_value.rs:144 (:146 in v1.1.0) calls the same function, and number.rs:159-167 (:180-188 in v1.1.0) returns "0" for a zero whose exponent fits in an i32 (883f8c7, fix(core): range check number exponents as DynamoDB does #619).
  • PartiQL, which first shipped in v0.1.15 (0cfb0d9), builds its writes in wire form and reads them back through the same deserializer: crates/engine/src/partiql/write.rs:371 and crates/engine/src/partiql/item_depth.rs:383 in v0.1.15 and v1.1.0.
  • ImportTable reads rows through serde_json::from_value into Item (crates/engine/src/import_export_io.rs:59), and CSV rows hold only S values (:125). Backup data files are read through the deserializer (crates/core/src/backup_format/data_file.rs:1022 in v1.1.0), and they are written from stored items. Stream records and index rows are built from stored items.

The raw zeros an older release could store are those whose exponent does not fit in an i32. Before #620 (3a5c84b, v0.1.15), BatchWriteItem and TransactWriteItems stored a refused value as written. Main already refuses those zeros, and this change does not alter how they are read. No catalog was scanned. The evidence is the code at each tag.

Side effects

  • Every caller of validate_and_normalize_number sees the new refusal: item values, keys, expression values, PartiQL parameters and literals, ImportTable rows, batch and transaction writes. An ImportTable row holding 0E126 is now skipped and the import ends FAILED, as the service does. It used to be imported with 0.
  • A zero with more than 130 fraction digits and no exponent is now refused as Number underflow. An example is a Java BigDecimal zero with scale 200 sent as 0. followed by 200 zeros. This was run on the branch with 0. followed by 140 zeros on PutItem and PartiQL. Whether the live service refuses that input was not measured. The upgrade manual lists it.
  • On the paths that check no number (Scan ExpressionAttributeValues, ScanFilter, legacy QueryFilter), a zero such as 0E126 used to become 0 and now keeps its raw spelling. A filter n = :v or n < :v with it matches nothing, and n <> :v (or NOT (n = :v)) matches every row with a number n. That is what 1E126 does on those paths today. A Scan or Query ExclusiveStartKey holding 0E126 is compared as written. That was not run.
  • A number set ["0E126", "0"] used to be refused as a duplicate, since the reader folded 0E126 into 0 (from the code at crates/core/src/types/attribute_value.rs:246; not run on main). It is now refused as Number overflow (run on this branch on SQLite, PostgreSQL and Cassandra). The request is invalid either way. The service's answer for it was not measured.
  • PartiQL's own check_number (crates/core/src/partiql/number.rs) still accepts a zero at any scale, and its test zero_is_accepted_at_any_scale (crates/core/src/partiql/number/tests.rs:71) pins 0e-500. PartiQL refuses these zeros today because its values also pass through validate_and_normalize_number. A future caller of check_number alone would accept them. fix(core): check a PartiQL number's magnitude before its digit count #1376 also changes check_number. This PR leaves it as it is.
  • UpdateItem arithmetic that gives zero from operands with many fraction digits, such as SET #n = :a - :b with both 1.5E-130, or ADD n :v of -1.5E-130 to a stored 1.5E-130, stores 0. The result has 131 fraction digits, but BigDecimal displays a zero of any scale as 0, so the result check reads 0 and needs no special case. A unit test pins this (a_zero_result_is_zero_whatever_its_scale), and both calls answer 200 with {"N": "0"} on SQLite on this branch. Main computes the result the same way. The live service was not measured.

Testing done

The desktop and Mac trees match (git rev-parse HEAD^{tree} is 3913d45a55ca715fb32c6c798af1d2499212b566). The branch has origin/main db8a90dad merged in.

Check Command Result
Format cargo fmt --check clean
Clippy cargo +1.97.0 clippy -p extenddb-core --all-targets -- -D warnings, and the same for -p extenddb-engine both exit 0
Unit tests cargo test --no-fail-fast -p extenddb-core -p extenddb-engine core lib 1261 passed, 1 ignored. Engine lib 980 passed, 1 failed in each of three full runs, a different backup test each time (see below)
Engine test reruns each failed test alone with cargo test -p extenddb-engine --lib -- --exact <name>, 20 runs 20 passed for each, on the branch and on main
Wire file, SQLite pytest tests/test_zero_exponent_range.py, three runs on one server, and one more on the final tree 124 passed, 1 skipped each run
Wire file, PostgreSQL the same, one run 124 passed, 1 skipped
Wire file, Cassandra the same, one run 124 passed, 1 skipped
Negative control, unit zero branch mutated back (patch below), cargo test --no-fail-fast -p extenddb-core --lib 1255 passed, 5 failed
Negative control, wire same mutation, SQLite server rebuilt, whole wire file 119 failed, 5 passed, 1 skipped

Each full engine run had one failing backup test, a different one each time. These were a_healthy_store_makes_create_backup_store_backed (left: Creating, right: Available, at a load average of about 70), a_second_unknown_outcome_whose_write_landed_is_matched, and a_put_failure_fails_the_row_then_deletes_the_prefix. Each passed 20 of 20 runs alone, on the branch and on main (db8a90dad for the first two, b6c859508 for the third; the first test was run alone only on the branch). The branch changes no file under crates/engine.

The wire file runs each issue value plus -0E126, 0E-131 and 0.0E-130 on these paths:

  • PutItem: attribute; inside an NS, L and M; sort key; condition value.
  • UpdateItem: value.
  • GetItem, DeleteItem, BatchGetItem and TransactGetItems: keys.
  • BatchWriteItem and TransactWriteItems: items.
  • Query: key condition, legacy KeyConditions, FilterExpression value.
  • PartiQL: SELECT and INSERT parameters, SELECT literal.

That makes 7 values times 17 test functions, 119 tests. One is skipped (the -0E126 PartiQL literal, which PartiQL refuses with its own negative zero text), so 118 run. The file also checks that 0E125, 0.0E126, 0E-130 and 0 are stored as 0 (4 tests), that a refused zero in a batch writes nothing, and the number set above. That gives 124 passed and 1 skipped.

The control mutation, applied on the clean committed tree and reverted with git checkout --, with git status --short empty afterwards:

--- a/crates/core/src/validation/number.rs
+++ b/crates/core/src/validation/number.rs
@@ zero branch of normalize_number @@
-        if normalized_exponent_exceeds_i64(point_offset) {
-            return Err(NumberFault::Underflow);
-        }
-        check_magnitude(point_offset)?;
+        if i32::try_from(explicit_exp).is_err() {
+            if normalized_exponent_exceeds_i64(point_offset) {
+                return Err(NumberFault::Underflow);
+            }
+            check_magnitude(point_offset)?;
+        }
         return Ok("0".to_owned());

With the mutation, five core unit tests fail:

  • zero_with_exponent_inside_i32_is_range_checked
  • zero_with_exponent_outside_i32_is_range_checked
  • exponents_at_the_ends_of_i32_classify_the_magnitude
  • doc_examples_normalize_as_stated
  • import_row_accepts_what_the_service_imports

The 119 failing wire tests are the 118 refusal tests that run, plus test_a_refused_zero_writes_nothing. The 5 that still pass are the 4 in-range zeros and the number set test, which gets a duplicate refusal instead of the overflow and only checks for a ValidationException. So the number set test detects no change. It records the answer.

Rerun on SQLite: build with cargo build -p extenddb --no-default-features --features sqlite, extenddb init --backend sqlite, extenddb serve, provision credentials with devtools/provision-test-credentials, then pytest tests/test_zero_exponent_range.py. PostgreSQL uses the default build and Cassandra --features cassandra.

Not run: MongoDB (outside 1.0), the MSRV build, and cargo check --target x86_64-pc-windows-msvc (no cfg(unix) item moved). The wire file was not run against main. The control stands in for it.

Overlap with open PRs, from git merge-tree against this branch:

Open question

The issue gives "the overflow text" and "the underflow text" for zeros, not the messages. This change reuses the repo's existing measured texts, and each operation's prefix comes from its existing wrapper. The wire tests match only the words Number overflow and Number underflow. Has anyone measured the exact text and prefix the live service gives for a zero on each operation, and its answer for 0. followed by 140 zeros?

Amol Navare added 12 commits December 3, 2026 09:54
DynamoDB range checks a zero on its exponent less its fraction digits, as
it does any other number: 0E126 is a Number overflow and 0E-131 a Number
underflow. ExtendDB range checked only a zero whose exponent did not fit in
an i32, so 0e999, 0e126, 0.0e200 and 0e-999 were stored as 0.

validate_and_normalize_number now applies the same range rule to a zero
with any exponent. A refused zero keeps its raw spelling, so the checks on
item, key, Query and expression values refuse it with the existing
overflow and underflow messages.
…and in comments

The ImportTable row of the differences table said a zero such as 0E126 is
imported as 0. It is skipped, as the service skips it. Two code comments said
the number check normalizes a zero with any 32-bit exponent to 0. The upgrade
entry names the ImportTable change and a zero with many fraction digits.
1.5E-130 - 1.5E-130 gives a zero with 131 fraction digits. The result is
checked as a written number, and a zero written that way is now below the
range, so UpdateItem answered Number overflow where main stored 0. A zero
result is now 0 before the check; other results are checked as before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant