我们遇到了完全相同的问题并找到了解决方法:
CREATE TABLE final_table ("ts_as_timestamptz" TIMESTAMPTZ);
CREATE TEMP TABLE helper_table ("ts_as_varchar" VARCHAR(64));
COPY "helper_table" FROM 's3://path/to/file.csv.gz'
CREDENTIALS 'aws_access_key_id=THE_KEY;aws_secret_access_key=THE_SECRET'
CSV
GZIP;
INSERT INTO final_table (ts_as_timestamptz)
SELECT ts_as_varchar::TIMESTAMPTZ FROM helper_table;
或者,或者:
CREATE TABLE final_table ("ts_as_timestamp" TIMESTAMP);
CREATE TEMP TABLE helper_table ("ts_as_varchar" VARCHAR(64));
COPY "helper_table" FROM 's3://path/to/file.csv.gz'
CREDENTIALS 'aws_access_key_id=THE_KEY;aws_secret_access_key=THE_SECRET'
CSV
GZIP;
INSERT INTO final_table (ts_as_timestamp)
SELECT ts_as_varchar::TIMESTAMPTZ FROM helper_table;
你可以用这个快速测试:
DROP TABLE IF EXISTS helper_table;
CREATE TEMP TABLE helper_table ("ts_as_varchar" VARCHAR(64));
INSERT INTO helper_table (ts_as_varchar) VALUES
('2015-01-13T11:13:08.869941+00:00'),
('2015-01-13T12:13:08.869941+01:00'),
('2015-01-13T13:13:08.869+02:00'),
('2015-01-13T14:13:08+03:00'),
('2015-01-13T11:13:08'),
('2015-01-13 11:13:08.869941+00:00'),
('2015-01-13 12:13:08.869941+01:00'),
('2015-01-13 13:13:08.869+02:00'),
('2015-01-13 14:13:08+03:00'),
('2015-01-13 11:13:08')
;
DROP TABLE IF EXISTS final_table;
CREATE TEMP TABLE final_table (
"ts_as_varchar" VARCHAR(64),
"ts_as_timestamptz" TIMESTAMPTZ,
"ts_as_timestamp" TIMESTAMP
);
INSERT INTO final_table (ts_as_varchar, ts_as_timestamptz, ts_as_timestamp)
SELECT ts_as_varchar, ts_as_varchar::TIMESTAMPTZ, ts_as_varchar::TIMESTAMPTZ
FROM helper_table;
-- The following depends on the time zone of your SQL client, so the results may vary. It is also vulnerable to the SQL client removing the sub-second parts.
-- SELECT * FROM final_table;
-- The following may (?) work better even if your SQL client is not in UTC
SELECT ts_as_varchar, ts_as_timestamptz::VARCHAR, ts_as_timestamp::VARCHAR
FROM final_table;
这给出了这些结果:
ts_as_varchar ts_as_timestamptz ts_as_timestamp
2015-01-13T11:13:08 2015-01-13 11:13:08+00 2015-01-13 11:13:08
2015-01-13T11:13:08.869941+00:00 2015-01-13 11:13:08.869941+00 2015-01-13 11:13:08.869941
2015-01-13T12:13:08.869941+01:00 2015-01-13 11:13:08.869941+00 2015-01-13 11:13:08.869941
2015-01-13T13:13:08.869+02:00 2015-01-13 11:13:08.869+00 2015-01-13 11:13:08.869
2015-01-13T14:13:08+03:00 2015-01-13 11:13:08+00 2015-01-13 11:13:08
2015-01-13 11:13:08 2015-01-13 11:13:08+00 2015-01-13 11:13:08
2015-01-13 11:13:08.869941+00:00 2015-01-13 11:13:08.869941+00 2015-01-13 11:13:08.869941
2015-01-13 12:13:08.869941+01:00 2015-01-13 11:13:08.869941+00 2015-01-13 11:13:08.869941
2015-01-13 13:13:08.869+02:00 2015-01-13 11:13:08.869+00 2015-01-13 11:13:08.869
2015-01-13 14:13:08+03:00 2015-01-13 11:13:08+00 2015-01-13 11:13:08
使用 Redshift 1.0.2610 测试
请注意,您的 SQL 客户端或驱动程序可能会进行一些可能具有误导性的时区转换,因此最好使用 UTC 作为您的计算机/驱动程序/SQL 客户端的时区来进行测试。
此外,一些 SQL 客户端会删除时间戳的亚秒级部分。