From 08dd6a8c5e42ea164ac33e8b321f395f8ee032fb Mon Sep 17 00:00:00 2001 From: David M <656337+davidov541@users.noreply.github.com> Date: Thu, 26 Jan 2023 13:46:36 -0600 Subject: [PATCH 1/4] Fixing issue where table does not exist doesn't return the right exception. --- src/snowflake/sqlalchemy/snowdialect.py | 8 +++++++- tests/test_core.py | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/snowflake/sqlalchemy/snowdialect.py b/src/snowflake/sqlalchemy/snowdialect.py index 5003f286..1cc9a0a3 100644 --- a/src/snowflake/sqlalchemy/snowdialect.py +++ b/src/snowflake/sqlalchemy/snowdialect.py @@ -637,6 +637,9 @@ def _get_table_columns(self, connection, table_name, schema=None, **kw): type_instance = col_type(**col_type_kw) + if table_name not in schema_primary_keys: + raise sa_exc.NoSuchTableError() + current_table_pks = schema_primary_keys.get(table_name) ans.append( @@ -669,7 +672,10 @@ def get_columns(self, connection, table_name, schema=None, **kw): if schema_columns is None: # Too many results, fall back to only query about single table return self._get_table_columns(connection, table_name, schema, **kw) - return schema_columns[self.normalize_name(table_name)] + normalized_table_name = self.normalize_name(table_name) + if normalized_table_name not in schema_columns: + raise sa_exc.NoSuchTableError() + return schema_columns[normalized_table_name] @reflection.cache def get_table_names(self, connection, schema=None, **kw): diff --git a/tests/test_core.py b/tests/test_core.py index 68a2a12e..353ddefc 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -32,7 +32,7 @@ inspect, text, ) -from sqlalchemy.exc import DBAPIError +from sqlalchemy.exc import DBAPIError, NoSuchTableError from sqlalchemy.pool import NullPool from sqlalchemy.sql import and_, not_, or_, select @@ -417,6 +417,18 @@ def test_insert_tables(engine_testaccount): users.drop(engine_testaccount) +def test_table_does_not_exist(engine_testaccount): + """ + Tests Correct Exception Thrown When Table Does Not Exist + """ + with engine_testaccount.connect() as conn: + meta = MetaData() + with pytest.raises(NoSuchTableError): + Table( + "does_not_exist", meta, autoload=True, autoload_with=engine_testaccount + ) + + @pytest.mark.skip( """ Reflection is not implemented yet. From fb39add6d906e25edb04352e6931ef2649d01904 Mon Sep 17 00:00:00 2001 From: David M <656337+davidov541@users.noreply.github.com> Date: Thu, 16 Feb 2023 12:54:34 -0600 Subject: [PATCH 2/4] Adding new entry in release notes for the changes we've made. --- DESCRIPTION.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DESCRIPTION.md b/DESCRIPTION.md index d5778699..4f475218 100644 --- a/DESCRIPTION.md +++ b/DESCRIPTION.md @@ -9,6 +9,10 @@ Source code is also available at: # Release Notes +- v1.4.7(Feb 16, 2023) + +- `SnowflakeDialect.get_columns` now throws a `NoSuchTableError` exception when the specified table doesn't exist, instead of the more vague `KeyError`. + - v1.4.6(Feb 8, 2023) - Bumped snowflake-connector-python dependency to newest version which supports Python 3.11. From 858af66f36e080e9e4658c1624dbb9a420b47786 Mon Sep 17 00:00:00 2001 From: David M <656337+davidov541@users.noreply.github.com> Date: Fri, 10 Mar 2023 17:52:23 -0600 Subject: [PATCH 3/4] Switching mechanism for checking for no such table. --- src/snowflake/sqlalchemy/snowdialect.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/snowflake/sqlalchemy/snowdialect.py b/src/snowflake/sqlalchemy/snowdialect.py index 1cc9a0a3..d96a3b09 100644 --- a/src/snowflake/sqlalchemy/snowdialect.py +++ b/src/snowflake/sqlalchemy/snowdialect.py @@ -637,9 +637,6 @@ def _get_table_columns(self, connection, table_name, schema=None, **kw): type_instance = col_type(**col_type_kw) - if table_name not in schema_primary_keys: - raise sa_exc.NoSuchTableError() - current_table_pks = schema_primary_keys.get(table_name) ans.append( @@ -658,6 +655,10 @@ def _get_table_columns(self, connection, table_name, schema=None, **kw): else False, } ) + + # If we didn't find any columns for the table, the table doesn't exist. + if len(ans) == 0: + raise sa_exc.NoSuchTableError() return ans def get_columns(self, connection, table_name, schema=None, **kw): From 59a4b37cf18ef51ab4799e85308158ee594b95a8 Mon Sep 17 00:00:00 2001 From: David M <656337+davidov541@users.noreply.github.com> Date: Tue, 14 Mar 2023 12:05:39 -0500 Subject: [PATCH 4/4] Removing connection for test that is not really needed. Co-authored-by: Adam Ling --- tests/test_core.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index ab334c9e..48a0d900 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -410,12 +410,9 @@ def test_table_does_not_exist(engine_testaccount): """ Tests Correct Exception Thrown When Table Does Not Exist """ - with engine_testaccount.connect() as conn: - meta = MetaData() - with pytest.raises(NoSuchTableError): - Table( - "does_not_exist", meta, autoload=True, autoload_with=engine_testaccount - ) + meta = MetaData() + with pytest.raises(NoSuchTableError): + Table("does_not_exist", meta, autoload=True, autoload_with=engine_testaccount) @pytest.mark.skip(