Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redshift - how to insert into table generated time series

I am trying to generate time series in Redshift and insert into table, but no luck. What I have tried so far:

insert into date(dateid,date)
SELECT
    to_char(datum, 'YYYYMMDD')::int AS dateid,
    datum::date AS date
FROM (
    select '1970-01-01'::date + generate_series(0, 20000) as datum
     ) tbl;

Getting the following error

SQL Error [500310] [0A000]: [Amazon](500310) Invalid operation: Specified types or functions (one per INFO message) not supported on Redshift tables.;

Any ideas or workaround ?

like image 836
bstent Avatar asked Nov 23 '25 12:11

bstent


2 Answers

The issue is that generate_series() can be run on the Leader node, but not on a compute node.

Therefore, it is possible to run a statement like this:

SELECT '1970-01-01'::date + generate_series(0, 20000)

However, it is not possible to use that statement in a FROM because that would involve the compute nodes.

Solution: Create a table of information externally and load the results into a date table, or use generate_series() directly to generate the desired values, save the results and import them into a date table.

like image 102
John Rotenstein Avatar answered Nov 25 '25 04:11

John Rotenstein


The generate_series function does not work on Redshift

it does however generate_confusion! This is because it looks like its working fine until you try to write to a table.

Technically, the reason for this is that it works ONLY on the leader node. and the leader node itself does not have access to the tables directly.

If you try

SELECT CURRENT_DATE::TIMESTAMP - (i * interval '1 day') as date_datetime
FROM generate_series(1,31) i
ORDER BY 1;

That produces a nice table output, because it has run on the leader node.

HOWEVER if you run

create table test as
SELECT CURRENT_DATE::TIMESTAMP - (i * interval '1 day') as date_datetime
FROM generate_series(1,31) i
ORDER BY 1;

This fails with

[2019-03-11 07:38:00] [0A000] ERROR: Specified types or functions (one per INFO message) not supported on Redshift tables. [2019-03-11 07:38:00] [00000] Function "generate_series(integer,integer)" not supported.

This is because we are now trying to create a table, so the sql has to be run on the worker nodes (not the leader node) and so it fails.

like image 34
Jon Scott Avatar answered Nov 25 '25 02:11

Jon Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!