Search code examples
pythongoogle-cloud-platformgoogle-bigquerypartitioning

BigQuery client python get column based partitioning column name


I am looking to include a where clause to an existing query to exclude data in the BigQuery streaming buffer.

To do this I would like to get the Partition column name so I can add

WHERE partition_column IS NOT NULL;

to my existing query.

I have been looking at the CLI and the get_table method however that just returns the value of the column not the column name. I get the same when searching on .INFORMATION_SCHEMA.PARTITIONS this returns a field for partition_id but I would prefer the column name itself, is there away to get this ?

Additionally the table is setup with column based partitioning.


Solution

  • Based on python BigQuery client documentation, use the attribute time_partitioning:

    from google.cloud import bigquery
    bq_table = client.get_table('my_partioned_table')
    bq_table.time_partitioning # TimePartitioning(field='column_name',type_='DAY')
    bq_table.time_partitioning.field # column_name
    

    Small tips, if you don't know where to search, print the API repr:

    bq_table.to_api_repr()