Skip to content

fix: preserve negative zero in JSONB floating-point values - #7821

Open
DarrenChangJR wants to merge 1 commit into
alibaba:mainfrom
DarrenChangJR:fix/jsonb-negative-zero
Open

fix: preserve negative zero in JSONB floating-point values#7821
DarrenChangJR wants to merge 1 commit into
alibaba:mainfrom
DarrenChangJR:fix/jsonb-negative-zero

Conversation

@DarrenChangJR

Copy link
Copy Markdown

What this PR does and why

JSONB currently changes -0.0F and -0.0D into positive zero. The writer's
compact integer encodings use numeric equality, under which negative zero equals
zero, and discard the IEEE-754 sign bit.

This change recognizes negative zero using raw bits and bypasses only the
integer/zero compact encodings. It then uses the existing BC_FLOAT or
BC_DOUBLE representation, which preserves the sign. Positive zero, one, and
other integral floating-point values remain compact.

The behavior matters even though -0.0 == 0.0: raw-bit APIs distinguish the
values, division produces infinities with different signs, and Fastjson2's text
path already preserves negative zero.

Tests

NegativeZeroTest covers both float and double through:

  • untyped scalar parsing;
  • explicitly typed scalar parsing;
  • primitive arrays;
  • boxed arrays;
  • primitive bean fields;
  • boxed bean fields.

Validation performed:

  • focused NegativeZeroTest;
  • clean full mvn -pl core clean test suite: 7,977 tests, no failures;
  • external JSONB invariant matrix: exactly 12 signed-zero violations removed,
    with all unrelated findings unchanged.

Checklist

  • The change is limited to JSONB floating-point encoding.
  • Existing compact encodings are preserved except where they lose the sign.
  • Tests compare raw IEEE-754 bits, not numeric equality.
  • No public API or documentation change is required.
中文说明

修改内容和原因

JSONB 当前会把 -0.0F-0.0D 编码成正零。writer 的紧凑整数编码使用数值
相等判断,而负零与正零在该判断下相等,因此 IEEE-754 符号位会丢失。

本修改使用原始位识别负零,并且只让负零绕过整数/零的紧凑编码,继续使用现有
BC_FLOATBC_DOUBLE 表示。正零、1 和其他可紧凑表示的浮点整数保持不变。

回归测试覆盖 float/double 的无类型和显式类型标量、primitive/boxed 数组以及
primitive/boxed bean 字段,并用原始 IEEE-754 位比较结果。focused test、clean
full core 7,977 项测试和外部 JSONB invariant matrix 均通过。

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