Using DATEADD() in SELECT column list causes later columns to be ignored

I have a query in which I want to take all columns from a table, but modify the time field. As an example, here I get all fields in my table but subtract one month from the timestamp.

select "system", sensor, sensordesc, avg(energy) as energy, avg(voltage) as voltage,
    dateadd('M', -1, "timestamp") as "timestamp", vbus, sensorname 
    from "emporia"
    where timestamp in '2024-11-24' sample by 10m

It works perfectly except that the final column of my SELECT (“sensordesc”) does not get returned. I get all columns up to and including “vbus” correctly. Odd and inconsistent things happen for any columns I place in the SELECT list after the DATEADD() function.

If I specify the column name twice, then it all works and I get all the columns as requested, with the column I asked for twice only appearing once. I.e., the following command does what I was expecting from the original command, returning every column once.

select "system", sensor, sensordesc, avg(energy) as energy, avg(voltage) as voltage,
    dateadd('M', -1, "timestamp") as "timestamp", vbus, sensorname, sensorname 
    from "emporia"
    where timestamp in '2024-11-24' sample by 10m

Is this expected behaviour? I could not find anything in the docs about not using dateadd() in SELECT lists.

QuestDB 8.1.4 running in a docker using image “questdb/questdb:latest”

And a screen shot of getting the correct result when I ask for “sensorname” twice.

Hi @rjs3273 ,

This is a bug related to an optimisation that converts SAMPLE BY into a GROUP BY clause that runs in parallel. I thought we had an open issue for this, but I’m unable to locate it.

It is certainly not expected behaviour. If you move your dateadd to be the final column of your query, it should work fine.

We will aim to fix this in the next release!

Follow up - did a quick test of this PR and this should resolve the issue.

Tested with:

select  dateadd('d', 1, timestamp) "timestamp" , symbol from trades
sample by 10m

Thanks. Good to know it is a known issue and there looks to be a solution. For the time being I have a solid workaround anyway.

For the record, putting the dateadd() last does not work as a workaround for me. When I do that the date field gets lost altogether. I can workaround just by naming the field twice.

That’s strange! We’ll try to get this fixed ASAP, and at least you have it working for now.