Skip to content

Commit

Permalink
Avoid unnecessarily reflecting stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Dec 3, 2024
1 parent bea370d commit 307d4d9
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions singer_sdk/connectors/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,15 +878,14 @@ def get_object_names(
def discover_catalog_entry(

Check warning on line 878 in singer_sdk/connectors/sql.py

View workflow job for this annotation

GitHub Actions / Check API Changes

SQLConnector.discover_catalog_entry(reflect_indices)

Parameter was removed: `` -> ``

Check warning on line 878 in singer_sdk/connectors/sql.py

View workflow job for this annotation

GitHub Actions / Check API Changes

SQLConnector.discover_catalog_entry(reflected_columns)

Parameter was added as required: `` -> ``

Check warning on line 878 in singer_sdk/connectors/sql.py

View workflow job for this annotation

GitHub Actions / Check API Changes

SQLConnector.discover_catalog_entry(reflected_pk)

Parameter was added as required: `` -> ``

Check warning on line 878 in singer_sdk/connectors/sql.py

View workflow job for this annotation

GitHub Actions / Check API Changes

SQLConnector.discover_catalog_entry(reflected_indices)

Parameter was added as required: `` -> ``
self,
engine: Engine, # noqa: ARG002
inspected: Inspector,
inspected: Inspector, # noqa: ARG002
schema_name: str | None,
table_name: str,
is_view: bool, # noqa: FBT001
*,
reflect_indices: bool = True,
reflected_columns: list[reflection.ReflectedColumn] | None = None,
reflected_pk: reflection.ReflectedPrimaryKeyConstraint | None = None,
reflected_indices: list[reflection.ReflectedIndex] | None = None,
reflected_columns: list[reflection.ReflectedColumn],
reflected_pk: reflection.ReflectedPrimaryKeyConstraint | None,
reflected_indices: list[reflection.ReflectedIndex],
) -> CatalogEntry:
"""Create `CatalogEntry` object for the given table or a view.
Expand All @@ -909,33 +908,20 @@ def discover_catalog_entry(

# Detect key properties
possible_primary_keys: list[list[str]] = []
pk_def = reflected_pk or inspected.get_pk_constraint(
table_name,
schema=schema_name,
)
if pk_def and "constrained_columns" in pk_def: # type: ignore[redundant-expr]
possible_primary_keys.append(pk_def["constrained_columns"])
if reflected_pk and "constrained_columns" in reflected_pk:
possible_primary_keys.append(reflected_pk["constrained_columns"])

# An element of the columns list is ``None`` if it's an expression and is
# returned in the ``expressions`` list of the reflected index.
if reflect_indices:
indexes = reflected_indices or inspected.get_indexes(
table_name,
schema=schema_name,
)
possible_primary_keys.extend(
index_def["column_names"] # type: ignore[misc]
for index_def in indexes
if index_def.get("unique", False)
)
possible_primary_keys.extend(
index_def["column_names"] # type: ignore[misc]
for index_def in reflected_indices
if index_def.get("unique", False)
)

key_properties = next(iter(possible_primary_keys), [])

# Initialize columns list
columns = reflected_columns or inspected.get_columns(
table_name,
schema=schema_name,
)
properties = [
th.Property(
name=column["name"],
Expand All @@ -944,7 +930,7 @@ def discover_catalog_entry(
required=column["name"] in key_properties,
description=column.get("comment"),
)
for column in columns
for column in reflected_columns
]
schema = th.PropertiesList(*properties).to_dict()

Expand Down Expand Up @@ -1026,10 +1012,9 @@ def discover_catalog_entries(
schema_name,
table,
is_view,
reflect_indices=reflect_indices,
reflected_columns=columns[schema, table],
reflected_pk=primary_keys.get((schema, table)),
reflected_indices=indices.get((schema, table)),
reflected_indices=indices.get((schema, table), []),
).to_dict()
for schema, table in columns
)
Expand Down

0 comments on commit 307d4d9

Please sign in to comment.