-- description: Test timestamp precision

CREATE TABLE ts_precision(
    sec TIMESTAMP(0),
    msec TIMESTAMP(3),
    micros TIMESTAMP(6),
    nanos TIMESTAMP (9) TIME INDEX
);

INSERT INTO ts_precision VALUES ('2020-01-01 01:23:45.123456789', '2020-01-01 01:23:45.123456789', '2020-01-01 01:23:45.123456789', '2020-01-01 01:23:45.123456789');

-- FIXME: nano seconds do not render correctly
SELECT CAST(sec AS VARCHAR), CAST(msec AS VARCHAR), CAST(micros AS VARCHAR), CAST(nanos AS VARCHAR) FROM ts_precision;

SELECT EXTRACT(MICROSECONDS FROM sec), EXTRACT(MICROSECONDS FROM msec), EXTRACT(MICROSECONDS FROM micros), EXTRACT(MICROSECONDS FROM nanos) FROM ts_precision;

-- we only support precisions 0, 3, 6, and 9
-- any other precision is rounded up (e.g. 1/2 -> 3, 4/5 -> 6, 7/8 -> 9)
SELECT TIMESTAMP '2020-01-01 01:23:45.123456789'::TIMESTAMP(0);

SELECT TIMESTAMP '2020-01-01 01:23:45.123456789'::TIMESTAMP(3);

SELECT TIMESTAMP '2020-01-01 01:23:45.123456789'::TIMESTAMP(6);

SELECT TIMESTAMP '2020-01-01 01:23:45.123456789'::TIMESTAMP(9);

SELECT TIMESTAMP '2020-01-01 01:23:45.12'::TIMESTAMP(3);

SELECT TIMESTAMP '2020-01-01 01:23:45.12345'::TIMESTAMP(6);

SELECT TIMESTAMP '2020-01-01 01:23:45.12345678'::TIMESTAMP(9);

DROP TABLE ts_precision;
