Skip to content

BigQuery: structs not populated correctly in query parameters #2887

@tswast

Description

@tswast

See documentation for structs in query parameters:
https://cloud.google.com/bigquery/querying-data#using_structs_in_parameterized_queries

  • Stacktrace
$ python sync_query_params.py struct 7 hello
Traceback (most recent call last):
  File "sync_query_params.py", line 204, in <module>
    sync_query_struct_params(args.x, args.y)
  File "sync_query_params.py", line 139, in sync_query_struct_params
    query_results.run()
  File "/Users/swast/src/google-cloud-python/bigquery/google/cloud/bigquery/query.py", line 360, in run
    method='POST', path=path, data=self._build_resource())
  File "/Users/swast/venvs/bq-dml/lib/python3.5/site-packages/google/cloud/_http.py", line 335, in api_request
    error_info=method + ' ' + url)
google.cloud.exceptions.BadRequest: 400 Invalid query parameter type (POST https://www.googleapis.com/bigquery/v2/projects/cloud-samples-tests/queries)
  • Code sample

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/tswast-bq/bigquery/cloud-client/sync_query_params.py#L129

The google-cloud-bigquery library produces a resource that looks like:

{
  'name': 'struct_value',
  'parameterValue': {
    'structValues': {'y': 'hello', 'x': '7'}
  },
  'parameterType': {
    'structTypes': [{'type': 'INT64', 'name': 'x'}, {'type': 'STRING', 'name': 'y'}]
  }
}

The API expects a resource that looks like:

{
      "name": "struct_value",
      "parameterType": {
        "type": "STRUCT",
        "structTypes": [
          {
            "name": "x",
            "type": {
              "type": "INT64"
            }
          },
          {
            "name": "y",
            "type": {
              "type": "STRING"
            }
          }
        ]
      },
      "parameterValue": {
        "structValues": {
          "x": {
            "value": "1"
          },
          "y": {
            "value": "foo"
          }
        }
      }
    }

Notice the missing { "value": "my-value" } in the structValues in the request object produced by this library.

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions