fix: serialize BigInt and non-finite elements of sql template arrays (#12616)

* fix: serialize BigInt and non-finite elements of sql template array args

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: note how json-typed array args keep serialized elements

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-24 10:22:05 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent db218c0d3a
commit 5044d02325
2 changed files with 18 additions and 0 deletions
+4
View File
@@ -256,8 +256,12 @@ function buildSqlStatement(
// silently became NULL in the database. The
// executor accepts these literals via
// `Value::String → FLOAT8` (`f64::from_str`).
// - array → each element serialized as above. `TYPE[]`
// slots parse string elements the same way; a
// JSON-typed array stores them as strings.
// - everything else → passed through unchanged.
function serializeArgValue(v: any): any {
if (Array.isArray(v)) return v.map(serializeArgValue);
if (typeof v === "bigint") return v.toString();
if (v instanceof Date) return v.toISOString();
if (typeof v === "number" && !Number.isFinite(v)) {
+14
View File
@@ -139,6 +139,7 @@ function parseTypeAnnotation(
}
function serializeArgValue(v: any): any {
if (Array.isArray(v)) return v.map(serializeArgValue);
if (typeof v === "bigint") return v.toString();
if (v instanceof Date) return v.toISOString();
if (typeof v === "number" && !Number.isFinite(v)) {
@@ -455,6 +456,19 @@ describe("datatable() — template tag", () => {
expect(sql`SELECT ${NaN}`.content).toContain("$1::DOUBLE PRECISION");
});
test("BigInt and non-finite array elements are stringified", () => {
const sql = dt();
const out = sql`SELECT ${[1n, 2n]}`;
expect(out.content).toContain("$1::BIGINT[]");
expect(out.args.arg1).toEqual(["1", "2"]);
expect(() => JSON.stringify(out.args)).not.toThrow();
expect(sql`SELECT ${[1.5, NaN, -Infinity]}`.args.arg1).toEqual([
1.5,
"NaN",
"-Infinity",
]);
});
test("homogeneous arrays auto-tag with TYPE[]", () => {
const sql = dt();
expect(sql`SELECT ${[1, 2, 3]}`.content).toContain("$1::BIGINT[]");