You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
suggest add synax select from generate_series() #10069
Is your feature request related to a problem or challenge?
it is complex and slowly to use unnest().
Describe the solution you'd like
it can run select from generate_series() as duckdb does.
Describe alternatives you've considered
No response
Additional context
D:\duckdb>datafusion-cli
DataFusion CLI v37.0.0>selectsum(a) from unnest(generate_series(1,1000000)) as t(a);
+--------------+
| SUM(t.a) |
+--------------+
| 500000500000 |
+--------------+1 row(s) fetched.
Elapsed 3.648 seconds.
>selectsum(a) from generate_series(1,1000000) as t(a);
Error during planning: table function 'generate_series' not found
>
D:\duckdb>duckdb011
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
D .timer on
D selectsum(a) from unnest(generate_series(1,1000000)) as t(a);
┌──────────────┐
│ sum(a) │
│ int128 │
├──────────────┤
│ 500000500000 │
└──────────────┘
Run Time (s): real0.854 user 0.859375 sys 0.078125
D selectsum(a) from generate_series(1,1000000) as t(a);
┌──────────────┐
│ sum(a) │
│ int128 │
├──────────────┤
│ 500000500000 │
└──────────────┘
Run Time (s): real0.033 user 0.015625 sys 0.000000
I think switching generate_series to a UDTF will break the API. I think in most cases it's an acceptable API change. However there is (at least for me) 1 particular use case that of the array generated from generate_series that will probably need closer attention.
Example
I need to spread the data equally over equal intervals between 2 data points. I use `generate_series` to generate an array per row. The way that I do this now is as follows:
with
index_values AS (
selecttimeAS to_time,
lag(time, 1) over (order bytime) AS from_time,
index_value AS to_index,
lag(index_value, 1) over (order bytime) AS from_index
from manual
),
series AS (
select
unnest(generate_series(from_time, to_time, interval '1 month')) AStime,
(case
when to_index < from_index THEN (999999- from_index) + to_index
else to_index - from_index end)
/ coalesce(nullif(array_length(generate_series(from_time, to_time, interval '1 month')), 0), 1) AS delta_value
from index_values
) SELECT*FROM series;
Specifically array_length(generate_series(from_time, to_time, interval '1 month')) will need to be done differently if generate_series becomes a UDTF. It will probably need to become COUNT(time) OVER (partition by to_time, from_time) :
with
index_values AS (
selecttimeAS to_time,
lag(time, 1) over (order bytime) AS from_time,
index_value AS to_index,
lag(index_value, 1) over (order bytime) AS from_index
from manual
),
series AS (
selecttime,
(case
when to_index < from_index THEN (999999- from_index) + to_index
else to_index - from_index end)
/ coalesce(nullif(count(time) over (partition by to_time, from_time), 0), 1) AS delta_value
from index_values, generate_series(from_time, to_time, interval '900 seconds') AStime
) SELECT*FROM series;
(If you have other/better suggestions for how data can be spreaded/interpolated in raw DF, all ears :) )
TL;DR: If you're using generate_series to generate a series on a row-based basis, you may need to consider OVER PARTITION BY (your_row_composite_key) if this API change takes place
Just mentioning this here in case anyone is using generate_series in a similar way as I do. and because I want to know if this is ever added :p then I know I need to update my queries
Maybe we can use the 2nd interpretation (the same as DuckDB), currently, DataFusion's generate_series in expression is already different from Postgres's semantics
I think we could add this as a user defined table function in datafusion-cli quite easily
@alamb Is it possible to implement this UDTF inside datafusion crate? 🤔 I find this table function can make end-to-end tests for spilling related queries much easier
I think we could add this as a user defined table function in datafusion-cli quite easily
@alamb Is it possible to implement this UDTF inside datafusion crate? 🤔 I find this table function can make end-to-end tests for spilling related queries much easier
Makes sense to me -- we may need to create a datafusion-functions-table sub crate too, however 🤔 as a place to put it
Activity
alamb commentedon Apr 13, 2024
I think we could add this as a user defined table function in datafusion-cli quite easily
For example we could follow the model of
parquet_metadata(which also follows duckdb):It is documented here https://arrow.apache.org/datafusion/user-guide/cli.html
The code for it is here; https://github.com/apache/arrow-datafusion/blob/637293580db0634a4efbd3f52e4700992ee3080d/datafusion-cli/src/functions.rs#L215-L442
Lordworms commentedon Apr 13, 2024
related #9354
Abdullahsab3 commentedon Sep 25, 2024
I think switching
generate_seriesto a UDTF will break the API. I think in most cases it's an acceptable API change. However there is (at least for me) 1 particular use case that of the array generated fromgenerate_seriesthat will probably need closer attention.Example
I need to spread the data equally over equal intervals between 2 data points. I use `generate_series` to generate an array per row. The way that I do this now is as follows:with index_values AS ( select time AS to_time, lag(time, 1) over (order by time) AS from_time, index_value AS to_index, lag(index_value, 1) over (order by time) AS from_index from manual ), series AS ( select unnest(generate_series(from_time, to_time, interval '1 month')) AS time, (case when to_index < from_index THEN (999999 - from_index) + to_index else to_index - from_index end) / coalesce(nullif(array_length(generate_series(from_time, to_time, interval '1 month')), 0), 1) AS delta_value from index_values ) SELECT * FROM series;Specifically
array_length(generate_series(from_time, to_time, interval '1 month'))will need to be done differently ifgenerate_seriesbecomes a UDTF. It will probably need to becomeCOUNT(time) OVER (partition by to_time, from_time):with index_values AS ( select time AS to_time, lag(time, 1) over (order by time) AS from_time, index_value AS to_index, lag(index_value, 1) over (order by time) AS from_index from manual ), series AS ( select time, (case when to_index < from_index THEN (999999 - from_index) + to_index else to_index - from_index end) / coalesce(nullif(count(time) over (partition by to_time, from_time), 0), 1) AS delta_value from index_values, generate_series(from_time, to_time, interval '900 seconds') AS time ) SELECT * FROM series;(If you have other/better suggestions for how data can be spreaded/interpolated in raw DF, all ears :) )
TL;DR: If you're using
generate_seriesto generate a series on a row-based basis, you may need to considerOVER PARTITION BY (your_row_composite_key)if this API change takes placeJust mentioning this here in case anyone is using
generate_seriesin a similar way as I do. and because I want to know if this is ever added :p then I know I need to update my queriesalamb commentedon Sep 25, 2024
FWIW I think we should consider keeping the existing
generate_seriesfunction and add a new user defined table function with the same nameAbdullahsab3 commentedon Sep 25, 2024
That might work, but I wonder how it will be inferred and whether it will cause any conflicts. Some examples that can be ambiguous on top of my head:
This can be either (postgresql behavior):
or
Also, I wonder how the results of something like this would look like 🤔:
2010YOUY01 commentedon Oct 19, 2024
Maybe we can use the 2nd interpretation (the same as DuckDB), currently, DataFusion's
generate_seriesin expression is already different from Postgres's semantics2010YOUY01 commentedon Oct 19, 2024
@alamb Is it possible to implement this UDTF inside
datafusioncrate? 🤔 I find this table function can make end-to-end tests for spilling related queries much easieralamb commentedon Oct 21, 2024
Makes sense to me -- we may need to create a
datafusion-functions-tablesub crate too, however 🤔 as a place to put italamb commentedon Oct 21, 2024
BTW you can get a similar effect with
unnest: