Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/executor/importer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ go_test(
embed = [":importer"],
flaky = True,
race = "on",
shard_count = 44,
shard_count = 45,
deps = [
"//br/pkg/mock",
"//br/pkg/streamhelper",
Expand Down
13 changes: 13 additions & 0 deletions pkg/executor/importer/precheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var GetEtcdClient = store.NewEtcdCli

// CheckRequirements checks the requirements for IMPORT INTO.
// we check the following things here:
// - target table should not have TTL enabled
// - when import from file
// 1. there is no active job on the target table
// 2. the total file size > 0
Expand All @@ -60,6 +61,18 @@ func (e *LoadDataController) CheckRequirementsBeforeInitDataFiles(ctx context.Co
}

func (e *LoadDataController) checkRequirements(ctx context.Context, se sessionctx.Context, checkTotalFileSize bool) error {
tableInfo := e.Plan.TableInfo
// Import table mode can prevent TTL from deleting data and causing a checksum
// mismatch. However, because TTL jobs run asynchronously, a job that races with the
// switch to import mode may still report a protected-table error. This precheck is
// also needed on versions that do not support table mode. Therefore, IMPORT INTO
// forbids importing into a table with TTL enabled. If later testing shows that IMPORT
// INTO can always switch table mode without an opt-out, TTL can check table mode before
// starting a job and this precheck can be removed.
if tableInfo.TTLInfo != nil && tableInfo.TTLInfo.Enable {
Comment thread
D3Hunter marked this conversation as resolved.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ [Info] IMPORT INTO into a TTL-enabled table becomes a hard rejection that DISABLE_PRECHECK cannot override

After upgrade, any existing IMPORT INTO job or automation whose target table has TTL enabled changes from success to a hard error 8173.
Because the guard sits before the if !e.DisablePrecheck block, WITH DISABLE_PRECHECK no longer provides an escape hatch, so operators relying on it to bypass prechecks lose that option for this case. This is a user-visible compatibility change that operators need to know about before upgrading.

return exeerrors.ErrLoadDataPreCheckFailed.FastGenByArgs("target table has TTL enabled, please disable TTL before IMPORT INTO")
}

conn := se.GetSQLExecutor()
if e.DataSourceType == DataSourceTypeFile {
cnt, err := GetActiveJobCnt(ctx, conn, e.Plan.DBName, e.Plan.TableInfo.Name.L)
Expand Down
44 changes: 44 additions & 0 deletions pkg/executor/importer/precheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,47 @@ func TestCheckRequirements(t *testing.T) {
c.Plan.CloudStorageURI = fmt.Sprintf("s3://test-bucket/path?region=us-east-1&endpoint=%s&access-key=xxxxxx&secret-access-key=xxxxxx", ts.URL)
require.NoError(t, c.CheckRequirements(ctx, tk.Session()))
}

func TestCheckRequirementsTTL(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
ctx := util.WithInternalSourceType(context.Background(), kv.InternalImportInto)

tk.MustExec("create table test.t(id int primary key, created_at datetime) TTL = `created_at` + INTERVAL 1 DAY")
is := tk.Session().GetLatestInfoSchema().(infoschema.InfoSchema)
tableObj, err := is.TableByName(ctx, ast.NewCIStr("test"), ast.NewCIStr("t"))
require.NoError(t, err)
require.True(t, tableObj.Meta().TTLInfo.Enable)

c := &importer.LoadDataController{
Plan: &importer.Plan{
DBName: "test",
DataSourceType: importer.DataSourceTypeQuery,
DisablePrecheck: true,
TableInfo: tableObj.Meta(),
},
Table: tableObj,
}
err = c.CheckRequirements(ctx, tk.Session())
require.ErrorIs(t, err, exeerrors.ErrLoadDataPreCheckFailed)
require.ErrorContains(t, err, "target table has TTL enabled, please disable TTL before IMPORT INTO")
err = c.CheckRequirementsBeforeInitDataFiles(ctx, tk.Session())
require.ErrorIs(t, err, exeerrors.ErrLoadDataPreCheckFailed)
require.ErrorContains(t, err, "target table has TTL enabled, please disable TTL before IMPORT INTO")

tk.MustExec("alter table test.t ttl_enable = 'OFF'")
is = tk.Session().GetLatestInfoSchema().(infoschema.InfoSchema)
tableObj, err = is.TableByName(ctx, ast.NewCIStr("test"), ast.NewCIStr("t"))
require.NoError(t, err)
c = &importer.LoadDataController{
Plan: &importer.Plan{
DBName: "test",
DataSourceType: importer.DataSourceTypeQuery,
DisablePrecheck: true,
TableInfo: tableObj.Meta(),
},
Table: tableObj,
}
require.NoError(t, c.CheckRequirements(ctx, tk.Session()))
require.NoError(t, c.CheckRequirementsBeforeInitDataFiles(ctx, tk.Session()))
}
6 changes: 6 additions & 0 deletions tests/integrationtest/r/executor/import_into.result
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,9 @@ create table cachetbl (id int);
alter table cachetbl cache;
import into cachetbl from '/file.csv';
Error 1105 (HY000): IMPORT INTO does not support cached table
drop table if exists import_into_ttl_precheck_source, import_into_ttl_precheck_target;
create table import_into_ttl_precheck_source (id int primary key, created_at datetime);
create table import_into_ttl_precheck_target (id int primary key, created_at datetime) TTL = `created_at` + INTERVAL 1 DAY;
IMPORT INTO import_into_ttl_precheck_target FROM SELECT * FROM import_into_ttl_precheck_source WITH DISABLE_PRECHECK;
Error 8173 (HY000): PreCheck failed: target table has TTL enabled, please disable TTL before IMPORT INTO
drop table if exists import_into_ttl_precheck_source, import_into_ttl_precheck_target;
8 changes: 8 additions & 0 deletions tests/integrationtest/t/executor/import_into.test
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,11 @@ create table cachetbl (id int);
alter table cachetbl cache;
-- error 1105
import into cachetbl from '/file.csv';

# TestImportIntoTTLPrecheck
drop table if exists import_into_ttl_precheck_source, import_into_ttl_precheck_target;
create table import_into_ttl_precheck_source (id int primary key, created_at datetime);
create table import_into_ttl_precheck_target (id int primary key, created_at datetime) TTL = `created_at` + INTERVAL 1 DAY;
-- error 8173
IMPORT INTO import_into_ttl_precheck_target FROM SELECT * FROM import_into_ttl_precheck_source WITH DISABLE_PRECHECK;
drop table if exists import_into_ttl_precheck_source, import_into_ttl_precheck_target;
Loading