-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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-68): Add DatasourceDAO class to manage querying different datasources easier #20030
Conversation
Codecov Report
@@ Coverage Diff @@
## master #20030 +/- ##
==========================================
- Coverage 66.33% 66.19% -0.14%
==========================================
Files 1713 1713
Lines 64083 64148 +65
Branches 6734 6744 +10
==========================================
- Hits 42509 42463 -46
- Misses 19863 19972 +109
- Partials 1711 1713 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
||
|
||
def test_get_datasource_sl_table(app_context: None, session: Session) -> None: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason these are just pass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently working on these test now and will push them up soon
0ebe324
to
0ff252d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, Hugh! I would just you to remove Any
from the Datasource
type and add a custom exception instead of reusing DatasetNotFoundError
. I love the unit tests in this PR!
superset/dao/datasource/dao.py
Outdated
from superset.tables.models import Table | ||
from superset.utils.core import DatasourceType | ||
|
||
Datasource = Union[Dataset, SqlaTable, Table, Query, SavedQuery, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Datasource = Union[Dataset, SqlaTable, Table, Query, SavedQuery, Any] | |
Datasource = Union[Dataset, SqlaTable, Table, Query, SavedQuery] |
We don't want Datasource
to be of type Any
— on the contrary, it's really nice that the type clearly defines what constitutes a datasource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@betodealmeida this is a hack to allow the types to play nice with mypy, the good thing is it still does some type checking. I was going to look into this more, but literally spent most of the day looking into it and had no luck. I can revisit this if you think it's a mandatory thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, if you need to add Any
to make mypy
pass it's because something is wrong. Adding Any
never fixes anything, it only makes the type checker happy. In this case it defeats the purpose of type checking, because all objects are of type Any
.
Was this fixed by using Type
?
superset/dao/datasource/dao.py
Outdated
cls, datasource_type: DatasourceType, datasource_id: int, session: Session | ||
) -> Datasource: | ||
if datasource_type not in cls.sources: | ||
raise DatasetNotFoundError() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a custom exception here (DatasourceTypeNotSupportedError
, for example).
superset/dao/datasource/dao.py
Outdated
|
||
@classmethod | ||
def get_datasource( | ||
cls, datasource_type: DatasourceType, datasource_id: int, session: Session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit, but it's nice to standardize: in your other methods the session
is the first argument, in this one it's the last. I'd move it to first argument here, for consistency.
superset/dao/datasource/dao.py
Outdated
schema: str, | ||
database_name: str, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A similar small nit here: in general schema
comes after database
in our functions/methods (since they are hierarchical), I would swap the order for consistency.
superset/dao/datasource/dao.py
Outdated
permissions: Set[str], | ||
schema_perms: Set[str], | ||
) -> List[Datasource]: | ||
# TODO(bogdan): add unit test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't leave TODOs for other people! :)
# TODO(bogdan): add unit test | |
# TODO(hughhhh): add unit test |
Or remove it altogether.
superset/dao/datasource/dao.py
Outdated
) -> List[Datasource]: | ||
# TODO(bogdan): add unit test | ||
datasource_class = DatasourceDAO.sources[DatasourceType[database.type]] | ||
if isinstance(datasource_class, SqlaTable): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, if you invert the if you can remove an indentation level:
if not isinstance(datasource_class, SqlaTable):
return []
return (
session.query(...)
...
)
superset/dao/datasource/dao.py
Outdated
) -> Optional[Datasource]: | ||
"""Returns datasource with columns and metrics.""" | ||
datasource_class = DatasourceDAO.sources[DatasourceType[datasource_type]] | ||
if isinstance(datasource_class, SqlaTable): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, if you invert the if the code is a bit easier to read:
if not isinstance(datasource_class, SqlaTable):
return None
return (...)
superset/dao/datasource/dao.py
Outdated
schema: Optional[str] = None, | ||
) -> List[Datasource]: | ||
datasource_class = DatasourceDAO.sources[DatasourceType[database.type]] | ||
if isinstance(datasource_class, SqlaTable): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why only sqlatable on these? Is the plan to add in the rest when we need them?
from superset.utils.core import DatasourceType | ||
|
||
|
||
def create_test_data(session: Session) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also rewrite this as a fixture:
@pytest.fixture
def session_with_data(session: Session) -> Iterator[Session]:
... # code of `create_test_data` here
yield session
And then in your tests:
def test_get_datasource_sqlatable(app_context: None, session_with_data: Session) -> None:
...
But there's no need, a function like this also works fine.
superset/dao/datasource/dao.py
Outdated
from superset.tables.models import Table | ||
from superset.utils.core import DatasourceType | ||
|
||
Datasource = Union[Dataset, SqlaTable, Table, Query, SavedQuery, Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we should keep the Datasource
type and DatasourceType
enum in the same file so that if one changes, we can be sure to change the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea i'll move the Datasource
reference next to the enum
superset/dao/datasource/dao.py
Outdated
if isinstance(source_class, SqlaTable): | ||
qry = source_class.default_query(qry) | ||
datasources.extend(qry.all()) | ||
return datasources |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can just limit this to just SqlaTable since no charts or dashboards will be able to be saved with any other datasources
superset/dao/datasource/dao.py
Outdated
) | ||
|
||
if not datasource: | ||
raise DatasetNotFoundError() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing here re what Beto said above. This could be a missing Table or Query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing the comments!
…tasources easier (apache#20030) * restart * update with enums * address concerns * remove any
SUMMARY
Building a data access object for Datasource to make it easier to query a source based upon id and type. With sip-68 work we'll be allowing users to power charts with different data object such as (queries, savedqueries, sl_datasets, and sl_tables) this work will make it easier to do quick look up of these objects with out having to write and ORM query.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
ADDITIONAL INFORMATION