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

refactor: Improve performance regression introduced in #20473 #20810

Merged
Merged
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
62 changes: 34 additions & 28 deletions superset/datasets/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ def update(
"""

if "columns" in properties:
properties["columns"] = cls.update_columns(model, properties["columns"])
cls.update_columns(model, properties.pop("columns"), commit=commit)
Copy link
Member

Choose a reason for hiding this comment

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

Why pop instead of direct access?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ktmud these properties need to be popped so they're not re-included in the base update method which updates the underlying dataset. The new columns and metrics are already bound to the model in the respective update_columns and update_metrics method.


if "metrics" in properties:
properties["metrics"] = cls.update_metrics(model, properties["metrics"])
cls.update_metrics(model, properties.pop("metrics"), commit=commit)

return super().update(model, properties, commit=commit)

Expand All @@ -166,7 +166,8 @@ def update_columns(
cls,
model: SqlaTable,
property_columns: List[Dict[str, Any]],
) -> List[TableColumn]:
commit: bool = True,
) -> None:
"""
Creates/updates and/or deletes a list of columns, based on a
list of Dict.
Expand All @@ -178,33 +179,36 @@ def update_columns(
"""

column_by_id = {column.id: column for column in model.columns}
columns = []
seen = set()

for properties in property_columns:
if "id" in properties:
columns.append(
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 was the root of the problem, i.e., existing columns shouldn't be added to the set of columns which need to be bound to the dataset when updated.

DatasetDAO.update_column(
column_by_id[properties["id"]],
properties,
commit=False,
)
seen.add(properties["id"])

DatasetDAO.update_column(
column_by_id[properties["id"]],
properties,
commit=False,
)
else:
DatasetDAO.create_column(
{**properties, "table_id": model.id},
commit=False,
)

# Note for new columns the primary key is undefined sans a commit/flush.
columns.append(DatasetDAO.create_column(properties, commit=False))

for id_ in {obj.id for obj in model.columns} - {obj.id for obj in columns}:
for id_ in {obj.id for obj in model.columns} - seen:
DatasetDAO.delete_column(column_by_id[id_], commit=False)

return columns
if commit:
db.session.commit()

@classmethod
def update_metrics(
cls,
model: SqlaTable,
property_metrics: List[Dict[str, Any]],
) -> List[SqlMetric]:
commit: bool = True,
) -> None:
"""
Creates/updates and/or deletes a list of metrics, based on a
list of Dict.
Expand All @@ -216,26 +220,28 @@ def update_metrics(
"""

metric_by_id = {metric.id: metric for metric in model.metrics}
metrics = []
seen = set()

for properties in property_metrics:
if "id" in properties:
metrics.append(
DatasetDAO.update_metric(
metric_by_id[properties["id"]],
properties,
commit=False,
)
seen.add(properties["id"])

DatasetDAO.update_metric(
metric_by_id[properties["id"]],
properties,
commit=False,
)
else:
DatasetDAO.create_metric(
{**properties, "table_id": model.id},
commit=False,
)

# Note for new metrics the primary key is undefined sans a commit/flush.
metrics.append(DatasetDAO.create_metric(properties, commit=False))

for id_ in {obj.id for obj in model.metrics} - {obj.id for obj in metrics}:
for id_ in {obj.id for obj in model.metrics} - seen:
DatasetDAO.delete_column(metric_by_id[id_], commit=False)

return metrics
if commit:
db.session.commit()

@classmethod
def find_dataset_column(
Expand Down