vchiapaikeo opened a new pull request, #30543:
URL: https://github.com/apache/airflow/pull/30543

   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   closes: https://github.com/apache/airflow/issues/29396
   
   This PR adds a parameter, returns_iterator to both BigQueryHook.list_rows 
and BigQueryHook.get_datasets_list. Param defaults to False for backwards 
compatibility. If set to True, it returns a subclass of PageIterator (either a 
RowIterator in the case of list_rows or a HTTPIterator in the case of 
get_datasets_list). Some notes on PageIterator: 
https://googleapis.dev/python/google-api-core/latest/_modules/google/api_core/page_iterator.html
   
   I also noticed the retry parameter was missing from list_rows. Added that as 
well and set it to the DEFAULT_RETRY object which is what it is set at if 
nothing is passed so should be backwards compatible but allow users to override 
if needed. --> 
https://github.com/googleapis/python-bigquery/blob/v2.34.4/google/cloud/bigquery/client.py#L3716
   
   ## Testing
   
   Test DAG:
   
   ```py
   from airflow import DAG
   from airflow.decorators import task
   
   
   DEFAULT_TASK_ARGS = {
       "owner": "gcp-data-platform",
       "start_date": "2023-03-13",
       "retries": 1,
       "retry_delay": 300,
   }
   
   
   @task
   def list_rows(
       project_id: str, dataset_id: str, table_id: list[str], selected_fields: 
list[str]
   ):
       from airflow.providers.google.cloud.hooks.bigquery import BigQueryHook
   
       hook = BigQueryHook(use_legacy_sql=False)
       iterator = hook.list_rows(
           project_id=project_id,
           dataset_id=dataset_id,
           table_id=table_id,
           selected_fields=selected_fields,
           return_iterator=True,
       )
       print(f"{iterator}")
       print(f"{iterator.next_page_token}")
       print(f"Num results {len(list(iterator))}")
   
   
   @task
   def list_datasets():
       from airflow.providers.google.cloud.hooks.bigquery import BigQueryHook
   
       hook = BigQueryHook(use_legacy_sql=False)
       iterator = hook.get_datasets_list(
           project_id="bigquery-public-data",
           include_all=True,
           return_iterator=True
       )
       print(f"{iterator}")
       print(f"{iterator.next_page_token}")
       print(f"Num results {len(list(iterator))}")
       print(f"{iterator.next_page_token}")
   
   
   with DAG(
       schedule_interval="@daily",
       max_active_runs=1,
       max_active_tasks=5,
       catchup=False,
       dag_id="test_bq_hook_ops",
       default_args=DEFAULT_TASK_ARGS,
   ) as dag:
       ls_rows = list_rows.override(task_id="ls_rows")(
           project_id="bigquery-public-data",
           dataset_id="baseball",
           table_id="schedules", # "games_wide",
           selected_fields=["gameId"],
       )
   
       ls_datasets = list_datasets.override(task_id="ls_datasets")()
   
   ```
   <img width="1150" alt="image" 
src="https://user-images.githubusercontent.com/9200263/230727891-2457686c-eecd-4397-ab09-061c8f8380ea.png";>
   
   
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)**
 for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal 
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
 is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party 
License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a 
newsfragment file, named `{pr_number}.significant.rst` or 
`{issue_number}.significant.rst`, in 
[newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to