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

*: enable tidb_enable_foreign_key variable default #39677

Merged
merged 15 commits into from
Dec 7, 2022
Merged
1 change: 0 additions & 1 deletion br/tests/br_foreign_key/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
set -eu
DB="$TEST_NAME"

run_sql "set @@global.tidb_enable_foreign_key=1;"
run_sql "set @@global.foreign_key_checks=1;"
run_sql "set @@foreign_key_checks=1;"
run_sql "create schema $DB;"
Expand Down
2 changes: 0 additions & 2 deletions cmd/explaintest/r/explain_foreign_key.result
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
use test;
drop table if exists t1,t2;
Expand Down Expand Up @@ -165,5 +164,4 @@ Delete N/A root N/A
└─TableReader(Probe) 3333.33 root data:TableRangeScan
└─TableRangeScan 3333.33 cop[tikv] table:t_1 range:(0,+inf], keep order:true, stats:pseudo
drop table if exists t_1,t_2,t_3,t_4;
set @@global.tidb_enable_foreign_key=0;
set @@foreign_key_checks=0;
17 changes: 5 additions & 12 deletions cmd/explaintest/r/tpch.result
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ S_NATIONKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
PRIMARY KEY (S_SUPPKEY),
CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (S_SUPPKEY));
CREATE TABLE IF NOT EXISTS partsupp ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK1 (PS_SUPPKEY) references supplier(S_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK2 (PS_PARTKEY) references part(P_PARTKEY));
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY));
CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
Expand All @@ -45,8 +42,7 @@ C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
PRIMARY KEY (C_CUSTKEY),
CONSTRAINT FOREIGN KEY CUSTOMER_FK1 (C_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (C_CUSTKEY));
CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS CHAR(1) NOT NULL,
Expand All @@ -56,8 +52,7 @@ O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
PRIMARY KEY (O_ORDERKEY),
CONSTRAINT FOREIGN KEY ORDERS_FK1 (O_CUSTKEY) references customer(C_CUSTKEY));
PRIMARY KEY (O_ORDERKEY));
CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
Expand All @@ -74,9 +69,7 @@ L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
CONSTRAINT FOREIGN KEY LINEITEM_FK1 (L_ORDERKEY) references orders(O_ORDERKEY),
CONSTRAINT FOREIGN KEY LINEITEM_FK2 (L_PARTKEY,L_SUPPKEY) references partsupp(PS_PARTKEY, PS_SUPPKEY));
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER));
load stats 's/tpch_stats/nation.json';
load stats 's/tpch_stats/region.json';
load stats 's/tpch_stats/part.json';
Expand Down
2 changes: 0 additions & 2 deletions cmd/explaintest/t/explain_foreign_key.test
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
set @@global.tidb_enable_foreign_key=1;
set @@foreign_key_checks=1;
use test;
drop table if exists t1,t2;
Expand Down Expand Up @@ -43,5 +42,4 @@ set @@foreign_key_checks=0;
explain format = 'brief' update t_1,t_2 set t_1.id=2,t_2.id=2 where t_1.id=t_2.id and t_1.id=1;
explain format = 'brief' delete t_1,t_2 from t_1 join t_2 where t_1.id=t_2.id and t_1.id > 0;
drop table if exists t_1,t_2,t_3,t_4;
set @@global.tidb_enable_foreign_key=0;
set @@foreign_key_checks=0;
17 changes: 5 additions & 12 deletions cmd/explaintest/t/tpch.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@ CREATE TABLE IF NOT EXISTS supplier ( S_SUPPKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
PRIMARY KEY (S_SUPPKEY),
CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (S_SUPPKEY));

CREATE TABLE IF NOT EXISTS partsupp ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK1 (PS_SUPPKEY) references supplier(S_SUPPKEY),
CONSTRAINT FOREIGN KEY PARTSUPP_FK2 (PS_PARTKEY) references part(P_PARTKEY));
PRIMARY KEY (PS_PARTKEY,PS_SUPPKEY));

CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_NAME VARCHAR(25) NOT NULL,
Expand All @@ -51,8 +48,7 @@ CREATE TABLE IF NOT EXISTS customer ( C_CUSTKEY INTEGER NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
PRIMARY KEY (C_CUSTKEY),
CONSTRAINT FOREIGN KEY CUSTOMER_FK1 (C_NATIONKEY) references nation(N_NATIONKEY));
PRIMARY KEY (C_CUSTKEY));

CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CUSTKEY INTEGER NOT NULL,
Expand All @@ -63,8 +59,7 @@ CREATE TABLE IF NOT EXISTS orders ( O_ORDERKEY INTEGER NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
PRIMARY KEY (O_ORDERKEY),
CONSTRAINT FOREIGN KEY ORDERS_FK1 (O_CUSTKEY) references customer(C_CUSTKEY));
PRIMARY KEY (O_ORDERKEY));

CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
Expand All @@ -82,9 +77,7 @@ CREATE TABLE IF NOT EXISTS lineitem ( L_ORDERKEY INTEGER NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
CONSTRAINT FOREIGN KEY LINEITEM_FK1 (L_ORDERKEY) references orders(O_ORDERKEY),
CONSTRAINT FOREIGN KEY LINEITEM_FK2 (L_PARTKEY,L_SUPPKEY) references partsupp(PS_PARTKEY, PS_SUPPKEY));
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER));
-- load stats.
load stats 's/tpch_stats/nation.json';
load stats 's/tpch_stats/region.json';
Expand Down
2 changes: 1 addition & 1 deletion ddl/db_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTableForeignKey(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (a int, b int);")
tk.MustExec("create table t1 (a int, b int, index(a), index(b));")
// test create table with foreign key.
failSQL := "create table t2 (c int, foreign key (a) references t1(a));"
tk.MustGetErrCode(failSQL, errno.ErrKeyColumnDoesNotExits)
Expand Down
13 changes: 12 additions & 1 deletion ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -107,7 +108,17 @@ func TestForeignKey(t *testing.T) {
testCreateSchema(t, testkit.NewTestKit(t, store).Session(), dom.DDL(), dbInfo)
tblInfo, err := testTableInfo(store, "t", 3)
require.NoError(t, err)

tblInfo.Indices = append(tblInfo.Indices, &model.IndexInfo{
ID: 1,
Name: model.NewCIStr("idx_fk"),
Table: model.NewCIStr("t"),
Columns: []*model.IndexColumn{{
Name: model.NewCIStr("c1"),
Offset: 0,
Length: types.UnspecifiedLength,
}},
State: model.StatePublic,
})
testCreateTable(t, testkit.NewTestKit(t, store).Session(), d, dbInfo, tblInfo)

// fix data race
Expand Down
1 change: 1 addition & 0 deletions executor/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,7 @@ func TestShowCreateTableWithForeignKey(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec("set @@global.tidb_enable_foreign_key=0")
tk.MustExec("create table t1 (id int key, leader int, leader2 int, index(leader), index(leader2), constraint fk foreign key (leader) references t1(id) ON DELETE CASCADE ON UPDATE SET NULL);")
tk.MustQuery("show create table t1").Check(testkit.Rows("t1 CREATE TABLE `t1` (\n" +
" `id` int(11) NOT NULL,\n" +
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ var defaultSysVars = []*SysVar{
}
return normalizedValue, ErrWrongValueForVar.GenWithStackByArgs(ForeignKeyChecks, originalValue)
}},
{Scope: ScopeGlobal, Name: TiDBEnableForeignKey, Value: BoolToOnOff(false), Type: TypeBool, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
{Scope: ScopeGlobal, Name: TiDBEnableForeignKey, Value: BoolToOnOff(true), Type: TypeBool, SetGlobal: func(_ context.Context, s *SessionVars, val string) error {
EnableForeignKey.Store(TiDBOptOn(val))
return nil
}, GetGlobal: func(_ context.Context, s *SessionVars) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ var (
// DDLDiskQuota is the temporary variable for set disk quota for lightning
DDLDiskQuota = atomic.NewUint64(DefTiDBDDLDiskQuota)
// EnableForeignKey indicates whether to enable foreign key feature.
EnableForeignKey = atomic.NewBool(false)
EnableForeignKey = atomic.NewBool(true)
EnableRCReadCheckTS = atomic.NewBool(false)

// DefTiDBServerMemoryLimit indicates the default value of TiDBServerMemoryLimit(TotalMem * 80%).
Expand Down