8

We have a table partitioned by day in BigQuery, which is updated by streaming inserts.

The doc says that: "when streaming to a partitioned table, data in the streaming buffer has a NULL value for the _PARTITIONTIME pseudo column"

But if I query for select count(*) from table where _PARTITIONTIME is NULL it always returns 0, even though bq show tells me that there are a lot of rows in the streaming buffer.

Does this mean that the pseudo column is not present at all for rows in streaming buffer? In any case, how can I query for the data ONLY in the streaming buffer without it becoming a full table scan?

Thanks in advance

2
  • what is the practical use case for this? i don't think you can query/read streaming buffer, but if you explain why you think you need to be able to read it - we might figure out how to make it Jan 26, 2017 at 5:12
  • I have a streaming job that keeps updating a table in BigQuery. I have a downstream job that triggers every 15 mins and aggregates the data for the day so far - so it needs to query something equivalen to where _PARTITIONTIME = today OR data_in_streaming_buffer. Is there any way to achieve this? Thanks. Jan 27, 2017 at 17:43

2 Answers 2

10

Data in the streaming buffer has a NULL value for the _PARTITIONTIME column.

SELECT
  fields
FROM
  `dataset.partitioned_table_name`
WHERE
  _PARTITIONTIME IS NULL

https://cloud.google.com/bigquery/docs/partitioned-tables#copying_to_partitioned_tables

3
  • What if I need to compute an aggregation on data in the buffer? What should I do? It seems that I cant even do a filter like "where value > =200". Any suggestions on what to do? Should I use any other product instead?
    – i_am_cris
    Mar 25, 2020 at 16:09
  • Answering my own question - Created a "With"-statement first with the query and then filtered.
    – i_am_cris
    Mar 25, 2020 at 20:07
  • 3
    This only works for ingestion-time partitioned tables. Otherwise the column _PARTITIONTIME doesn’t exist. May 4, 2021 at 1:18
3

When you stream data to BQ you usually have the "warming-up" period and that's the time it takes for the streamed data to become available for operations such as querying, copying and exporting.

The doc states in the end that after a period of up to 90 mins the pseudo-column _PARTITIONTIME receives a non-null value, which means your streamed data is fully ready for any type of operation you want to run on the data (being able to run queries usually takes a few seconds).

That means that you don't query partitioned tables looking for when this field is null but instead, you do like so:

SELECT
  fields
FROM
  `dataset.partitioned_table_name`
WHERE
  _PARTITIONTIME = TIMESTAMP('2017-01-20') 

In this example, you would be querying only data streamed in the dates partition Jan/20 (which avoids a full table scan).

You can also select for a range of dates, you would just have to change the WHERE clause to:

WHERE _PARTITIONTIME BETWEEN TIMESTAMP('2017-01-20') AND TIMESTAMP('2017-01-22') 

Which would query for 2 days in your table.

3
  • 1
    Using WHERE _PARTITIONTIME = TIMESTAMP(some_day) does not query the streaming buffer - if I query for the latest record using a time field in the schema, the result of full table scan is more than an hour newer than the one with a where partitiontime clause Jan 26, 2017 at 2:38
  • Do you have a link that points to the docs where the 90 mins period is mentioned?
    – lv10
    Jun 29, 2018 at 18:32
  • 1
    Hi @lv10, here it goes: doc. I have the impression that my answer is not quite correct now that I read it again. Still, it can take up to 90 mins for copy and export operations to be fully ready for streamed data. Jun 29, 2018 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.