Skip to content

Conversation

@jialuoo
Copy link
Contributor

@jialuoo jialuoo commented Oct 15, 2025

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes b/450913424 🦕

@product-auto-label product-auto-label bot added size: s Pull request size is small. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Oct 15, 2025
@jialuoo jialuoo marked this pull request as ready for review October 16, 2025 20:16
@jialuoo jialuoo requested review from a team as code owners October 16, 2025 20:16
Comment on lines 81 to 97
def _cleanup_old_udfs(self):
"""Clean up old UDFs in the anonymous dataset."""
dataset = self._session._anon_dataset_manager.dataset
routines = list(self._session.bqclient.list_routines(dataset))
cleanup_cutoff_time = datetime.datetime.now(
datetime.timezone.utc
) - datetime.timedelta(days=_UDF_CLEANUP_THRESHOLD_DAYS)

for routine in routines:
if (
routine.created < cleanup_cutoff_time
and routine._properties["routineType"] == "SCALAR_FUNCTION"
):
try:
self._session.bqclient.delete_routine(routine.reference)
except Exception:
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems this code should just be in the AnonymousDatasetManager itself? Also, are we sure on the timing of when we want to trigger this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to AnonymousDatasetManager.

Copy link
Contributor

@TrevorBergeron TrevorBergeron left a comment

Choose a reason for hiding this comment

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

Also, maybe some tests to validate the behavior?

@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Oct 24, 2025
@jialuoo
Copy link
Contributor Author

jialuoo commented Oct 31, 2025

Also, maybe some tests to validate the behavior?

Added a test for it.

Comment on lines +180 to +185
except Exception as e:
# Log a warning on the failure, do not interrupt the workflow.
msg = bfe.format_message(
f"Failed to clean up the old Python UDFs before closing the session: {e}"
)
warnings.warn(msg, category=bfe.CleanupFailedWarning)
Copy link
Contributor

Choose a reason for hiding this comment

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

This will only be warned if the list_routines call fails, but if all the individual deletions fail, we won't warn?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added. Thanks.

@jialuoo jialuoo merged commit bfcc08f into main Nov 6, 2025
25 checks passed
@jialuoo jialuoo deleted the routine-cleanup branch November 6, 2025 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants