From 9bba55110c87ad359c3bf6788e720d2a95d02c82 Mon Sep 17 00:00:00 2001 From: kssenii Date: Sat, 16 Jul 2022 12:44:47 +0200 Subject: [PATCH 1/2] Fix --- .../fetchPostgreSQLTableStructure.cpp | 14 ++++++----- .../test_postgresql_database_engine/test.py | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp b/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp index 10cde43e9e1..fffd4d4ef7c 100644 --- a/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp +++ b/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp @@ -39,9 +39,10 @@ std::set fetchPostgreSQLTablesList(T & tx, const String & postgres_schem std::set tables; if (schemas.size() <= 1) { - std::string query = fmt::format("SELECT tablename FROM pg_catalog.pg_tables " - "WHERE schemaname != 'pg_catalog' AND {}", - postgres_schema.empty() ? "schemaname != 'information_schema'" : "schemaname = " + quoteString(postgres_schema)); + std::string query = fmt::format( + "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname = {}", + postgres_schema.empty() ? quoteString("public") : quoteString(postgres_schema)); + for (auto table_name : tx.template stream(query)) tables.insert(std::get<0>(table_name)); @@ -53,9 +54,10 @@ std::set fetchPostgreSQLTablesList(T & tx, const String & postgres_schem /// If we add schema to table name then table can be accessed only this way: database_name.`schema_name.table_name` for (const auto & schema : schemas) { - std::string query = fmt::format("SELECT tablename FROM pg_catalog.pg_tables " - "WHERE schemaname != 'pg_catalog' AND {}", - postgres_schema.empty() ? "schemaname != 'information_schema'" : "schemaname = " + quoteString(schema)); + std::string query = fmt::format( + "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname = {}", + quoteString(schema)); + for (auto table_name : tx.template stream(query)) tables.insert(schema + '.' + std::get<0>(table_name)); } diff --git a/tests/integration/test_postgresql_database_engine/test.py b/tests/integration/test_postgresql_database_engine/test.py index 5619c551c71..d07f62f8a80 100644 --- a/tests/integration/test_postgresql_database_engine/test.py +++ b/tests/integration/test_postgresql_database_engine/test.py @@ -349,6 +349,29 @@ def test_postgres_database_old_syntax(started_cluster): node1.query("DROP DATABASE IF EXISTS postgres_database;") +def test_postgresql_fetch_tables(started_cluster): + conn = get_postgres_conn( + started_cluster.postgres_ip, started_cluster.postgres_port, database=True + ) + cursor = conn.cursor() + + cursor.execute("DROP SCHEMA IF EXISTS test_schema CASCADE") + cursor.execute("CREATE SCHEMA test_schema") + cursor.execute("CREATE TABLE test_schema.table1 (a integer)") + cursor.execute("CREATE TABLE test_schema.table2 (a integer)") + cursor.execute("CREATE TABLE table3 (a integer)") + + node1.query( + "CREATE DATABASE postgres_database ENGINE = PostgreSQL('postgres1:5432', 'postgres_database', 'postgres', 'mysecretpassword')" + ) + + assert node1.query("SHOW TABLES FROM postgres_database") == "table3\n" + assert not node1.contains_in_log("PostgreSQL table table1 does not exist") + + cursor.execute(f"DROP TABLE table3") + cursor.execute("DROP SCHEMA IF EXISTS test_schema CASCADE") + + if __name__ == "__main__": cluster.start() input("Cluster created, press any key to destroy...") From 4b11a718cbd24f5d1be6a6e99d5b75bbe3c7a6f5 Mon Sep 17 00:00:00 2001 From: Kseniia Sumarokova <54203879+kssenii@users.noreply.github.com> Date: Sun, 17 Jul 2022 11:55:34 +0200 Subject: [PATCH 2/2] Update fetchPostgreSQLTableStructure.cpp --- src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp b/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp index fffd4d4ef7c..eeae110cddf 100644 --- a/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp +++ b/src/Databases/PostgreSQL/fetchPostgreSQLTableStructure.cpp @@ -40,7 +40,7 @@ std::set fetchPostgreSQLTablesList(T & tx, const String & postgres_schem if (schemas.size() <= 1) { std::string query = fmt::format( - "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname = {}", + "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = {}", postgres_schema.empty() ? quoteString("public") : quoteString(postgres_schema)); for (auto table_name : tx.template stream(query)) @@ -55,7 +55,7 @@ std::set fetchPostgreSQLTablesList(T & tx, const String & postgres_schem for (const auto & schema : schemas) { std::string query = fmt::format( - "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname = {}", + "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = {}", quoteString(schema)); for (auto table_name : tx.template stream(query))