apache/beam · error · ValueError

Both a BigQuery table and a query were specified. Please spe

Error message

Both a BigQuery table and a query were specified. Please specify only one of these.

What it means

Raised by the validation guard at the top of ReadFromDatastore/__init__ in apache_beam/io/gcp/bigquery.py: this transform accepts either an immutable table reference (table/dataset/project) or a SQL query (query), never both. The check `if table is not None and query is not None` fires only when the caller passed a non-None table argument together with a non-None query argument, making the read target ambiguous. Fix by supplying exactly one source: either the table path (table='project:dataset.table') or the query string, leaving the other as None.

Source

Thrown at sdks/python/apache_beam/io/gcp/bigquery.py:721

      table=None,
      dataset=None,
      project=None,
      query=None,
      validate=False,
      pipeline_options=None,
      coder=None,
      use_standard_sql=False,
      flatten_results=True,
      kms_key=None,
      bigquery_job_labels=None,
      use_json_exports=False,
      job_name=None,
      step_name=None,
      unique_id=None,
      temp_dataset=None,
      query_priority=BigQueryQueryPriority.BATCH):
    if table is not None and query is not None:
      raise ValueError(
          'Both a BigQuery table and a query were specified.'
          ' Please specify only one of these.')
    elif table is None and query is None:
      raise ValueError('A BigQuery table or a query must be specified')
    elif table is not None:
      self.table_reference = bigquery_tools.parse_table_reference(
          table, dataset, project)
      self.query = None
      self.use_legacy_sql = True
    else:
      if isinstance(query, str):
        query = StaticValueProvider(str, query)
      self.query = query
      # TODO(BEAM-1082): Change the internal flag to be standard_sql
      self.use_legacy_sql = not use_standard_sql
      self.table_reference = None

    self.method = method

View on GitHub (pinned to 12126d8942)

Solutions

  1. Remove the table argument and keep query, or vice versa
  2. Make one of them conditionally None based on configuration
  3. Check config merging so only one source key is populated

Example fix

// before
ReadFromBigQuery(table='proj:ds.tbl', query='SELECT 1')
// after
ReadFromBigQuery(query='SELECT 1')
Defensive patterns

Strategy: validation

Validate before calling

if table is not None and query is not None:
    raise ValueError('Specify only one of table or query before constructing BigQuerySource')

Type guard

def source_args_ok(table, query):
    return (table is None) != (query is None)

Try / catch

try:
    source = BigQuerySource(table=table, query=query)
except ValueError as e:
    if 'only one of these' in str(e):
        table = None  # prefer query
        source = BigQuerySource(table=table, query=query)
    else:
        raise

Prevention

When it happens

Trigger: Constructing BigQuerySource(table=..., query=...) or calling beam.io.ReadFromBigQuery with both table and query set to non-None values.

Common situations: Config-driven pipelines where both keys default to values, or refactoring where an old table argument was left in place after adding a query.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/423e78be70aa18d7. Report an issue: GitHub.