Fix SQLAlchemy alias conflict in _search_runs for dataset filters#19498
Merged
harupy merged 3 commits intomlflow:masterfrom Dec 19, 2025
Merged
Conversation
Replaced hardcoded `anon_n` aliases with direct column references from the subquery. This prevents sqlite3.OperationalError when multiple filters are applied. To reproduce the problem (prior to fix): 1. Filter runs manually 2. Apply dataset filter 3. Check logs I've been using this fix locally for a while now, but I'm quite scared to contribute to repositories (also had to figure out how to do it). So apologies if things are not in the correct way. Signed-off-by: Frederico Santos <1119791+fredericosantos@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug in the SQLAlchemy tracking store where combining dataset filters with other filter types (parameters, metrics, tags) would cause database errors due to alias collisions. The fix replaces hardcoded string-based SQL alias references with SQLAlchemy's proper column reference syntax, allowing the ORM to correctly track and resolve subquery aliases.
Key Changes:
- Removed manual enumeration and hardcoded
anon_{idx+1}alias construction - Replaced
text(f"runs.run_uuid = {anon_table_name}.destination_id")with proper SQLAlchemy expressionSqlRun.run_uuid == dataset_filter.c.destination_id - Aligned dataset filter join logic with the established pattern used elsewhere in the codebase (e.g.,
search_traces)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
@fredericosantos Thanks for the PR! Do you have a minimum example that can reproduce the error? |
Contributor
|
Documentation preview for 6187939 is available at: More info
|
Signed-off-by: harupy <17039389+harupy@users.noreply.github.com>
harupy
approved these changes
Dec 19, 2025
harupy
approved these changes
Dec 19, 2025
WeichenXu123
pushed a commit
to WeichenXu123/mlflow
that referenced
this pull request
Dec 19, 2025
…lflow#19498) Signed-off-by: Frederico Santos <1119791+fredericosantos@users.noreply.github.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com> Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
WeichenXu123
pushed a commit
that referenced
this pull request
Dec 19, 2025
…19498) Signed-off-by: Frederico Santos <1119791+fredericosantos@users.noreply.github.com> Signed-off-by: harupy <17039389+harupy@users.noreply.github.com> Co-authored-by: harupy <17039389+harupy@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🛠 DevTools 🛠
Install mlflow from this PR
For Databricks, use the following command:
Replaced hardcoded
anon_naliases with direct column references from the subquery. This prevents sqlite3.OperationalError when multiple filters are applied.To reproduce the problem (prior to fix):
I've been using this fix locally for a while now, but I'm quite scared to contribute, this is my first ever public PR, so apologies if things are not in the correct way.
Related Issues/PRs
#xxxWhat changes are proposed in this pull request?
This PR fixes a database error in the SQLAlchemy store where combined filters (attributes/params + datasets) caused invalid SQL generation. Specifically, the code was manually constructing join conditions using string aliases like
f"anon_{idx+1}". Since SQLAlchemy also uses an internal counter for anonymous aliases, these manual names frequently collided or referenced the wrong subquery when the total number of subqueries exceeded the number of dataset filters.The fix replaces these manual strings with
dataset_filter.c.destination_id, which allows SQLAlchemy to correctly track and resolve aliases regardless of join order or complexity.How is this PR tested?
Does this PR require documentation update?
Release Notes
Is this a user-facing change?
What component(s), interfaces, languages, and integrations does this PR affect?
Components
area/tracking: Tracking Service, tracking client APIs, autologgingHow should the PR be classified in the release notes? Choose one:
rn/bug-fix- A user-facing bug fix worth mentioning in the release notesShould this PR be included in the next patch release?