diff --git a/src/duckdb.js b/src/duckdb.js index 698fadc4..cebf7fea 100644 --- a/src/duckdb.js +++ b/src/duckdb.js @@ -172,12 +172,19 @@ async function insertFile(database, name, file, options) { try { switch (file.mimeType) { case "text/csv": - case "text/tab-separated-values": + case "text/tab-separated-values": { return await connection.insertCSVFromPath(file.name, { name, schema: "main", ...options + }).catch(async (error) => { + // If initial attempt to insert CSV resulted in a conversion + // error, try again, this time treating all columns as strings. + if (error.toString().includes("Could not convert")) { + return await insertUntypedCSV(connection, file, name); + } }); + } case "application/json": return await connection.insertJSONFromPath(file.name, { name, @@ -205,6 +212,13 @@ async function insertFile(database, name, file, options) { } } +async function insertUntypedCSV(connection, file, name) { + const statement = await connection.prepare( + `CREATE TABLE '${name}' AS SELECT * FROM read_csv_auto(?, ALL_VARCHAR=TRUE)` + ); + return await statement.send(file.name); +} + async function insertArrowTable(database, name, table, options) { const connection = await database.connect(); try {