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

Question regarding lag #806

Open
KVeschgini opened this issue Jul 19, 2024 · 4 comments
Open

Question regarding lag #806

KVeschgini opened this issue Jul 19, 2024 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@KVeschgini
Copy link

Can someone please help me understand the result of the following query:

SELECT
  *
FROM
  (
    SELECT
      signal_name, value_float64, lag(value_float64), value_float64 - lag(value_float64)
    FROM
      telemetry
    PARTITION BY
      signal_name
  )
WHERE
  signal_name = 'Speed'
LIMIT 5

Query id: 881b81a5-7fa0-4240-a424-31bb765e64ec

┌─signal_name─┬─value_float64─┬─lag(value_float64)─┬─minus(value_float64, lag(value_float64))─┐
│ Speed       │     48.506393 │                  0 │                            -68499.438607 │
└─────────────┴───────────────┴────────────────────┴──────────────────────────────────────────┘
┌─signal_name─┬─value_float64─┬─lag(value_float64)─┬─minus(value_float64, lag(value_float64))─┐
│ Speed       │      48.48633 │          48.506393 │                              48.08114934 │
└─────────────┴───────────────┴────────────────────┴──────────────────────────────────────────┘
┌─signal_name─┬─value_float64─┬─lag(value_float64)─┬─minus(value_float64, lag(value_float64))─┐
│ Speed       │     48.467033 │           48.48633 │                       -8.410789999999999 │
└─────────────┴───────────────┴────────────────────┴──────────────────────────────────────────┘
┌─signal_name─┬─value_float64─┬─lag(value_float64)─┬─minus(value_float64, lag(value_float64))─┐
│ Speed       │     48.448513 │          48.467033 │                               48.0331646 │
└─────────────┴───────────────┴────────────────────┴──────────────────────────────────────────┘
┌─signal_name─┬─value_float64─┬─lag(value_float64)─┬─minus(value_float64, lag(value_float64))─┐
│ Speed       │     48.430805 │          48.448513 │                             48.413310828 │
└─────────────┴───────────────┴────────────────────┴──────────────────────────────────────────┘

lag(value_float64) is as expected the same as the value_float64 from the last row but the difference value_float64 - lag(value_float64) seems to be arbitrary. What am I doing wrong here?

@KVeschgini KVeschgini added the question Further information is requested label Jul 19, 2024
@jovezhong
Copy link
Contributor

Hi @KVeschgini , I am a bit surprised to see this. I don't have your data set, but on one of our demo server, I can get the correct diff

select cid, speed_kmh,lag(speed_kmh),speed_kmh-lag(speed_kmh) from car_live_data where cid like 'c0000%' partition by cid

the url-addressable link (you only need to use Google/Micosoft ID to SSO): https://demo.timeplus.cloud/default/console/query?sql=select+cid,+speed_kmh,lag(speed_kmh),speed_kmh-lag(speed_kmh)+from+car_live_data+where+cid+like+'c0000%25'+partition+by+cid
image

Timeplus leverages the similar query engine as ClickHouse, which provides some shortcut to compute such a-b, when b is defined in the same query. In many other database, you have to create a CTE/subquery. So you may try this "safe" approach

SELECT
  signal_name, value_float64,  theLag, value_float64 - theLag
FROM
  (
    SELECT
      signal_name, value_float64, lag(value_float64) as theLag
    FROM
      telemetry
    PARTITION BY
      signal_name
  )
WHERE
  signal_name = 'Speed'
LIMIT 5

Also, maybe you can create such question in the Discussions tab, instead of an Issue. No big deal, just try to consolidate the questions from the community. Thanks for trying Timeplus and sharing your feedback.

@jovezhong jovezhong self-assigned this Jul 20, 2024
@chenziliang
Copy link
Collaborator

@KVeschgini May i ask which version of proton you are using ? Thanks. SELECT version()

@yl-lisen
Copy link
Collaborator

@KVeschgini
So far, Proton cannot correctly interpret/handle this expression value_float64 - lag(value_float64) (stateless function + stateful function) in the current PARTITION BY implementation. This is a problem we need to solve.

Before we solve it, you may try "safe" approach mentioned by @jovezhong .

SELECT
  signal_name, value_float64,  theLag, value_float64 - theLag
FROM
  (
    SELECT
      signal_name, value_float64, lag(value_float64) as theLag
    FROM
      telemetry
    PARTITION BY
      signal_name
  )
WHERE
  signal_name = 'Speed'
LIMIT 5

@KVeschgini
Copy link
Author

I posted the question here because I suspected it to be a bug. I am using proton version 1.5.14.
I found out that value_float64 - lag(value_float64) dosen't respect the partition and takes the value from the global last row.

Thanks @yl-lisen for confirming the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants