Skip to content

Commit b9034ca

Browse files
authored
ddl: check TTL constraints for temporary tables (#65246)
close #64948
1 parent 69aa3a9 commit b9034ca

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

pkg/ddl/create_table.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,19 @@ func BuildSessionTemporaryTableInfo(ctx *metabuild.Context, store kv.Storage, is
769769
}
770770
tbInfo, err = BuildTableInfoWithLike(ident, referTbl.Meta(), s)
771771
} else {
772-
tbInfo, err = buildTableInfoWithCheck(ctx, store, s, dbCharset, dbCollate, placementPolicyRef)
772+
tbInfo, err = BuildTableInfoWithStmt(ctx, s, dbCharset, dbCollate, placementPolicyRef)
773773
}
774-
return tbInfo, err
774+
775+
if err != nil {
776+
return nil, err
777+
}
778+
if err = checkTableInfoValidWithStmt(ctx, tbInfo, s); err != nil {
779+
return nil, err
780+
}
781+
if err = checkTableInfoValidExtra(ctx.GetExprCtx().GetEvalCtx().ErrCtx(), store, s.Table.Schema, tbInfo); err != nil {
782+
return nil, err
783+
}
784+
return tbInfo, nil
775785
}
776786

777787
// BuildTableInfoWithStmt builds model.TableInfo from a SQL statement without validity check
@@ -1268,7 +1278,10 @@ func BuildTableInfoWithLike(ident ast.Ident, referTblInfo *model.TableInfo, s *a
12681278
tblInfo.Partition = &pi
12691279
}
12701280

1271-
if referTblInfo.TTLInfo != nil {
1281+
// for issue #64948, temporary table does not support TLL, we should remove it
1282+
if s.TemporaryKeyword != ast.TemporaryNone {
1283+
tblInfo.TTLInfo = nil
1284+
} else if referTblInfo.TTLInfo != nil {
12721285
tblInfo.TTLInfo = referTblInfo.TTLInfo.Clone()
12731286
}
12741287

tests/integrationtest/r/executor/ddl.result

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,24 @@ c_int
588588
1
589589
alter table t add index idx_4 (c_str);
590590
rollback;
591+
DROP TABLE IF EXISTS ttl_src, temp_local, temp_global, normal_copy;
592+
CREATE TABLE ttl_src (created_at TIMESTAMP) TTL = `created_at` + INTERVAL 1 HOUR;
593+
CREATE TEMPORARY TABLE temp_local LIKE ttl_src;
594+
SHOW CREATE TABLE temp_local;
595+
Table Create Table
596+
temp_local CREATE TEMPORARY TABLE `temp_local` (
597+
`created_at` timestamp NULL DEFAULT NULL
598+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
599+
CREATE GLOBAL TEMPORARY TABLE temp_global LIKE ttl_src ON COMMIT DELETE ROWS;
600+
SHOW CREATE TABLE temp_global;
601+
Table Create Table
602+
temp_global CREATE GLOBAL TEMPORARY TABLE `temp_global` (
603+
`created_at` timestamp NULL DEFAULT NULL
604+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ON COMMIT DELETE ROWS
605+
CREATE TABLE normal_copy LIKE ttl_src;
606+
SHOW CREATE TABLE normal_copy;
607+
Table Create Table
608+
normal_copy CREATE TABLE `normal_copy` (
609+
`created_at` timestamp NULL DEFAULT NULL
610+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![ttl] TTL=`created_at` + INTERVAL 1 HOUR */ /*T![ttl] TTL_ENABLE='ON' */ /*T![ttl] TTL_JOB_INTERVAL='24h' */
611+
DROP TABLE IF EXISTS ttl_src, temp_local, temp_global, normal_copy;

tests/integrationtest/t/executor/ddl.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,13 @@ select c_int from t where c_str is not null for update;
534534
alter table t add index idx_4 (c_str);
535535
rollback;
536536

537+
# TestIssue64948
538+
DROP TABLE IF EXISTS ttl_src, temp_local, temp_global, normal_copy;
539+
CREATE TABLE ttl_src (created_at TIMESTAMP) TTL = `created_at` + INTERVAL 1 HOUR;
540+
CREATE TEMPORARY TABLE temp_local LIKE ttl_src;
541+
SHOW CREATE TABLE temp_local;
542+
CREATE GLOBAL TEMPORARY TABLE temp_global LIKE ttl_src ON COMMIT DELETE ROWS;
543+
SHOW CREATE TABLE temp_global;
544+
CREATE TABLE normal_copy LIKE ttl_src;
545+
SHOW CREATE TABLE normal_copy;
546+
DROP TABLE IF EXISTS ttl_src, temp_local, temp_global, normal_copy;

0 commit comments

Comments
 (0)