mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-08-26 07:58:47 +00:00
c894a2468c
* fix(query): follow timestamp insert assignment lineage Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(query): fold constant insert timestamp literals at the assignment Following lineage by retyping the source column changed every output column that reads it: a string column sharing the literal was silently rewritten to a formatted timestamp, and a nanosecond column was truncated to the precision of whichever column was converted first. Resolve the constant read-only and fold it into the assignment expression instead, which leaves the source query untouched and also covers literals behind WHERE, ORDER BY and DISTINCT. VALUES rows and UNION branches carry per-row values, so they keep the in-place rewrite, now guarded against columns with more than one consumer. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(query): strengthen insert lineage regression Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * test(query): trim redundant insert coverage Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * fix(query): rebuild insert unions loosely and cover UNION distinct Per review: rebuilding a rewritten union with the strict constructor rejected legal pre-coercion plans whose untouched columns still differ across branches. Use try_new_with_loose_types, matching the SQL planner. Distinct::All joins the rewrite passthrough so UNION (distinct) literals get session-timezone parsing like UNION ALL; deduplication then keys on parsed instants instead of raw strings. The top-level rewrite path gains the same single-consumer guard as rewrite_projection for hand-built DML plans that share a source column between targets. Signed-off-by: Dennis Zhuang <killme2008@gmail.com> * style(query): tighten insert assignment comments Signed-off-by: Dennis Zhuang <killme2008@gmail.com> --------- Signed-off-by: Dennis Zhuang <killme2008@gmail.com> (cherry picked from commit b2fc4b9cba4c0e137cc6f704d88dfe7f187b57aa) (cherry picked from commit 1775e31a7916cf09a7da8238cebea5e3d74817f7)
153 lines
3.8 KiB
Plaintext
153 lines
3.8 KiB
Plaintext
--- insert timestamp with default values aware of session timezone test ---
|
|
CREATE TABLE test1 (i INTEGER, j TIMESTAMP default '2024-01-30 00:01:01' TIME INDEX, PRIMARY KEY(i));
|
|
|
|
Affected Rows: 0
|
|
|
|
INSERT INTO test1 VALUES (1, DEFAULT), (2, DEFAULT), (3, '2024-01-31 00:01:01'), (4, '2025-02-01 00:01:01');
|
|
|
|
Affected Rows: 4
|
|
|
|
SELECT * FROM test1 ORDER BY j;
|
|
|
|
+---+---------------------+
|
|
| i | j |
|
|
+---+---------------------+
|
|
| 1 | 2024-01-30T00:01:01 |
|
|
| 2 | 2024-01-30T00:01:01 |
|
|
| 3 | 2024-01-31T00:01:01 |
|
|
| 4 | 2025-02-01T00:01:01 |
|
|
+---+---------------------+
|
|
|
|
SET time_zone = 'Asia/Shanghai';
|
|
|
|
Affected Rows: 0
|
|
|
|
CREATE TABLE test2 (i INTEGER, j TIMESTAMP default '2024-01-30 00:01:01' TIME INDEX, PRIMARY KEY(i));
|
|
|
|
Affected Rows: 0
|
|
|
|
INSERT INTO test2 VALUES (1, DEFAULT), (2, DEFAULT), (3, '2024-01-31 00:01:01'), (4, '2025-02-01 00:01:01');
|
|
|
|
Affected Rows: 4
|
|
|
|
SELECT * FROM test2 ORDER BY j;
|
|
|
|
+---+---------------------+
|
|
| i | j |
|
|
+---+---------------------+
|
|
| 1 | 2024-01-29T16:01:01 |
|
|
| 2 | 2024-01-29T16:01:01 |
|
|
| 3 | 2024-01-30T16:01:01 |
|
|
| 4 | 2025-01-31T16:01:01 |
|
|
+---+---------------------+
|
|
|
|
SELECT * FROM test1 ORDER BY j;
|
|
|
|
+---+---------------------+
|
|
| i | j |
|
|
+---+---------------------+
|
|
| 1 | 2024-01-30T00:01:01 |
|
|
| 2 | 2024-01-30T00:01:01 |
|
|
| 3 | 2024-01-31T00:01:01 |
|
|
| 4 | 2025-02-01T00:01:01 |
|
|
+---+---------------------+
|
|
|
|
CREATE TABLE test3 (ts TIMESTAMP TIME INDEX, st TIMESTAMP, ts_ns TIMESTAMP(9));
|
|
|
|
Affected Rows: 0
|
|
|
|
INSERT INTO test3 (ts) VALUES ('2026-08-01 12:00:00.001');
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st) VALUES ('2026-08-02 12:00:00.001', now());
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st) SELECT '2026-08-03 12:00:00.001', now();
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st) SELECT '2026-08-04 12:00:00.001', now() LIMIT 1;
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st)
|
|
SELECT '2026-08-06 12:00:00.001', now()
|
|
UNION ALL
|
|
SELECT '2026-08-07 12:00:00.001', now();
|
|
|
|
Affected Rows: 2
|
|
|
|
INSERT INTO test3 (ts, st) VALUES (
|
|
CAST('2026-08-08 12:00:00.001' AS TIMESTAMP),
|
|
now()
|
|
);
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st)
|
|
SELECT '2026-08-10 12:00:00.001', now()
|
|
UNION ALL
|
|
SELECT CAST('2026-08-11 12:00:00.001' AS TIMESTAMP), now();
|
|
|
|
Affected Rows: 2
|
|
|
|
INSERT INTO test3 (ts, st)
|
|
SELECT '2026-08-16 12:00:00.001', now()
|
|
UNION
|
|
SELECT '2026-08-17 12:00:00.001', now();
|
|
|
|
Affected Rows: 2
|
|
|
|
INSERT INTO test3 (ts, ts_ns) SELECT a, b FROM (
|
|
SELECT c AS a, c AS b FROM (SELECT '2026-08-12 12:00:00.123456789' AS c) AS t1
|
|
) AS t2;
|
|
|
|
Affected Rows: 1
|
|
|
|
INSERT INTO test3 (ts, st, ts_ns) VALUES (
|
|
'2026-08-09 12:00:00.001',
|
|
now(),
|
|
'2026-08-09 12:00:00.123456789'
|
|
);
|
|
|
|
Affected Rows: 1
|
|
|
|
SELECT ts, ts_ns FROM test3 ORDER BY ts;
|
|
|
|
+-------------------------+-------------------------------+
|
|
| ts | ts_ns |
|
|
+-------------------------+-------------------------------+
|
|
| 2026-08-01T04:00:00.001 | |
|
|
| 2026-08-02T04:00:00.001 | |
|
|
| 2026-08-03T04:00:00.001 | |
|
|
| 2026-08-04T04:00:00.001 | |
|
|
| 2026-08-06T04:00:00.001 | |
|
|
| 2026-08-07T04:00:00.001 | |
|
|
| 2026-08-08T12:00:00.001 | |
|
|
| 2026-08-09T04:00:00.001 | 2026-08-09T04:00:00.123456789 |
|
|
| 2026-08-10T12:00:00.001 | |
|
|
| 2026-08-11T12:00:00.001 | |
|
|
| 2026-08-12T04:00:00.123 | 2026-08-12T04:00:00.123456789 |
|
|
| 2026-08-16T04:00:00.001 | |
|
|
| 2026-08-17T04:00:00.001 | |
|
|
+-------------------------+-------------------------------+
|
|
|
|
SET time_zone = 'UTC';
|
|
|
|
Affected Rows: 0
|
|
|
|
DROP TABLE test1;
|
|
|
|
Affected Rows: 0
|
|
|
|
DROP TABLE test2;
|
|
|
|
Affected Rows: 0
|
|
|
|
DROP TABLE test3;
|
|
|
|
Affected Rows: 0
|
|
|