Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: DuckDB GeoJSON import on newer duckdb-wasm (#3616)
Igor Dykhta07422bfce6f6
diff --git a/src/duckdb/src/table/duckdb-table-utils.ts b/src/duckdb/src/table/duckdb-table-utils.tsindex 7e9b755b0..6aab1b0d8 100644--- a/src/duckdb/src/table/duckdb-table-utils.ts+++ b/src/duckdb/src/table/duckdb-table-utils.ts@@ -113,7 +113,7 @@ export function quoteTableName(tableName: string): string {* @param columnName The column name to quote.* @returns The column name, properly quoted.*/-function quoteColumnName(columnName: string): string {+export function quoteColumnName(columnName: string): string {return `"${columnName.replace(/"/g, '""')}"`;}@@ -469,7 +469,7 @@ export function removeSQLComments(sql: string): string {*/export const dropTableIfExists = async (connection: DatabaseConnection, tableName: string) => {try {- await connection.query(`DROP TABLE IF EXISTS "${tableName}";`);+ await connection.query(`DROP TABLE IF EXISTS ${quoteTableName(tableName)};`);} catch (error) {console.error('Dropping table failed', tableName, error);}@@ -512,27 +512,28 @@ export async function tableFromFile(file: File | null): Promise<null | Error> {} else {await db.registerFileHandle(sourceName, file, DuckDBDataProtocol.BROWSER_FILEREADER, true);+ const quotedTableName = quoteTableName(tableName);if (fileExt === 'csv') {await c.query(`- CREATE TABLE '${tableName}' AS+ CREATE TABLE ${quotedTableName} ASSELECT *FROM read_csv('${sourceName}', header = true, auto_detect = true, sample_size = -1);`);} else if (fileExt === 'json') {await c.query(`- CREATE TABLE '${tableName}' AS+ CREATE TABLE ${quotedTableName} ASSELECT *FROM read_json_auto('${sourceName}');`);} else if (fileExt === 'geojson') {await c.query(`- CREATE TABLE '${tableName}' AS+ CREATE TABLE ${quotedTableName} ASSELECT *FROM ST_READ('${sourceName}', keep_wkb = TRUE);`);} else if (fileExt === 'parquet') {await c.query(`- CREATE TABLE '${tableName}' AS+ CREATE TABLE ${quotedTableName} ASSELECT *FROM read_parquet('${sourceName}')`);diff --git a/src/duckdb/src/table/duckdb-table.ts b/src/duckdb/src/table/duckdb-table.tsindex 71ded7e8e..f02db4eb2 100644--- a/src/duckdb/src/table/duckdb-table.ts+++ b/src/duckdb/src/table/duckdb-table.ts@@ -48,6 +48,8 @@ import {dropTableIfExists,getDuckDBColumnTypes,getDuckDBColumnTypesMap,+ quoteColumnName,+ quoteTableName,removeUnsupportedExtensions,restoreArrowTable,restoreUnsupportedExtensions@@ -124,7 +126,7 @@ export class KeplerGlDuckDbTable extends KeplerTable {}, '');const createTableSql = `- CREATE TABLE '${this.label}' AS+ CREATE TABLE ${quoteTableName(this.label)} ASSELECT *FROM read_json('${this.id}',columns = {${columns}});@@ -141,13 +143,17 @@ export class KeplerGlDuckDbTable extends KeplerTable {const {rows} = data;await db.registerFileText(this.id, JSON.stringify(rows));+ // Identifiers need double quotes; file paths stay single-quoted string literals.+ const tableName = quoteTableName(this.label);const createTableSql = `install spatial;load spatial;- CREATE TABLE '${this.label}' AS+ CREATE TABLE ${tableName} ASSELECT *FROM ST_READ('${this.id}', keep_wkb = TRUE);- ALTER TABLE '${this.label}' RENAME '${DUCKDB_WKB_COLUMN}' TO '${KEPLER_GEOM_FROM_GEOJSON_COLUMN}';+ ALTER TABLE ${tableName}+ RENAME ${quoteColumnName(DUCKDB_WKB_COLUMN)}+ TO ${quoteColumnName(KEPLER_GEOM_FROM_GEOJSON_COLUMN)};`;await c.query(createTableSql);