Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First_value should support other datatypes, specifically timestamp #4395

Open
javier opened this issue Apr 12, 2024 · 0 comments
Open

First_value should support other datatypes, specifically timestamp #4395

javier opened this issue Apr 12, 2024 · 0 comments
Labels
Friction New feature Feature requests

Comments

@javier
Copy link
Contributor

javier commented Apr 12, 2024

Is your feature request related to a problem?

first_value OVER() is a great way of getting LAG functionality, but as of today we support only the Double data type. While this is good, a very common use case for LAG is elapsed time, and QuestDB is after all a time-series database. I would expect to be able to find elapsed time using a window function.

Describe the solution you'd like.

Right now I have to do something like this to find elapsed_time. I cast timestamp to long and back. It is ugly and probably not too efficient

WITH time_and_prev AS (
  SELECT timestamp, symbol, first_value(timestamp::long) OVER (PARTITION BY symbol ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING ) AS prevTimestamp 
FROM trades where timestamp IN '2024-04'
)
SELECT timestamp, symbol, prevTimestamp::timestamp, datediff('s', timestamp, prevTimestamp::timestamp) as gap_s, (timestamp - prevTimestamp::timestamp) / 1000 as gap_ms FROM time_and_prev 
limit -50;

I would like to use first_value(timestamp) and that's it, as in

WITH time_and_prev AS (
  SELECT timestamp, symbol, first_value(timestamp) OVER (PARTITION BY symbol ORDER BY timestamp ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING ) AS prevTimestamp 
FROM trades where timestamp IN '2024-04'
)
SELECT timestamp, symbol, prevTimestamp, datediff('s', timestamp, prevTimestamp) as gap_s, (timestamp - prevTimestamp) / 1000 as gap_ms FROM time_and_prev 
limit -50;

For what is worth, I think it would also make a lot of sense for Symbol to be supported. Imagine you have a 'Status' column and want to check when it changed value. By having first_value(symbol_column) you could do that easily.

Describe alternatives you've considered.

As explained above, I can always cast from timestamp to long, it is just ugly

Full Name:

Javier Ramirez

Affiliation:

QuestDB

Additional context

No response

@javier javier added New feature Feature requests Friction labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Friction New feature Feature requests
Projects
None yet
Development

No branches or pull requests

1 participant