Skip to content

BigQuery: Support WHERE IN or WHERE ANY clause in DB-API #9177

Closed

Description

@haibin

BigQuery DB-API throws the following error when I pass parameters to the Cursor.execute() for WHERE IN or WHERE ANY clause. Any idea if it will be supported?

  File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/cursor.py", line 159, in execute
    query_parameters = _helpers.to_query_parameters(parameters)
  File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 117, in to_query_parameters
    return to_query_parameters_list(parameters)
  File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 84, in to_query_parameters_list
    return [scalar_to_query_parameter(value) for value in parameters]
  File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 84, in <listcomp>
    return [scalar_to_query_parameter(value) for value in parameters]
  File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 69, in scalar_to_query_parameter
    name, value
google.cloud.bigquery.dbapi.exceptions.ProgrammingError: encountered parameter None with value ['Apple.com', 'Coles'] of unexpected type

Activity

  1. added
    api: bigqueryIssues related to the BigQuery API.
    type: questionRequest for information or clarification. Not an issue.
    on Sep 5, 2019
  2. plamut commented on Sep 5, 2019

    @plamut
    Contributor

    @haibin Thanks for the report!

    Would you mind providing more details, such as the BigQuery client version, and a reproducible code sample, if possible? That and any other info would be really helpful with investigating the cause, thanks!

  3. haibin commented on Sep 5, 2019

    @haibin
    Author

    google-cloud-bigquery version: 1.19.0

    from google.cloud import bigquery
    
    from google.cloud.bigquery import dbapi
    
    client = bigquery.Client()
    conn = dbapi.Connection(client)
    curr = conn.cursor()
    
    query = """
      SELECT name, state
      FROM `bigquery-public-data.usa_names.usa_1910_2013`
      WHERE state = %s
      LIMIT 2
    """
    curr.execute(query, ('NY', ))
    result = curr.fetchall()
    print(result)
    
    query = """
      SELECT name, state
      FROM `bigquery-public-data.usa_names.usa_1910_2013`
      WHERE state IN %s
      LIMIT 2
    """
    curr.execute(query, (('NY', 'TX'), ))
    result = curr.fetchall()
    print(result)

    Output

    [Row(('Mildred', 'NY'), {'name': 0, 'state': 1}), Row(('Irene', 'NY'), {'name': 0, 'state': 1})]
    Traceback (most recent call last):
      File "hello_bq.py", line 25, in <module>
        curr.execute(query, (('NY', 'TX'), ))
      File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/cursor.py", line 159, in execute
        query_parameters = _helpers.to_query_parameters(parameters)
      File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 117, in to_query_parameters
        return to_query_parameters_list(parameters)
      File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 84, in to_query_parameters_list
        return [scalar_to_query_parameter(value) for value in parameters]
      File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 84, in <listcomp>
        return [scalar_to_query_parameter(value) for value in parameters]
      File "/home/haibin/.local/share/virtualenvs/python-6nCS1ipk/lib/python3.6/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 69, in scalar_to_query_parameter
        name, value
    google.cloud.bigquery.dbapi.exceptions.ProgrammingError: encountered parameter None with value ('NY', 'TX') of unexpected type
    
    
  4. fhoffa commented on Sep 6, 2019

    @fhoffa

    Alternative approach in the meantime:

    query = """
      SELECT name, state
      FROM `bigquery-public-data.usa_names.usa_1910_2013`
      WHERE state IN UNNEST(SPLIT(%s))
      LIMIT 2
    """
    curr.execute(query, ('NY,TX', ))
    
    
  5. plamut commented on Sep 8, 2019

    @plamut
    Contributor

    I can confirm that the issue is reproducible.

  6. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    and removed
    type: questionRequest for information or clarification. Not an issue.
    on Sep 8, 2019
  7. self-assigned this
    on Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.priority: p2Moderately-important priority. Fix may not be included in next release.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