Skip to content

BigQuery: query_parameters fails if empty array is bound as parameter #7309

Closed

Description

@peku33

OS Type & Version: Ubuntu 18.10 x64
Python version: Python 3.7.1
Packges: latest up to this date:

(...)
google-cloud-bigquery==1.9.0
(...)

Steps to reproduce

  1. Create a query, bind empty array as parameter
  2. Execute it
  3. Call query_parameters

Code example

from google.cloud import bigquery

client = bigquery.Client()
job = client.query(
    "SELECT ARRAY_LENGTH(@empty_array)",
    job_config=bigquery.QueryJobConfig(
        query_parameters=[
            bigquery.ArrayQueryParameter('empty_array', 'INT64', [])
        ]
    )
)
result = list(job.result())
query_parameters = job.query_parameters

Stack trace

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    query_parameters = job.query_parameters()
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/job.py", line 2373, in query_parameters
    return self._configuration.query_parameters
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/job.py", line 2115, in query_parameters
    return _from_api_repr_query_parameters(prop)
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/job.py", line 1908, in _from_api_repr_query_parameters
    return [_query_param_from_api_repr(mapping) for mapping in resource]
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/job.py", line 1908, in <listcomp>
    return [_query_param_from_api_repr(mapping) for mapping in resource]
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/query.py", line 623, in _query_param_from_api_repr
    return klass.from_api_repr(resource)
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/query.py", line 252, in from_api_repr
    return cls._from_api_repr_scalar(resource)
  File "python-bigquery-bug/venv/lib/python3.7/site-packages/google/cloud/bigquery/query.py", line 233, in _from_api_repr_scalar
    values = [value["value"] for value in resource["parameterValue"]["arrayValues"]]
KeyError: 'parameterValue'

from_api_repr in ArrayQueryParameter is called with resource = {'name': 'empty_array', 'parameterType': {'arrayType': {'type': 'INT64'}, 'type': 'ARRAY'}}
For empty array this value does not contain parameterValue which leads to KeyError

Activity

  1. added
    type: question
    api: bigquery
    type: bug
    priority: p2
    and removed
    type: question
    on Feb 12, 2019
  2. self-assigned this
    on Feb 12, 2019
  3. tseaver commented on Feb 12, 2019

    @tseaver
    Contributor

    @peku33 Thank you for the report!

  4. added a commit that references this issue on Feb 12, 2019
    b360a63
  5. peku33 commented on Aug 12, 2019

    @peku33
    Author

    @tseaver This is still an issue on 1.18.0 if bound parameter is None.

  6. tseaver commented on Aug 12, 2019

    @tseaver
    Contributor

    @peku33

    This is still an issue on 1.18.0 if bound parameter is None.

    Can you supply an example showing how to trigger that case?

  7. peku33 commented on Aug 12, 2019

    @peku33
    Author
    from google.cloud import bigquery
    
    client = bigquery.Client.from_service_account_json(
        # <snip>
    )
    job = client.query(
        "SELECT LOWER(@none_value)",
        job_config=bigquery.QueryJobConfig(
            query_parameters=[
                bigquery.ScalarQueryParameter('none_value', 'STRING', None)
            ]
        )
    )
    result = list(job.result())
    query_parameters = job.query_parameters
    

    raises:

        value = resource["parameterValue"]["value"]
    KeyError: 'parameterValue'
    
  8. peku33 commented on Aug 13, 2019

    @peku33
    Author

    @tseaver Is it enough to report the issue or should I open new request?

  9. tseaver commented on Aug 13, 2019

    @tseaver
    Contributor

    @peku33 The exceptoin you report is really a slightly different issue: please open it, and reference this one in the description.

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

Metadata

Metadata

Assignees

Labels

api: bigquerypriority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions