120 lines
5.6 KiB
Go
120 lines
5.6 KiB
Go
// Copyright 2018 PingCAP, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package executor_test
|
|
|
|
import (
|
|
. "github.com/pingcap/check"
|
|
"github.com/pingcap/tidb/table"
|
|
"github.com/pingcap/tidb/terror"
|
|
"github.com/pingcap/tidb/util/testkit"
|
|
)
|
|
|
|
func (s *testSuite) TestInsertOnDuplicateKey(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a1 bigint primary key, b1 bigint);`)
|
|
tk.MustExec(`create table t2(a2 bigint primary key, b2 bigint);`)
|
|
tk.MustExec(`insert into t1 values(1, 100);`)
|
|
tk.MustExec(`insert into t2 values(1, 200);`)
|
|
|
|
tk.MustExec(`insert into t1 select a2, b2 from t2 on duplicate key update b1 = a2;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 1"))
|
|
|
|
tk.MustExec(`insert into t1 select a2, b2 from t2 on duplicate key update b1 = b2;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 200"))
|
|
|
|
tk.MustExec(`insert into t1 select a2, b2 from t2 on duplicate key update a1 = a2;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 200"))
|
|
|
|
tk.MustExec(`insert into t1 select a2, b2 from t2 on duplicate key update b1 = 300;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 300"))
|
|
|
|
tk.MustExec(`insert into t1 values(1, 1) on duplicate key update b1 = 400;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 400"))
|
|
|
|
tk.MustExec(`insert into t1 select 1, 500 from t2 on duplicate key update b1 = 400;`)
|
|
tk.MustQuery(`select * from t1;`).Check(testkit.Rows("1 400"))
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a bigint primary key, b bigint);`)
|
|
tk.MustExec(`create table t2(a bigint primary key, b bigint);`)
|
|
_, err := tk.Exec(`insert into t1 select * from t2 on duplicate key update c = t2.b;`)
|
|
c.Assert(err.Error(), Equals, `[planner:1054]Unknown column 'c' in 'field list'`)
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a bigint primary key, b bigint);`)
|
|
tk.MustExec(`create table t2(a bigint primary key, b bigint);`)
|
|
_, err = tk.Exec(`insert into t1 select * from t2 on duplicate key update a = b;`)
|
|
c.Assert(err.Error(), Equals, `[planner:1052]Column 'b' in field list is ambiguous`)
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a bigint primary key, b bigint);`)
|
|
tk.MustExec(`create table t2(a bigint primary key, b bigint);`)
|
|
_, err = tk.Exec(`insert into t1 select * from t2 on duplicate key update c = b;`)
|
|
c.Assert(err.Error(), Equals, `[planner:1054]Unknown column 'c' in 'field list'`)
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a1 bigint primary key, b1 bigint);`)
|
|
tk.MustExec(`create table t2(a2 bigint primary key, b2 bigint);`)
|
|
_, err = tk.Exec(`insert into t1 select * from t2 on duplicate key update a1 = values(b2);`)
|
|
c.Assert(err.Error(), Equals, `[planner:1054]Unknown column 'b2' in 'field list'`)
|
|
|
|
tk.MustExec(`drop table if exists t1, t2;`)
|
|
tk.MustExec(`create table t1(a1 bigint primary key, b1 bigint);`)
|
|
tk.MustExec(`create table t2(a2 bigint primary key, b2 bigint);`)
|
|
tk.MustExec(`insert into t1 values(1, 100);`)
|
|
tk.MustExec(`insert into t2 values(1, 200);`)
|
|
tk.MustExec(`insert into t1 select * from t2 on duplicate key update b1 = values(b1) + b2;`)
|
|
tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400"))
|
|
tk.MustExec(`insert into t1 select * from t2 on duplicate key update b1 = values(b1) + b2;`)
|
|
tk.MustQuery(`select * from t1`).Check(testkit.Rows("1 400"))
|
|
|
|
tk.MustExec(`drop table if exists t;`)
|
|
tk.MustExec(`create table t(k1 bigint, k2 bigint, val bigint, primary key(k1, k2));`)
|
|
tk.MustExec(`insert into t (val, k1, k2) values (3, 1, 2);`)
|
|
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
|
|
tk.MustExec(`insert into t (val, k1, k2) select c, a, b from (select 1 as a, 2 as b, 4 as c) tmp on duplicate key update val = tmp.c;`)
|
|
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 4`))
|
|
|
|
tk.MustExec(`drop table if exists t;`)
|
|
tk.MustExec(`create table t(k1 double, k2 double, v double, primary key(k1, k2));`)
|
|
tk.MustExec(`insert into t (v, k1, k2) select c, a, b from (select "3" c, "1" a, "2" b) tmp on duplicate key update v=c;`)
|
|
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
|
|
tk.MustExec(`insert into t (v, k1, k2) select c, a, b from (select "3" c, "1" a, "2" b) tmp on duplicate key update v=c;`)
|
|
tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 2 3`))
|
|
}
|
|
|
|
func (s *testSuite) TestUpdateDuplicateKey(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
|
|
tk.MustExec(`drop table if exists t;`)
|
|
tk.MustExec(`create table c(i int,j int,k int,primary key(i,j,k));`)
|
|
tk.MustExec(`insert into c values(1,2,3);`)
|
|
tk.MustExec(`insert into c values(1,2,4);`)
|
|
_, err := tk.Exec(`update c set i=1,j=2,k=4 where i=1 and j=2 and k=3;`)
|
|
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1-2-4' for key 'PRIMARY'")
|
|
}
|
|
|
|
func (s *testSuite) TestInsertWrongValueForField(c *C) {
|
|
tk := testkit.NewTestKit(c, s.store)
|
|
tk.MustExec("use test")
|
|
tk.MustExec(`drop table if exists t1;`)
|
|
tk.MustExec(`create table t1(a bigint);`)
|
|
_, err := tk.Exec(`insert into t1 values("asfasdfsajhlkhlksdaf");`)
|
|
c.Assert(terror.ErrorEqual(err, table.ErrTruncatedWrongValueForField), IsTrue)
|
|
}
|