Skip to content

Add query_output_schema to ReadFromBigQuery for BEAM_ROW + query support#39160

Open
nikitagrover19 wants to merge 2 commits into
apache:masterfrom
nikitagrover19:feat/ng/yaml-bq-query-beam-row
Open

Add query_output_schema to ReadFromBigQuery for BEAM_ROW + query support#39160
nikitagrover19 wants to merge 2 commits into
apache:masterfrom
nikitagrover19:feat/ng/yaml-bq-query-beam-row

Conversation

@nikitagrover19

Copy link
Copy Markdown
Contributor

Schema cannot be auto-derived from a table when a query is used, so this adds an explicit query_output_schema param for that case.

Fixes #36988

Problem
BEAM_ROW output was disallowed with query= since schema can't be derived from a table when there's no table. This blocked the YAML transform from using BEAM_ROW with queries.

Change
Adds an optional query_output_schema param to ReadFromBigQuery, used directly as the row schema when set. YAML's read_from_bigquery() exposes this as schema, required when query is set. Backward compatible, table-based reads unaffected.

Testing
Unit tests cover constructor validation, schema pass-through, and that get_table() is skipped when query_output_schema is supplied. Not covered: correctness of convert_to_usertype output against a real query execution - would need a live BQ connection or a heavier fake.

Known testing gap
These tests verify the schema is passed through and used (i.e., convert_to_usertype is called with the right arguments, and get_table is correctly skipped). They do not verify that convert_to_usertype produces correct Beam Rows at runtime from a real query execution, that would require either a live BigQuery connection or a more substantial fake of the job-execution lifecycle. Open to adding that if reviewers feel it's necessary for merge.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

Schema cannot be auto-derived from a table when a query is used, so
this adds an explicit query_output_schema param for that case.

Fixes apache#36988
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enables the use of BEAM_ROW output with BigQuery queries by allowing users to explicitly provide the output schema. Previously, this was blocked because schemas could not be auto-derived from queries. The changes introduce a new parameter to the ReadFromBigQuery transform and the corresponding YAML interface, ensuring that schema-based pipelines can now function correctly with query-based inputs.

Highlights

  • Added query_output_schema parameter: Introduced an optional query_output_schema parameter to ReadFromBigQuery to allow explicit schema definition when using queries with BEAM_ROW output.
  • YAML transform update: Updated the YAML read_from_bigquery transform to accept and require a schema when a query is provided, enabling BEAM_ROW support in YAML pipelines.
  • Validation and testing: Added constructor validation to ensure schema is provided for query-based BEAM_ROW reads and included comprehensive unit tests for both Python SDK and YAML interfaces.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for specifying a query_output_schema when reading from BigQuery using a query with the BEAM_ROW output type, allowing schemas to be defined when they cannot be auto-derived from a table. The changes span both the standard Python SDK and the YAML-based IO transforms, along with accompanying unit tests. The review feedback highlights a critical runtime issue where the schema needs to be normalized to a TableSchema object using bigquery_tools.get_dict_table_schema to prevent an AttributeError. Additionally, the reviewer suggests raising an error if a schema is incorrectly provided for table-based reads in YAML, and adding corresponding tests for both of these cases.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +3024 to +3026
if self._kwargs.get('query', None) is not None:
return output_pcollection | bigquery_schema_tools.convert_to_usertype(
self.query_output_schema, self._kwargs.get('selected_fields', None))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The query_output_schema parameter can be passed as a dictionary or a JSON string (as documented in the docstring and used in YAML). However, bigquery_schema_tools.convert_to_usertype expects a TableSchema object. Passing a dictionary or string directly will result in an AttributeError at runtime (e.g., 'dict' object has no attribute 'fields'). Use bigquery_tools.get_dict_table_schema to normalize the schema before passing it to convert_to_usertype.

Suggested change
if self._kwargs.get('query', None) is not None:
return output_pcollection | bigquery_schema_tools.convert_to_usertype(
self.query_output_schema, self._kwargs.get('selected_fields', None))
if self._kwargs.get('query', None) is not None:
user_schema = bigquery_tools.get_dict_table_schema(self.query_output_schema)
return output_pcollection | bigquery_schema_tools.convert_to_usertype(
user_schema, self._kwargs.get('selected_fields', None))

Comment on lines +829 to +830
mock_get_table.assert_not_called()
mock_convert.assert_called_once_with(schema, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

Since query_output_schema is now normalized to a TableSchema object using bigquery_tools.get_dict_table_schema, the mocked convert_to_usertype will be called with the converted TableSchema object rather than the raw dictionary. Update the assertion to reflect this conversion.

Suggested change
mock_get_table.assert_not_called()
mock_convert.assert_called_once_with(schema, None)
mock_get_table.assert_not_called()
mock_convert.assert_called_once_with(
bigquery_tools.get_dict_table_schema(schema), None)

Comment on lines 128 to 130
if query is None:
assert table is not None
else:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

If a user provides a schema parameter for a table-based read (where query is None), it will be silently ignored because ReadFromBigQuery only uses query_output_schema when a query is specified. To prevent silent failures and improve usability, raise a ValueError if schema is provided for a table-based read.

  if query is None:
    assert table is not None
    if schema is not None:
      raise ValueError(
          "The 'schema' parameter is only supported when reading with a 'query'. "
          "For table-based reads, the schema is automatically derived.")
  else:

Comment on lines +785 to +786
self.assertIsNone(call_kwargs.get('query_output_schema'))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Add a unit test to verify that providing a schema for a table-based read raises a ValueError as expected.

Suggested change
self.assertIsNone(call_kwargs.get('query_output_schema'))
self.assertIsNone(call_kwargs.get('query_output_schema'))
def test_table_with_schema_raises(self):
from apache_beam.yaml.yaml_io import read_from_bigquery
with self.assertRaisesRegex(ValueError, 'only supported when reading with a'):
read_from_bigquery(table='project:dataset.table', schema={'fields': []})

@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Specify output_type in ReadFromBigQuery Beam YAML transform

1 participant