Skip to content

[feature] Implement declare decimal-format for XQuery 3.1#6339

Merged
line-o merged 3 commits into
eXist-db:developfrom
joewiz:extract/declare-decimal-format
May 11, 2026
Merged

[feature] Implement declare decimal-format for XQuery 3.1#6339
line-o merged 3 commits into
eXist-db:developfrom
joewiz:extract/declare-decimal-format

Conversation

@joewiz
Copy link
Copy Markdown
Member

@joewiz joewiz commented May 10, 2026

Summary

Adds the XQuery 3.1 declare decimal-format prolog declaration. Per W3C XPath/XQuery 3.1 §4.16 (https://www.w3.org/TR/xquery-31/#prod-xquery31-DecimalFormatDecl), this prolog form lets an XQuery module declare named (or default) decimal-format properties that subsequent fn:format-number() calls reference.

This PR is an extraction from PR #6217 (v2/declare-decimal-format) per the 2026-05-10 v2/* extraction audit. The whole branch is XQ 3.1-mandatory by spec — extracting it directly to develop removes a 3.1 conformance gap from the 7.0 release path without waiting for the broader v2/xq4-* feature set.

What changed

Three commits cherry-picked from joewiz:v2/declare-decimal-format:

  1. [feature] Implement declare decimal-format for XQuery 3.1
  2. [refactor] Address review: add df prefix to decimal-format helpers, add Javadoc
  3. [refactor] Use switch expression for decimal-format property dispatch

Touched files:

  • exist-core/src/main/antlr/org/exist/xquery/parser/XQuery.g — adds DecimalFormatDecl production
  • exist-core/src/main/antlr/org/exist/xquery/parser/XQueryTree.g — tree-walker integration
  • exist-core/src/main/java/org/exist/xquery/DecimalFormat.java — prolog handler with property semantics
  • exist-core/src/main/java/org/exist/xquery/ErrorCodes.java — adds XQST0097, XQST0098
  • exist-core/src/main/java/org/exist/xquery/XQueryContext.java — wiring
  • exist-core/src/test/xquery/numbers/format-numbers.xql — test additions

One conflict resolved during cherry-pick: ErrorCodes.java had additive divergence between v2's older base (string-concat XQDY0101, no XQST0118/XQST0125) and current develop (text-block XQDY0101, XQST0118/XQST0125 present). Resolution kept develop's modern forms and appended the two new decimal-format codes (XQST0097, XQST0098) using the same text-block style.

Spec references

XQTS deltas (XQ 3.1, 2026-05-10)

Test set Before After Delta
prod-DecimalFormatDecl 2/30 23/29 +21 passing
fn-format-number 76/78 76/78 0 (already passing)

Lift matches the audit's +19-25 prediction. The remaining 6 prod-DecimalFormatDecl failures are spec-edge cases (picture-string property validation paths) tracked separately on the v2 branch and out of scope for this minimal extraction.

Test plan

  • mvn install -pl exist-core -am -DskipTests green
  • mvn test -pl exist-core -Dtest='xquery.numbers.NumberTests' — 161 tests, 0 failures (covers format-numbers.xql)
  • Full mvn test -pl exist-core (lock-coordinated) — 6708 tests, 0 failures, 0 errors, 97 skipped, BUILD SUCCESS in 18:49
  • XQTS XQ 3.1 confirmation — patched runner (applyVersionHint cap-at-3.1), 367 test sets, +21 in prod-DecimalFormatDecl
  • CI gate (will run on push)

Source

Cherry-picked from joewiz:v2/declare-decimal-format (PR #6217). After this PR merges, PR #6217 should be closed as superseded.

🤖 Generated with Claude Code

joewiz and others added 3 commits May 10, 2026 16:37
Add parser support for the XQuery 3.1 `declare decimal-format` and
`declare default decimal-format` prolog declarations (spec section 4.10),
enabling users to customize number formatting via fn:format-number.

The runtime infrastructure (DecimalFormat class, XQueryContext storage,
FnFormatNumbers 3-arg support) was already in place — this adds the
missing parser recognition and tree walker processing.

Changes:
- XQuery.g: Add DECIMAL_FORMAT_DECL/DEF_DECIMAL_FORMAT_DECL tokens,
  grammar rules for named and default forms, property keywords
- XQueryTree.g: Walk AST, validate properties (single-char, zero-digit,
  distinctness), register formats in XQueryContext
- ErrorCodes.java: Add XQST0097 (duplicate) and XQST0098 (invalid)
- XQueryContext.java: Add setDefaultStaticDecimalFormat() convenience
- format-numbers.xql: Add tests for named/default formats, custom
  NaN/infinity, and error cases

Closes eXist-db#56

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…dd Javadoc

Rename the three decimal-format validation helper methods in XQueryTree.g
with a `df` prefix to clarify their scope:

- requireSingleChar → dfRequireSingleChar
- validateZeroDigit → dfValidateZeroDigit
- validateDistinctPictureChars → dfValidateDistinctPictureChars

Add Javadoc comments on DecimalFormat.UNNAMED and UNNAMED_DECIMAL_FORMAT
explaining the XPath 3.1 spec origin of the "unnamed" terminology.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Address @reinhapa's review comment on PR eXist-db#6217: convert the
property-name dispatch in the decimal-format AST handler from a
traditional switch/break statement into arrow-syntax cases. No
behavioural change; NumberTests still pass (161 run, 0 failures).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@joewiz joewiz requested a review from a team as a code owner May 10, 2026 21:15
Copy link
Copy Markdown
Member

@line-o line-o left a comment

Choose a reason for hiding this comment

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

Nice and clean!

@line-o line-o requested a review from a team May 10, 2026 21:24
@line-o line-o added the xquery issue is related to xquery implementation label May 10, 2026
@line-o line-o added this to Wave 2 and v7.0.0 May 10, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Wave 2 May 10, 2026
@line-o line-o added this to the eXist-7.0.0 milestone May 10, 2026
@line-o line-o moved this from Todo to In progress in Wave 2 May 10, 2026
@line-o line-o moved this to In review in v7.0.0 May 10, 2026
@line-o line-o merged commit 136dcdd into eXist-db:develop May 11, 2026
14 of 15 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in Wave 2 May 11, 2026
@github-project-automation github-project-automation Bot moved this from In review to Done in v7.0.0 May 11, 2026
duncdrum pushed a commit that referenced this pull request May 11, 2026
The MathContext.DECIMAL64 cap on the final BigDecimal coercion truncated
high-precision xs:decimal/xs:integer inputs to 16 significant digits.
Removing the multiply-by-ONE noise op preserves full precision; the
subsequent .round() still handles the maximum fractional part size.

xs:double inputs are unaffected: DoubleValue.toDecimalValue() already
uses BigDecimal.valueOf(value), which produces the canonical short form
rather than the exact bit-pattern decimal expansion.

XQTS 3.1 fn-format-number delta:
- before: 74 / 77 passing (3 failures: numberformat63, numberformat64,
  numberformat901err -- the last unrelated, fixed by #6339)
- after:  78 / 78 passing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

xquery issue is related to xquery implementation

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants