Skip to content

Commit

Permalink
Merge pull request #288 from Mytherin/uppercaseops
Browse files Browse the repository at this point in the history
Fix #284: quote uppercase identifiers when shipping queries to Postgres
  • Loading branch information
Mytherin authored Jan 28, 2025
2 parents 9b0dbd1 + e548757 commit f6ea049
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/include/postgres_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class PostgresUtils {
static bool SupportedPostgresOid(const LogicalType &input);
static LogicalType RemoveAlias(const LogicalType &type);
static PostgresType CreateEmptyPostgresType(const LogicalType &type);
static string QuotePostgresIdentifier(const string &text);

static PostgresVersion ExtractPostgresVersion(const string &version);
};
Expand Down
4 changes: 4 additions & 0 deletions src/postgres_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,8 @@ PostgresVersion PostgresUtils::ExtractPostgresVersion(const string &version_str)
return result;
}

string PostgresUtils::QuotePostgresIdentifier(const string &text) {
return KeywordHelper::WriteOptionallyQuoted(text, '"', false);
}

} // namespace duckdb
2 changes: 1 addition & 1 deletion src/storage/postgres_delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ string GetDeleteSQL(const PostgresTableEntry &table, const string &ctid_list) {
string result;
result = "DELETE FROM ";
result += KeywordHelper::WriteQuoted(table.schema.name, '"') + ".";
result += KeywordHelper::WriteOptionallyQuoted(table.name);
result += PostgresUtils::QuotePostgresIdentifier(table.name);
result += " WHERE ctid IN (" + ctid_list + ")";
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/storage/postgres_index_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ string PGGetCreateIndexSQL(CreateIndexInfo &info, TableCatalogEntry &tbl) {
sql += " UNIQUE";
}
sql += " INDEX ";
sql += KeywordHelper::WriteOptionallyQuoted(info.index_name);
sql += PostgresUtils::QuotePostgresIdentifier(info.index_name);
sql += " ON ";
sql += KeywordHelper::WriteOptionallyQuoted(tbl.schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(tbl.name);
sql += PostgresUtils::QuotePostgresIdentifier(tbl.schema.name) + ".";
sql += PostgresUtils::QuotePostgresIdentifier(tbl.name);
sql += "(";
for (idx_t i = 0; i < info.parsed_expressions.size(); i++) {
if (i > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/storage/postgres_schema_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ optional_ptr<CatalogEntry> PostgresSchemaEntry::CreateIndex(CatalogTransaction t
string PGGetCreateViewSQL(PostgresSchemaEntry &schema, CreateViewInfo &info) {
string sql;
sql = "CREATE VIEW ";
sql += KeywordHelper::WriteOptionallyQuoted(schema.name) + ".";
sql += KeywordHelper::WriteOptionallyQuoted(info.view_name);
sql += PostgresUtils::QuotePostgresIdentifier(schema.name) + ".";
sql += PostgresUtils::QuotePostgresIdentifier(info.view_name);
sql += " ";
if (!info.aliases.empty()) {
sql += "(";
Expand All @@ -83,7 +83,7 @@ string PGGetCreateViewSQL(PostgresSchemaEntry &schema, CreateViewInfo &info) {
sql += ", ";
}
auto &alias = info.aliases[i];
sql += KeywordHelper::WriteOptionallyQuoted(alias);
sql += PostgresUtils::QuotePostgresIdentifier(alias);
}
sql += ") ";
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage/postgres_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PostgresUpdateGlobalState : public GlobalSinkState {

string CreateUpdateTable(const string &name, PostgresTableEntry &table, const vector<PhysicalIndex> &index) {
string result;
result = "CREATE LOCAL TEMPORARY TABLE " + KeywordHelper::WriteOptionallyQuoted(name);
result = "CREATE LOCAL TEMPORARY TABLE " + PostgresUtils::QuotePostgresIdentifier(name);
result += "(";
for (idx_t i = 0; i < index.size(); i++) {
if (i > 0) {
Expand Down Expand Up @@ -63,7 +63,7 @@ string GetUpdateSQL(const string &name, PostgresTableEntry &table, const vector<
result += ".";
result += KeywordHelper::WriteQuoted(column_name, '"');
}
result += " FROM " + KeywordHelper::WriteOptionallyQuoted(name);
result += " FROM " + PostgresUtils::QuotePostgresIdentifier(name);
result += " WHERE ";
result += KeywordHelper::WriteQuoted(table.name, '"');
result += ".ctid=__page_id_string::TID";
Expand Down
39 changes: 39 additions & 0 deletions test/sql/storage/attach_upper_case.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# name: test/sql/storage/attach_upper_case.test
# description: Test modifying an upper-case table
# group: [storage]

require postgres_scanner

require-env POSTGRES_TEST_DATABASE_AVAILABLE

statement ok
PRAGMA enable_verification

statement ok
ATTACH 'dbname=postgresscanner' AS s1 (TYPE POSTGRES)

statement ok
USE s1

statement ok
DROP SCHEMA IF EXISTS SCHEM01 CASCADE

statement ok
CREATE SCHEMA SCHEM01

statement ok
create or replace table SCHEM01.TAB01(COL01 VARCHAR);

statement ok
insert into SCHEM01.TAB01 values ('abc')

statement ok
update SCHEM01.TAB01 set COL01='zxcv'

query I
SELECT COL01 FROM SCHEM01.TAB01
----
zxcv

statement ok
truncate SCHEM01.tab01

0 comments on commit f6ea049

Please sign in to comment.