SQL Expressions fail when source table has no rows

I don’t know if this counts as a bug or not, but I run into this every time I try to use the new SQL Expressions I hit the same problem. They work well in the normal case but they error when the source table has no rows.

For example, if I have a loki query like:
{service_name="MyService", level="Error"}

Then a SQL Expressions query like:

SELECT MessageTemplate, count(*) 
FROM A
group by MessageTemplate

This works fine when the loki query returns results because all of our log entries have a label called MessageTemplate. But when there are no results (i.e. no errors) then my dashboard gets an error because there is no column named ‘MessageTemplate’

You also run into the same problem if you are trying to do a select on a column/label which is optional.

This case isn’t so bad because you wouldn’t have seen any results anyway, but you can imagine more complicated examples like:

SELECT Message
FROM A
UNION ALL
SELECT Message
FROM B

This will fail if A has no rows, even if B does.

I think we need a way to tell it to not be so picky, because at the moment SQL Expressions seems basically unusable to me, even know at first glance it seems like an awesome feature.