-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Search pipeline name in pipeline run API #129
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
Open
yuechao-qin
wants to merge
1
commit into
ycq/search-pipeline-run-legacy-filter
Choose a base branch
from
ycq/search-pipeline-run-name
base: ycq/search-pipeline-run-legacy-filter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+792
−124
Open
Changes from all commits
Commits
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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import logging | ||
| from typing import Any | ||
|
|
||
| import sqlalchemy | ||
| from sqlalchemy import orm | ||
|
|
||
| from . import backend_types_sql as bts | ||
| from . import component_structures as structures | ||
| from . import filter_query_sql | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def create_db_engine_and_migrate_db( | ||
| database_uri: str, | ||
|
|
@@ -87,6 +93,7 @@ def migrate_db(db_engine: sqlalchemy.Engine): | |
| break | ||
|
|
||
| _backfill_pipeline_run_created_by_annotations(db_engine=db_engine) | ||
| _backfill_pipeline_run_name_annotations(db_engine=db_engine) | ||
|
|
||
|
|
||
| def _is_pipeline_run_annotation_key_already_backfilled( | ||
|
|
@@ -106,6 +113,27 @@ def _is_pipeline_run_annotation_key_already_backfilled( | |
| ).scalar() | ||
|
|
||
|
|
||
| def get_pipeline_name_from_task_spec( | ||
| *, | ||
| task_spec_dict: dict[str, Any], | ||
| ) -> str | None: | ||
| """Extract pipeline name from a task_spec dict via component_ref.spec.name. | ||
|
|
||
| Traversal path: | ||
| task_spec_dict -> TaskSpec -> component_ref -> spec -> name | ||
|
|
||
| Returns None if any step in the chain is missing or parsing fails. | ||
| """ | ||
| try: | ||
| task_spec = structures.TaskSpec.from_json_dict(task_spec_dict) | ||
| except Exception: | ||
| return None | ||
| spec = task_spec.component_ref.spec | ||
| if spec is None: | ||
| return None | ||
| return spec.name or None | ||
|
|
||
|
|
||
| def _backfill_pipeline_run_created_by_annotations( | ||
| *, | ||
| db_engine: sqlalchemy.Engine, | ||
|
|
@@ -142,3 +170,155 @@ def _backfill_pipeline_run_created_by_annotations( | |
| ) | ||
| session.execute(stmt) | ||
| session.commit() | ||
|
|
||
|
|
||
| def _backfill_pipeline_names_from_extra_data( | ||
| *, | ||
| db_engine: sqlalchemy.Engine, | ||
| ) -> None: | ||
| """Phase 1: bulk SQL backfill from extra_data['pipeline_name']. | ||
|
|
||
| INSERT INTO pipeline_run_annotation | ||
| SELECT id, key, json_extract(extra_data, '$.pipeline_name') | ||
| FROM pipeline_run | ||
| WHERE json_extract(...) IS NOT NULL AND != '' | ||
|
|
||
| SQLAlchemy's JSON path extraction is NULL-safe: returns SQL NULL | ||
| when extra_data is NULL or the key is absent (no Python error). | ||
| """ | ||
| with orm.Session(db_engine) as session: | ||
| pipeline_name_expr = bts.PipelineRun.extra_data["pipeline_name"].as_string() | ||
| stmt = sqlalchemy.insert(bts.PipelineRunAnnotation).from_select( | ||
| ["pipeline_run_id", "key", "value"], | ||
| sqlalchemy.select( | ||
| bts.PipelineRun.id, | ||
| sqlalchemy.literal( | ||
| filter_query_sql.PipelineRunAnnotationSystemKey.NAME | ||
| ), | ||
| pipeline_name_expr, | ||
| ).where( | ||
| pipeline_name_expr.isnot(None), | ||
| pipeline_name_expr != "", | ||
| ), | ||
| ) | ||
| session.execute(stmt) | ||
| session.commit() | ||
|
|
||
|
|
||
| def _backfill_pipeline_names_from_component_spec( | ||
| *, | ||
| db_engine: sqlalchemy.Engine, | ||
| ) -> None: | ||
| """Phase 2: Python fallback for runs still missing a name annotation. | ||
|
|
||
| Find the "delta" -- runs that still have no name annotation | ||
| after Phase 1 -- using a LEFT JOIN anti-join pattern: | ||
|
|
||
| SELECT pr.id, pr.root_execution_id | ||
| FROM pipeline_run pr | ||
| LEFT JOIN pipeline_run_annotation ann | ||
| ON ann.pipeline_run_id = pr.id | ||
| AND ann.key = 'system/pipeline_run.name' | ||
| WHERE ann.pipeline_run_id IS NULL | ||
|
|
||
| How the LEFT JOIN works: | ||
|
|
||
| pipeline_run pipeline_run_annotation | ||
| +----+------------------+ +--------+---------------------------+-------+ | ||
| | id | root_exec_id | | run_id | key | value | | ||
| +----+------------------+ +--------+---------------------------+-------+ | ||
| | 1 | exec_1 | | 1 | system/pipeline_run.name | foo | | ||
| | 2 | exec_2 | | 3 | system/pipeline_run.name | bar | | ||
| | 3 | exec_3 | +--------+---------------------------+-------+ | ||
| | 4 | exec_4 | | ||
| +----+------------------+ | ||
|
|
||
| LEFT JOIN result (ON run_id = id AND key = 'system/pipeline_run.name'): | ||
| +----+------------------+------------+-----------+ | ||
| | id | root_exec_id | ann.run_id | ann.value | | ||
| +----+------------------+------------+-----------+ | ||
| | 1 | exec_1 | 1 | foo | <- matched | ||
| | 2 | exec_2 | NULL | NULL | <- no match | ||
| | 3 | exec_3 | 3 | bar | <- matched | ||
| | 4 | exec_4 | NULL | NULL | <- no match | ||
| +----+------------------+------------+-----------+ | ||
|
|
||
| + WHERE ann.pipeline_run_id IS NULL -> rows 2, 4 (the delta) | ||
|
|
||
| For each delta run, load execution_node.task_spec and extract | ||
| the name via: | ||
| task_spec_dict -> TaskSpec -> component_ref -> spec -> name | ||
| """ | ||
| key = filter_query_sql.PipelineRunAnnotationSystemKey.NAME | ||
| ann = bts.PipelineRunAnnotation | ||
| with orm.Session(db_engine) as session: | ||
| delta_query = ( | ||
| sqlalchemy.select( | ||
| bts.PipelineRun.id, | ||
| bts.PipelineRun.root_execution_id, | ||
| ) | ||
| .outerjoin( | ||
| ann, | ||
| sqlalchemy.and_( | ||
| ann.pipeline_run_id == bts.PipelineRun.id, | ||
| ann.key == key, | ||
| ), | ||
| ) | ||
| .where(ann.pipeline_run_id.is_(None)) | ||
| ) | ||
| delta_rows = session.execute(delta_query).all() | ||
|
|
||
| for run_id, root_execution_id in delta_rows: | ||
| execution_node = session.get(bts.ExecutionNode, root_execution_id) | ||
| if execution_node is None: | ||
| logger.warning( | ||
| f"Backfill pipeline run name: run {run_id} has no " | ||
| f"execution node (root_execution_id={root_execution_id}), " | ||
| "skipping. TODO: consider inserting 'UNKNOWN'?" | ||
| ) | ||
| continue | ||
| name = get_pipeline_name_from_task_spec( | ||
| task_spec_dict=execution_node.task_spec | ||
| ) | ||
| if name: | ||
| session.add( | ||
| bts.PipelineRunAnnotation( | ||
| pipeline_run_id=run_id, key=key, value=name | ||
| ) | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| f"Backfill pipeline run name: run {run_id} has no " | ||
| "resolvable pipeline name from task_spec " | ||
| f"(root_execution_id={root_execution_id}), " | ||
| "skipping. TODO: consider inserting 'UNKNOWN'?" | ||
| ) | ||
| session.commit() | ||
|
|
||
|
|
||
| def _backfill_pipeline_run_name_annotations( | ||
| *, | ||
| db_engine: sqlalchemy.Engine, | ||
| ) -> None: | ||
| """Backfill pipeline_run_annotation with pipeline names. | ||
|
|
||
| Skips entirely if any name annotation already exists (i.e. the | ||
| write-path is populating them, so the backfill has already run or is | ||
| no longer needed). | ||
|
|
||
| Phase 1 -- _backfill_pipeline_names_from_extra_data: | ||
| Bulk SQL insert from extra_data['pipeline_name']. | ||
|
|
||
| Phase 2 -- _backfill_pipeline_names_from_component_spec: | ||
| Python fallback for runs Phase 1 missed (extra_data is NULL or | ||
| missing the key). Resolves name via component_ref.spec.name. | ||
| """ | ||
| with orm.Session(db_engine) as session: | ||
| if _is_pipeline_run_annotation_key_already_backfilled( | ||
| session=session, | ||
| key=filter_query_sql.PipelineRunAnnotationSystemKey.NAME, | ||
| ): | ||
| return | ||
|
|
||
| _backfill_pipeline_names_from_extra_data(db_engine=db_engine) | ||
| _backfill_pipeline_names_from_component_spec(db_engine=db_engine) | ||
|
Comment on lines
+322
to
+324
Collaborator
Author
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. Do these in a single transaction. Also, look into removing phase 1 (extra_data) and see if we can do eveyrhting for phase 2 in a single query. https://dev.mysql.com/doc/refman/8.4/en/json-search-functions.html |
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.