fix(iotdb): correct timestamp display and table editing

This commit is contained in:
zipg authored and GitHub committed 2026-08-18 23:24:57 +08:00
1 parent fa4238c5d3
commit b7338518d1
16 files changed
+1031 -73

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { normalizeSupportedDateTimePattern } from "@/lib/dataGrid/columnFormatter";
import { applyColumnFormatter, defaultIoTDBTimestampFormatter, formatIoTDBTimestampEditorValue, normalizeSupportedDateTimePattern, parseIoTDBTimestampEditorValue } from "@/lib/dataGrid/columnFormatter";
describe("normalizeSupportedDateTimePattern", () => {
it("accepts the format grammar shared by the frontend and backend", () => {
@@ -12,3 +12,54 @@ describe("normalizeSupportedDateTimePattern", () => {
expect(normalizeSupportedDateTimePattern("%Y-%m-%d")).toBe("");
});
});
describe("defaultIoTDBTimestampFormatter", () => {
it.each([
["ms", "1786954706123", "2026-08-17T16:18:26.123+08:00"],
["us", "1786954706123456", "2026-08-17T16:18:26.123456+08:00"],
["ns", "1786954706123456789", "2026-08-17T16:18:26.123456789+08:00"],
])("formats %s precision in the connection time zone without replacing the raw value", (precision, rawValue, expected) => {
const formatter = defaultIoTDBTimestampFormatter("iotdb", `TIMESTAMP(${precision})`, "time_zone=Asia%2FShanghai");
const row = [rawValue];
expect(applyColumnFormatter(row[0], formatter)).toBe(expected);
expect(row).toEqual([rawValue]);
});
it("does not guess when precision metadata is absent or invalid", () => {
expect(defaultIoTDBTimestampFormatter("iotdb", "TIMESTAMP", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(defaultIoTDBTimestampFormatter("iotdb", "TIMESTAMP(seconds)", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(defaultIoTDBTimestampFormatter("mysql", "TIMESTAMP(ms)", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(defaultIoTDBTimestampFormatter("iotdb", "INT64", "time_zone=Asia%2FShanghai")).toBeUndefined();
});
it("uses UTC when the connection does not specify a time zone", () => {
const formatter = defaultIoTDBTimestampFormatter("iotdb", "TIMESTAMP(ms)", "");
expect(applyColumnFormatter(1, formatter)).toBe("1970-01-01T00:00:00.001+00:00");
});
it("round-trips a negative nanosecond timestamp without truncating toward zero", () => {
const formatter = defaultIoTDBTimestampFormatter("iotdb", "TIMESTAMP(ns)", "time_zone=UTC");
const display = "1969-12-31T23:59:59.999999999+00:00";
expect(applyColumnFormatter("-1", formatter)).toBe(display);
expect(parseIoTDBTimestampEditorValue(display, "iotdb", "TIMESTAMP(ns)", "time_zone=UTC")).toBe("-1");
});
it.each([
["ms", "1786954706123", "2026-08-17T16:18:26.123+08:00"],
["us", "1786954706123456", "2026-08-17T16:18:26.123456+08:00"],
["ns", "1786954706123456789", "2026-08-17T16:18:26.123456789+08:00"],
])("round-trips %s precision through the temporal editor", (precision, rawValue, editorValue) => {
const columnType = `TIMESTAMP(${precision})`;
expect(formatIoTDBTimestampEditorValue(rawValue, "iotdb", columnType, "time_zone=Asia%2FShanghai")).toBe(editorValue);
expect(parseIoTDBTimestampEditorValue(editorValue, "iotdb", columnType, "time_zone=UTC")).toBe(rawValue);
expect(parseIoTDBTimestampEditorValue(editorValue.replace("T", " ").replace("+08:00", ""), "iotdb", columnType, "time_zone=Asia%2FShanghai")).toBe(rawValue);
});
it("keeps invalid and unrelated editor values on the generic path", () => {
expect(parseIoTDBTimestampEditorValue("2026-02-30 10:00:00", "iotdb", "TIMESTAMP(ms)", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(parseIoTDBTimestampEditorValue("2026-08-17 10:00:00.1234", "iotdb", "TIMESTAMP(ms)", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(parseIoTDBTimestampEditorValue("2026-08-17 10:00:00", "mysql", "TIMESTAMP(ms)", "time_zone=Asia%2FShanghai")).toBeUndefined();
expect(parseIoTDBTimestampEditorValue("2026-08-17 10:00:00", "iotdb", "TIMESTAMP", "time_zone=Asia%2FShanghai")).toBeUndefined();
});
});
@@ -9,6 +9,7 @@ dayjs.extend(utc);
dayjs.extend(timezone);
export type DateTimeFormatterUnit = "seconds" | "milliseconds" | "auto";
export type IoTDBTimestampPrecision = "ms" | "us" | "ns";
const DEFAULT_DATETIME_PATTERN = "YYYY-MM-DD HH:mm:ss";
export const DateTimePatterns = [
"YYYY-MM-DD",
@@ -101,6 +102,7 @@ export function normalizeSupportedDateTimePattern(value: string): string {
export type ColumnFormatterConfig =
| { kind: "datetime"; unit: DateTimeFormatterUnit; pattern: string; timezone: string | undefined }
| { kind: "iotdb-timestamp"; precision: IoTDBTimestampPrecision; timezone: string }
| { kind: "json-path"; path: string }
| { kind: "mask"; prefix: number; suffix: number }
| { kind: "foreign-key-display"; refSchema?: string; refTable: string; refColumn: string; displayColumn: string }
@@ -212,6 +214,77 @@ export function resolveColumnFormatter(formatter: ColumnFormatterConfig | undefi
return customFormatter ? { kind: "custom-template", template: customFormatter.template } : undefined;
}
export function defaultIoTDBTimestampFormatter(databaseType: string | undefined, columnType: string | null | undefined, urlParams: string | undefined): ColumnFormatterConfig | undefined {
const precision = iotdbTimestampPrecision(databaseType, columnType);
if (!precision) return undefined;
const timezone = iotdbTimestampTimeZone(urlParams);
return { kind: "iotdb-timestamp", precision, timezone };
}
export function formatIoTDBTimestampEditorValue(value: CellValue, databaseType: string | undefined, columnType: string | null | undefined, urlParams: string | undefined): string | undefined {
const formatter = defaultIoTDBTimestampFormatter(databaseType, columnType, urlParams);
if (!formatter || formatter.kind !== "iotdb-timestamp") return undefined;
return formatIoTDBTimestamp(value, formatter.precision, formatter.timezone);
}
export function parseIoTDBTimestampEditorValue(value: string, databaseType: string | undefined, columnType: string | null | undefined, urlParams: string | undefined): string | null | undefined {
const precision = iotdbTimestampPrecision(databaseType, columnType);
if (!precision) return undefined;
const text = value.trim();
if (!text || text.toUpperCase() === "NULL") return null;
if (isIntegerString(text)) {
try {
return BigInt(text).toString();
} catch {
return undefined;
}
}
const offsetMatch = text.match(ISO_OFFSET_DATETIME_PATTERN);
if (offsetMatch) {
const [, year, separator, month, day, hour, minute, second, fraction = "", zone] = offsetMatch;
if (!isValidDateTimeParts(year, month, day, hour, minute, second) || !isValidOffset(zone)) return undefined;
const baseMilliseconds = Date.parse(`${year}${separator}${month}${separator}${day}T${hour}:${minute}:${second}${zone}`);
if (!Number.isFinite(baseMilliseconds)) return undefined;
return iotdbRawTimestamp(baseMilliseconds, fraction.slice(1), precision);
}
const match = text.match(FRACTIONAL_LOCAL_DATETIME_PATTERN) ?? text.match(/^(\d{4})([-/])(\d{1,2})\2(\d{1,2})([ T])(\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (!match) return undefined;
const [, year, , month, day, , hour, minute, second] = match;
if (!isValidDateTimeParts(year, month, day, hour, minute, second)) return undefined;
const fraction = match.length > 9 ? String(match[9] ?? "") : "";
const normalized = `${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}T${hour.padStart(2, "0")}:${minute.padStart(2, "0")}:${second.padStart(2, "0")}`;
const pattern = "YYYY-MM-DDTHH:mm:ss";
try {
const parsed = dayjs.tz(normalized, pattern, iotdbTimestampTimeZone(urlParams));
return parsed.isValid() && parsed.format(pattern) === normalized ? iotdbRawTimestamp(parsed.valueOf(), fraction, precision) : undefined;
} catch {
return undefined;
}
}
export function iotdbTimestampPrecision(databaseType: string | undefined, columnType: string | null | undefined): IoTDBTimestampPrecision | undefined {
if (databaseType !== "iotdb") return undefined;
const match = columnType?.trim().match(/^TIMESTAMP\((ms|us|ns)\)$/i);
return match?.[1]?.toLowerCase() as IoTDBTimestampPrecision | undefined;
}
export function iotdbTimestampFractionDigits(precision: IoTDBTimestampPrecision): number {
return precision === "ms" ? 3 : precision === "us" ? 6 : 9;
}
function iotdbTimestampTimeZone(urlParams: string | undefined): string {
const params = new URLSearchParams(urlParams ?? "");
const candidate = params.get("time_zone") || params.get("timezone") || params.get("zone_id") || "UTC";
try {
dayjs(0).tz(candidate);
return candidate;
} catch {
return "UTC";
}
}
export function formatTemporalRowsForExport<T extends CellValue>(rows: readonly (readonly T[])[], columnTypes: readonly (string | null | undefined)[], pattern: string): T[][] {
const normalizedPattern = normalizeGlobalDateTimePattern(pattern);
if (!normalizedPattern) return rows.map((row) => [...row]);
@@ -248,6 +321,7 @@ export function applyColumnFormatter(value: CellValue, formatter: ColumnFormatte
if (!formatter) return displayCellValue(value);
try {
if (formatter.kind === "iotdb-timestamp") return formatIoTDBTimestamp(value, formatter.precision, formatter.timezone) ?? displayCellValue(value);
if (formatter.kind === "datetime") return formatDateTime(value, formatter.unit, formatter.pattern, formatter.timezone);
if (formatter.kind === "json-path") return formatJsonPath(value, formatter.path);
if (formatter.kind === "mask") return formatMask(value, formatter);
@@ -378,6 +452,43 @@ function parseTimestampMilliseconds(value: string | number, unit: DateTimeFormat
return isAutoTimestampValue(value, numeric) ? convertToMilliseconds(numeric, unit) : undefined;
}
function formatIoTDBTimestamp(value: CellValue, precision: IoTDBTimestampPrecision, timezone: string): string | undefined {
if (typeof value !== "number" && (typeof value !== "string" || !isIntegerString(value))) return undefined;
let raw: bigint;
try {
raw = BigInt(value);
} catch {
return undefined;
}
const fractionDigits = iotdbTimestampFractionDigits(precision);
const factor = 10n ** BigInt(fractionDigits);
let seconds = raw / factor;
let fraction = raw % factor;
if (fraction < 0) {
seconds -= 1n;
fraction += factor;
}
const milliseconds = seconds * 1000n + (fraction * 1000n) / factor;
const numericMilliseconds = Number(milliseconds);
if (!Number.isSafeInteger(numericMilliseconds)) return undefined;
const parsed = dayjs(numericMilliseconds);
if (!parsed.isValid()) return undefined;
try {
const zoned = parsed.tz(timezone);
return `${zoned.format("YYYY-MM-DDTHH:mm:ss")}.${fraction.toString().padStart(fractionDigits, "0")}${zoned.format("Z")}`;
} catch {
return undefined;
}
}
function iotdbRawTimestamp(baseMilliseconds: number, fraction: string, precision: IoTDBTimestampPrecision): string | undefined {
if (!Number.isSafeInteger(baseMilliseconds) || baseMilliseconds % 1000 !== 0) return undefined;
const digits = iotdbTimestampFractionDigits(precision);
if (fraction.length > digits && /[1-9]/.test(fraction.slice(digits))) return undefined;
const normalizedFraction = fraction.slice(0, digits).padEnd(digits, "0");
return (BigInt(baseMilliseconds / 1000) * 10n ** BigInt(digits) + BigInt(normalizedFraction || "0")).toString();
}
function convertToMilliseconds(value: number, unit: DateTimeFormatterUnit): number {
return unit === "seconds" || (unit === "auto" && Math.abs(value) < 100_000_000_000) ? value * 1000 : value;
}