-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
john-bodley
merged 2 commits into
apache:master
from
john-bodley:john-bodley--optimize-dataset-update
Jul 27, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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) | ||
|
||
|
@@ -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. | ||
|
@@ -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( | ||
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. 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. | ||
|
@@ -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( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
pop
instead of direct access?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.
@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 respectiveupdate_columns
andupdate_metrics
method.