Skip to content

Commit 526a830

Browse files
committed
Fixing bug on tables catalog tables w/o PartitionKeys.
1 parent b67cf9f commit 526a830

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

awswrangler/catalog.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,9 @@ def get_table_types(
439439
dtypes: Dict[str, str] = {}
440440
for col in response["Table"]["StorageDescriptor"]["Columns"]:
441441
dtypes[col["Name"]] = col["Type"]
442-
for par in response["Table"]["PartitionKeys"]:
443-
dtypes[par["Name"]] = par["Type"]
442+
if "PartitionKeys" in response["Table"]:
443+
for par in response["Table"]["PartitionKeys"]:
444+
dtypes[par["Name"]] = par["Type"]
444445
return dtypes
445446

446447

@@ -527,6 +528,11 @@ def get_tables(
527528
) -> Iterator[Dict[str, Any]]:
528529
"""Get an iterator of tables.
529530
531+
Note
532+
----
533+
Please, does not filter using name_contains and name_prefix/name_suffix at the same time.
534+
Only name_prefix and name_suffix can be combined together.
535+
530536
Parameters
531537
----------
532538
catalog_id : str, optional
@@ -560,15 +566,17 @@ def get_tables(
560566
if catalog_id is not None:
561567
args["CatalogId"] = catalog_id
562568
if (name_prefix is not None) and (name_suffix is not None) and (name_contains is not None):
563-
args["Expression"] = f"{name_prefix}.*{name_contains}.*{name_suffix}"
569+
raise exceptions.InvalidArgumentCombination("Please, does not filter using name_contains and "
570+
"name_prefix/name_suffix at the same time. Only "
571+
"name_prefix and name_suffix can be combined together.")
564572
elif (name_prefix is not None) and (name_suffix is not None):
565-
args["Expression"] = f"{name_prefix}.*{name_suffix}"
573+
args["Expression"] = f"{name_prefix}*{name_suffix}"
566574
elif name_contains is not None:
567-
args["Expression"] = f".*{name_contains}.*"
575+
args["Expression"] = f"*{name_contains}*"
568576
elif name_prefix is not None:
569-
args["Expression"] = f"{name_prefix}.*"
577+
args["Expression"] = f"{name_prefix}*"
570578
elif name_suffix is not None:
571-
args["Expression"] = f".*{name_suffix}"
579+
args["Expression"] = f"*{name_suffix}"
572580
if database is not None:
573581
dbs: List[str] = [database]
574582
else:
@@ -647,15 +655,21 @@ def tables(
647655
tbls = tbls[:limit]
648656

649657
df_dict: Dict[str, List] = {"Database": [], "Table": [], "Description": [], "Columns": [], "Partitions": []}
650-
for table in tbls:
651-
df_dict["Database"].append(table["DatabaseName"])
652-
df_dict["Table"].append(table["Name"])
653-
if "Description" in table:
654-
df_dict["Description"].append(table["Description"])
658+
for tbl in tbls:
659+
df_dict["Database"].append(tbl["DatabaseName"])
660+
df_dict["Table"].append(tbl["Name"])
661+
if "Description" in tbl:
662+
df_dict["Description"].append(tbl["Description"])
655663
else:
656664
df_dict["Description"].append("")
657-
df_dict["Columns"].append(", ".join([x["Name"] for x in table["StorageDescriptor"]["Columns"]]))
658-
df_dict["Partitions"].append(", ".join([x["Name"] for x in table["PartitionKeys"]]))
665+
if "Columns" in tbl["StorageDescriptor"]:
666+
df_dict["Columns"].append(", ".join([x["Name"] for x in tbl["StorageDescriptor"]["Columns"]]))
667+
else:
668+
df_dict["Columns"].append("")
669+
if "PartitionKeys" in tbl:
670+
df_dict["Partitions"].append(", ".join([x["Name"] for x in tbl["PartitionKeys"]]))
671+
else:
672+
df_dict["Partitions"].append("")
659673
return pd.DataFrame(data=df_dict)
660674

661675

@@ -771,14 +785,15 @@ def table(
771785
df_dict["Comment"].append(col["Comment"])
772786
else:
773787
df_dict["Comment"].append("")
774-
for col in tbl["PartitionKeys"]:
775-
df_dict["Column Name"].append(col["Name"])
776-
df_dict["Type"].append(col["Type"])
777-
df_dict["Partition"].append(True)
778-
if "Comment" in col:
779-
df_dict["Comment"].append(col["Comment"])
780-
else:
781-
df_dict["Comment"].append("")
788+
if "PartitionKeys" in tbl:
789+
for col in tbl["PartitionKeys"]:
790+
df_dict["Column Name"].append(col["Name"])
791+
df_dict["Type"].append(col["Type"])
792+
df_dict["Partition"].append(True)
793+
if "Comment" in col:
794+
df_dict["Comment"].append(col["Comment"])
795+
else:
796+
df_dict["Comment"].append("")
782797
return pd.DataFrame(data=df_dict)
783798

784799

@@ -1692,8 +1707,9 @@ def get_columns_comments(
16921707
comments: Dict[str, str] = {}
16931708
for c in response["Table"]["StorageDescriptor"]["Columns"]:
16941709
comments[c["Name"]] = c["Comment"]
1695-
for p in response["Table"]["PartitionKeys"]:
1696-
comments[p["Name"]] = p["Comment"]
1710+
if "PartitionKeys" in response["Table"]:
1711+
for p in response["Table"]["PartitionKeys"]:
1712+
comments[p["Name"]] = p["Comment"]
16971713
return comments
16981714

16991715

0 commit comments

Comments
 (0)