Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "INSERT INTO … SELECT … AS OF timestamp" Query #18672

Open
kennytm opened this issue Jul 17, 2020 · 7 comments
Open

Support "INSERT INTO … SELECT … AS OF timestamp" Query #18672

kennytm opened this issue Jul 17, 2020 · 7 comments
Labels
feature/accepted This feature request is accepted by product managers priority/P1 The issue has P1 priority. type/feature-request Categorizes issue or PR as related to a new feature.

Comments

@kennytm
Copy link
Contributor

kennytm commented Jul 17, 2020

Feature Request

(On behalf of Marvin Jin)

Is your feature request related to a problem? Please describe:

TiDB using TiKV storage supports reading historical data using the @@tidb_snapshot system variable. However, you are not allowed to write anything while @@tidb_snapshot is set, so it is not possible to aggregate these historical data unless using external programs (backup/restore).

ERROR 1105 (HY000): can not execute write statement when 'tidb_snapshot' is set

Describe the feature you'd like:

In Oracle DB, there is an AS OF clause to read from a historical table without affecting the global timestamp.

INSERT INTO recovered_table 

    SELECT * FROM old_table AS OF TIMESTAMP TO_TIMESTAMP('2005-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
--                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        WHERE id BETWEEN 1000 AND 1999;

A similar syntax exists on CockroachDB (but not sure if INSERT/SELECT works):

SELECT * FROM old_table AS OF SYSTEM TIME '2005-04-04 09:30:00'
--                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    WHERE id BETWEEN 1000 AND 1999;

ANSI SQL has the "Temporal" feature, which is supported on MariaDB, MS SQL server, DB2 and more. When a table is set to be "temporal", the following syntax is available:

INSERT INTO recovered_table

    SELECT * FROM old_table FOR system_time AS OF TIMESTAMP '2005-04-04 09:30:00'
--                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        WHERE id BETWEEN 1000 AND 1999;

Note, however, the system_time refers to a PERIOD bound to two columns of the table old_table, which is a totally different concept from @@tidb_snapshot.

Describe alternatives you've considered:

  • Support creating "temporary" tables not governed by @@tidb_snapshot.
  • Do nothing, rely on SELECT INTO OUTFILE in historical query and LOAD DATA INFILE to restore data to current time.

Teachability, Documentation, Adoption, Migration Strategy:

@kennytm kennytm added the type/feature-request Categorizes issue or PR as related to a new feature. label Jul 17, 2020
@ghost
Copy link

ghost commented Jul 17, 2020

Thank you for the feature request! This is a duplicate of #11703

@kennytm
Copy link
Contributor Author

kennytm commented Jul 17, 2020

@nullnotnil No, #11703 is about using AS OF SYSTEM TIME as an alternative way of saying SET @@tidb_snapshot = .... But this issue is about restricting a table of SELECT to one particular snapshot, allowing e.g.

SELECT `before`.id`, `before`.`value`, `after`.`value`
FROM `values` AS OF (NOW() - INTERVAL 1 HOUR) AS `before`
INNER JOIN `values` AS `after`
ON `before`.`id` = `after`.`id`

and in particular the presented example, using INSERT/SELECT to recover lost rows (like in https://docs.oracle.com/cd/B28359_01/appdev.111/b28424/adfns_flashback.htm#g1025584):

INSERT INTO employees
  (SELECT * FROM employees
     AS OF TIMESTAMP
       TO_TIMESTAMP('2004-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
         WHERE last_name = 'Chung');

Note that CrDB has these specific examples:

-- invalid
SELECT * FROM (SELECT * FROM t AS OF SYSTEM TIME '-4h'), u

SELECT * FROM (SELECT * FROM t AS OF SYSTEM TIME '-4h') tp
           JOIN u ON tp.x = u.y
           AS OF SYSTEM TIME '-4h'  -- same timestamp as above - OK.
     WHERE x < 123;

implying AS OF SYSTEM TIME is just a different way of setting the Snapshot TS.


SQL Server's FOR system_time AS OF <ts> is about Temporal Tables and is mostly irrelevant to @@tidb_snapshot.

@zz-jason
Copy link
Member

#11703 has a good title "Support AS OF SYSTEM TIME syntax", but the issue description only discussed the inconvenience of set @@ tidb_snapshot operation.

I listed some statements CockraochDB supported in #11703 (comment), maybe we can consider them as well.

@zz-jason
Copy link
Member

Your description of the syntax in ANSI SQL and other database systems inspired me about what syntax we should use in TiDB. Can we close one issue and discuss them in one place?

@zz-jason zz-jason added the feature/reviewing This feature request is reviewing by product managers label Aug 6, 2020
@zz-jason zz-jason added feature/discussing This feature request is discussing among product managers and removed feature/reviewing This feature request is reviewing by product managers labels Aug 10, 2020
@scsldb scsldb added priority/P1 The issue has P1 priority. feature/accepted This feature request is accepted by product managers and removed feature/discussing This feature request is discussing among product managers labels Aug 28, 2020
@scsldb scsldb added this to the Requirement pool milestone Aug 28, 2020
@SunRunAway
Copy link
Contributor

Duplicate of #11703

@SunRunAway SunRunAway marked this as a duplicate of #11703 Dec 30, 2022
@kennytm
Copy link
Contributor Author

kennytm commented Jan 3, 2023

@SunRunAway I have explained previously that it is not a duplicate of #11703 in #18672 (comment). (Plus #11703 was closed-as-dupe to this issue in #11703 (comment) making this a silly cyclic dependency (#11703 should be a dupe of #22765, not this issue))

I have also checked v6.5.0 and nightly that the matching Oracle DB feature mentioned at the top is still not supported.

mysql> select * from x as of timestamp '2023-01-03 18:49:38' union all select * from x;
ERROR 8135 (HY000): can not set different time in the as of

I'm going to reopen given that there's no justification why my comment above is no longer valid, and edited the title to clarify the desired feature.

@kennytm kennytm reopened this Jan 3, 2023
@kennytm kennytm changed the title Support Flashback Query / Time-travel Query / "AS OF" Query Support Flashback Query / Time-travel Query / "AS OF" Query with multiple timestamps (sans ERROR 8135) Jan 3, 2023
@SunRunAway SunRunAway changed the title Support Flashback Query / Time-travel Query / "AS OF" Query with multiple timestamps (sans ERROR 8135) Support INSERT INTO … SELECT … AS OF timestamp" Query (sans ERROR 8135) Jan 20, 2023
@SunRunAway SunRunAway changed the title Support INSERT INTO … SELECT … AS OF timestamp" Query (sans ERROR 8135) Support INSERT INTO … SELECT … AS OF timestamp" Query Jan 20, 2023
@SunRunAway SunRunAway changed the title Support INSERT INTO … SELECT … AS OF timestamp" Query Support "INSERT INTO … SELECT … AS OF timestamp" Query Jan 20, 2023
@SunRunAway
Copy link
Contributor

SunRunAway commented Jan 20, 2023

@SunRunAway I have explained previously that it is not a duplicate of #11703 in #18672 (comment). (Plus #11703 was closed-as-dupe to this issue in #11703 (comment) making this a silly cyclic dependency (#11703 should be a dupe of #22765, not this issue))

I have also checked v6.5.0 and nightly that the matching Oracle DB feature mentioned at the top is still not supported.

mysql> select * from x as of timestamp '2023-01-03 18:49:38' union all select * from x;
ERROR 8135 (HY000): can not set different time in the as of

I'm going to reopen given that there's no justification why my comment above is no longer valid, and edited the title to clarify the desired feature.

Thanks for your patient explanation. I’ve changed title to an explicit syntax for better understanding for other people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/accepted This feature request is accepted by product managers priority/P1 The issue has P1 priority. type/feature-request Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

4 participants