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

fix(migration): Ensure the paginated update is deterministic #21778

Merged
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
31 changes: 20 additions & 11 deletions superset/migrations/shared/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,31 @@ def paginated_update(
"""
Update models in small batches so we don't have to load everything in memory.
"""
start = 0
count = query.count()

total = query.count()
processed = 0
session: Session = inspect(query).session
result = session.execute(query)

if print_page_progress is None or print_page_progress is True:
print_page_progress = lambda current, total: print(
f" {current}/{total}", end="\r"
print_page_progress = lambda processed, total: print(
f" {processed}/{total}", end="\r"
)
while start < count:
end = min(start + batch_size, count)
for obj in query[start:end]:
yield obj
session.merge(obj)
Copy link
Member Author

Choose a reason for hiding this comment

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

There's no need to merge the record. The caller should handle this if required.


while True:
Copy link
Member Author

@john-bodley john-bodley Oct 12, 2022

Choose a reason for hiding this comment

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

The option was to use either query.slice(...) or session.execute(query).fetchmany(...). I opted for the later because:

  1. Otherwise one would need to include an order_by(...) condition and there's no guarantee that it would be defined.
  2. Re-executing the query n times using a different OFFSET per query is likely neither efficient nor guarantees correct pagination given that the filter condition could change.

rows = result.fetchmany(batch_size)

if not rows:
break

for row in rows:
yield row[0]

session.commit()
processed += len(rows)

if print_page_progress:
print_page_progress(end, count)
start += batch_size
print_page_progress(processed, total)


def try_load_json(data: Optional[str]) -> Dict[str, Any]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def upgrade():
state["anchor"] = state["hash"]
del state["hash"]
entry.value = pickle.dumps(value)
session.commit()
Copy link
Member Author

Choose a reason for hiding this comment

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

There's no need to commit as the paginated_update handles it.



def downgrade():
Expand All @@ -87,5 +86,3 @@ def downgrade():
state["hash"] = state["anchor"]
del state["anchor"]
entry.value = pickle.dumps(value)
session.merge(entry)
Copy link
Member Author

Choose a reason for hiding this comment

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

There's no need to merge the existing entry. See here for details:

Used when you may have more than 1 in-memory objects which map to the same database record with some key.

session.commit()
Copy link
Member Author

Choose a reason for hiding this comment

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

There's no need to commit as the paginated_update handles it.