Skip to content

Commit 96e0f71

Browse files
authored
Fix MetaAPIHandler: allow empty result (#11932)
1 parent 577dc18 commit 96e0f71

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

mindsdb/integrations/libs/api_handler.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,17 @@ def meta_get_tables(self, table_names: Optional[List[str]] = None, **kwargs) ->
553553
except Exception:
554554
logger.exception(f"Error retrieving metadata for table {table_name}:")
555555

556+
if len(df.columns) == 0:
557+
df = pd.DataFrame(
558+
columns=[
559+
"TABLE_NAME",
560+
"TABLE_TYPE",
561+
"TABLE_SCHEMA",
562+
"TABLE_DESCRIPTION",
563+
"ROW_COUNT",
564+
]
565+
)
566+
556567
return Response(RESPONSE_TYPE.TABLE, df)
557568

558569
def meta_get_columns(self, table_names: Optional[List[str]] = None, **kwargs) -> Response:
@@ -575,6 +586,18 @@ def meta_get_columns(self, table_names: Optional[List[str]] = None, **kwargs) ->
575586
except Exception:
576587
logger.exception(f"Error retrieving column metadata for table {table_name}:")
577588

589+
if len(df.columns) == 0:
590+
df = pd.DataFrame(
591+
columns=[
592+
"TABLE_NAME",
593+
"COLUMN_NAME",
594+
"DATA_TYPE",
595+
"COLUMN_DESCRIPTION",
596+
"IS_NULLABLE",
597+
"COLUMN_DEFAULT",
598+
]
599+
)
600+
578601
return Response(RESPONSE_TYPE.TABLE, df)
579602

580603
def meta_get_column_statistics(self, table_names: Optional[List[str]] = None, **kwargs) -> Response:
@@ -597,6 +620,20 @@ def meta_get_column_statistics(self, table_names: Optional[List[str]] = None, **
597620
except Exception:
598621
logger.exception(f"Error retrieving column statistics for table {table_name}:")
599622

623+
if len(df.columns) == 0:
624+
df = pd.DataFrame(
625+
columns=[
626+
"TABLE_NAME",
627+
"COLUMN_NAME",
628+
"MOST_COMMON_VALUES",
629+
"MOST_COMMON_FREQUENCIES",
630+
"NULL_PERCENTAGE",
631+
"MINIMUM_VALUE",
632+
"MAXIMUM_VALUE",
633+
"DISTINCT_VALUES_COUNT",
634+
]
635+
)
636+
600637
return Response(RESPONSE_TYPE.TABLE, df)
601638

602639
def meta_get_primary_keys(self, table_names: Optional[List[str]] = None, **kwargs) -> Response:
@@ -619,6 +656,16 @@ def meta_get_primary_keys(self, table_names: Optional[List[str]] = None, **kwarg
619656
except Exception:
620657
logger.exception(f"Error retrieving primary keys for table {table_name}:")
621658

659+
if len(df.columns) == 0:
660+
df = pd.DataFrame(
661+
columns=[
662+
"TABLE_NAME",
663+
"COLUMN_NAME",
664+
"ORDINAL_POSITION",
665+
"CONSTRAINT_NAME",
666+
]
667+
)
668+
622669
return Response(RESPONSE_TYPE.TABLE, df)
623670

624671
def meta_get_foreign_keys(self, table_names: Optional[List[str]] = None, **kwargs) -> Response:
@@ -644,6 +691,17 @@ def meta_get_foreign_keys(self, table_names: Optional[List[str]] = None, **kwarg
644691
except Exception:
645692
logger.exception(f"Error retrieving foreign keys for table {table_name}:")
646693

694+
if len(df.columns) == 0:
695+
df = pd.DataFrame(
696+
columns=[
697+
"PARENT_TABLE_NAME",
698+
"PARENT_COLUMN_NAME",
699+
"CHILD_TABLE_NAME",
700+
"CHILD_COLUMN_NAME",
701+
"CONSTRAINT_NAME",
702+
]
703+
)
704+
647705
return Response(RESPONSE_TYPE.TABLE, df)
648706

649707

mindsdb/interfaces/data_catalog/data_catalog_retriever.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,14 @@ def _construct_metadata_string_for_columns(
173173
"""
174174
Construct a formatted string representation of the columns for a single table.
175175
"""
176-
columns_str = "\n\nColumns:"
176+
columns_str = "\n\nColumns:\n"
177177
for _, column_row in columns_df.iterrows():
178178
# Ideally, there should be only one stats row per column.
179-
stats_row = column_stats_df[column_stats_df["COLUMN_NAME"] == column_row["COLUMN_NAME"]].iloc[0]
179+
stats_row = column_stats_df[column_stats_df["COLUMN_NAME"] == column_row["COLUMN_NAME"]]
180+
if len(stats_row) == 0:
181+
stats_row = pd.Series()
182+
else:
183+
stats_row = stats_row.iloc[0]
180184
columns_str += self._construct_metadata_string_for_column(
181185
column_row,
182186
stats_row,

0 commit comments

Comments
 (0)