Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): keep selection SUM and AVG exact for BIGINT and DECIMAL

Jeff4f3e9cfc1380
diff --git a/apps/desktop/src/lib/__tests__/dataGrid/gridSelection.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/gridSelection.spec.ts
index 07a8d6dfe7..49c46ed6c9 100644
--- a/apps/desktop/src/lib/__tests__/dataGrid/gridSelection.spec.ts
+++ b/apps/desktop/src/lib/__tests__/dataGrid/gridSelection.spec.ts
@@ -7,7 +7,7 @@ describe("gridSelection", () => {
cellCount: 0,
rowCount: 0,
numericCount: 0,
- sum: 0,
+ sum: "0",
average: null,
});
});
@@ -25,8 +25,8 @@ describe("gridSelection", () => {
cellCount: 4,
rowCount: 2,
numericCount: 4,
- sum: 10,
- average: 2.5,
+ sum: "10",
+ average: "2.5",
});
});
@@ -43,8 +43,8 @@ describe("gridSelection", () => {
cellCount: 6,
rowCount: 2,
numericCount: 3,
- sum: 105.5,
- average: 105.5 / 3,
+ sum: "105.5",
+ average: "35.166666666667",
});
});
@@ -58,15 +58,15 @@ describe("gridSelection", () => {
cellCount: 8,
rowCount: 8,
numericCount: 0,
- sum: 0,
+ sum: "0",
average: null,
});
});
it.each([
- ["single cell", [[8]], 1, 1, 8],
- ["row", [[1, 3]], 2, 1, 2],
- ["column", [[1], [3]], 2, 2, 2],
+ ["single cell", [[8]], 1, 1, "8"],
+ ["row", [[1, 3]], 2, 1, "2"],
+ ["column", [[1], [3]], 2, 2, "2"],
[
"range",
[
@@ -75,17 +75,58 @@ describe("gridSelection", () => {
],
4,
2,
- 2.5,
+ "2.5",
],
] as const)("averages a %s selection without changing its counts", (_label, rows, cellCount, rowCount, average) => {
expect(summarizeSelection({ columns: [], rows: rows.map((row) => [...row]) })).toMatchObject({ cellCount, rowCount, average });
});
- it("does not expose an infinite average when a finite-value sum overflows", () => {
+ it("keeps a sum of finite values finite where a double would overflow", () => {
+ // The shortest decimal that round-trips to Number.MAX_VALUE, as the grid shows it.
+ const max = `17976931348623157${"0".repeat(292)}`;
expect(summarizeSelection({ columns: ["value"], rows: [[Number.MAX_VALUE], [Number.MAX_VALUE]] })).toMatchObject({
numericCount: 2,
- sum: Number.POSITIVE_INFINITY,
- average: null,
+ sum: (BigInt(max) * 2n).toString(),
+ average: max,
+ });
+ });
+
+ it("sums integers beyond 2^53 exactly, as MySQL BIGINT text", () => {
+ // MySQL returns these for `SUM(big)` / `AVG(big)`; a double prints 1762018465054523400.
+ expect(summarizeSelection({ columns: ["big"], rows: [["1762018465054523393"]] })).toMatchObject({
+ sum: "1762018465054523393",
+ average: "1762018465054523393",
+ });
+ expect(summarizeSelection({ columns: ["big"], rows: [["1762018465054523393"], ["2041776918834970625"], ["-9007199254740993"]] })).toMatchObject({
+ sum: "3794788184634753025",
+ average: "1264929394878251008.333333333333",
+ });
+ expect(summarizeSelection({ columns: ["ubig"], rows: [["18446744073709551615"], ["1"], ["2"]] })).toMatchObject({
+ sum: "18446744073709551618",
+ average: "6148914691236517206",
+ });
+ });
+
+ it("sums DECIMAL text digit for digit", () => {
+ expect(summarizeSelection({ columns: ["dec4"], rows: [["1234567890123456.7891"], ["9876543210987654.3219"], ["0.0001"]] })).toMatchObject({
+ sum: "11111111101111111.1111",
+ average: "3703703700370370.370366666667",
+ });
+ expect(summarizeSelection({ columns: ["value"], rows: [["0.1"], [0.2], ["-0.3"]] })).toMatchObject({ sum: "0", average: "0" });
+ expect(summarizeSelection({ columns: ["value"], rows: [["-0.105"], ["-12.345"], ["3.333"]] })).toMatchObject({ sum: "-9.117", average: "-3.039" });
+ });
+
+ it("keeps at least the digits of the averaged values and rounds the average half away from zero", () => {
+ expect(summarizeSelection({ columns: ["value"], rows: [[1], [1], [2]] })).toMatchObject({ average: "1.333333333333" });
+ expect(summarizeSelection({ columns: ["value"], rows: [[1], [2], [2]] })).toMatchObject({ average: "1.666666666667" });
+ expect(summarizeSelection({ columns: ["value"], rows: [[-1], [-2], [-2]] })).toMatchObject({ average: "-1.666666666667" });
+ expect(summarizeSelection({ columns: ["value"], rows: [["0.00000000000000000001"], ["0"]] })).toMatchObject({ average: "0.000000000000000000005" });
+ });
+
+ it("reads exponent and other numeric text by the value it denotes", () => {
+ expect(summarizeSelection({ columns: ["value"], rows: [["1e3"], ["1E-7"], [1e21], ["0x10"], ["1e-999999"]] })).toMatchObject({
+ numericCount: 5,
+ sum: "1000000000000000001016.0000001",
});
});
@@ -99,8 +140,8 @@ describe("gridSelection", () => {
};
const selection = { columns: ["value"], rows: [[10], ["-4"], ["#6"], ["#-0.5"], ["x"]] };
- expect(summarizeSelection(selection)).toMatchObject({ numericCount: 2, sum: 6, average: 3 });
- expect(summarizeSelection(selection, { numericValue })).toMatchObject({ numericCount: 4, sum: 11.5, average: 2.875 });
+ expect(summarizeSelection(selection)).toMatchObject({ numericCount: 2, sum: "6", average: "3" });
+ expect(summarizeSelection(selection, { numericValue })).toMatchObject({ numericCount: 4, sum: "11.5", average: "2.875" });
});
it("keeps pending summaries count-only and formats sum and average identically", () => {
@@ -108,7 +149,7 @@ describe("gridSelection", () => {
cellCount: 1_000_000,
rowCount: 100_000,
numericCount: 0,
- sum: 0,
+ sum: "0",
average: null,
});
expect(formatSelectionAggregate(-0, "en-US")).toBe("0");
@@ -119,6 +160,15 @@ describe("gridSelection", () => {
expect(formatSelectionAverage(Number.POSITIVE_INFINITY, "en-US")).toBe("—");
});
+ it("formats exact decimal text without a round trip through a double", () => {
+ expect(formatSelectionAggregate("1762018465054523393", "en-US")).toBe("1762018465054523393");
+ expect(formatSelectionAggregate("-0", "en-US")).toBe("0");
+ expect(formatSelectionAggregate("11111111101111111.1111", "en-US")).toBe("11,111,111,101,111,111.1111");
+ expect(formatSelectionAggregate("0.000000000000000000005", "en-US")).toBe("0.000000000000000000005");
+ expect(formatSelectionAggregate("-1234.5", "de-DE")).toBe("-1.234,5");
+ expect(formatSelectionAverage("2.5", "en-US")).toBe("2.5");
+ });
+
it("ignores non-finite and non-numeric cells in the default resolver", () => {
expect(plainSelectionNumericValue(2.5)).toBe(2.5);
expect(plainSelectionNumericValue(" -4 ")).toBe(-4);
diff --git a/apps/desktop/src/lib/dataGrid/gridSelection.ts b/apps/desktop/src/lib/dataGrid/gridSelection.ts
index bb271b0c57..cf8399495d 100644
--- a/apps/desktop/src/lib/dataGrid/gridSelection.ts
+++ b/apps/desktop/src/lib/dataGrid/gridSelection.ts
@@ -26,8 +26,9 @@ export interface SelectionSummary {
cellCount: number;
rowCount: number;
numericCount: number;
- sum: number;
- average: number | null;
+ /** Exact decimal text, so integers beyond 2^53 and DECIMAL scales survive. */
+ sum: string;
+ average: string | null;
}
export interface SelectionSummaryOptions {
@@ -126,26 +127,107 @@ export function plainSelectionNumericValue(value: GridCellValue): number | undef
return Number.isFinite(numeric) ? numeric : undefined;
}
+// Fraction digits an average keeps: at least as many as the previous floating-point
+// display, and beyond that four more than the values it averages, as MySQL does
+// for AVG under its default `div_precision_increment`.
+const SELECTION_AVERAGE_MIN_SCALE = 12;
+const SELECTION_AVERAGE_EXTRA_SCALE = 4;
+// Bounds the exponent of numeric text such as `1e-999999`, which would otherwise
+// build a power of ten large enough to stall the grid.
+const SELECTION_DECIMAL_MAX_SCALE = 400;
+// `Intl.NumberFormat` rejects more fraction digits than this.
+const SELECTION_FORMAT_MAX_FRACTION_DIGITS = 100;
+const DECIMAL_TEXT_PATTERN = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
+
+interface ExactDecimal {
+ coefficient: bigint;
+ scale: number;
+}
+
+function parseExactDecimal(text: string): ExactDecimal | undefined {
+ const match = DECIMAL_TEXT_PATTERN.exec(text);
+ if (!match) return undefined;
+ const [, sign, integerDigits = "", fractionDigits = "", exponentText] = match;
+ if (!integerDigits && !fractionDigits) return undefined;
+ const exponent = exponentText === undefined ? 0 : Number(exponentText);
+ let scale = fractionDigits.length - exponent;
+ if (!Number.isSafeInteger(scale) || Math.abs(scale) > SELECTION_DECIMAL_MAX_SCALE) return undefined;
+ let digits = `${integerDigits}${fractionDigits}`;
+ if (scale < 0) {
+ digits += "0".repeat(-scale);
+ scale = 0;
+ }
+ const magnitude = BigInt(digits);
+ return { coefficient: sign === "-" ? -magnitude : magnitude, scale };
+}
+
+/**
+ * The exact decimal a cell contributes. Numeric text is read digit for digit,
+ * because MySQL BIGINT and DECIMAL values arrive as text precisely so they keep
+ * digits a double cannot hold. A JS number contributes the shortest decimal that
+ * round-trips to it, which is the text the grid displays.
+ */
+function exactSelectionValue(value: GridCellValue, resolved: number): ExactDecimal {
+ if (typeof value === "string") {
+ const trimmed = value.trim();
+ // A grid-specific resolver may map text to some other number; the text only
+ // counts as written when it denotes the same value.
+ if (Number(trimmed) === resolved) {
+ const exact = parseExactDecimal(trimmed);
+ if (exact) return exact;
+ }
+ }
+ return parseExactDecimal(String(resolved)) ?? { coefficient: 0n, scale: 0 };
+}
+
+function rescaleDecimal(value: ExactDecimal, scale: number): bigint {
+ return value.scale === scale ? value.coefficient : value.coefficient * 10n ** BigInt(scale - value.scale);
+}
+
+/** Integer division rounding half away from zero, as MySQL rounds DECIMAL results. */
+function divideRoundHalfAwayFromZero(numerator: bigint, denominator: bigint): bigint {
+ const negative = numerator < 0n !== denominator < 0n;
+ const absNumerator = numerator < 0n ? -numerator : numerator;
+ const absDenominator = denominator < 0n ? -denominator : denominator;
+ const quotient = absNumerator / absDenominator;
+ const rounded = (absNumerator % absDenominator) * 2n >= absDenominator ? quotient + 1n : quotient;
+ return negative ? -rounded : rounded;
+}
+
+function exactDecimalText(value: ExactDecimal): string {
+ const negative = value.coefficient < 0n;
+ const digits = (negative ? -value.coefficient : value.coefficient).toString().padStart(value.scale + 1, "0");
+ const integerPart = digits.slice(0, digits.length - value.scale);
+ const fractionPart = digits.slice(digits.length - value.scale).replace(/0+$/, "");
+ const text = fractionPart ? `${integerPart}.${fractionPart}` : integerPart;
+ return negative && text !== "0" ? `-${text}` : text;
+}
+
export function summarizeSelection(selection: SelectionData, options: SelectionSummaryOptions = {}): SelectionSummary {
const numericValue = options.numericValue ?? plainSelectionNumericValue;
let numericCount = 0;
- let sum = 0;
+ let sum: ExactDecimal = { coefficient: 0n, scale: 0 };
for (const row of selection.rows) {
for (const value of row) {
const resolved = numericValue(value);
if (resolved === undefined || !Number.isFinite(resolved)) continue;
numericCount += 1;
- sum += resolved;
+ const exact = exactSelectionValue(value, resolved);
+ const scale = Math.max(sum.scale, exact.scale);
+ sum = { coefficient: rescaleDecimal(sum, scale) + rescaleDecimal(exact, scale), scale };
}
}
+ const averageScale = Math.max(sum.scale + SELECTION_AVERAGE_EXTRA_SCALE, SELECTION_AVERAGE_MIN_SCALE);
+ const average = numericCount > 0 ? exactDecimalText({ coefficient: divideRoundHalfAwayFromZero(rescaleDecimal(sum, averageScale), BigInt(numericCount)), scale: averageScale }) : null;
+
return {
cellCount: selection.rows.reduce((count, row) => count + row.length, 0),
rowCount: selection.rows.length,
numericCount,
- sum,
- average: numericCount > 0 && Number.isFinite(sum) ? sum / numericCount : null,
+ sum: exactDecimalText(sum),
+ average,
};
}
@@ -154,16 +236,28 @@ export function createPendingSelectionSummary(cellCount: number, rowCount: numbe
cellCount,
rowCount,
numericCount: 0,
- sum: 0,
+ sum: "0",
average: null,
};
}
-export function formatSelectionAggregate(value: number, locales?: Intl.LocalesArgument): string {
+/**
+ * Integers print as plain digits and fractions in the locale's notation. Decimal
+ * text from {@link summarizeSelection} is formatted digit for digit; a JS number
+ * keeps at most 12 fraction digits.
+ */
+export function formatSelectionAggregate(value: number | string, locales?: Intl.LocalesArgument): string {
+ if (typeof value === "string") {
+ if (!value.includes(".")) return value === "-0" ? "0" : value;
+ const fractionDigits = Math.min(value.length - value.indexOf(".") - 1, SELECTION_FORMAT_MAX_FRACTION_DIGITS);
+ // Intl.NumberFormat formats string input as an exact decimal, without a round trip through a double.
+ return new Intl.NumberFormat(locales, { maximumFractionDigits: fractionDigits }).format(value as unknown as number);
+ }
const normalized = Object.is(value, -0) ? 0 : value;
return Number.isInteger(normalized) ? String(normalized) : normalized.toLocaleString(locales, { maximumFractionDigits: 12 });
}
-export function formatSelectionAverage(value: number | null | undefined, locales?: Intl.LocalesArgument): string {
+export function formatSelectionAverage(value: number | string | null | undefined, locales?: Intl.LocalesArgument): string {
+ if (typeof value === "string") return formatSelectionAggregate(value, locales);
return typeof value === "number" && Number.isFinite(value) ? formatSelectionAggregate(value, locales) : "—";
}
diff --git a/packages/app-tests/mongoDocumentValues.test.ts b/packages/app-tests/mongoDocumentValues.test.ts
index 2c8100d25f..61321e3d44 100644
--- a/packages/app-tests/mongoDocumentValues.test.ts
+++ b/packages/app-tests/mongoDocumentValues.test.ts
@@ -532,8 +532,8 @@ test("counts typed BSON scalars as numbers so a column total keeps its negative
const summary = summarizeSelection({ columns: ["charactersUsed"], rows }, { numericValue: mongoDocumentGridNumericValue });
assert.equal(summary.numericCount, 5);
- assert.equal(summary.sum, 2528.125 - 12.5 - 100.5 - 5 + 9);
- assert.equal(summary.average, (2528.125 - 12.5 - 100.5 - 5 + 9) / 5);
+ assert.equal(summary.sum, "2419.125");
+ assert.equal(summary.average, "483.825");
});
test("keeps non-numeric Mongo cells out of a column total", () => {