Handling empty result sets

Hi,

I’m using the Influx Python3 library to query an InfluxDB (2.6.1). The query I’m working on at the moment will return an empty set of results at times and this is expected.

How can I check a set of results to see if they are empty or not before processing them?

The query:

    open_query = ' from(bucket:"' + influx_client_bucket + '")\
        |> range(start: -1d)\
        |> filter(fn: (r) => r["_measurement"] == "a")\
        |> last()\
        |> filter(fn: (r) => r["_value"] == 1)\
        |> drop(columns: ["_start", "_stop", "_time", "_field", "_measurement", "_value"])'


    open_query_result = query_api.query(org=influx_client_org, query=open_query)

As per this: Empty condition for Result Set in Python Client
The problem is even though the query returns 0 results the length of the result set is not 0:

print(len(open_query_result))
1

for table in open_query_result:
...     for record in table.records:
...             print((record.values.get("SOMETHINGHERE")))
...
None

What is the current method for checking that a query has some results?

Hoping someone might be able to give me a little direction here…

Hello @olliecampbell,
You could append your query

import "array"
alternative = array.from(rows: [{v: 0}]) 

 open_query = ' from(bucket:"' + influx_client_bucket + '")\
        |> range(start: -1d)\
        |> filter(fn: (r) => r["_measurement"] == "a")\
        |> last()\
        |> filter(fn: (r) => r["_value"] == 1)\
        |> drop(columns: ["_start", "_stop", "_time", "_field", "_measurement", "_value"])

check = (open_query |> count()  |> findRecord( fn: (key) => true, idx: 0) )._value
 
if check > 0 then 
open_query |> yield() else 
alternative |> yield() 

Here’s a dummy example;

import "array"
alternative = array.from(rows: [{_value: 0}]) 

 open_query = array.from(rows: [{_value: 5}]) 

check = (open_query |> count()  |> findRecord( fn: (key) => true, idx: 0) )._value
 
if check > 0 then 
open_query |> yield() else 
alternative |> yield() 

Hope that helps

Perfect, thank you. My SQL statements aren’t up to scratch, but that should help nicely.