Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions src/duckdb/src/table/duckdb-table-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export async function getDuckDBColumnTypes(
connection: DatabaseConnection,
tableName: string
): Promise<DuckDBColumnDesc[]> {
const resDescribe = await connection.query(`DESCRIBE "${tableName}"`);
const quotedTableName = quoteTableName(tableName);
const resDescribe = await connection.query(`DESCRIBE ${quotedTableName}`);

const duckDbTypes: DuckDBColumnDesc[] = [];
const numRows = resDescribe.numRows;
Expand Down Expand Up @@ -60,6 +61,46 @@ export function getDuckDBColumnTypesMap(columns: DuckDBColumnDesc[]) {
}, {} as Record<string, string>);
}

/**
* Quotes a table name for safe SQL usage.
* Always quotes to handle all edge cases (spaces, special characters, reserved words).
* For fully qualified names (containing dots), preserves the existing structure.
* @param columnName The column name to quote.
* @returns The column name, properly quoted.
*/
export function quoteTableName(columnName: string): string {
// If it's already a properly quoted simple identifier (starts and ends with quotes), return as-is
// This handles cases like "table", "table with spaces", and "table.with.dots"
if (columnName.startsWith('"') && columnName.endsWith('"')) {
return columnName;
}

// If it contains dots and quotes, it might be a fully qualified name
if (columnName.includes('.') && columnName.includes('"')) {
// Simple heuristic: if it looks like "part1"."part2" or "part1"."part2"."part3"
// Split on dots and check if each part is quoted
const parts = columnName.split('.');
Comment thread
igorDykhta marked this conversation as resolved.
Outdated
const allPartsQuoted = parts.every(
part => part.trim().startsWith('"') && part.trim().endsWith('"') && part.trim().length > 2
);

if (allPartsQuoted && parts.length >= 2) {
return columnName;
}
}
return `"${columnName.replace(/"/g, '""')}"`;
}

/**
* Quotes a column name for safe SQL usage.
* Always quotes to handle all edge cases (spaces, special characters, reserved words).
* @param columnName The column name to quote.
* @returns The column name, properly quoted.
*/
function quoteColumnName(columnName: string): string {
return `"${columnName.replace(/"/g, '""')}"`;
}

/**
* Constructs an SQL query to select all columns from a given table,
* converting specified columns to Well-Known Binary (WKB) format using ST_AsWKB,
Expand All @@ -76,15 +117,17 @@ export function castDuckDBTypesForKepler(
): string {
const modifiedColumns = columns.map(column => {
const {name, type} = column;
const quotedName = quoteColumnName(name);
if (type === 'GEOMETRY' && options.geometryToWKB) {
return `ST_AsWKB(${name}) as ${name}`;
return `ST_AsWKB(${quotedName}) as ${quotedName}`;
} else if (type === 'BIGINT' && options.bigIntToDouble) {
return `CAST(${name} AS DOUBLE) as ${name}`;
return `CAST(${quotedName} AS DOUBLE) as ${quotedName}`;
}
return name;
return quotedName;
});

return `SELECT ${modifiedColumns.join(', ')} FROM '${tableName}'`;
const quotedTableName = quoteTableName(tableName);
return `SELECT ${modifiedColumns.join(', ')} FROM ${quotedTableName}`;
}

/**
Expand Down
Loading
Loading