duckdb / postgres_scanner

MIT License
194 stars 35 forks source link

Case Insensitive Handling of Identifier Names in Table Alteration #212

Closed the-ALAM closed 2 months ago

the-ALAM commented 2 months ago

What happens?

Description

Behavior

these are the steps that are done in normal flow 1 - start a transaction 2 - create the new version of the table in a temporary location (suffixed dbt_tmp ) 3 - rename the existing version of the table to be the backup version (suffixed __dbt_backup) 4 - rename the new version of the table to remove the suffix (dbt_tmp → ``) 5 - commit the transaction 6 - drop the old/backup version (on many databases, drops need to happen outside of transactions)

but it crashes in the middle returning

Runtime Error in model {model_name} (models_{dir_name}\{model_name}.sql) 
Failed to execute query "ALTER TABLE {schema_name}.{capitalized_table_name}__dbt_tmp RENAME TO {capitalized_table_name}":
ERROR: relation "{schema_name}.{lowered_table_name}__dbt_tmp" does not exist

To Reproduce

1 - connect to the postgres database

D attach 'connection_string' as pg;

2 - create a table named Tst and check existence

D create table pg.rca_standardized.Tst(id INT);
D describe pg.rca_standardized.Tst;

3 - try and alter the name to lower case

D alter table pg.rca_standardized.Tst rename to tst;
Error: Invalid Error: Failed to execute query "BEGIN TRANSACTION;
ALTER TABLE rca_standardized.Tst RENAME TO tst": ERROR:  relation "rca_standardized.tst" does not exist

already searches for the lower-cased relation

4 - try and alter the table to add a column

D alter table pg.rca_standardized.Tst add column name varchar; 
Error: Invalid Error: Failed to execute query "BEGIN TRANSACTION;
ALTER TABLE rca_standardized.Tst ADD COLUMN  "name" VARCHAR": ERROR:  relation "rca_standardized.tst" does not exist

again searches for the lower-cased relation

5 - tried different ways to double-quote it like

D alter table pg.rca_standardized."Tst" add column "name" varchar; 
Error: Invalid Error: Failed to execute query "BEGIN TRANSACTION;
ALTER TABLE rca_standardized.Tst ADD COLUMN  "name" VARCHAR": ERROR:  relation "rca_standardized.tst" does not exist

OS:

Win11 & Ubuntu 22.04

PostgreSQL Version:

15.6

DuckDB Version:

0.10.0 (20b1486d11)

DuckDB Client:

Python 3.12.0

Full Name:

M. Alam

Affiliation:

nWeave

Have you tried this on the latest main branch?

Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?