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(presto): get_catalog_names #23599

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion superset/db_engine_specs/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def get_catalog_names(
SELECT datname FROM pg_database
WHERE datistemplate = false;
"""
).fetchall()

Choose a reason for hiding this comment

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

Curious about why this had to change?

Copy link
Member Author

Choose a reason for hiding this comment

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

@Antonio-RiveroMartnez in this case it's redundant, the expression generator will implicitly call fetchall() here. Eg:

>>> from sqlalchemy import create_engine
>>> engine = create_engine("shillelagh://")
>>> url = "https://docs.google.com/spreadsheets/d/1LcWZMsdCl92g7nA-D6qGRqg1T5TiHyuKJUY1u9XAnsk/edit#gid=0"
>>> engine.execute(f'SELECT * FROM "{url}"').fetchall()
[('BR', 2), ('BR', 4), ('ZA', 7), ('CR', 11), ('CR', 11), ('FR', 100), ('AR', 42)]
>>> [row for row in engine.execute(f'SELECT * FROM "{url}"')]
[('BR', 2), ('BR', 4), ('ZA', 7), ('CR', 11), ('CR', 11), ('FR', 100), ('AR', 42)]

)
)

@classmethod
Expand Down
11 changes: 11 additions & 0 deletions superset/db_engine_specs/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,17 @@ def get_view_names(
results = cursor.fetchall()
return {row[0] for row in results}

@classmethod
def get_catalog_names(
cls,
database: Database,
inspector: Inspector,
) -> List[str]:
"""
Get all catalogs.
"""
return [catalog for (catalog,) in inspector.bind.execute("SHOW CATALOGS")]

@classmethod
def _create_column_info(
cls, name: str, data_type: types.TypeEngine
Expand Down
21 changes: 21 additions & 0 deletions tests/integration_tests/db_engine_specs/presto_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
from unittest import mock, skipUnless

import pandas as pd
from flask.ctx import AppContext
from sqlalchemy import types
from sqlalchemy.sql import select

from superset.db_engine_specs.presto import PrestoEngineSpec
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.sql_parse import ParsedQuery
from superset.utils.database import get_example_database
from tests.integration_tests.db_engine_specs.base_tests import TestDbEngineSpec


Expand Down Expand Up @@ -1032,3 +1034,22 @@ def is_readonly(sql: str) -> bool:
assert is_readonly("EXPLAIN SELECT 1")
assert is_readonly("SELECT 1")
assert is_readonly("WITH (SELECT 1) bla SELECT * from bla")


def test_get_catalog_names(app_context: AppContext) -> None:
"""
Test the ``get_catalog_names`` method.
"""
database = get_example_database()

if database.backend != "presto":
return

with database.get_inspector_with_context() as inspector:
assert PrestoEngineSpec.get_catalog_names(database, inspector) == [
"jmx",
"memory",
"system",
"tpcds",
"tpch",
]