How to check if a table is not empty

is there a way to find out if a from() returned an empty set?

I am working on a larger script with several from() statements returning several tables
x = from()
|> range(…)
|> filter(fn: (r ) => r._measurement == “x” )
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: “_value”)

y = from()
|> range(…)
|> filter(fn: (r ) => r._measurement == “y )
|> pivot(rowKey:[”_time"], columnKey: ["_field"], valueColumn: “_value”)

z = from()
|> range(…)
|> filter(fn: (r ) => r._measurement == “z” )
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: “_value”)

I want to join all the different tables at the end with join(), but it could happen that some of the tables are empty. In this case I want to ignore the empty tables in the join() operation and just join the others.

Unfortunately a join will drop all my results in case any of x,y,z returned an empty set.
I want to include some conditional logic like

united_tables = if (exists x) then join(x,y) else y

I have no idea how to implement the “exists x” part. I have tried using the count() function but it will fail, if the original x returned no results. Any way to check if a table is not empty?

@Aga-Schwarz I am worry that there isn’t a way to find out yet, see the similar post here Return zero 0 when no data from flux query

But if you found the way to check this I would really very much use this function as well as many times filter does not provide data but following routines expect the data.


@salvq : Thaks for your insights!

I am not completely sure if this is exactly the same usecase (if I understood your post correctly), as I do not want to return 0 but check for existance within the script. There seems to be a command “exists” supported for values but seems not to be working for streams/ tables:

Is it known if the “exists” check for tabels/streams will be included in future releases?

@Aga-Schwarz there is no working solution yet for checking if streams / tables are empty.

I would utilize such a function a lot as well also for my use cases. Many times I am getting just an error in Grafana when no streams are coming from Influxdb and I would like to be in control when there is no tables, present error code for instance.

I was checking around however did not find the answer yet when or even if such a feature will be available :frowning: