Olivia Ng
Olivia Ng

Reputation: 1

BigQuery Python Client Library - Named Parameters Error

I'm trying to write a simple query using the Python client library named "parameter", but kept encountering errors.

I keep getting "Undeclared query parameters" when I try to run the code. Did I miss out anything?

My Code:

import datetime
import os
from google.cloud import bigquery

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=<path>
client = bigquery.Client(project='project_id')

query = """
    SELECT * from `<project_id>.<dataset_id>.*` 
    WHERE CAST(REGEXP_EXTRACT(_TABLE_SUFFIX, r"^(\d{8})$") AS INT64) = @date
    limit 10;
    """

query_params = [
    bigquery.ScalarQueryParameter(
        'date', 
        'INT64', 
        int((datetime.date.today().strftime('%Y%m%d'))
        )
    ]

job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params

query_job = client.query(
    query,
    location = 'US') 

for row in query_job:
    print(row)

assert query_job.state == 'DONE'

Upvotes: 0

Views: 243

Answers (1)

VictorGGl
VictorGGl

Reputation: 1916

It looks like you are missing to enter your job_config into the arguments of your client.query() method. You should have:

query_job = client.query(
    query,
    location = 'US',
    job_config=job_config) 

Official docs here.

Upvotes: 1

Related Questions