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): sort bigint primary keys and numbers numerically

lxk955f3a3424738c3
diff --git a/apps/desktop/src/lib/__tests__/dataGrid/dataGridSort.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/dataGridSort.spec.ts
index 2f5faa3524..a0ed7e7255 100644
--- a/apps/desktop/src/lib/__tests__/dataGrid/dataGridSort.spec.ts
+++ b/apps/desktop/src/lib/__tests__/dataGrid/dataGridSort.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { compareDataGridValues, databaseSortSupportedForDatabase, primaryKeyTieBreakerIndexes, simpleDataGridOrderByColumn, simpleDataGridOrderByMatchesSort, simpleDataGridOrderByReferencesMissingColumn } from "@/lib/dataGrid/dataGridSort";
+import { compareDataGridValues, databaseSortSupportedForDatabase, primaryKeyTieBreakerIndexes, simpleDataGridOrderByColumn, simpleDataGridOrderByMatchesSort, simpleDataGridOrderByReferencesMissingColumn, sortDataGridRows } from "@/lib/dataGrid/dataGridSort";
describe("databaseSortSupportedForDatabase", () => {
it("keeps database-side sorting for databases that support ORDER BY", () => {
@@ -78,6 +78,41 @@ describe("compareDataGridValues numeric ordering", () => {
expect(compareDataGridValues("9007199254740993.123456789012", "9007199254740993.123456789013", columnType)).toBeLessThan(0);
expect(compareDataGridValues("-9007199254740993.123456789013", "-9007199254740993.123456789012", columnType)).toBeLessThan(0);
});
+
+ it("sorts bigint primary keys numerically without columnType (issue #13668)", () => {
+ // When columnType is omitted or undefined, numeric strings such as bigint IDs
+ // must compare numerically rather than using string dictionary collation.
+ expect(compareDataGridValues("9249", "92489")).toBeLessThan(0);
+ expect(compareDataGridValues("92489", "9249")).toBeGreaterThan(0);
+ expect(compareDataGridValues("92492", "100000")).toBeLessThan(0);
+ expect(compareDataGridValues("92492", "1786326962000000000")).toBeLessThan(0);
+ expect(compareDataGridValues("-10", "-5")).toBeLessThan(0);
+ });
+
+ it("sorts MySQL bigint(20) and unsigned bigint columns numerically (issue #13668)", () => {
+ for (const columnType of ["bigint", "bigint(20)", "bigint unsigned", "bigint(20) unsigned", "unsigned bigint"]) {
+ expect(compareDataGridValues("9249", "92489", columnType)).toBeLessThan(0);
+ expect(compareDataGridValues("92492", "100000", columnType)).toBeLessThan(0);
+ expect(compareDataGridValues("92492", "1786326962000000000", columnType)).toBeLessThan(0);
+ }
+ });
+
+ it("orders native bigint cell values", () => {
+ expect(compareDataGridValues(9249n, 92489n)).toBeLessThan(0);
+ expect(compareDataGridValues(1786326962000000000n, 92492n)).toBeGreaterThan(0);
+ expect(compareDataGridValues(-10n, -5n)).toBeLessThan(0);
+ expect(compareDataGridValues(42n, 42n)).toBe(0);
+ });
+
+ it("sorts rows by bigint column in descending and ascending order", () => {
+ const rows = [["92492"], ["92491"], ["92490"], ["9249"], ["92489"], ["100000"], ["1786326962000000000"]];
+
+ const sortedDesc = sortDataGridRows(rows, 0, "desc", "bigint(20)");
+ expect(sortedDesc).toEqual([["1786326962000000000"], ["100000"], ["92492"], ["92491"], ["92490"], ["92489"], ["9249"]]);
+
+ const sortedAsc = sortDataGridRows(rows, 0, "asc", "bigint(20)");
+ expect(sortedAsc).toEqual([["9249"], ["92489"], ["92490"], ["92491"], ["92492"], ["100000"], ["1786326962000000000"]]);
+ });
});
describe("compareDataGridValues datetime ordering", () => {
diff --git a/apps/desktop/src/lib/dataGrid/dataGridColumnType.ts b/apps/desktop/src/lib/dataGrid/dataGridColumnType.ts
index 3ff6007308..4b9772e17d 100644
--- a/apps/desktop/src/lib/dataGrid/dataGridColumnType.ts
+++ b/apps/desktop/src/lib/dataGrid/dataGridColumnType.ts
@@ -197,6 +197,9 @@ export function isNumericColumnType(dataType: string | undefined): boolean {
if (openIndex <= 0 || !TRANSPARENT_NUMERIC_TYPE_WRAPPERS.has(normalized.slice(0, openIndex).trim())) break;
normalized = normalized.slice(openIndex + 1, -1).trim();
}
+ if (normalized.startsWith("unsigned ") || normalized.startsWith("signed ")) {
+ normalized = normalized.slice(normalized.indexOf(" ") + 1).trim();
+ }
const base = normalized.split(/[\s([]/, 1)[0];
return NUMERIC_COLUMN_TYPE_BASES.has(base);
}
diff --git a/apps/desktop/src/lib/dataGrid/dataGridSort.ts b/apps/desktop/src/lib/dataGrid/dataGridSort.ts
index 3a2ff7e1ac..5910314cfb 100644
--- a/apps/desktop/src/lib/dataGrid/dataGridSort.ts
+++ b/apps/desktop/src/lib/dataGrid/dataGridSort.ts
@@ -83,7 +83,7 @@ export function nextDataGridSortState(current: DataGridSortState, column: string
return { column, columnIndex, direction: "asc" };
}
-type DataGridCellValue = string | number | boolean | null | undefined;
+type DataGridCellValue = string | number | bigint | boolean | null | undefined;
type DataGridRow = DataGridCellValue[];
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
@@ -122,9 +122,21 @@ export function compareDataGridValues(left: DataGridCellValue, right: DataGridCe
if (typeof left === "number" && typeof right === "number") {
return compareNumbers(left, right);
}
+ if (typeof left === "bigint" && typeof right === "bigint") {
+ return compareBigInts(left, right);
+ }
if (typeof left === "boolean" && typeof right === "boolean") {
return Number(left) - Number(right);
}
+
+ // When columnType is not specified (e.g. untyped results, omitted columnType),
+ // if both values represent valid numbers/bigints, compare them numerically so
+ // numeric columns and primary keys do not fall back to string dictionary collation (#13668).
+ if (!columnType) {
+ const numericCompared = compareNumericCellValues(left, right);
+ if (numericCompared !== null) return numericCompared;
+ }
+
if (typeof left === "string" && typeof right === "string") {
const leftDate = dateSortValue(left);
const rightDate = dateSortValue(right);
@@ -157,6 +169,12 @@ function compareNumericCellValues(left: DataGridCellValue, right: DataGridCellVa
}
function parseNumericSortValue(value: DataGridCellValue): NumericSortValue | null {
+ if (typeof value === "bigint") {
+ if (value === 0n) return { sign: 0, magnitude: 0n, digits: "0" };
+ const sign = value < 0n ? -1 : 1;
+ const unsigned = (sign === -1 ? -value : value).toString();
+ return { sign, magnitude: BigInt(unsigned.length), digits: unsigned };
+ }
if (typeof value !== "string" && typeof value !== "number") return null;
const text = String(value).trim();
const negative = text.startsWith("-");
diff --git a/apps/desktop/src/stores/__tests__/queryStore.resultViewGeneration.spec.ts b/apps/desktop/src/stores/__tests__/queryStore.resultViewGeneration.spec.ts
index e2a552ce19..0b8a29267f 100644
--- a/apps/desktop/src/stores/__tests__/queryStore.resultViewGeneration.spec.ts
+++ b/apps/desktop/src/stores/__tests__/queryStore.resultViewGeneration.spec.ts
@@ -56,6 +56,28 @@ describe("queryStore resultViewGeneration", { timeout: 30_000 }, () => {
expect(tab.resultViewGeneration).not.toBe("gen-old");
});
+ it("sorts bigint primary keys numerically using tableMeta type when result column_types is omitted (issue #13668)", async () => {
+ const { useQueryStore } = await import("@/stores/queryStore");
+ const store = useQueryStore();
+ const tabId = store.createTab("mysql-1", "app", "users", "data");
+ const tab = store.tabs.find((item) => item.id === tabId)!;
+ tab.tableMeta = {
+ tableName: "users",
+ columns: [{ name: "id", data_type: "bigint(20)", is_primary_key: true } as any],
+ primaryKeys: ["id"],
+ };
+ tab.result = {
+ columns: ["id"],
+ rows: [["92492"], ["9249"], ["100000"], ["92489"], ["1786326962000000000"]],
+ affected_rows: 0,
+ execution_time_ms: 1,
+ };
+
+ store.sortTabResultLocally(tabId, "id", 0, "desc");
+
+ expect(tab.result.rows.map((row) => row[0])).toEqual(["1786326962000000000", "100000", "92492", "92489", "9249"]);
+ });
+
it("inherits the generation when a result is restored from disk", async () => {
const cached = sampleResult(2);
vi.doMock("@/lib/tabs/tabResultCache", async (importOriginal) => {
diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts
index 1d76800749..158ae9c3f5 100644
--- a/apps/desktop/src/stores/queryStore.ts
+++ b/apps/desktop/src/stores/queryStore.ts
@@ -2710,7 +2710,7 @@ export const useQueryStore = defineStore("query", () => {
}
const originalRows = tab.resultLocalSortOriginalRows;
- const columnType = tab.result.column_types?.[columnIndex];
+ const columnType = (tab.result.column_types?.[columnIndex]?.trim() || undefined) ?? (tab.tableMeta?.columns?.find((c) => c.name.toLowerCase() === column.toLowerCase())?.data_type?.trim() || undefined) ?? (tab.resultColumnTypeLabels?.[columnIndex]?.trim() || undefined);
const rowIndexes = direction ? sortDataGridRowIndexes(originalRows, columnIndex, direction, columnType) : originalRows.map((_, index) => index);
const rows = rowIndexes.map((index) => originalRows[index]!);
const originalMongoDocuments = tab.resultLocalSortOriginalMongoDocuments;