Skip to content

storage: Match Decimal columns' 20-place precision - #7866

Merged
StefanMaron merged 2 commits into
aldevtools:mainfrom
bstaeheli:pr-decimal-store-current
Nov 23, 2026
Merged

StefanMaron merged 2 commits into
aldevtools:mainfrom
bstaeheli:pr-decimal-store-current

Conversation

@bstaeheli

@bstaeheli bstaeheli commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

n/a (no linked issue)

What changed

  • Cut stored Decimal values toward zero to the 20 fractional places supported by the database column while keeping the record buffer unchanged.
  • Refuse writes of a Decimal primary-key value that would need truncation.
  • A follow-up is still needed for filters, unique Decimal keys, assumption tracking, the scale fast path and documentation. This PR is not ready to merge as it stands.

Evidence from real Business Central (required)

Where the behavior is pinned

Not pinned yet. The probe PR contains the measurements; the assumption registry entry is part of the pending follow-up.

Shapes that keep today's behavior

Temporary records keep their input precision; writes of a Decimal primary key requiring truncation are refused. Primary-key lookups, filtering and unique secondary keys still need the requested follow-up.

Checks

  • A test that failed before this change and passes now (feedback regressions are being addressed locally)
  • Tests that neighbor shapes keep main's result (not yet verified for the follow-up)
  • al-test-cli --root . --baseline-diff: no regression; stale lines deleted (--prune-not-passing) (the unpublished follow-up has XMLport regressions on BC 27.5, 28.5, and 29.0; further evidence is needed)
  • ./check.sh (not run locally; PR CI is the project gate)
  • I signed the contributor agreement (see CONTRIBUTING.md)
  • No machine names, URLs, credentials, tenant ids or customer code in the change or this text (the documentation cleanup is pending)

Earlier validation of the published branch:

  • cargo fmt --all -- --check: passed.
  • cargo test -p al-vm --test store decimal_store_truncation: passed after rebase.
  • cargo test -p al-vm --test store: passed before rebase.
  • cargo clippy -p al-vm --all-targets: passed before rebase.

bstaeheli and others added 2 commits November 12, 2026 13:57
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 12, 2026 12:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Decimal truncation can bypass secondary-key uniqueness checks and store duplicate unique values, while the unpinned behavior also lets dependent tests appear as clean passes.

2 open issues
What changed in this PR

This PR updates the AL runtime’s database write normalization to match locally measured Business Central Decimal precision.

Changes:

  • Truncate stored Decimals toward zero to 20 fractional places without changing the record buffer.
  • Refuse Decimal primary-key values that require truncation.
  • Add regression tests and document the measurements and remaining evidence gaps.
File Description
docs/​probes-needed.md Tracks Decimal storage behavior and pending probes.
docs/​decimal-store-probe.md Records local BC measurements and unmeasured cases.
crates/​al-vm/​tests/​store/​main.rs Registers the Decimal storage test module.
crates/​al-vm/​tests/​store/​decimal_store_truncation.rs Tests Insert/Modify truncation, buffer preservation, and primary-key handling.
crates/​al-vm/​src/​sqltime.rs Adds Decimal storage truncation and primary-key refusal.

Review effort: Balanced

.into(),
));
}
row[i] = Value::Decimal(cut);
Comment on lines +153 to +155
(RtType::Decimal, Value::Decimal(d)) => {
let cut = d.round_dp_with_strategy(20, rust_decimal::RoundingStrategy::ToZero);
if cut != *d {

@StefanMaron StefanMaron left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for digging into this. The measurement makes sense given the decimal(38,20) column you already
found in your earlier issue, and I like that you refuse the primary key case instead of guessing.

I can't merge it as it is, for these reasons:

  1. Filters. After this change a row stored from 1000 / 3600000 holds the cut value, but
    SetRange(Amount, 1000 / 3600000) still compares with the uncut value. Today that filter finds the row;
    with this PR it would not. Your notes say filters are not measured yet. I expect BC cuts the filter value
    the same way, so we would swap a correct answer for a wrong one. Storing and filtering need to land together.
  2. The corpus is our only oracle. Could you turn the probe into a test on the corpus repository
    (BusinessCentral.AL.Language.Tests)? Its CI runs on BC 27, 28 and 29. Please include: the values from your
    table, the filter cells (=, <, >, a range, with both the full and the cut value), a Decimal primary key,
    a Unique = true secondary key, and a temporary table.
  3. Until the corpus pins it, the cut must be registered as an assumption (an entry under
    crates/al-builtins/src/assumptions/, assume called when the value really changes, and
    docs/probes-needed.md regenerated with --assumptions-doc rather than a hand-written row).
  4. A Unique = true key over a Decimal field has the same problem as the primary key: two values can become
    equal after the cut. Please refuse that case too.
  5. This runs for every Decimal on every database write. Please check d.scale() > 20 before rounding and
    share a before and after timing.
  6. Small things: sqltime.rs is about date and time columns, so a small module of its own (called from
    store) would be easier to find; and please remove the container name from the docs file.

If you prefer, open the corpus test first and I will line up the runner side once its results are in.

@bstaeheli

bstaeheli commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Measured on BC 27.5, 28.5, and 29.1: persisted Decimal fields truncate values to database precision while the AL record buffer retains the supplied value. Filters and Decimal primary-key lookups resolve against persisted values; a unique Decimal secondary-key collision after storage rejects Insert/Modify, while Rename raises DB:RecordExists. Temporary records retain the full value. The corresponding corpus assertions passed on all three versions.

Corpus probe: aldevtools/BusinessCentral.AL.Language.Tests#1427 (draft; CI awaiting repository administrator approval).

@StefanMaron

Copy link
Copy Markdown
Collaborator

Thanks for measuring on BC 27.5, 28.5 and 29.1: stored values are cut, the record buffer keeps the full value, filters and primary-key lookups use the stored value, a unique secondary-key collision fails Insert and Modify, Rename raises DB:RecordExists, and temporary records keep the full value.

Next is corpus pull request aldevtools/BusinessCentral.AL.Language.Tests#1427. When it is ready for review, I approve its CI run and review it.

This pull request still needs the other points from my review. Your measurement shows filters compare with the stored value, so the filter change has to be in this pull request too. The other points:

  • an entry for this behavior in crates/al-builtins/src/assumptions/ that cites the probe, and docs/probes-needed.md regenerated with --assumptions-doc;
  • refuse a Unique = true key over a Decimal field;
  • check d.scale() > 20 before rounding, with a before and after timing;
  • move the code out of sqltime.rs into a module of its own, and remove the container name from the docs file.

@StefanMaron

Copy link
Copy Markdown
Collaborator

Thank you for this pull request. I am sorry the review took 10 days. I saw you said it is not ready yet, so treat this as early feedback.

Verdict: needs a rebase on main, then changes. Conflict: crates/al-vm/src/sqltime.rs. Main rewrote store (now at sqltime.rs:421, takes schema, moved the date checks into bad_field, adds the Time rollover rule). The Decimal arm must be placed again inside main's loop.

  1. HIGH - crates/al-vm/src/sqltime.rs:150-164 on your branch. The stored value is cut, but filter values are not. Example: R.D := 1000 / 3600000; R.Insert(); G.SetRange(D, 1000 / 3600000); G.Count(). Main returns 1. Your branch returns 0, because the stored 0.00027777777777777777 no longer equals the filter value with more than 20 decimals. BC is unmeasured here, but a SQL parameter typed to the decimal(38,20) column would match, so 1 is the likely answer. Real code does this, for example a currency factor stored as 1 / rate and later filtered with the in-memory value. The same applies to SetFilter text, FieldRef.SetRange, CalcFormula filters and a unique secondary key on such a field.
    Fix: cut only where the probe measures it (Insert and Modify, then Get and Find reading the row). For a filter or key comparison on a Decimal field of a database table whose filter value has more than 20 decimals, refuse until the probe measures it. Neighbor app tests/neighbors/decimal-store-cut/ should cover SetRange, SetFilter, FieldRef.SetRange, a FlowField Sum with a Decimal filter, CalcSums, a unique secondary key, ModifyAll, and a temporary table (must keep main's result or refuse).
  2. MED - sqltime.rs:150-152 and docs/probes-needed.md:12. The probe (corpus PR 1427) is open and not pinned, yet the path runs with no assumption entry, so passes are not flagged. The probes-needed row says "Measured ..., not assumed", which contradicts the rule.
    Fix: add an entry under crates/al-builtins/src/assumptions/ that cites corpus PR 1427. Gate the cut through it, flag every pass, and reword the row as an assumption.
  3. MED - sqltime.rs:150-152 and docs/decimal-store-probe.md. The code cites a docs file, and the PR template says a doc is not evidence. The doc says BC 28.5 only. The PR body says 27.5, 28.5 and 29.1, and that 29.0 (the CI major) is not established.
    Fix: cite the corpus PR's file:line for each cell. State per major what was measured. Delete docs/decimal-store-probe.md once the cells live in the probe.
  4. MED - sqltime.rs:156-161 on your branch. The refusal covers a primary key field only. A Decimal field in a unique secondary key, or in a key used by Find('=') or SetCurrentKey ordering, is cut silently when two values differ only after 20 decimals. Ties then break by primary key. This is unmeasured.
    Fix: refuse a cut value in any key field until the probe measures it, or add those cells to corpus PR 1427.
  5. LOW - docs/decimal-store-probe.md:3 and docs/probes-needed.md:12. The doc names a specific machine, a country tag, an app GUID and a container cmdlet. Please remove the machine name and those details. This is moot if the doc is deleted per item 3.
  6. LOW - sqltime.rs:154. round_dp_with_strategy runs for every Decimal of every database write. Check d.scale() > 20 first so the common case costs one compare.
  7. LOW - PR body. "ModifyAll (via Modify)" and the XMLport regressions are unverified. After the rebase, list every writer that reaches store (Insert, Modify, ModifyAll, Rename, RecordRef and FieldRef writes, XMLport import, TransferFields then Insert) and show --baseline-diff on BC 27, 28 and 29.

The review flow changed on 2026-11-21. After the fixes, merge main, add the neighbor folder (tests/neighbors/<topic>/ with a neighbor app and differences.tsv, or none.txt), and run tools/neighbor-diff.sh --changed. Paste its table into the pull request, push once, and post a comment with the words "ready for review" when the draft CI is green. The steps are in CONTRIBUTING.md, section "How a pull request gets reviewed".

Generated by an AI agent (Claude) working for Stefan Maron.

@StefanMaron

Copy link
Copy Markdown
Collaborator

Thank you for this pull request, and sorry it waited so long without a review. I'm going to finish it on our side now, so you do not need to do anything more: we merge the current main, add the neighbor check folder the new review flow needs, review it and land it. Your commits stay in the history with your name. If you would rather finish it yourself, say so here and I'll stop.

Generated by an AI agent (Claude) working for Stefan Maron.

@StefanMaron
StefanMaron merged commit 9d62ac0 into aldevtools:main Nov 23, 2026
11 checks passed
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.

3 participants