|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Runs garf workflow.""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import logging |
| 19 | +import pathlib |
| 20 | +import re |
| 21 | +from typing import Final |
| 22 | + |
| 23 | +from garf.executors import exceptions, setup, workflow |
| 24 | +from garf.executors.telemetry import tracer |
| 25 | +from garf.io import reader |
| 26 | + |
| 27 | +logger = logging.getLogger(__name__) |
| 28 | + |
| 29 | +_REMOTE_FILES_PATTERN: Final[str] = ( |
| 30 | + '^(http|gs|s3|aruze|hdfs|webhdfs|ssh|scp|sftp)' |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +class WorkflowRunner: |
| 35 | + """Runs garf workflow. |
| 36 | +
|
| 37 | + Attributes: |
| 38 | + workflow: Workflow to execute. |
| 39 | + wf_parent: Optional location of a workflow file. |
| 40 | + parallel_threshold: Max allowed parallelism for the queries in the workflow. |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + execution_workflow: workflow.Workflow, |
| 46 | + wf_parent: pathlib.Path | str, |
| 47 | + parallel_threshold: int = 10, |
| 48 | + ) -> None: |
| 49 | + """Initializes WorkflowRunner.""" |
| 50 | + self.workflow = execution_workflow |
| 51 | + self.wf_parent = wf_parent |
| 52 | + self.parallel_threshold = parallel_threshold |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def from_file(cls, workflow_file: str | pathlib.Path) -> WorkflowRunner: |
| 56 | + """Initialized Workflow runner from a local or remote file.""" |
| 57 | + if isinstance(workflow_file, str): |
| 58 | + workflow_file = pathlib.Path(workflow_file) |
| 59 | + execution_workflow = workflow.Workflow.from_file(workflow_file) |
| 60 | + return cls( |
| 61 | + execution_workflow=execution_workflow, wf_parent=workflow_file.parent |
| 62 | + ) |
| 63 | + |
| 64 | + def run( |
| 65 | + self, enable_cache: bool = False, cache_ttl_seconds: int = 3600 |
| 66 | + ) -> list[str]: |
| 67 | + reader_client = reader.create_reader('file') |
| 68 | + execution_results = [] |
| 69 | + for i, step in enumerate(self.workflow.steps, 1): |
| 70 | + step_name = f'{i}-{step.fetcher}' |
| 71 | + if step.alias: |
| 72 | + step_name = f'{step_name}-{step.alias}' |
| 73 | + with tracer.start_as_current_span(step_name): |
| 74 | + query_executor = setup.setup_executor( |
| 75 | + source=step.fetcher, |
| 76 | + fetcher_parameters=step.fetcher_parameters, |
| 77 | + enable_cache=enable_cache, |
| 78 | + cache_ttl_seconds=cache_ttl_seconds, |
| 79 | + ) |
| 80 | + batch = {} |
| 81 | + if not (queries := step.queries): |
| 82 | + logger.error('Please provide one or more queries to run') |
| 83 | + raise exceptions.GarfExecutorError( |
| 84 | + 'Please provide one or more queries to run' |
| 85 | + ) |
| 86 | + for query in queries: |
| 87 | + if isinstance(query, workflow.QueryPath): |
| 88 | + query_path = query.full_path |
| 89 | + if re.match(_REMOTE_FILES_PATTERN, query_path): |
| 90 | + batch[query.path] = reader_client.read(query_path) |
| 91 | + else: |
| 92 | + if not query.prefix: |
| 93 | + query_path = self.wf_parent / pathlib.Path(query.path) |
| 94 | + if not query_path.exists(): |
| 95 | + raise workflow.GarfWorkflowError( |
| 96 | + f'Query: {query_path} not found' |
| 97 | + ) |
| 98 | + batch[query.path] = reader_client.read(query_path) |
| 99 | + elif isinstance(query, workflow.QueryFolder): |
| 100 | + query_path = self.wf_parent / pathlib.Path(query.folder) |
| 101 | + if not query_path.exists(): |
| 102 | + raise workflow.GarfWorkflowError( |
| 103 | + f'Folder: {query_path} not found' |
| 104 | + ) |
| 105 | + for p in query_path.rglob('*'): |
| 106 | + if p.suffix == '.sql': |
| 107 | + batch[p.stem] = reader_client.read(p) |
| 108 | + else: |
| 109 | + batch[query.query.title] = query.query.text |
| 110 | + query_executor.execute_batch( |
| 111 | + batch, step.context, self.parallel_threshold |
| 112 | + ) |
| 113 | + execution_results.append(step_name) |
| 114 | + return execution_results |
0 commit comments