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(query): keep multi-database merged column totals exact

Jeff270a61cd1e87
diff --git a/apps/desktop/src/lib/dataGrid/gridSelection.ts b/apps/desktop/src/lib/dataGrid/gridSelection.ts
index cf8399495d..652b331aaf 100644
--- a/apps/desktop/src/lib/dataGrid/gridSelection.ts
+++ b/apps/desktop/src/lib/dataGrid/gridSelection.ts
@@ -139,7 +139,7 @@ const SELECTION_DECIMAL_MAX_SCALE = 400;
const SELECTION_FORMAT_MAX_FRACTION_DIGITS = 100;
const DECIMAL_TEXT_PATTERN = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
-interface ExactDecimal {
+export interface ExactDecimal {
coefficient: bigint;
scale: number;
}
@@ -184,6 +184,13 @@ function rescaleDecimal(value: ExactDecimal, scale: number): bigint {
return value.scale === scale ? value.coefficient : value.coefficient * 10n ** BigInt(scale - value.scale);
}
+/** Adds a cell to an exact running total; `resolved` is the number the cell counts as. */
+export function addExactSelectionValue(sum: ExactDecimal, value: GridCellValue, resolved: number): ExactDecimal {
+ const exact = exactSelectionValue(value, resolved);
+ const scale = Math.max(sum.scale, exact.scale);
+ return { coefficient: rescaleDecimal(sum, scale) + rescaleDecimal(exact, scale), 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;
@@ -194,7 +201,7 @@ function divideRoundHalfAwayFromZero(numerator: bigint, denominator: bigint): bi
return negative ? -rounded : rounded;
}
-function exactDecimalText(value: ExactDecimal): string {
+export 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);
@@ -213,9 +220,7 @@ export function summarizeSelection(selection: SelectionData, options: SelectionS
const resolved = numericValue(value);
if (resolved === undefined || !Number.isFinite(resolved)) continue;
numericCount += 1;
- const exact = exactSelectionValue(value, resolved);
- const scale = Math.max(sum.scale, exact.scale);
- sum = { coefficient: rescaleDecimal(sum, scale) + rescaleDecimal(exact, scale), scale };
+ sum = addExactSelectionValue(sum, value, resolved);
}
}
diff --git a/apps/desktop/src/lib/query/__tests__/multiSourceResult.spec.ts b/apps/desktop/src/lib/query/__tests__/multiSourceResult.spec.ts
index c27d7ecde9..410b77dad8 100644
--- a/apps/desktop/src/lib/query/__tests__/multiSourceResult.spec.ts
+++ b/apps/desktop/src/lib/query/__tests__/multiSourceResult.spec.ts
@@ -68,7 +68,7 @@ describe("mergeMultiSourceResults", () => {
["a", "Alice", 10, "north"],
["b", "Bob", 20, null],
]);
- expect(merged.summaries).toEqual([{ columnIndex: 2, column: "amount", sum: 30, count: 2 }]);
+ expect(merged.summaries).toEqual([{ columnIndex: 2, column: "amount", sum: "30", count: 2 }]);
expect(merged.sources.map((source) => source.columnCount)).toEqual([3, 2]);
expect(first.columns).toEqual(["name", "__DBX_PK_0", "amount", "region"]);
expect(first.rows).toEqual([["Alice", 17, 10, "north"]]);
@@ -88,7 +88,7 @@ describe("mergeMultiSourceResults", () => {
["a", 1, 2, 3],
["b", 4, 5, 6],
]);
- expect(merged.summaries.map((summary) => summary.sum)).toEqual([5, 7, 9]);
+ expect(merged.summaries.map((summary) => summary.sum)).toEqual(["5", "7", "9"]);
});
it("skips sources without a tabular payload instead of failing", () => {
@@ -148,8 +148,8 @@ describe("mergeMultiSourceResults", () => {
);
expect(merged.summaries).toEqual([
- { columnIndex: 1, column: "id", sum: 3, count: 2 },
- { columnIndex: 2, column: "amount", sum: 42.5, count: 2 },
+ { columnIndex: 1, column: "id", sum: "3", count: 2 },
+ { columnIndex: 2, column: "amount", sum: "42.5", count: 2 },
]);
});
@@ -189,7 +189,25 @@ describe("summarizeNumericColumns", () => {
["", 3],
],
),
- ).toEqual([{ columnIndex: 0, column: "a", sum: 1, count: 1 }]);
+ ).toEqual([{ columnIndex: 0, column: "a", sum: "1", count: 1 }]);
+ });
+
+ it("sums BIGINT and DECIMAL text exactly instead of through a double", () => {
+ expect(
+ summarizeNumericColumns(
+ ["big", "ubig", "dec4"],
+ [
+ ["1762018465054523393", "18446744073709551615", "1234567890123456.7891"],
+ ["9007199254740993", "1", "0.0001"],
+ ["1762018465054523393", "2", "9876543210987654.3219"],
+ ["-1", "9007199254740993", "0.0003"],
+ ],
+ ).map((summary) => summary.sum),
+ ).toEqual(["3533044129363787778", "18455751272964292611", "11111111101111111.1114"]);
+ });
+
+ it("adds numbers by their displayed text and other numeric text by its value", () => {
+ expect(summarizeNumericColumns(["n"], [[0.1], [0.2], ["0x10"], [" 1e3 "]])).toEqual([{ columnIndex: 0, column: "n", sum: "1016.3", count: 4 }]);
});
it("never sums boolean or all-empty columns", () => {
diff --git a/apps/desktop/src/lib/query/multiSourceResult.ts b/apps/desktop/src/lib/query/multiSourceResult.ts
index 1d21fc0b90..6266ec7629 100644
--- a/apps/desktop/src/lib/query/multiSourceResult.ts
+++ b/apps/desktop/src/lib/query/multiSourceResult.ts
@@ -1,3 +1,4 @@
+import { addExactSelectionValue, exactDecimalText, type ExactDecimal } from "@/lib/dataGrid/gridSelection";
import type { QueryResult } from "@/types/database";
/**
@@ -34,7 +35,8 @@ export interface MultiSourceMergeOptions {
export interface MultiSourceColumnSummary {
columnIndex: number;
column: string;
- sum: number;
+ /** Exact decimal text, so integers beyond 2^53 and DECIMAL scales survive. */
+ sum: string;
/** Number of contributing values; empty cells never count. */
count: number;
}
@@ -86,7 +88,7 @@ function isEmptyCell(value: MultiSourceCellValue | undefined): boolean {
* parses to one (drivers return DECIMAL/BIGINT as strings). Booleans are not
* numeric, so a flag column never turns into a total.
*/
-export function isNumericSummaryCellValue(value: MultiSourceCellValue | undefined): boolean {
+export function isNumericSummaryCellValue(value: MultiSourceCellValue | undefined): value is number | string {
if (typeof value === "number") return Number.isFinite(value);
if (typeof value === "string") {
const trimmed = value.trim();
@@ -102,7 +104,7 @@ export function isNumericSummaryCellValue(value: MultiSourceCellValue | undefine
*/
export function summarizeNumericColumns(columns: readonly string[], rows: readonly (readonly MultiSourceCellValue[])[], options: { skipColumnIndexes?: readonly number[] } = {}): MultiSourceColumnSummary[] {
const skipped = new Set(options.skipColumnIndexes ?? []);
- const sums: number[] = Array.from({ length: columns.length }, () => 0);
+ const sums: ExactDecimal[] = Array.from({ length: columns.length }, () => ({ coefficient: 0n, scale: 0 }));
const counts: number[] = Array.from({ length: columns.length }, () => 0);
const stillNumeric: boolean[] = Array.from({ length: columns.length }, () => true);
@@ -115,7 +117,7 @@ export function summarizeNumericColumns(columns: readonly string[], rows: readon
stillNumeric[columnIndex] = false;
continue;
}
- sums[columnIndex] += typeof value === "number" ? value : Number(value);
+ sums[columnIndex] = addExactSelectionValue(sums[columnIndex]!, value, typeof value === "number" ? value : Number(value));
counts[columnIndex] += 1;
}
}
@@ -123,7 +125,7 @@ export function summarizeNumericColumns(columns: readonly string[], rows: readon
const summaries: MultiSourceColumnSummary[] = [];
for (let columnIndex = 0; columnIndex < columns.length; columnIndex += 1) {
if (skipped.has(columnIndex) || !stillNumeric[columnIndex] || counts[columnIndex] === 0) continue;
- summaries.push({ columnIndex, column: columns[columnIndex] ?? "", sum: sums[columnIndex]!, count: counts[columnIndex]! });
+ summaries.push({ columnIndex, column: columns[columnIndex] ?? "", sum: exactDecimalText(sums[columnIndex]!), count: counts[columnIndex]! });
}
return summaries;
}