Skip to content

Commit a6fa6f0

Browse files
DuckDB to import columns as strings as a fallback (#341)
1 parent e709459 commit a6fa6f0

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/duckdb.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,19 @@ async function insertFile(database, name, file, options) {
172172
try {
173173
switch (file.mimeType) {
174174
case "text/csv":
175-
case "text/tab-separated-values":
175+
case "text/tab-separated-values": {
176176
return await connection.insertCSVFromPath(file.name, {
177177
name,
178178
schema: "main",
179179
...options
180+
}).catch(async (error) => {
181+
// If initial attempt to insert CSV resulted in a conversion
182+
// error, try again, this time treating all columns as strings.
183+
if (error.toString().includes("Could not convert")) {
184+
return await insertUntypedCSV(connection, file, name);
185+
}
180186
});
187+
}
181188
case "application/json":
182189
return await connection.insertJSONFromPath(file.name, {
183190
name,
@@ -205,6 +212,13 @@ async function insertFile(database, name, file, options) {
205212
}
206213
}
207214

215+
async function insertUntypedCSV(connection, file, name) {
216+
const statement = await connection.prepare(
217+
`CREATE TABLE '${name}' AS SELECT * FROM read_csv_auto(?, ALL_VARCHAR=TRUE)`
218+
);
219+
return await statement.send(file.name);
220+
}
221+
208222
async function insertArrowTable(database, name, table, options) {
209223
const connection = await database.connect();
210224
try {

0 commit comments

Comments
 (0)