-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
perf: Implement model specific lookups by id to improve performance #20974
Changes from 1 commit
a695fc6
0ffe859
fc63ff2
ab59aa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,14 +50,17 @@ class BaseDAO: | |
|
||
@classmethod | ||
def find_by_id( | ||
cls, model_id: Union[str, int], session: Session = None | ||
cls, | ||
model_id: Union[str, int], | ||
session: Session = None, | ||
no_filter: bool = False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, let's go with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit nit: can we maybe call this |
||
) -> Optional[Model]: | ||
""" | ||
Find a model by id, if defined applies `base_filter` | ||
""" | ||
session = session or db.session | ||
query = session.query(cls.model_cls) | ||
if cls.base_filter: | ||
if cls.base_filter and not no_filter: | ||
data_model = SQLAInterface(cls.model_cls, session) | ||
query = cls.base_filter( # pylint: disable=not-callable | ||
cls.id_column_name, data_model | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from typing import Iterator | ||
|
||
import pytest | ||
from sqlalchemy.orm.session import Session | ||
|
||
from superset.utils.core import DatasourceType | ||
|
||
|
||
@pytest.fixture | ||
def session_with_data(session: Session) -> Iterator[Session]: | ||
from superset.models.slice import Slice | ||
|
||
engine = session.get_bind() | ||
Slice.metadata.create_all(engine) # pylint: disable=no-member | ||
|
||
slice_obj = Slice( | ||
id=1, | ||
datasource_id=1, | ||
datasource_type=DatasourceType.TABLE, | ||
datasource_name="tmp_perm_table", | ||
slice_name="slice_name", | ||
) | ||
|
||
session.add(slice_obj) | ||
session.flush() | ||
yield session | ||
villebro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_slice_find_by_id_no_filter(session_with_data: Session) -> None: | ||
from superset.charts.dao import ChartDAO | ||
from superset.models.slice import Slice | ||
|
||
result = ChartDAO.find_by_id(1, session=session_with_data, no_filter=True) | ||
|
||
assert result | ||
assert 1 == result.id | ||
assert "slice_name" == result.slice_name | ||
assert isinstance(result, Slice) | ||
|
||
|
||
def test_datasource_find_by_id_no_filter_not_found(session_with_data: Session) -> None: | ||
from superset.charts.dao import ChartDAO | ||
|
||
result = ChartDAO.find_by_id(125326326, session=session_with_data, no_filter=True) | ||
assert result is None |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from typing import Iterator | ||
|
||
import pytest | ||
from sqlalchemy.orm.session import Session | ||
|
||
|
||
@pytest.fixture | ||
def session_with_data(session: Session) -> Iterator[Session]: | ||
from superset.connectors.sqla.models import SqlaTable | ||
from superset.models.core import Database | ||
|
||
engine = session.get_bind() | ||
SqlaTable.metadata.create_all(engine) # pylint: disable=no-member | ||
|
||
db = Database(database_name="my_database", sqlalchemy_uri="sqlite://") | ||
sqla_table = SqlaTable( | ||
table_name="my_sqla_table", | ||
columns=[], | ||
metrics=[], | ||
database=db, | ||
) | ||
|
||
session.add(db) | ||
session.add(sqla_table) | ||
session.flush() | ||
yield session | ||
Comment on lines
+26
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note about missing cleanup as previously. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
|
||
def test_datasource_find_by_id_no_filter(session_with_data: Session) -> None: | ||
from superset.connectors.sqla.models import SqlaTable | ||
from superset.datasets.dao import DatasetDAO | ||
|
||
result = DatasetDAO.find_by_id( | ||
1, | ||
session=session_with_data, | ||
no_filter=True, | ||
) | ||
|
||
assert result | ||
assert 1 == result.id | ||
assert "my_sqla_table" == result.table_name | ||
assert isinstance(result, SqlaTable) | ||
|
||
|
||
def test_datasource_find_by_id_no_filter_not_found(session_with_data: Session) -> None: | ||
from superset.datasets.dao import DatasetDAO | ||
|
||
result = DatasetDAO.find_by_id( | ||
125326326, | ||
session=session_with_data, | ||
no_filter=True, | ||
) | ||
assert result is 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.
addresses mypy issue in