redshift - how to insert into table generated time

2019-08-16 01:18发布

问题:

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 ?

回答1:

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.



回答2:

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.