Skip to content

Commit

Permalink
feat: SHOW PRIMARY KEYS for table (#114)
Browse files Browse the repository at this point in the history
Previously there was a `NotImplemented` error when running a query to
request the PKs of a table.

I added support for listing the PKs of a table.
  • Loading branch information
Guillem96 authored Jul 10, 2024
1 parent 957d9e8 commit 2c006b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fakesnow/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,11 @@ def show_keys(

if schema:
statement += f"AND schema_name = '{schema}' "
elif scope_kind == "TABLE":
if not table:
raise ValueError(f"SHOW PRIMARY KEYS with {scope_kind} scope requires a table")

statement += f"AND table_name = '{table.name}' "
else:
raise NotImplementedError(f"SHOW PRIMARY KEYS with {scope_kind} not yet supported")
return sqlglot.parse_one(statement)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_info_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,21 @@ def test_info_schema_views_with_views(conn: snowflake.connector.SnowflakeConnect
"comment": None,
}
]


def test_info_schema_show_primary_keys_from_table(cur: snowflake.connector.cursor.SnowflakeCursor) -> None:
cur.execute(
"""
CREATE TABLE test_table (
ID varchar,
VERSION varchar,
PRIMARY KEY (ID, VERSION)
)
"""
)

cur.execute("SHOW PRIMARY KEYS IN test_table")
pk_result = cur.fetchall()

pk_columns = [result[4] for result in pk_result]
assert pk_columns == ["ID", "VERSION"]

0 comments on commit 2c006b3

Please sign in to comment.