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

SNOW-733235: Returning correct exception when table does not exist #375

Merged
merged 7 commits into from
Mar 14, 2023
1 change: 1 addition & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Source code is also available at:
- v1.4.7(Unreleased)

- Re-applied the application name of driver connection `SnowflakeConnection` to `SnowflakeSQLAlchemy`.
- `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)

Expand Down
9 changes: 8 additions & 1 deletion src/snowflake/sqlalchemy/snowdialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,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):
Expand All @@ -673,7 +677,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()
sfc-gh-aling marked this conversation as resolved.
Show resolved Hide resolved
return schema_columns[normalized_table_name]
sfc-gh-aling marked this conversation as resolved.
Show resolved Hide resolved

@reflection.cache
def get_table_names(self, connection, schema=None, **kw):
Expand Down
11 changes: 10 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -406,6 +406,15 @@ 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
"""
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.
Expand Down