BigQuery query_parameters空

时间:2018-07-13 08:06:15

标签: google-bigquery

如何将查询参数设置为null?

query = """
SELECT word, word_count
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @corpus
AND word_count >= @min_word_count
ORDER BY word_count DESC;
"""

query_params = [
bigquery.ScalarQueryParameter('corpus', 'STRING', 'romeoandjuliet'),
bigquery.ScalarQueryParameter('min_word_count', 'INT64', 250)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params

如果我希望主体为空,我该如何编码?

1 个答案:

答案 0 :(得分:0)

如果您为查询参数指定None的值,它将起作用:

query = """
SELECT word, word_count
FROM `bigquery-public-data.samples.shakespeare`
WHERE corpus = @corpus
AND word_count >= @min_word_count
ORDER BY word_count DESC;
"""

query_params = [
bigquery.ScalarQueryParameter('corpus', 'STRING', None),
bigquery.ScalarQueryParameter('min_word_count', 'INT64', 250)
]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params

不过,正如我在上面的注释中所建议的那样,在这种特殊情况下,这可能不是您想要的,因为比较corpus = @corpus(查询参数为NULL的情况)将始终返回{ {1}},因此您不会从该查询中获得任何结果。