Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #284: quote uppercase identifiers when shipping queries to Postgres #288

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all 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
1 change: 1 addition & 0 deletions src/include/postgres_utils.hpp
Original file line number Diff line number Diff line change
@@ -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);
};
4 changes: 4 additions & 0 deletions src/postgres_utils.cpp
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
}
6 changes: 3 additions & 3 deletions src/storage/postgres_index_set.cpp
Original file line number Diff line number Diff line change
@@ -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) {
6 changes: 3 additions & 3 deletions src/storage/postgres_schema_entry.cpp
Original file line number Diff line number Diff line change
@@ -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 += "(";
@@ -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 += ") ";
}
4 changes: 2 additions & 2 deletions src/storage/postgres_update.cpp
Original file line number Diff line number Diff line change
@@ -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) {
@@ -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";
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
Loading