From f225832b87bf71a40f156e41ad4bc208b0b43e7c Mon Sep 17 00:00:00 2001 From: kowshikdev Date: Thu, 3 Dec 2026 19:42:04 +0530 Subject: [PATCH] fix(tables): keep integers past the safe range as text when inferring CSV column types CSV import inferred a column as number whenever every sampled value parsed with Number(). An integer above 2^53 (Discord or Twitter snowflake ids, long account numbers) parses, but as a rounded double, so 1234567890123456789 was stored as 1234567890123456800 and the original digits were lost for good. inferColumnType now treats an integer string that is not a safe integer as text, the same way #9754 keeps leading-zero codes as text. Decimals and integers within the safe range still infer as numbers. Co-Authored-By: Claude Opus 5.5 --- apps/sim/lib/table/import.test.ts | 11 +++++++++++ apps/sim/lib/table/import.ts | 11 +++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/sim/lib/table/import.test.ts b/apps/sim/lib/table/import.test.ts index 812d2b59ad3..8872f987d61 100644 --- a/apps/sim/lib/table/import.test.ts +++ b/apps/sim/lib/table/import.test.ts @@ -74,6 +74,17 @@ describe('import', () => { ) expect(columns.map((c) => c.type)).toEqual(['string', 'number']) }) + + it('keeps integers past the safe range as text so no digit is rounded away', () => { + const { columns } = inferSchemaFromCsv( + ['discord_id', 'quantity'], + [ + { discord_id: '1234567890123456789', quantity: '9007199254740991' }, + { discord_id: '987654321098765432', quantity: '-12' }, + ] + ) + expect(columns.map((c) => c.type)).toEqual(['string', 'number']) + }) }) describe('coerceValue', () => { diff --git a/apps/sim/lib/table/import.ts b/apps/sim/lib/table/import.ts index 3738669530f..e73e0a72e1b 100644 --- a/apps/sim/lib/table/import.ts +++ b/apps/sim/lib/table/import.ts @@ -389,10 +389,17 @@ export function inferColumnType(values: unknown[]): InferredCsvColumnType { if (nonEmpty.length === 0) return 'string' // A leading zero before another digit marks a code (ZIP, phone, SKU), not a - // quantity: storing it as a number would drop the zero for good. + // quantity: storing it as a number would drop the zero for good. So does an + // integer past 2^53 (snowflake ids, long account numbers): a number + // column would round away its last digits. const allNumber = nonEmpty.every((v) => { const s = String(v).trim() - return s !== '' && !Number.isNaN(Number(s)) && !/^[+-]?0\d/.test(s) + return ( + s !== '' && + !Number.isNaN(Number(s)) && + !/^[+-]?0\d/.test(s) && + !(/^[+-]?\d+$/.test(s) && !Number.isSafeInteger(Number(s))) + ) }) if (allNumber) return 'number'