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

feat(SIP-95): new endpoint for table metadata #28122

Merged
merged 3 commits into from
Apr 25, 2024

Conversation

betodealmeida
Copy link
Member

@betodealmeida betodealmeida commented Apr 18, 2024

SUMMARY

This PR adds a new endpoint to get metadata from a table, optionally passing a schema and a catalog.

In order to support the new endpoint, this PR has two refactors. Despite touching many files, they are simple:

  1. All methods that take a table_name and a schema were updated to received a Table instance instead, since it carries the catalog:

@dataclass(eq=True, frozen=True)
class Table:
"""
A fully qualified SQL table conforming to [[catalog.]schema.]table.
"""
table: str
schema: str | None = None
catalog: str | None = None

  1. All other methods that take a schema now also take a catalog.

Note that currently the catalog being passed around is always None. It gets passed all the way to the adjust_engine_params method in the DB engine spec, and DB engine specs are responsible for modifying the SQLAlchemy URI to connect to the catalog.

In the next PR I'll start implementing support for catalogs in Postgres, and will modify the UI to allow choosing a catalog when running SQL or creating a dataset.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A

TESTING INSTRUCTIONS

I added unit tests for the new endpoint, and updated existing tests.

ADDITIONAL INFORMATION

  • Has associated issue: [SIP-95] Proposal for Catalog Support in SQL Lab #22862
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@betodealmeida betodealmeida requested a review from a team as a code owner April 18, 2024 13:32
@github-actions github-actions bot added risk:db-migration PRs that require a DB migration api Related to the REST API labels Apr 18, 2024
@@ -403,7 +413,7 @@ def get_sqla_engine(
# if ssh_tunnel is available build engine with information
engine_context = ssh_manager_factory.instance.create_tunnel(
ssh_tunnel=ssh_tunnel,
sqlalchemy_database_uri=self.sqlalchemy_uri_decrypted,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line 396 above.

Comment on lines -427 to -430
# The `get_sqla_engine_with_context` was renamed to `get_sqla_engine`, but we kept a
# reference to the old method to prevent breaking third-party applications.
# TODO (betodealmeida): Remove in 5.0
get_sqla_engine_with_context = get_sqla_engine
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because the signature for get_sqla_engine changed to accept a catalog.

Comment on lines -450 to -464
# The ``adjust_database_uri`` method was renamed to ``adjust_engine_params`` and
# had its signature changed in order to support more DB engine specs. Since DB
# engine specs can be released as 3rd party modules we want to make sure the old
# method is still supported so we don't introduce a breaking change.
if hasattr(self.db_engine_spec, "adjust_database_uri"):
sqlalchemy_url = self.db_engine_spec.adjust_database_uri(
sqlalchemy_url,
schema,
)
logger.warning(
"DB engine spec %s implements the method `adjust_database_uri`, which is "
"deprecated and will be removed in version 3.0. Please update it to "
"implement `adjust_engine_params` instead.",
self.db_engine_spec,
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're on 4.0 now.

@github-actions github-actions bot added doc Namespace | Anything related to documentation plugins dependencies:npm github_actions Pull requests that update GitHub Actions code labels Apr 18, 2024
@betodealmeida betodealmeida changed the base branch from new-extra-table-metadata-api to master April 18, 2024 14:54
@github-actions github-actions bot removed the doc Namespace | Anything related to documentation label Apr 18, 2024
@betodealmeida betodealmeida force-pushed the new-table-metadata-api branch 13 times, most recently from 092fd9d to cc6088d Compare April 19, 2024 08:00
Comment on lines -217 to -223
@property
def sqla_metadata(self) -> None:
# pylint: disable=no-member
with self.get_sqla_engine() as engine:
meta = MetaData(bind=engine)
meta.reflect()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere, AFAICT.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/Users/beto/Projects/superset/superset (new-table-metadata-api)$ git grep '\.sqla_metadata'
/Users/beto/Projects/superset/superset (new-table-metadata-api)$

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, after I removed this property there are no occurrences of sqla_metadata in the codebase:

/Users/beto/Projects/superset (new-table-metadata-api)$ git grep sqla_metadata
/Users/beto/Projects/superset (new-table-metadata-api)$

@betodealmeida betodealmeida force-pushed the new-table-metadata-api branch 4 times, most recently from 171569f to 1f73f2f Compare April 22, 2024 17:14
def get_table_metadata(
database: Any, table_name: str, schema_name: Optional[str]
) -> dict[str, Any]:
def get_table_metadata(database: Any, table: Table) -> TableMetadataResponse:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the database type be a Database model?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch!

@betodealmeida betodealmeida merged commit 6cf681d into master Apr 25, 2024
29 checks passed
qleroy pushed a commit to qleroy/superset that referenced this pull request Apr 28, 2024
jzhao62 pushed a commit to jzhao62/superset that referenced this pull request May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to the REST API preset-io risk:db-migration PRs that require a DB migration size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants