1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hanchuanchuan-goInception

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
db_test.go 120 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hanchuanchuan Отправлено 6 лет назад 27f3c5a
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459
// Copyright 2015 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 ddl_test
import (
"fmt"
"io"
"math"
"math/rand"
"strconv"
"strings"
"sync"
"time"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/ddl"
"github.com/hanchuanchuan/goInception/domain"
"github.com/hanchuanchuan/goInception/infoschema"
"github.com/hanchuanchuan/goInception/kv"
"github.com/hanchuanchuan/goInception/meta"
"github.com/hanchuanchuan/goInception/meta/autoid"
"github.com/hanchuanchuan/goInception/model"
"github.com/hanchuanchuan/goInception/mysql"
tmysql "github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/session"
"github.com/hanchuanchuan/goInception/sessionctx"
"github.com/hanchuanchuan/goInception/store/mockstore"
"github.com/hanchuanchuan/goInception/store/mockstore/mocktikv"
"github.com/hanchuanchuan/goInception/table"
"github.com/hanchuanchuan/goInception/table/tables"
"github.com/hanchuanchuan/goInception/tablecodec"
"github.com/hanchuanchuan/goInception/terror"
"github.com/hanchuanchuan/goInception/types"
"github.com/hanchuanchuan/goInception/util/admin"
"github.com/hanchuanchuan/goInception/util/mock"
"github.com/hanchuanchuan/goInception/util/testkit"
"github.com/hanchuanchuan/goInception/util/testleak"
"github.com/hanchuanchuan/goInception/util/testutil"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"golang.org/x/net/context"
)
const (
// waitForCleanDataRound indicates how many times should we check data is cleaned or not.
waitForCleanDataRound = 150
// waitForCleanDataInterval is a min duration between 2 check for data clean.
waitForCleanDataInterval = time.Millisecond * 100
)
var _ = Suite(&testDBSuite{})
const defaultBatchSize = 2048
type testDBSuite struct {
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
dom *domain.Domain
schemaName string
tk *testkit.TestKit
s session.Session
lease time.Duration
autoIDStep int64
}
func (s *testDBSuite) SetUpSuite(c *C) {
var err error
testleak.BeforeTest()
s.lease = 200 * time.Millisecond
session.SetSchemaLease(s.lease)
session.SetStatsLease(0)
s.schemaName = "test_db"
s.autoIDStep = autoid.GetStep()
autoid.SetStep(5000)
ddl.WaitTimeWhenErrorOccured = 1 * time.Microsecond
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.MustNewMVCCStore()
s.store, err = mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
s.s, err = session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
_, err = s.s.Execute(context.Background(), "create database test_db")
c.Assert(err, IsNil)
s.tk = testkit.NewTestKit(c, s.store)
}
func (s *testDBSuite) TearDownSuite(c *C) {
s.s.Execute(context.Background(), "drop database if exists test_db")
s.s.Close()
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
autoid.SetStep(s.autoIDStep)
}
func (s *testDBSuite) testErrorCode(c *C, sql string, errCode int) {
_, err := s.tk.Exec(sql)
c.Assert(err, NotNil)
originErr := errors.Cause(err)
tErr, ok := originErr.(*terror.Error)
c.Assert(ok, IsTrue, Commentf("err: %T", originErr))
c.Assert(tErr.ToSQLError().Code, DeepEquals, uint16(errCode), Commentf("MySQL code:%v", tErr.ToSQLError()))
}
func (s *testDBSuite) TestMySQLErrorCode(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
// create database
sql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
s.testErrorCode(c, sql, tmysql.ErrTooLongIdent)
sql = "create database test"
s.testErrorCode(c, sql, tmysql.ErrDBCreateExists)
sql = "create database test1 character set uft8;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
sql = "create database test2 character set gkb;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
sql = "create database test3 character set laitn1;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
// drop database
sql = "drop database db_not_exist"
s.testErrorCode(c, sql, tmysql.ErrDBDropExists)
// create table
s.tk.MustExec("create table test_error_code_succ (c1 int, c2 int, c3 int, primary key(c3))")
sql = "create table test_error_code_succ (c1 int, c2 int, c3 int)"
s.testErrorCode(c, sql, tmysql.ErrTableExists)
sql = "create table test_error_code1 (c1 int, c2 int, c2 int)"
s.testErrorCode(c, sql, tmysql.ErrDupFieldName)
sql = "create table test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
s.testErrorCode(c, sql, tmysql.ErrTooLongIdent)
sql = "create table aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)"
s.testErrorCode(c, sql, tmysql.ErrTooLongIdent)
sql = "create table test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))"
s.testErrorCode(c, sql, tmysql.ErrDupKeyName)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))"
s.testErrorCode(c, sql, tmysql.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))"
s.testErrorCode(c, sql, tmysql.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int not null default '')"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT now());"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "create table t1(a int) character set uft8;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set gkb;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set laitn1;"
s.testErrorCode(c, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));"
s.testErrorCode(c, sql, tmysql.ErrWrongFkDef)
sql = "create table test_error_code_2;"
s.testErrorCode(c, sql, tmysql.ErrTableMustHaveColumns)
sql = "create table test_error_code_2 (unique(c1));"
s.testErrorCode(c, sql, tmysql.ErrTableMustHaveColumns)
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));"
s.testErrorCode(c, sql, tmysql.ErrMultiplePriKey)
sql = "create table test_error_code_3(pt blob ,primary key (pt));"
s.testErrorCode(c, sql, tmysql.ErrBlobKeyWithoutLength)
sql = "create table test_error_code_3(a text, unique (a(3073)));"
s.testErrorCode(c, sql, tmysql.ErrTooLongKey)
sql = "create table test_error_code_3(`id` int, key `primary`(`id`));"
s.testErrorCode(c, sql, tmysql.ErrWrongNameForIndex)
sql = "create table t2(c1.c2 blob default null);"
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
sql = "create table t2 (id int default null primary key , age int);"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "create table t2 (id int null primary key , age int);"
s.testErrorCode(c, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int default null, age int, primary key(id));"
s.testErrorCode(c, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int null, age int, primary key(id));"
s.testErrorCode(c, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int primary key , age int);"
s.tk.MustExec(sql)
// add column
sql = "alter table test_error_code_succ add column c1 int"
s.testErrorCode(c, sql, tmysql.ErrDupFieldName)
sql = "alter table test_error_code_succ add column aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int"
s.testErrorCode(c, sql, tmysql.ErrTooLongIdent)
sql = "alter table test_comment comment 'test comment'"
s.testErrorCode(c, sql, tmysql.ErrNoSuchTable)
sql = "alter table test_error_code_succ add column `a ` int ;"
s.testErrorCode(c, sql, tmysql.ErrWrongColumnName)
s.tk.MustExec("create table test_on_update (c1 int, c2 int);")
sql = "alter table test_on_update add column c3 int on update current_timestamp;"
s.testErrorCode(c, sql, tmysql.ErrInvalidOnUpdate)
sql = "create table test_on_update_2(c int on update current_timestamp);"
s.testErrorCode(c, sql, tmysql.ErrInvalidOnUpdate)
// drop column
sql = "alter table test_error_code_succ drop c_not_exist"
s.testErrorCode(c, sql, tmysql.ErrCantDropFieldOrKey)
s.tk.MustExec("create table test_drop_column (c1 int );")
sql = "alter table test_drop_column drop column c1;"
s.testErrorCode(c, sql, tmysql.ErrCantRemoveAllFields)
// add index
sql = "alter table test_error_code_succ add index idx (c_not_exist)"
s.testErrorCode(c, sql, tmysql.ErrKeyColumnDoesNotExits)
s.tk.Exec("alter table test_error_code_succ add index idx (c1)")
sql = "alter table test_error_code_succ add index idx (c1)"
s.testErrorCode(c, sql, tmysql.ErrDupKeyName)
// drop index
sql = "alter table test_error_code_succ drop index idx_not_exist"
s.testErrorCode(c, sql, tmysql.ErrCantDropFieldOrKey)
sql = "alter table test_error_code_succ drop column c3"
s.testErrorCode(c, sql, int(tmysql.ErrUnknown))
// modify column
sql = "alter table test_error_code_succ modify testx.test_error_code_succ.c1 bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongDBName)
sql = "alter table test_error_code_succ modify t.c1 bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
// insert value
s.tk.MustExec("create table test_error_code_null(c1 char(100) not null);")
sql = "insert into test_error_code_null (c1) values(null);"
s.testErrorCode(c, sql, tmysql.ErrBadNull)
}
func (s *testDBSuite) TestAddIndexAfterAddColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table test_add_index_after_add_col(a int, b int not null default '0')")
s.tk.MustExec("insert into test_add_index_after_add_col values(1, 2),(2,2)")
s.tk.MustExec("alter table test_add_index_after_add_col add column c int not null default '0'")
sql := "alter table test_add_index_after_add_col add unique index cc(c) "
s.testErrorCode(c, sql, tmysql.ErrDupEntry)
sql = "alter table test_add_index_after_add_col add index idx_test(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17);"
s.testErrorCode(c, sql, tmysql.ErrTooManyKeyParts)
}
func (s *testDBSuite) TestAddIndexWithPK(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table test_add_index_with_pk(a int not null, b int not null default '0', primary key(a))")
s.tk.MustExec("insert into test_add_index_with_pk values(1, 2)")
s.tk.MustExec("alter table test_add_index_with_pk add index idx (a)")
s.tk.MustQuery("select a from test_add_index_with_pk").Check(testkit.Rows("1"))
s.tk.MustExec("insert into test_add_index_with_pk values(2, 2)")
s.tk.MustExec("alter table test_add_index_with_pk add index idx1 (a, b)")
s.tk.MustQuery("select * from test_add_index_with_pk").Check(testkit.Rows("1 2", "2 2"))
s.tk.MustExec("create table test_add_index_with_pk1(a int not null, b int not null default '0', c int, d int, primary key(c))")
s.tk.MustExec("insert into test_add_index_with_pk1 values(1, 1, 1, 1)")
s.tk.MustExec("alter table test_add_index_with_pk1 add index idx (c)")
s.tk.MustExec("insert into test_add_index_with_pk1 values(2, 2, 2, 2)")
s.tk.MustQuery("select * from test_add_index_with_pk1").Check(testkit.Rows("1 1 1 1", "2 2 2 2"))
s.tk.MustExec("create table test_add_index_with_pk2(a int not null, b int not null default '0', c int unsigned, d int, primary key(c))")
s.tk.MustExec("insert into test_add_index_with_pk2 values(1, 1, 1, 1)")
s.tk.MustExec("alter table test_add_index_with_pk2 add index idx (c)")
s.tk.MustExec("insert into test_add_index_with_pk2 values(2, 2, 2, 2)")
s.tk.MustQuery("select * from test_add_index_with_pk2").Check(testkit.Rows("1 1 1 1", "2 2 2 2"))
}
func (s *testDBSuite) TestRenameIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table t (pk int primary key, c int default 1, c1 int default 1, unique key k1(c), key k2(c1))")
// Test rename success
s.tk.MustExec("alter table t rename index k1 to k3")
s.tk.MustExec("admin check index t k3")
// Test rename to the same name
s.tk.MustExec("alter table t rename index k3 to k3")
s.tk.MustExec("admin check index t k3")
// Test rename on non-exists keys
s.testErrorCode(c, "alter table t rename index x to x", mysql.ErrKeyDoesNotExist)
// Test rename on already-exists keys
s.testErrorCode(c, "alter table t rename index k3 to k2", mysql.ErrDupKeyName)
s.tk.MustExec("alter table t rename index k2 to K2")
s.testErrorCode(c, "alter table t rename key k3 to K2", mysql.ErrDupKeyName)
}
func (s *testDBSuite) testGetTable(c *C, name string) table.Table {
ctx := s.s.(sessionctx.Context)
dom := domain.GetDomain(ctx)
// Make sure the table schema is the new schema.
err := dom.Reload()
c.Assert(err, IsNil)
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(s.schemaName), model.NewCIStr(name))
c.Assert(err, IsNil)
return tbl
}
func backgroundExec(s kv.Storage, sql string, done chan error) {
se, err := session.CreateSession4Test(s)
if err != nil {
done <- errors.Trace(err)
return
}
defer se.Close()
_, err = se.Execute(context.Background(), "use test_db")
if err != nil {
done <- errors.Trace(err)
return
}
_, err = se.Execute(context.Background(), sql)
done <- errors.Trace(err)
}
func (s *testDBSuite) TestAddUniqueIndexRollback(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.mustExec(c, "use test_db")
s.mustExec(c, "drop table if exists t1")
s.mustExec(c, "create table t1 (c1 int, c2 int, c3 int, primary key(c1))")
// defaultBatchSize is equal to ddl.defaultBatchSize
base := defaultBatchSize * 2
count := base
// add some rows
for i := 0; i < count; i++ {
s.mustExec(c, "insert into t1 values (?, ?, ?)", i, i, i)
}
// add some duplicate rows
for i := count - 10; i < count; i++ {
s.mustExec(c, "insert into t1 values (?, ?, ?)", i+10, i, i)
}
done := make(chan error, 1)
go backgroundExec(s.store, "create unique index c3_index on t1 (c3)", done)
times := 0
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate for key c3_index", Commentf("err:%v", err))
break LOOP
case <-ticker.C:
if times >= 10 {
break
}
step := 10
// delete some rows, and add some data
for i := count; i < count+step; i++ {
n := rand.Intn(count)
s.mustExec(c, "delete from t1 where c1 = ?", n)
s.mustExec(c, "insert into t1 values (?, ?, ?)", i+10, i, i)
}
count += step
times++
}
}
t := s.testGetTable(c, "t1")
for _, tidx := range t.Indices() {
c.Assert(strings.EqualFold(tidx.Meta().Name.L, "c3_index"), IsFalse)
}
// delete duplicate rows, then add index
for i := base - 10; i < base; i++ {
s.mustExec(c, "delete from t1 where c1 = ?", i+10)
}
sessionExec(c, s.store, "create index c3_index on t1 (c3)")
s.mustExec(c, "drop table t1")
}
func (s *testDBSuite) TestCancelAddIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.mustExec(c, "use test_db")
s.mustExec(c, "drop table if exists t1")
s.mustExec(c, "create table t1 (c1 int, c2 int, c3 int, primary key(c1))")
// defaultBatchSize is equal to ddl.defaultBatchSize
base := defaultBatchSize * 2
count := base
// add some rows
for i := 0; i < count; i++ {
s.mustExec(c, "insert into t1 values (?, ?, ?)", i, i, i)
}
var checkErr error
var c3IdxInfo *model.IndexInfo
hook := &ddl.TestDDLCallback{}
oldReorgWaitTimeout := ddl.ReorgWaitTimeout
// let hook.OnJobUpdatedExported has chance to cancel the job.
// the hook.OnJobUpdatedExported is called when the job is updated, runReorgJob will wait ddl.ReorgWaitTimeout, then return the ddl.runDDLJob.
// After that ddl call d.hook.OnJobUpdated(job), so that we can canceled the job in this test case.
ddl.ReorgWaitTimeout = 50 * time.Millisecond
hook.OnJobUpdatedExported, c3IdxInfo = backgroundExecOnJobUpdatedExported(c, s, hook, checkErr)
s.dom.DDL().(ddl.DDLForTest).SetHook(hook)
done := make(chan error, 1)
go backgroundExec(s.store, "create unique index c3_index on t1 (c3)", done)
times := 0
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
c.Assert(checkErr, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job")
break LOOP
case <-ticker.C:
if times >= 10 {
break
}
step := 10
// delete some rows, and add some data
for i := count; i < count+step; i++ {
n := rand.Intn(count)
s.mustExec(c, "delete from t1 where c1 = ?", n)
s.mustExec(c, "insert into t1 values (?, ?, ?)", i+10, i, i)
}
count += step
times++
}
}
t := s.testGetTable(c, "t1")
for _, tidx := range t.Indices() {
c.Assert(strings.EqualFold(tidx.Meta().Name.L, "c3_index"), IsFalse)
}
ctx := s.s.(sessionctx.Context)
idx := tables.NewIndex(t.Meta().ID, t.Meta(), c3IdxInfo)
checkDelRangeDone(c, ctx, idx)
s.mustExec(c, "drop table t1")
ddl.ReorgWaitTimeout = oldReorgWaitTimeout
callback := &ddl.TestDDLCallback{}
s.dom.DDL().(ddl.DDLForTest).SetHook(callback)
}
func (s *testDBSuite) TestAddAnonymousIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table t_anonymous_index (c1 int, c2 int, C3 int)")
s.mustExec(c, "alter table t_anonymous_index add index (c1, c2)")
// for dropping empty index
_, err := s.tk.Exec("alter table t_anonymous_index drop index")
c.Assert(err, NotNil)
// The index name is c1 when adding index (c1, c2).
s.mustExec(c, "alter table t_anonymous_index drop index c1")
t := s.testGetTable(c, "t_anonymous_index")
c.Assert(t.Indices(), HasLen, 0)
// for adding some indices that the first column name is c1
s.mustExec(c, "alter table t_anonymous_index add index (c1)")
_, err = s.tk.Exec("alter table t_anonymous_index add index c1 (c2)")
c.Assert(err, NotNil)
t = s.testGetTable(c, "t_anonymous_index")
c.Assert(t.Indices(), HasLen, 1)
idx := t.Indices()[0].Meta().Name.L
c.Assert(idx, Equals, "c1")
// The MySQL will be a warning.
s.mustExec(c, "alter table t_anonymous_index add index c1_3 (c1)")
s.mustExec(c, "alter table t_anonymous_index add index (c1, c2, C3)")
// The MySQL will be a warning.
s.mustExec(c, "alter table t_anonymous_index add index (c1)")
t = s.testGetTable(c, "t_anonymous_index")
c.Assert(t.Indices(), HasLen, 4)
s.mustExec(c, "alter table t_anonymous_index drop index c1")
s.mustExec(c, "alter table t_anonymous_index drop index c1_2")
s.mustExec(c, "alter table t_anonymous_index drop index c1_3")
s.mustExec(c, "alter table t_anonymous_index drop index c1_4")
// for case insensitive
s.mustExec(c, "alter table t_anonymous_index add index (C3)")
s.mustExec(c, "alter table t_anonymous_index drop index c3")
s.mustExec(c, "alter table t_anonymous_index add index c3 (C3)")
s.mustExec(c, "alter table t_anonymous_index drop index C3")
// for anonymous index with column name `primary`
s.mustExec(c, "create table t_primary (`primary` int, key (`primary`))")
t = s.testGetTable(c, "t_primary")
c.Assert(t.Indices()[0].Meta().Name.String(), Equals, "primary_2")
s.mustExec(c, "create table t_primary_2 (`primary` int, key primary_2 (`primary`), key (`primary`))")
t = s.testGetTable(c, "t_primary_2")
c.Assert(t.Indices()[0].Meta().Name.String(), Equals, "primary_2")
c.Assert(t.Indices()[1].Meta().Name.String(), Equals, "primary_3")
s.mustExec(c, "create table t_primary_3 (`primary_2` int, key(`primary_2`), `primary` int, key(`primary`));")
t = s.testGetTable(c, "t_primary_3")
c.Assert(t.Indices()[0].Meta().Name.String(), Equals, "primary_2")
c.Assert(t.Indices()[1].Meta().Name.String(), Equals, "primary_3")
}
// TestModifyColumnAfterAddIndex Issue 5134
func (s *testDBSuite) TestModifyColumnAfterAddIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table city (city VARCHAR(2) KEY);")
s.mustExec(c, "alter table city change column city city varchar(50);")
s.mustExec(c, `insert into city values ("abc"), ("abd");`)
}
func (s *testDBSuite) testAlterLock(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table t_index_lock (c1 int, c2 int, C3 int)")
s.mustExec(c, "alter table t_indx_lock add index (c1, c2), lock=none")
}
func (s *testDBSuite) TestAddMultiColumnsIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("drop database if exists tidb;")
s.tk.MustExec("create database tidb;")
s.tk.MustExec("use tidb;")
s.tk.MustExec("create table tidb.test (a int auto_increment primary key, b int);")
s.tk.MustExec("insert tidb.test values (1, 1);")
s.tk.MustExec("update tidb.test set b = b + 1 where a = 1;")
s.tk.MustExec("insert into tidb.test values (2, 2);")
// Test that the b value is nil.
s.tk.MustExec("insert into tidb.test (a) values (3);")
s.tk.MustExec("insert into tidb.test values (4, 4);")
// Test that the b value is nil again.
s.tk.MustExec("insert into tidb.test (a) values (5);")
s.tk.MustExec("insert tidb.test values (6, 6);")
s.tk.MustExec("alter table tidb.test add index idx1 (a, b);")
s.tk.MustExec("admin check table test")
}
func (s *testDBSuite) TestAddIndex(c *C) {
s.testAddIndex(c, false, "create table test_add_index (c1 bigint, c2 bigint, c3 bigint, primary key(c1))")
s.testAddIndex(c, true, `create table test_add_index (c1 bigint, c2 bigint, c3 bigint, primary key(c1))
partition by range (c1) (
partition p0 values less than (3440),
partition p1 values less than (61440),
partition p2 values less than (122880),
partition p3 values less than (204800),
partition p4 values less than maxvalue)`)
}
func (s *testDBSuite) testAddIndex(c *C, testPartition bool, createTableSQL string) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("set @@tidb_enable_table_partition = 1")
s.tk.MustExec("drop table if exists test_add_index")
s.tk.MustExec(createTableSQL)
done := make(chan error, 1)
start := -10
num := defaultBatchSize
// first add some rows
for i := start; i < num; i++ {
sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", i, i, i)
s.mustExec(c, sql)
}
// Add some discrete rows.
maxBatch := 20
batchCnt := 100
otherKeys := make([]int, 0, batchCnt*maxBatch)
// Make sure there are no duplicate keys.
base := defaultBatchSize * 20
for i := 1; i < batchCnt; i++ {
n := base + i*defaultBatchSize + i
for j := 0; j < rand.Intn(maxBatch); j++ {
n += j
sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", n, n, n)
s.mustExec(c, sql)
otherKeys = append(otherKeys, n)
}
}
// Encounter the value of math.MaxInt64 in middle of
v := math.MaxInt64 - defaultBatchSize/2
sql := fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", v, v, v)
s.mustExec(c, sql)
otherKeys = append(otherKeys, v)
sessionExecInGoroutine(c, s.store, "create index c3_index on test_add_index (c3)", done)
deletedKeys := make(map[int]struct{})
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
if err == nil {
break LOOP
}
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
// When the server performance is particularly poor,
// the adding index operation can not be completed.
// So here is a limit to the number of rows inserted.
if num > defaultBatchSize*10 {
break
}
step := 10
// delete some rows, and add some data
for i := num; i < num+step; i++ {
n := rand.Intn(num)
deletedKeys[n] = struct{}{}
sql := fmt.Sprintf("delete from test_add_index where c1 = %d", n)
s.mustExec(c, sql)
sql = fmt.Sprintf("insert into test_add_index values (%d, %d, %d)", i, i, i)
s.mustExec(c, sql)
}
num += step
}
}
// get exists keys
keys := make([]int, 0, num)
for i := start; i < num; i++ {
if _, ok := deletedKeys[i]; ok {
continue
}
keys = append(keys, i)
}
keys = append(keys, otherKeys...)
// test index key
expectedRows := make([][]interface{}, 0, len(keys))
for _, key := range keys {
expectedRows = append(expectedRows, []interface{}{key})
}
rows := s.mustQuery(c, fmt.Sprintf("select c1 from test_add_index where c3 >= %d order by c1", start))
matchRows(c, rows, expectedRows)
if testPartition {
s.tk.MustExec("admin check table test_add_index")
return
}
// test index range
for i := 0; i < 100; i++ {
index := rand.Intn(len(keys) - 3)
rows := s.mustQuery(c, "select c1 from test_add_index where c3 >= ? limit 3", keys[index])
matchRows(c, rows, [][]interface{}{{keys[index]}, {keys[index+1]}, {keys[index+2]}})
}
// TODO: Support explain in future.
// rows := s.mustQuery(c, "explain select c1 from test_add_index where c3 >= 100")
// ay := dumpRows(c, rows)
// c.Assert(strings.Contains(fmt.Sprintf("%v", ay), "c3_index"), IsTrue)
// get all row handles
ctx := s.s.(sessionctx.Context)
c.Assert(ctx.NewTxn(), IsNil)
t := s.testGetTable(c, "test_add_index")
handles := make(map[int64]struct{})
startKey := t.RecordKey(math.MinInt64)
err := t.IterRecords(ctx, startKey, t.Cols(),
func(h int64, data []types.Datum, cols []*table.Column) (bool, error) {
handles[h] = struct{}{}
return true, nil
})
c.Assert(err, IsNil)
// check in index
var nidx table.Index
for _, tidx := range t.Indices() {
if tidx.Meta().Name.L == "c3_index" {
nidx = tidx
break
}
}
// Make sure there is index with name c3_index.
c.Assert(nidx, NotNil)
c.Assert(nidx.Meta().ID, Greater, int64(0))
ctx.Txn().Rollback()
c.Assert(ctx.NewTxn(), IsNil)
defer ctx.Txn().Rollback()
it, err := nidx.SeekFirst(ctx.Txn())
c.Assert(err, IsNil)
defer it.Close()
for {
_, h, err := it.Next()
if terror.ErrorEqual(err, io.EOF) {
break
}
c.Assert(err, IsNil)
_, ok := handles[h]
c.Assert(ok, IsTrue)
delete(handles, h)
}
c.Assert(handles, HasLen, 0)
s.tk.MustExec("drop table test_add_index")
}
func (s *testDBSuite) TestDropIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("drop table if exists test_drop_index")
s.tk.MustExec("create table test_drop_index (c1 int, c2 int, c3 int, primary key(c1))")
s.tk.MustExec("create index c3_index on test_drop_index (c3)")
done := make(chan error, 1)
s.mustExec(c, "delete from test_drop_index")
num := 100
// add some rows
for i := 0; i < num; i++ {
s.mustExec(c, "insert into test_drop_index values (?, ?, ?)", i, i, i)
}
t := s.testGetTable(c, "test_drop_index")
var c3idx table.Index
for _, tidx := range t.Indices() {
if tidx.Meta().Name.L == "c3_index" {
c3idx = tidx
break
}
}
c.Assert(c3idx, NotNil)
sessionExecInGoroutine(c, s.store, "drop index c3_index on test_drop_index", done)
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
if err == nil {
break LOOP
}
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
step := 10
// delete some rows, and add some data
for i := num; i < num+step; i++ {
n := rand.Intn(num)
s.mustExec(c, "update test_drop_index set c2 = 1 where c1 = ?", n)
s.mustExec(c, "insert into test_drop_index values (?, ?, ?)", i, i, i)
}
num += step
}
}
rows := s.mustQuery(c, "explain select c1 from test_drop_index where c3 >= 0")
c.Assert(strings.Contains(fmt.Sprintf("%v", rows), "c3_index"), IsFalse)
// check in index, must no index in kv
ctx := s.s.(sessionctx.Context)
// Make sure there is no index with name c3_index.
t = s.testGetTable(c, "test_drop_index")
var nidx table.Index
for _, tidx := range t.Indices() {
if tidx.Meta().Name.L == "c3_index" {
nidx = tidx
break
}
}
c.Assert(nidx, IsNil)
idx := tables.NewIndex(t.Meta().ID, t.Meta(), c3idx.Meta())
checkDelRangeDone(c, ctx, idx)
s.tk.MustExec("drop table test_drop_index")
}
func checkDelRangeDone(c *C, ctx sessionctx.Context, idx table.Index) {
startTime := time.Now()
f := func() map[int64]struct{} {
handles := make(map[int64]struct{})
c.Assert(ctx.NewTxn(), IsNil)
defer ctx.Txn().Rollback()
it, err := idx.SeekFirst(ctx.Txn())
c.Assert(err, IsNil)
defer it.Close()
for {
_, h, err := it.Next()
if terror.ErrorEqual(err, io.EOF) {
break
}
c.Assert(err, IsNil)
handles[h] = struct{}{}
}
return handles
}
var handles map[int64]struct{}
for i := 0; i < waitForCleanDataRound; i++ {
handles = f()
if len(handles) != 0 {
time.Sleep(waitForCleanDataInterval)
} else {
break
}
}
c.Assert(handles, HasLen, 0, Commentf("take time %v", time.Since(startTime)))
}
func (s *testDBSuite) TestAddIndexWithDupCols(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
err1 := infoschema.ErrColumnExists.GenWithStackByArgs("b")
err2 := infoschema.ErrColumnExists.GenWithStackByArgs("B")
s.tk.MustExec("create table test_add_index_with_dup (a int, b int)")
_, err := s.tk.Exec("create index c on test_add_index_with_dup(b, a, b)")
c.Check(errors.Cause(err1).(*terror.Error).Equal(err), Equals, true)
_, err = s.tk.Exec("create index c on test_add_index_with_dup(b, a, B)")
c.Check(errors.Cause(err2).(*terror.Error).Equal(err), Equals, true)
_, err = s.tk.Exec("alter table test_add_index_with_dup add index c (b, a, b)")
c.Check(errors.Cause(err1).(*terror.Error).Equal(err), Equals, true)
_, err = s.tk.Exec("alter table test_add_index_with_dup add index c (b, a, B)")
c.Check(errors.Cause(err2).(*terror.Error).Equal(err), Equals, true)
s.tk.MustExec("drop table test_add_index_with_dup")
}
func (s *testDBSuite) showColumns(c *C, tableName string) [][]interface{} {
return s.mustQuery(c, fmt.Sprintf("show columns from %s", tableName))
}
func (s *testDBSuite) TestIssue2293(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table t_issue_2293 (a int)")
sql := "alter table t_issue_2293 add b int not null default 'a'"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
s.tk.MustExec("insert into t_issue_2293 value(1)")
s.tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1"))
}
func (s *testDBSuite) TestIssue6101(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
_, err := s.tk.Exec("insert into t1 values (500), (-500), (~0), (-1);")
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(tmysql.ErrWarnDataOutOfRange))
s.tk.MustExec("drop table t1")
s.tk.MustExec("set sql_mode=''")
s.tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
s.tk.MustExec("insert into t1 values (500), (-500), (~0), (-1);")
s.tk.MustQuery("select * from t1").Check(testkit.Rows("99", "0", "99", "0"))
s.tk.MustExec("drop table t1")
}
func (s *testDBSuite) TestCreateIndexType(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
sql := `CREATE TABLE test_index (
price int(5) DEFAULT '0' NOT NULL,
area varchar(40) DEFAULT '' NOT NULL,
type varchar(40) DEFAULT '' NOT NULL,
transityes set('a','b'),
shopsyes enum('Y','N') DEFAULT 'Y' NOT NULL,
schoolsyes enum('Y','N') DEFAULT 'Y' NOT NULL,
petsyes enum('Y','N') DEFAULT 'Y' NOT NULL,
KEY price (price,area,type,transityes,shopsyes,schoolsyes,petsyes));`
s.tk.MustExec(sql)
}
func (s *testDBSuite) TestIssue3833(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table issue3833 (b char(0))")
s.testErrorCode(c, "create index idx on issue3833 (b)", tmysql.ErrWrongKeyColumn)
s.testErrorCode(c, "alter table issue3833 add index idx (b)", tmysql.ErrWrongKeyColumn)
s.testErrorCode(c, "create table issue3833_2 (b char(0), index (b))", tmysql.ErrWrongKeyColumn)
}
func (s *testDBSuite) TestColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table t2 (c1 int, c2 int, c3 int)")
s.testAddColumn(c)
s.testDropColumn(c)
s.tk.MustExec("drop table t2")
}
func (s *testDBSuite) TestAddColumnTooMany(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
count := ddl.TableColumnCountLimit - 1
var cols []string
for i := 0; i < count; i++ {
cols = append(cols, fmt.Sprintf("a%d int", i))
}
createSQL := fmt.Sprintf("create table t_column_too_many (%s)", strings.Join(cols, ","))
s.tk.MustExec(createSQL)
s.tk.MustExec("alter table t_column_too_many add column a_512 int")
alterSQL := "alter table t_column_too_many add column a_513 int"
s.testErrorCode(c, alterSQL, tmysql.ErrTooManyFields)
}
func sessionExec(c *C, s kv.Storage, sql string) {
se, err := session.CreateSession4Test(s)
c.Assert(err, IsNil)
_, err = se.Execute(context.Background(), "use test_db")
c.Assert(err, IsNil)
rs, err := se.Execute(context.Background(), sql)
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
c.Assert(rs, IsNil)
se.Close()
}
func sessionExecInGoroutine(c *C, s kv.Storage, sql string, done chan error) {
execMultiSQLInGoroutine(c, s, "test_db", []string{sql}, done)
}
func execMultiSQLInGoroutine(c *C, s kv.Storage, dbName string, multiSQL []string, done chan error) {
go func() {
se, err := session.CreateSession4Test(s)
if err != nil {
done <- errors.Trace(err)
return
}
defer se.Close()
_, err = se.Execute(context.Background(), "use "+dbName)
if err != nil {
done <- errors.Trace(err)
return
}
for _, sql := range multiSQL {
rs, err := se.Execute(context.Background(), sql)
if err != nil {
done <- errors.Trace(err)
return
}
if rs != nil {
done <- errors.Errorf("RecordSet should be empty.")
return
}
done <- nil
}
}()
}
func (s *testDBSuite) testAddColumn(c *C) {
done := make(chan error, 1)
num := defaultBatchSize + 10
// add some rows
for i := 0; i < num; i++ {
s.mustExec(c, "insert into t2 values (?, ?, ?)", i, i, i)
}
sessionExecInGoroutine(c, s.store, "alter table t2 add column c4 int default -1", done)
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
step := 10
LOOP:
for {
select {
case err := <-done:
if err == nil {
break LOOP
}
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
// delete some rows, and add some data
for i := num; i < num+step; i++ {
n := rand.Intn(num)
s.tk.MustExec("begin")
s.tk.MustExec("delete from t2 where c1 = ?", n)
s.tk.MustExec("commit")
// Make sure that statement of insert and show use the same infoSchema.
s.tk.MustExec("begin")
_, err := s.tk.Exec("insert into t2 values (?, ?, ?)", i, i, i)
if err != nil {
// if err is failed, the column number must be 4 now.
values := s.showColumns(c, "t2")
c.Assert(values, HasLen, 4, Commentf("err:%v", errors.ErrorStack(err)))
}
s.tk.MustExec("commit")
}
num += step
}
}
// add data, here c4 must exist
for i := num; i < num+step; i++ {
s.tk.MustExec("insert into t2 values (?, ?, ?, ?)", i, i, i, i)
}
rows := s.mustQuery(c, "select count(c4) from t2")
c.Assert(rows, HasLen, 1)
c.Assert(rows[0], HasLen, 1)
count, err := strconv.ParseInt(rows[0][0].(string), 10, 64)
c.Assert(err, IsNil)
c.Assert(count, Greater, int64(0))
rows = s.mustQuery(c, "select count(c4) from t2 where c4 = -1")
matchRows(c, rows, [][]interface{}{{count - int64(step)}})
for i := num; i < num+step; i++ {
rows = s.mustQuery(c, "select c4 from t2 where c4 = ?", i)
matchRows(c, rows, [][]interface{}{{i}})
}
ctx := s.s.(sessionctx.Context)
t := s.testGetTable(c, "t2")
i := 0
j := 0
ctx.NewTxn()
defer ctx.Txn().Rollback()
err = t.IterRecords(ctx, t.FirstKey(), t.Cols(),
func(h int64, data []types.Datum, cols []*table.Column) (bool, error) {
i++
// c4 must be -1 or > 0
v, err1 := data[3].ToInt64(ctx.GetSessionVars().StmtCtx)
c.Assert(err1, IsNil)
if v == -1 {
j++
} else {
c.Assert(v, Greater, int64(0))
}
return true, nil
})
c.Assert(err, IsNil)
c.Assert(i, Equals, int(count))
c.Assert(i, LessEqual, num+step)
c.Assert(j, Equals, int(count)-step)
// for modifying columns after adding columns
s.tk.MustExec("alter table t2 modify c4 int default 11")
for i := num + step; i < num+step+10; i++ {
s.mustExec(c, "insert into t2 values (?, ?, ?, ?)", i, i, i, i)
}
rows = s.mustQuery(c, "select count(c4) from t2 where c4 = -1")
matchRows(c, rows, [][]interface{}{{count - int64(step)}})
// add timestamp type column
s.mustExec(c, "create table test_on_update_c (c1 int, c2 timestamp);")
s.mustExec(c, "alter table test_on_update_c add column c3 timestamp null default '2017-02-11' on update current_timestamp;")
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_on_update_c"))
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
colC := tblInfo.Columns[2]
c.Assert(colC.Tp, Equals, mysql.TypeTimestamp)
hasNotNull := tmysql.HasNotNullFlag(colC.Flag)
c.Assert(hasNotNull, IsFalse)
// add datetime type column
s.mustExec(c, "create table test_on_update_d (c1 int, c2 datetime);")
s.mustExec(c, "alter table test_on_update_d add column c3 datetime on update current_timestamp;")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_on_update_d"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC = tblInfo.Columns[2]
c.Assert(colC.Tp, Equals, mysql.TypeDatetime)
hasNotNull = tmysql.HasNotNullFlag(colC.Flag)
c.Assert(hasNotNull, IsFalse)
}
func (s *testDBSuite) testDropColumn(c *C) {
done := make(chan error, 1)
s.mustExec(c, "delete from t2")
num := 100
// add some rows
for i := 0; i < num; i++ {
s.mustExec(c, "insert into t2 values (?, ?, ?, ?)", i, i, i, i)
}
// get c4 column id
sessionExecInGoroutine(c, s.store, "alter table t2 drop column c4", done)
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
step := 10
LOOP:
for {
select {
case err := <-done:
if err == nil {
break LOOP
}
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
// delete some rows, and add some data
for i := num; i < num+step; i++ {
// Make sure that statement of insert and show use the same infoSchema.
s.tk.MustExec("begin")
_, err := s.tk.Exec("insert into t2 values (?, ?, ?)", i, i, i)
if err != nil {
// If executing is failed, the column number must be 4 now.
values := s.showColumns(c, "t2")
c.Assert(values, HasLen, 4, Commentf("err:%v", errors.ErrorStack(err)))
}
s.tk.MustExec("commit")
}
num += step
}
}
// add data, here c4 must not exist
for i := num; i < num+step; i++ {
s.mustExec(c, "insert into t2 values (?, ?, ?)", i, i, i)
}
rows := s.mustQuery(c, "select count(*) from t2")
c.Assert(rows, HasLen, 1)
c.Assert(rows[0], HasLen, 1)
count, err := strconv.ParseInt(rows[0][0].(string), 10, 64)
c.Assert(err, IsNil)
c.Assert(count, Greater, int64(0))
}
// TestDropColumn is for inserting value with a to-be-dropped column when do drop column.
// Column info from schema in build-insert-plan should be public only,
// otherwise they will not be consist with Table.Col(), then the server will panic.
func (s *testDBSuite) TestDropColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("create database drop_col_db")
s.tk.MustExec("use drop_col_db")
s.tk.MustExec("create table t2 (c1 int, c2 int, c3 int)")
num := 50
dmlDone := make(chan error, num)
ddlDone := make(chan error, num)
multiDDL := make([]string, 0, num)
for i := 0; i < num/2; i++ {
multiDDL = append(multiDDL, "alter table t2 add column c4 int", "alter table t2 drop column c4")
}
execMultiSQLInGoroutine(c, s.store, "drop_col_db", multiDDL, ddlDone)
for i := 0; i < num; i++ {
execMultiSQLInGoroutine(c, s.store, "drop_col_db", []string{"insert into t2 set c1 = 1, c2 = 1, c3 = 1, c4 = 1"}, dmlDone)
}
for i := 0; i < num; i++ {
select {
case err := <-ddlDone:
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
}
}
s.tk.MustExec("drop database drop_col_db")
}
func (s *testDBSuite) TestPrimaryKey(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table primary_key_test (a int, b varchar(10))")
_, err := s.tk.Exec("alter table primary_key_test add primary key(a)")
c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue)
_, err = s.tk.Exec("alter table primary_key_test drop primary key")
c.Assert(ddl.ErrUnsupportedModifyPrimaryKey.Equal(err), IsTrue)
}
func (s *testDBSuite) TestChangeColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table t3 (a int default '0', b varchar(10), d int not null default '0')")
s.mustExec(c, "insert into t3 set b = 'a'")
s.tk.MustQuery("select a from t3").Check(testkit.Rows("0"))
s.mustExec(c, "alter table t3 change a aa bigint")
s.mustExec(c, "insert into t3 set b = 'b'")
s.tk.MustQuery("select aa from t3").Check(testkit.Rows("0", "<nil>"))
// for no default flag
s.mustExec(c, "alter table t3 change d dd bigint not null")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
colD := tblInfo.Columns[2]
hasNoDefault := tmysql.HasNoDefaultValueFlag(colD.Flag)
c.Assert(hasNoDefault, IsTrue)
// for the following definitions: 'not null', 'null', 'default value' and 'comment'
s.mustExec(c, "alter table t3 change b b varchar(20) null default 'c' comment 'my comment'")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colB := tblInfo.Columns[1]
c.Assert(colB.Comment, Equals, "my comment")
hasNotNull := tmysql.HasNotNullFlag(colB.Flag)
c.Assert(hasNotNull, IsFalse)
s.mustExec(c, "insert into t3 set aa = 3, dd = 5")
s.tk.MustQuery("select b from t3").Check(testkit.Rows("a", "b", "c"))
// for timestamp
s.mustExec(c, "alter table t3 add column c timestamp not null")
s.mustExec(c, "alter table t3 change c c timestamp null default '2017-02-11' comment 'col c comment' on update current_timestamp")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC := tblInfo.Columns[3]
c.Assert(colC.Comment, Equals, "col c comment")
hasNotNull = tmysql.HasNotNullFlag(colC.Flag)
c.Assert(hasNotNull, IsFalse)
// for enum
s.mustExec(c, "alter table t3 add column en enum('a', 'b', 'c') not null default 'a'")
// for failing tests
sql := "alter table t3 change aa a bigint default ''"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
sql = "alter table t3 change a testx.t3.aa bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongDBName)
sql = "alter table t3 change t.a aa bigint"
s.testErrorCode(c, sql, tmysql.ErrWrongTableName)
sql = "alter table t3 change aa a bigint not null"
s.testErrorCode(c, sql, tmysql.ErrUnknown)
sql = "alter table t3 modify en enum('a', 'z', 'b', 'c') not null default 'a'"
s.testErrorCode(c, sql, tmysql.ErrUnknown)
// Rename to an existing column.
s.mustExec(c, "alter table t3 add column a bigint")
sql = "alter table t3 change aa a bigint"
s.testErrorCode(c, sql, tmysql.ErrDupFieldName)
s.tk.MustExec("drop table t3")
}
func (s *testDBSuite) TestAlterColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.mustExec(c, "create table test_alter_column (a int default 111, b varchar(8), c varchar(8) not null, d timestamp on update current_timestamp)")
s.mustExec(c, "insert into test_alter_column set b = 'a', c = 'aa'")
s.tk.MustQuery("select a from test_alter_column").Check(testkit.Rows("111"))
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_alter_column"))
c.Assert(err, IsNil)
tblInfo := tbl.Meta()
colA := tblInfo.Columns[0]
hasNoDefault := tmysql.HasNoDefaultValueFlag(colA.Flag)
c.Assert(hasNoDefault, IsFalse)
s.mustExec(c, "alter table test_alter_column alter column a set default 222")
s.mustExec(c, "insert into test_alter_column set b = 'b', c = 'bb'")
s.tk.MustQuery("select a from test_alter_column").Check(testkit.Rows("111", "222"))
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_alter_column"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colA = tblInfo.Columns[0]
hasNoDefault = tmysql.HasNoDefaultValueFlag(colA.Flag)
c.Assert(hasNoDefault, IsFalse)
s.mustExec(c, "alter table test_alter_column alter column b set default null")
s.mustExec(c, "insert into test_alter_column set c = 'cc'")
s.tk.MustQuery("select b from test_alter_column").Check(testkit.Rows("a", "b", "<nil>"))
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_alter_column"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC := tblInfo.Columns[2]
hasNoDefault = tmysql.HasNoDefaultValueFlag(colC.Flag)
c.Assert(hasNoDefault, IsTrue)
s.mustExec(c, "alter table test_alter_column alter column c set default 'xx'")
s.mustExec(c, "insert into test_alter_column set a = 123")
s.tk.MustQuery("select c from test_alter_column").Check(testkit.Rows("aa", "bb", "cc", "xx"))
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test_db"), model.NewCIStr("test_alter_column"))
c.Assert(err, IsNil)
tblInfo = tbl.Meta()
colC = tblInfo.Columns[2]
hasNoDefault = tmysql.HasNoDefaultValueFlag(colC.Flag)
c.Assert(hasNoDefault, IsFalse)
// TODO: After fix issue 2606.
// s.mustExec(c, "alter table test_alter_column alter column d set default null")
s.mustExec(c, "alter table test_alter_column alter column a drop default")
s.mustExec(c, "insert into test_alter_column set b = 'd', c = 'dd'")
s.tk.MustQuery("select a from test_alter_column").Check(testkit.Rows("111", "222", "222", "123", "<nil>"))
// for failing tests
sql := "alter table db_not_exist.test_alter_column alter column b set default 'c'"
s.testErrorCode(c, sql, tmysql.ErrNoSuchTable)
sql = "alter table test_not_exist alter column b set default 'c'"
s.testErrorCode(c, sql, tmysql.ErrNoSuchTable)
sql = "alter table test_alter_column alter column col_not_exist set default 'c'"
s.testErrorCode(c, sql, tmysql.ErrBadField)
sql = "alter table test_alter_column alter column c set default null"
s.testErrorCode(c, sql, tmysql.ErrInvalidDefault)
// The followings tests whether adding constraints via change / modify column
// is forbidden as expected.
s.mustExec(c, "drop table if exists mc")
s.mustExec(c, "create table mc(a int key, b int, c int)")
_, err = s.tk.Exec("alter table mc modify column a int key") // Adds a new primary key
c.Assert(err, NotNil)
_, err = s.tk.Exec("alter table mc modify column c int unique") // Adds a new unique key
c.Assert(err, NotNil)
result := s.tk.MustQuery("show create table mc")
createSQL := result.Rows()[0][1]
expected := "CREATE TABLE `mc` (\n `a` int(11) NOT NULL,\n `b` int(11) DEFAULT NULL,\n `c` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"
c.Assert(createSQL, Equals, expected)
// Change / modify column should preserve index options.
s.mustExec(c, "drop table if exists mc")
s.mustExec(c, "create table mc(a int key, b int, c int unique)")
s.mustExec(c, "alter table mc modify column a bigint") // NOT NULL & PRIMARY KEY should be preserved
s.mustExec(c, "alter table mc modify column b bigint")
s.mustExec(c, "alter table mc modify column c bigint") // Unique should be preserved
result = s.tk.MustQuery("show create table mc")
createSQL = result.Rows()[0][1]
expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` bigint(20) DEFAULT NULL,\n `c` bigint(20) DEFAULT NULL,\n PRIMARY KEY (`a`),\n UNIQUE KEY `c` (`c`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"
c.Assert(createSQL, Equals, expected)
// Dropping or keeping auto_increment is allowed, however adding is not allowed.
s.mustExec(c, "drop table if exists mc")
s.mustExec(c, "create table mc(a int key auto_increment, b int)")
s.mustExec(c, "alter table mc modify column a bigint auto_increment") // Keeps auto_increment
result = s.tk.MustQuery("show create table mc")
createSQL = result.Rows()[0][1]
expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL AUTO_INCREMENT,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"
s.mustExec(c, "alter table mc modify column a bigint") // Drops auto_increment
result = s.tk.MustQuery("show create table mc")
createSQL = result.Rows()[0][1]
expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"
c.Assert(createSQL, Equals, expected)
_, err = s.tk.Exec("alter table mc modify column a bigint auto_increment") // Adds auto_increment should throw error
c.Assert(err, NotNil)
}
func (s *testDBSuite) mustExec(c *C, query string, args ...interface{}) {
s.tk.MustExec(query, args...)
}
func (s *testDBSuite) mustQuery(c *C, query string, args ...interface{}) [][]interface{} {
r := s.tk.MustQuery(query, args...)
return r.Rows()
}
func matchRows(c *C, rows [][]interface{}, expected [][]interface{}) {
c.Assert(len(rows), Equals, len(expected), Commentf("got %v, expected %v", rows, expected))
for i := range rows {
match(c, rows[i], expected[i]...)
}
}
func match(c *C, row []interface{}, expected ...interface{}) {
c.Assert(len(row), Equals, len(expected))
for i := range row {
got := fmt.Sprintf("%v", row[i])
need := fmt.Sprintf("%v", expected[i])
c.Assert(got, Equals, need)
}
}
func (s *testDBSuite) TestUpdateMultipleTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.tk.MustExec("create database umt_db")
tk.MustExec("use umt_db")
tk.MustExec("create table t1 (c1 int, c2 int)")
tk.MustExec("insert t1 values (1, 1), (2, 2)")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("insert t2 values (1, 3), (2, 5)")
ctx := tk.Se.(sessionctx.Context)
dom := domain.GetDomain(ctx)
is := dom.InfoSchema()
db, ok := is.SchemaByName(model.NewCIStr("umt_db"))
c.Assert(ok, IsTrue)
t1Tbl, err := is.TableByName(model.NewCIStr("umt_db"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
t1Info := t1Tbl.Meta()
// Add a new column in write only state.
newColumn := &model.ColumnInfo{
ID: 100,
Name: model.NewCIStr("c3"),
Offset: 2,
DefaultValue: 9,
OriginDefaultValue: 9,
FieldType: *types.NewFieldType(tmysql.TypeLonglong),
State: model.StateWriteOnly,
}
t1Info.Columns = append(t1Info.Columns, newColumn)
kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
c.Assert(err, IsNil)
c.Assert(m.UpdateTable(db.ID, t1Info), IsNil)
return nil
})
err = dom.Reload()
c.Assert(err, IsNil)
tk.MustExec("update t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1")
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1", "8 2"))
tk.MustQuery("select * from t2").Check(testkit.Rows("1 10", "2 10"))
newColumn.State = model.StatePublic
kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
c.Assert(err, IsNil)
c.Assert(m.UpdateTable(db.ID, t1Info), IsNil)
return nil
})
err = dom.Reload()
c.Assert(err, IsNil)
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1 9", "8 2 9"))
tk.MustExec("drop database umt_db")
}
func (s *testDBSuite) TestCreateTableTooLarge(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
sql := "create table t_too_large ("
cnt := 3000
for i := 1; i <= cnt; i++ {
sql += fmt.Sprintf("a%d double, b%d double, c%d double, d%d double", i, i, i, i)
if i != cnt {
sql += ","
}
}
sql += ");"
s.testErrorCode(c, sql, tmysql.ErrTooManyFields)
originLimit := ddl.TableColumnCountLimit
ddl.TableColumnCountLimit = cnt * 4
_, err := s.tk.Exec(sql)
c.Assert(kv.ErrEntryTooLarge.Equal(err), IsTrue, Commentf("err:%v", err))
ddl.TableColumnCountLimit = originLimit
}
func (s *testDBSuite) TestCreateTableWithLike(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
// for the same database
s.tk.MustExec("create database ctwl_db")
s.tk.MustExec("use ctwl_db")
s.tk.MustExec("create table tt(id int primary key)")
s.tk.MustExec("create table t (c1 int not null auto_increment, c2 int, constraint cc foreign key (c2) references tt(id), primary key(c1)) auto_increment = 10")
s.tk.MustExec("insert into t set c2=1")
s.tk.MustExec("create table t1 like ctwl_db.t")
s.tk.MustExec("insert into t1 set c2=11")
s.tk.MustExec("create table t2 (like ctwl_db.t1)")
s.tk.MustExec("insert into t2 set c2=12")
s.tk.MustQuery("select * from t").Check(testkit.Rows("10 1"))
s.tk.MustQuery("select * from t1").Check(testkit.Rows("1 11"))
s.tk.MustQuery("select * from t2").Check(testkit.Rows("1 12"))
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl1, err := is.TableByName(model.NewCIStr("ctwl_db"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
tbl1Info := tbl1.Meta()
c.Assert(tbl1Info.ForeignKeys, IsNil)
c.Assert(tbl1Info.PKIsHandle, Equals, true)
col := tbl1Info.Columns[0]
hasNotNull := tmysql.HasNotNullFlag(col.Flag)
c.Assert(hasNotNull, IsTrue)
tbl2, err := is.TableByName(model.NewCIStr("ctwl_db"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
tbl2Info := tbl2.Meta()
c.Assert(tbl2Info.ForeignKeys, IsNil)
c.Assert(tbl2Info.PKIsHandle, Equals, true)
c.Assert(tmysql.HasNotNullFlag(tbl2Info.Columns[0].Flag), IsTrue)
// for different databases
s.tk.MustExec("create database ctwl_db1")
s.tk.MustExec("use ctwl_db1")
s.tk.MustExec("create table t1 like ctwl_db.t")
s.tk.MustExec("insert into t1 set c2=11")
s.tk.MustQuery("select * from t1").Check(testkit.Rows("1 11"))
is = domain.GetDomain(ctx).InfoSchema()
tbl1, err = is.TableByName(model.NewCIStr("ctwl_db1"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
c.Assert(tbl1.Meta().ForeignKeys, IsNil)
// for failure cases
failSQL := fmt.Sprintf("create table t1 like test_not_exist.t")
s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable)
failSQL = fmt.Sprintf("create table t1 like test.t_not_exist")
s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable)
failSQL = fmt.Sprintf("create table t1 (like test_not_exist.t)")
s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable)
failSQL = fmt.Sprintf("create table test_not_exis.t1 like ctwl_db.t")
s.testErrorCode(c, failSQL, tmysql.ErrBadDB)
failSQL = fmt.Sprintf("create table t1 like ctwl_db.t")
s.testErrorCode(c, failSQL, tmysql.ErrTableExists)
s.tk.MustExec("drop database ctwl_db")
s.tk.MustExec("drop database ctwl_db1")
}
func (s *testDBSuite) TestCreateTable(c *C) {
s.tk.MustExec("use test")
s.tk.MustExec("CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 );")
s.tk.MustExec("CREATE TABLE IF NOT EXISTS `t` (`a` double DEFAULT 1.0 DEFAULT now() DEFAULT 2.0 );")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
cols := tbl.Cols()
c.Assert(len(cols), Equals, 1)
col := cols[0]
c.Assert(col.Name.L, Equals, "a")
d, ok := col.DefaultValue.(string)
c.Assert(ok, IsTrue)
c.Assert(d, Equals, "2.0")
s.tk.MustExec("drop table t")
_, err = s.tk.Exec("CREATE TABLE `t` (`a` int) DEFAULT CHARSET=abcdefg")
c.Assert(err, NotNil)
}
func (s *testDBSuite) TestBitDefaultValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_bit (c1 bit(10) default 250, c2 int);")
tk.MustExec("insert into t_bit set c2=1;")
tk.MustQuery("select bin(c1),c2 from t_bit").Check(testkit.Rows("11111010 1"))
tk.MustExec("drop table t_bit")
tk.MustExec(`create table testalltypes1 (
field_1 bit default 1,
field_2 tinyint null default null
);`)
tk.MustExec(`create table testalltypes2 (
field_1 bit null default null,
field_2 tinyint null default null,
field_3 tinyint unsigned null default null,
field_4 bigint null default null,
field_5 bigint unsigned null default null,
field_6 mediumblob null default null,
field_7 longblob null default null,
field_8 blob null default null,
field_9 tinyblob null default null,
field_10 varbinary(255) null default null,
field_11 binary(255) null default null,
field_12 mediumtext null default null,
field_13 longtext null default null,
field_14 text null default null,
field_15 tinytext null default null,
field_16 char(255) null default null,
field_17 numeric null default null,
field_18 decimal null default null,
field_19 integer null default null,
field_20 integer unsigned null default null,
field_21 int null default null,
field_22 int unsigned null default null,
field_23 mediumint null default null,
field_24 mediumint unsigned null default null,
field_25 smallint null default null,
field_26 smallint unsigned null default null,
field_27 float null default null,
field_28 double null default null,
field_29 double precision null default null,
field_30 real null default null,
field_31 varchar(255) null default null,
field_32 date null default null,
field_33 time null default null,
field_34 datetime null default null,
field_35 timestamp null default null
);`)
}
func (s *testDBSuite) TestCreateTableWithPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("drop table if exists tp;")
s.tk.MustExec(`CREATE TABLE tp (a int) PARTITION BY RANGE(a) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (MAXVALUE)
);`)
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("tp"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().Partition, NotNil)
part := tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`a`")
for _, pdef := range part.Definitions {
c.Assert(pdef.ID, Greater, int64(0))
}
c.Assert(part.Definitions, HasLen, 3)
c.Assert(part.Definitions[0].LessThan[0], Equals, "10")
c.Assert(part.Definitions[0].Name.L, Equals, "p0")
c.Assert(part.Definitions[1].LessThan[0], Equals, "20")
c.Assert(part.Definitions[1].Name.L, Equals, "p1")
c.Assert(part.Definitions[2].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[2].Name.L, Equals, "p2")
s.tk.MustExec("drop table if exists employees;")
sql1 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p2 values less than (2001)
);`
s.testErrorCode(c, sql1, tmysql.ErrSameNamePartition)
sql2 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1998),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`
s.testErrorCode(c, sql2, tmysql.ErrRangeNotIncreasing)
sql3 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1998),
partition p2 values less than maxvalue,
partition p3 values less than (2001)
);`
s.testErrorCode(c, sql3, tmysql.ErrPartitionMaxvalue)
sql4 := `create table t4 (
a int not null,
b int not null
)
partition by range( id ) (
partition p1 values less than maxvalue,
partition p2 values less than (1991),
partition p3 values less than (1995)
);`
s.testErrorCode(c, sql4, tmysql.ErrPartitionMaxvalue)
_, err = s.tk.Exec(`CREATE TABLE rc (
a INT NOT NULL,
b INT NOT NULL,
c INT NOT NULL
)
partition by range columns(a,b,c) (
partition p0 values less than (10,5,1),
partition p2 values less than (50,maxvalue,10),
partition p3 values less than (65,30,13),
partition p4 values less than (maxvalue,30,40)
);`)
c.Assert(err, IsNil)
sql6 := `create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p0 values less than (6 , 10)
);`
s.testErrorCode(c, sql6, tmysql.ErrTooManyValues)
sql7 := `create table t7 (
a int not null,
b int not null
)
partition by range( id ) (
partition p1 values less than (1991),
partition p2 values less than maxvalue,
partition p3 values less than maxvalue,
partition p4 values less than (1995),
partition p5 values less than maxvalue
);`
s.testErrorCode(c, sql7, tmysql.ErrPartitionMaxvalue)
_, err = s.tk.Exec(`create table t8 (
a int not null,
b int not null
)
partition by range( id ) (
partition p1 values less than (19xx91),
partition p2 values less than maxvalue
);`)
c.Assert(ddl.ErrNotAllowedTypeInPartition.Equal(err), IsTrue)
sql9 := `create TABLE t9 (
col1 int
)
partition by range( case when col1 > 0 then 10 else 20 end ) (
partition p0 values less than (2),
partition p1 values less than (6)
);`
s.testErrorCode(c, sql9, tmysql.ErrPartitionFunctionIsNotAllowed)
s.testErrorCode(c, `create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed)
s.tk.MustExec(`create TABLE t11 (c1 int,c2 int) partition by range(c1 div c2 ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t12 (c1 int,c2 int) partition by range(c1 + c2 ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t13 (c1 int,c2 int) partition by range(c1 - c2 ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t14 (c1 int,c2 int) partition by range(c1 * c2 ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t15 (c1 int,c2 int) partition by range( abs(c1) ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t16 (c1 int) partition by range( c1) (partition p0 values less than (10));`)
s.testErrorCode(c, `create TABLE t17 (c1 int,c2 float) partition by range(c1 + c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed)
s.testErrorCode(c, `create TABLE t18 (c1 int,c2 float) partition by range( floor(c2) ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed)
s.tk.MustExec(`create TABLE t19 (c1 int,c2 float) partition by range( floor(c1) ) (partition p0 values less than (2));`)
s.tk.MustExec(`create TABLE t20 (c1 int,c2 bit(10)) partition by range(c2) (partition p0 values less than (10));`)
s.tk.MustExec(`create TABLE t21 (c1 int,c2 year) partition by range( c2 ) (partition p0 values less than (2000));`)
s.testErrorCode(c, `create TABLE t24 (c1 float) partition by range( c1 ) (partition p0 values less than (2000));`, tmysql.ErrFieldTypeNotAllowedAsPartitionField)
// test check order. The sql below have 2 problem: 1. ErrFieldTypeNotAllowedAsPartitionField 2. ErrPartitionMaxvalue , mysql will return ErrPartitionMaxvalue.
s.testErrorCode(c, `create TABLE t25 (c1 float) partition by range( c1 ) (partition p1 values less than maxvalue,partition p0 values less than (2000));`, tmysql.ErrPartitionMaxvalue)
// Fix issue 7362.
s.tk.MustExec("create table test_partition(id bigint, name varchar(255), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 PARTITION BY RANGE COLUMNS(id) (PARTITION p1 VALUES LESS THAN (10) ENGINE = InnoDB);")
// 'Less than' in partition expression could be a constant expression, notice that
// the SHOW result changed.
s.tk.MustExec(`create table t26 (a date)
partition by range(to_seconds(a))(
partition p0 values less than (to_seconds('2004-01-01')),
partition p1 values less than (to_seconds('2005-01-01')));`)
s.tk.MustQuery("show create table t26").Check(
testkit.Rows("t26 CREATE TABLE `t26` (\n `a` date DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin\nPARTITION BY RANGE ( to_seconds(`a`) ) (\n PARTITION p0 VALUES LESS THAN (63240134400),\n PARTITION p1 VALUES LESS THAN (63271756800)\n)"))
s.tk.MustExec(`create table t27 (a bigint unsigned not null)
partition by range(a) (
partition p0 values less than (10),
partition p1 values less than (100),
partition p2 values less than (1000),
partition p3 values less than (18446744073709551000),
partition p4 values less than (18446744073709551614)
);`)
s.tk.MustExec(`create table t28 (a bigint unsigned not null)
partition by range(a) (
partition p0 values less than (10),
partition p1 values less than (100),
partition p2 values less than (1000),
partition p3 values less than (18446744073709551000 + 1),
partition p4 values less than (18446744073709551000 + 10)
);`)
// Only range type partition is now supported, range columns partition only implements the parser, so it will not be checked.
// So the following SQL statements can be executed successfully.
s.tk.MustExec(`create table t29 (
a decimal
)
partition by range columns (a)
(partition p0 values less than (0));`)
}
func (s *testDBSuite) TestCreateTableWithHashPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("drop table if exists employees;")
s.tk.MustExec(`
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int,
store_id int
)
partition by hash(store_id) partitions 4;`)
s.tk.MustExec("drop table if exists employees;")
s.tk.MustExec(`
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int,
store_id int
)
partition by hash( year(hired) ) partitions 4;`)
}
func (s *testDBSuite) TestCreateTableWithKeyPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("drop table if exists tm1;")
s.tk.MustExec(`create table tm1
(
s1 char(32) primary key
)
partition by key(s1) partitions 10;`)
}
func (s *testDBSuite) TestTableDDLWithFloatType(c *C) {
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
s.testErrorCode(c, "create table t (a decimal(1, 2))", tmysql.ErrMBiggerThanD)
s.testErrorCode(c, "create table t (a float(1, 2))", tmysql.ErrMBiggerThanD)
s.testErrorCode(c, "create table t (a double(1, 2))", tmysql.ErrMBiggerThanD)
s.mustExec(c, "create table t (a double(1, 1))")
s.testErrorCode(c, "alter table t add column b decimal(1, 2)", tmysql.ErrMBiggerThanD)
// add multi columns now not support, so no case.
s.testErrorCode(c, "alter table t modify column a float(1, 4)", tmysql.ErrMBiggerThanD)
s.testErrorCode(c, "alter table t change column a aa float(1, 4)", tmysql.ErrMBiggerThanD)
s.mustExec(c, "drop table t")
}
func (s *testDBSuite) TestTableDDLWithTimeType(c *C) {
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
s.testErrorCode(c, "create table t (a time(7))", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "create table t (a datetime(7))", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "create table t (a timestamp(7))", tmysql.ErrTooBigPrecision)
_, err := s.tk.Exec("create table t (a time(-1))")
c.Assert(err, NotNil)
s.mustExec(c, "create table t (a datetime)")
s.testErrorCode(c, "alter table t add column b time(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t add column b datetime(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t add column b timestamp(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t modify column a time(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t modify column a datetime(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t modify column a timestamp(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t change column a aa time(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t change column a aa datetime(7)", tmysql.ErrTooBigPrecision)
s.testErrorCode(c, "alter table t change column a aa timestamp(7)", tmysql.ErrTooBigPrecision)
s.mustExec(c, "alter table t change column a aa datetime(0)")
s.mustExec(c, "drop table t")
}
func (s *testDBSuite) TestTruncateTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table truncate_table (c1 int, c2 int)")
tk.MustExec("insert truncate_table values (1, 1), (2, 2)")
ctx := tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("truncate_table"))
c.Assert(err, IsNil)
oldTblID := oldTblInfo.Meta().ID
tk.MustExec("truncate table truncate_table")
tk.MustExec("insert truncate_table values (3, 3), (4, 4)")
tk.MustQuery("select * from truncate_table").Check(testkit.Rows("3 3", "4 4"))
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("truncate_table"))
c.Assert(err, IsNil)
c.Assert(newTblInfo.Meta().ID, Greater, oldTblID)
// Verify that the old table data has been deleted by background worker.
tablePrefix := tablecodec.EncodeTablePrefix(oldTblID)
hasOldTableData := true
for i := 0; i < waitForCleanDataRound; i++ {
err = kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
it, err1 := txn.Seek(tablePrefix)
if err1 != nil {
return err1
}
if !it.Valid() {
hasOldTableData = false
} else {
hasOldTableData = it.Key().HasPrefix(tablePrefix)
}
it.Close()
return nil
})
c.Assert(err, IsNil)
if !hasOldTableData {
break
}
time.Sleep(waitForCleanDataInterval)
}
c.Assert(hasOldTableData, IsFalse)
}
func (s *testDBSuite) TestRenameTable(c *C) {
s.testRenameTable(c, "rename table %s to %s")
}
func (s *testDBSuite) TestAlterTableRenameTable(c *C) {
s.testRenameTable(c, "alter table %s rename to %s")
}
func (s *testDBSuite) testRenameTable(c *C, sql string) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
// for different databases
s.tk.MustExec("create table t (c1 int, c2 int)")
s.tk.MustExec("insert t values (1, 1), (2, 2)")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
oldTblID := oldTblInfo.Meta().ID
s.tk.MustExec("create database test1")
s.tk.MustExec("use test1")
s.tk.MustExec(fmt.Sprintf(sql, "test.t", "test1.t1"))
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
c.Assert(newTblInfo.Meta().ID, Equals, oldTblID)
s.tk.MustQuery("select * from t1").Check(testkit.Rows("1 1", "2 2"))
s.tk.MustExec("use test")
// Make sure t doesn't exist.
s.tk.MustExec("create table t (c1 int, c2 int)")
s.tk.MustExec("drop table t")
// for the same database
s.tk.MustExec("use test1")
s.tk.MustExec(fmt.Sprintf(sql, "t1", "t2"))
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
c.Assert(newTblInfo.Meta().ID, Equals, oldTblID)
s.tk.MustQuery("select * from t2").Check(testkit.Rows("1 1", "2 2"))
isExist := is.TableExists(model.NewCIStr("test1"), model.NewCIStr("t1"))
c.Assert(isExist, IsFalse)
s.tk.MustQuery("show tables").Check(testkit.Rows("t2"))
// for failure case
failSQL := fmt.Sprintf(sql, "test_not_exist.t", "test_not_exist.t")
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
failSQL = fmt.Sprintf(sql, "test.test_not_exist", "test.test_not_exist")
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test_not_exist.t")
s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound)
failSQL = fmt.Sprintf(sql, "test1.t2", "test_not_exist.t")
s.testErrorCode(c, failSQL, tmysql.ErrErrorOnRename)
// for the same table name
s.tk.MustExec("use test1")
s.tk.MustExec("create table if not exists t (c1 int, c2 int)")
s.tk.MustExec("create table if not exists t1 (c1 int, c2 int)")
s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t"))
s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.t1"))
s.tk.MustExec("drop database test1")
}
func (s *testDBSuite) TestRenameMultiTables(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("create table t1(id int)")
s.tk.MustExec("create table t2(id int)")
// Currently it will fail only.
sql := fmt.Sprintf("rename table t1 to t3, t2 to t4")
_, err := s.tk.Exec(sql)
c.Assert(err, NotNil)
originErr := errors.Cause(err)
c.Assert(originErr.Error(), Equals, "can't run multi schema change")
s.tk.MustExec("drop table t1, t2")
}
func (s *testDBSuite) TestAddNotNullColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test_db")
// for different databases
s.tk.MustExec("create table tnn (c1 int primary key auto_increment, c2 int)")
s.tk.MustExec("insert tnn (c2) values (0)" + strings.Repeat(",(0)", 99))
done := make(chan error, 1)
sessionExecInGoroutine(c, s.store, "alter table tnn add column c3 int not null default 3", done)
updateCnt := 0
out:
for {
select {
case err := <-done:
c.Assert(err, IsNil)
break out
default:
s.tk.MustExec("update tnn set c2 = c2 + 1 where c1 = 99")
updateCnt++
}
}
expected := fmt.Sprintf("%d %d", updateCnt, 3)
s.tk.MustQuery("select c2, c3 from tnn where c1 = 99").Check(testkit.Rows(expected))
s.tk.MustExec("drop table tnn")
}
func (s *testDBSuite) TestIssue2858And2717(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table t_issue_2858_bit (a bit(64) default b'0')")
s.tk.MustExec("insert into t_issue_2858_bit value ()")
s.tk.MustExec(`insert into t_issue_2858_bit values (100), ('10'), ('\0')`)
s.tk.MustQuery("select a+0 from t_issue_2858_bit").Check(testkit.Rows("0", "100", "12592", "0"))
s.tk.MustExec(`alter table t_issue_2858_bit alter column a set default '\0'`)
s.tk.MustExec("create table t_issue_2858_hex (a int default 0x123)")
s.tk.MustExec("insert into t_issue_2858_hex value ()")
s.tk.MustExec("insert into t_issue_2858_hex values (123), (0x321)")
s.tk.MustQuery("select a from t_issue_2858_hex").Check(testkit.Rows("291", "123", "801"))
s.tk.MustExec(`alter table t_issue_2858_hex alter column a set default 0x321`)
}
func (s *testDBSuite) TestIssue4432(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table tx (col bit(10) default 'a')")
s.tk.MustExec("insert into tx value ()")
s.tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
s.tk.MustExec("drop table tx")
s.tk.MustExec("create table tx (col bit(10) default 0x61)")
s.tk.MustExec("insert into tx value ()")
s.tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
s.tk.MustExec("drop table tx")
s.tk.MustExec("create table tx (col bit(10) default 97)")
s.tk.MustExec("insert into tx value ()")
s.tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
s.tk.MustExec("drop table tx")
s.tk.MustExec("create table tx (col bit(10) default 0b1100001)")
s.tk.MustExec("insert into tx value ()")
s.tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
s.tk.MustExec("drop table tx")
}
func (s *testDBSuite) TestChangeColumnPosition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("create table position (a int default 1, b int default 2)")
s.tk.MustExec("insert into position value ()")
s.tk.MustExec("insert into position values (3,4)")
s.tk.MustQuery("select * from position").Check(testkit.Rows("1 2", "3 4"))
s.tk.MustExec("alter table position modify column b int first")
s.tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3"))
s.tk.MustExec("insert into position value ()")
s.tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3", "<nil> 1"))
s.tk.MustExec("drop table position")
s.tk.MustExec("create table position (a int, b int, c double, d varchar(5))")
s.tk.MustExec(`insert into position value (1, 2, 3.14, 'TiDB')`)
s.tk.MustExec("alter table position modify column d varchar(5) after a")
s.tk.MustQuery("select * from position").Check(testkit.Rows("1 TiDB 2 3.14"))
s.tk.MustExec("alter table position modify column a int after c")
s.tk.MustQuery("select * from position").Check(testkit.Rows("TiDB 2 3.14 1"))
s.tk.MustExec("alter table position modify column c double first")
s.tk.MustQuery("select * from position").Check(testkit.Rows("3.14 TiDB 2 1"))
s.testErrorCode(c, "alter table position modify column b int after b", tmysql.ErrBadField)
s.tk.MustExec("drop table position")
s.tk.MustExec("create table position (a int, b int)")
s.tk.MustExec("alter table position add index t(a, b)")
s.tk.MustExec("alter table position modify column b int first")
s.tk.MustExec("insert into position value (3, 5)")
s.tk.MustQuery("select a from position where a = 3").Check(testkit.Rows())
s.tk.MustExec("alter table position change column b c int first")
s.tk.MustQuery("select * from position where c = 3").Check(testkit.Rows("3 5"))
s.testErrorCode(c, "alter table position change column c b int after c", tmysql.ErrBadField)
s.tk.MustExec("drop table position")
s.tk.MustExec("create table position (a int default 2)")
s.tk.MustExec("alter table position modify column a int default 5 first")
s.tk.MustExec("insert into position value ()")
s.tk.MustQuery("select * from position").Check(testkit.Rows("5"))
s.tk.MustExec("drop table position")
s.tk.MustExec("create table position (a int, b int)")
s.tk.MustExec("alter table position add index t(b)")
s.tk.MustExec("alter table position change column b c int first")
createSQL := s.tk.MustQuery("show create table position").Rows()[0][1]
exceptedSQL := []string{
"CREATE TABLE `position` (",
" `c` int(11) DEFAULT NULL,",
" `a` int(11) DEFAULT NULL,",
" KEY `t` (`c`)",
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
}
c.Assert(createSQL, Equals, strings.Join(exceptedSQL, "\n"))
}
func (s *testDBSuite) TestGeneratedColumnDDL(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
// Check create table with virtual generated column.
s.tk.MustExec(`CREATE TABLE test_gv_ddl(a int, b int as (a+8) virtual)`)
// Check desc table with virtual generated column.
result := s.tk.MustQuery(`DESC test_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b int(11) YES <nil> VIRTUAL GENERATED`))
// Check show create table with virtual generated column.
result = s.tk.MustQuery(`show create table test_gv_ddl`)
result.Check(testkit.Rows(
"test_gv_ddl CREATE TABLE `test_gv_ddl` (\n `a` int(11) DEFAULT NULL,\n `b` int(11) GENERATED ALWAYS AS (`a` + 8) VIRTUAL DEFAULT NULL\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
))
// Check alter table add a stored generated column.
s.tk.MustExec(`alter table test_gv_ddl add column c int as (b+2) stored`)
result = s.tk.MustQuery(`DESC test_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b int(11) YES <nil> VIRTUAL GENERATED`, `c int(11) YES <nil> STORED GENERATED`))
// Check generated expression with blanks.
s.tk.MustExec("create table table_with_gen_col_blanks (a int, b char(20) as (cast( \r\n\t a \r\n\tas char)))")
result = s.tk.MustQuery(`show create table table_with_gen_col_blanks`)
result.Check(testkit.Rows("table_with_gen_col_blanks CREATE TABLE `table_with_gen_col_blanks` (\n" +
" `a` int(11) DEFAULT NULL,\n" +
" `b` char(20) GENERATED ALWAYS AS (CAST(`a` AS CHAR)) VIRTUAL DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))
genExprTests := []struct {
stmt string
err int
}{
// drop/rename columns dependent by other column.
{`alter table test_gv_ddl drop column a`, mysql.ErrDependentByGeneratedColumn},
{`alter table test_gv_ddl change column a anew int`, mysql.ErrBadField},
// modify/change stored status of generated columns.
{`alter table test_gv_ddl modify column b bigint`, mysql.ErrUnsupportedOnGeneratedColumn},
{`alter table test_gv_ddl change column c cnew bigint as (a+100)`, mysql.ErrUnsupportedOnGeneratedColumn},
// modify/change generated columns breaking prior.
{`alter table test_gv_ddl modify column b int as (c+100)`, mysql.ErrGeneratedColumnNonPrior},
{`alter table test_gv_ddl change column b bnew int as (c+100)`, mysql.ErrGeneratedColumnNonPrior},
// refer not exist columns in generation expression.
{`create table test_gv_ddl_bad (a int, b int as (c+8))`, mysql.ErrBadField},
// refer generated columns non prior.
{`create table test_gv_ddl_bad (a int, b int as (c+1), c int as (a+1))`, mysql.ErrGeneratedColumnNonPrior},
// virtual generated columns cannot be primary key.
{`create table test_gv_ddl_bad (a int, b int, c int as (a+b) primary key)`, mysql.ErrUnsupportedOnGeneratedColumn},
{`create table test_gv_ddl_bad (a int, b int, c int as (a+b), primary key(c))`, mysql.ErrUnsupportedOnGeneratedColumn},
{`create table test_gv_ddl_bad (a int, b int, c int as (a+b), primary key(a, c))`, mysql.ErrUnsupportedOnGeneratedColumn},
}
for _, tt := range genExprTests {
s.testErrorCode(c, tt.stmt, tt.err)
}
// Check alter table modify/change generated column.
s.tk.MustExec(`alter table test_gv_ddl modify column c bigint as (b+200) stored`)
result = s.tk.MustQuery(`DESC test_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b int(11) YES <nil> VIRTUAL GENERATED`, `c bigint(20) YES <nil> STORED GENERATED`))
s.tk.MustExec(`alter table test_gv_ddl change column b b bigint as (a+100) virtual`)
result = s.tk.MustQuery(`DESC test_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b bigint(20) YES <nil> VIRTUAL GENERATED`, `c bigint(20) YES <nil> STORED GENERATED`))
s.tk.MustExec(`alter table test_gv_ddl change column c cnew bigint`)
result = s.tk.MustQuery(`DESC test_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b bigint(20) YES <nil> VIRTUAL GENERATED`, `cnew bigint(20) YES <nil> `))
}
func (s *testDBSuite) TestComment(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("drop table if exists ct, ct1")
validComment := strings.Repeat("a", 1024)
invalidComment := strings.Repeat("b", 1025)
s.tk.MustExec("create table ct (c int, d int, e int, key (c) comment '" + validComment + "')")
s.tk.MustExec("create index i on ct (d) comment '" + validComment + "'")
s.tk.MustExec("alter table ct add key (e) comment '" + validComment + "'")
s.testErrorCode(c, "create table ct1 (c int, key (c) comment '"+invalidComment+"')", tmysql.ErrTooLongIndexComment)
s.testErrorCode(c, "create index i1 on ct (d) comment '"+invalidComment+"b"+"'", tmysql.ErrTooLongIndexComment)
s.testErrorCode(c, "alter table ct add key (e) comment '"+invalidComment+"'", tmysql.ErrTooLongIndexComment)
s.tk.MustExec("set @@sql_mode=''")
s.tk.MustExec("create table ct1 (c int, d int, e int, key (c) comment '" + invalidComment + "')")
c.Assert(s.tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
s.tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1688|Comment for index 'c' is too long (max = 1024)"))
s.tk.MustExec("create index i1 on ct1 (d) comment '" + invalidComment + "b" + "'")
c.Assert(s.tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
s.tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1688|Comment for index 'i1' is too long (max = 1024)"))
s.tk.MustExec("alter table ct1 add key (e) comment '" + invalidComment + "'")
c.Assert(s.tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
s.tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1688|Comment for index 'e' is too long (max = 1024)"))
s.tk.MustExec("drop table if exists ct, ct1")
}
func (s *testDBSuite) TestRebaseAutoID(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("drop database if exists tidb;")
s.tk.MustExec("create database tidb;")
s.tk.MustExec("use tidb;")
s.tk.MustExec("create table tidb.test (a int auto_increment primary key, b int);")
s.tk.MustExec("insert tidb.test values (null, 1);")
s.tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1"))
s.tk.MustExec("alter table tidb.test auto_increment = 6000;")
s.tk.MustExec("insert tidb.test values (null, 1);")
s.tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1"))
s.tk.MustExec("alter table tidb.test auto_increment = 5;")
s.tk.MustExec("insert tidb.test values (null, 1);")
s.tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1", "11000 1"))
// Current range for table test is [11000, 15999].
// Though it does not have a tuple "a = 15999", its global next auto increment id should be 16000.
// Anyway it is not compatible with MySQL.
s.tk.MustExec("alter table tidb.test auto_increment = 12000;")
s.tk.MustExec("insert tidb.test values (null, 1);")
s.tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1", "11000 1", "16000 1"))
s.tk.MustExec("create table tidb.test2 (a int);")
s.testErrorCode(c, "alter table tidb.test2 add column b int auto_increment key, auto_increment=10;", tmysql.ErrUnknown)
}
func (s *testDBSuite) TestZeroFillCreateTable(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists abc;")
s.tk.MustExec("create table abc(y year, z tinyint(10) zerofill, primary key(y));")
is := s.dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("abc"))
c.Assert(err, IsNil)
var yearCol, zCol *model.ColumnInfo
for _, col := range tbl.Meta().Columns {
if col.Name.String() == "y" {
yearCol = col
}
if col.Name.String() == "z" {
zCol = col
}
}
c.Assert(yearCol, NotNil)
c.Assert(yearCol.Tp, Equals, mysql.TypeYear)
c.Assert(mysql.HasUnsignedFlag(yearCol.Flag), IsTrue)
c.Assert(zCol, NotNil)
c.Assert(mysql.HasUnsignedFlag(zCol.Flag), IsTrue)
}
func (s *testDBSuite) TestCheckColumnDefaultValue(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("drop table if exists text_default_text;")
s.testErrorCode(c, "create table text_default_text(c1 text not null default '');", tmysql.ErrBlobCantHaveDefault)
s.testErrorCode(c, "create table text_default_text(c1 text not null default 'scds');", tmysql.ErrBlobCantHaveDefault)
s.tk.MustExec("drop table if exists text_default_json;")
s.testErrorCode(c, "create table text_default_json(c1 json not null default '');", tmysql.ErrBlobCantHaveDefault)
s.testErrorCode(c, "create table text_default_json(c1 json not null default 'dfew555');", tmysql.ErrBlobCantHaveDefault)
s.tk.MustExec("drop table if exists text_default_blob;")
s.testErrorCode(c, "create table text_default_blob(c1 blob not null default '');", tmysql.ErrBlobCantHaveDefault)
s.testErrorCode(c, "create table text_default_blob(c1 blob not null default 'scds54');", tmysql.ErrBlobCantHaveDefault)
s.tk.MustExec("set sql_mode='';")
s.tk.MustExec("drop table if exists text_default_text;")
s.tk.MustExec("create table text_default_text(c1 text not null default '');")
s.tk.MustQuery(`show create table text_default_text`).Check(testutil.RowsWithSep("|",
"text_default_text CREATE TABLE `text_default_text` (\n"+
" `c1` text NOT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
))
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("text_default_text"))
c.Assert(err, IsNil)
c.Assert(tblInfo.Meta().Columns[0].DefaultValue, Equals, "")
s.tk.MustExec("drop table if exists text_default_blob;")
s.tk.MustExec("create table text_default_blob(c1 blob not null default '');")
s.tk.MustQuery(`show create table text_default_blob`).Check(testutil.RowsWithSep("|",
"text_default_blob CREATE TABLE `text_default_blob` (\n"+
" `c1` blob NOT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
))
is = domain.GetDomain(ctx).InfoSchema()
tblInfo, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("text_default_blob"))
c.Assert(err, IsNil)
c.Assert(tblInfo.Meta().Columns[0].DefaultValue, Equals, "")
s.tk.MustExec("drop table if exists text_default_json;")
s.tk.MustExec("create table text_default_json(c1 json not null default '');")
s.tk.MustQuery(`show create table text_default_json`).Check(testutil.RowsWithSep("|",
"text_default_json CREATE TABLE `text_default_json` (\n"+
" `c1` json NOT NULL DEFAULT 'null'\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin",
))
is = domain.GetDomain(ctx).InfoSchema()
tblInfo, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("text_default_json"))
c.Assert(err, IsNil)
c.Assert(tblInfo.Meta().Columns[0].DefaultValue, Equals, `null`)
}
func (s *testDBSuite) TestCharacterSetInColumns(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("drop database if exists varchar_test;")
s.tk.MustExec("create database varchar_test;")
s.tk.MustExec("use varchar_test")
s.tk.MustExec("drop table if exists t")
s.tk.MustExec("create table t (c1 int, s1 varchar(10), s2 text)")
s.tk.MustQuery("select count(*) from information_schema.columns where table_schema = 'varchar_test' and character_set_name != 'utf8'").Check(testkit.Rows("0"))
s.tk.MustQuery("select count(*) from information_schema.columns where table_schema = 'varchar_test' and character_set_name = 'utf8'").Check(testkit.Rows("2"))
s.tk.MustExec("drop table if exists t5")
s.tk.MustExec("create table t5(id int) charset=UTF8;")
s.tk.MustExec("drop table if exists t5")
s.tk.MustExec("create table t5(id int) charset=BINARY;")
s.tk.MustExec("drop table if exists t5")
s.tk.MustExec("create table t5(id int) charset=LATIN1;")
s.tk.MustExec("drop table if exists t5")
s.tk.MustExec("create table t5(id int) charset=ASCII;")
s.tk.MustExec("drop table if exists t5")
s.tk.MustExec("create table t5(id int) charset=UTF8MB4;")
s.tk.MustExec("drop table if exists t6")
s.tk.MustExec("create table t6(id int) charset=utf8;")
s.tk.MustExec("drop table if exists t6")
s.tk.MustExec("create table t6(id int) charset=binary;")
s.tk.MustExec("drop table if exists t6")
s.tk.MustExec("create table t6(id int) charset=latin1;")
s.tk.MustExec("drop table if exists t6")
s.tk.MustExec("create table t6(id int) charset=ascii;")
s.tk.MustExec("drop table if exists t6")
s.tk.MustExec("create table t6(id int) charset=utf8mb4;")
}
func (s *testDBSuite) TestAddNotNullColumnWhileInsertOnDupUpdate(c *C) {
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustExec("use " + s.schemaName)
tk2 := testkit.NewTestKit(c, s.store)
tk2.MustExec("use " + s.schemaName)
closeCh := make(chan bool)
wg := new(sync.WaitGroup)
wg.Add(1)
tk1.MustExec("create table nn (a int primary key, b int)")
tk1.MustExec("insert nn values (1, 1)")
var tk2Err error
go func() {
defer wg.Done()
for {
select {
case <-closeCh:
return
default:
}
_, tk2Err = tk2.Exec("insert nn (a, b) values (1, 1) on duplicate key update a = 1, b = b + 1")
if tk2Err != nil {
return
}
}
}()
tk1.MustExec("alter table nn add column c int not null default 0")
close(closeCh)
wg.Wait()
c.Assert(tk2Err, IsNil)
}
type testMaxTableRowIDContext struct {
c *C
d ddl.DDL
tbl table.Table
}
func newTestMaxTableRowIDContext(c *C, d ddl.DDL, tbl table.Table) *testMaxTableRowIDContext {
return &testMaxTableRowIDContext{
c: c,
d: d,
tbl: tbl,
}
}
func (s *testDBSuite) getMaxTableRowID(ctx *testMaxTableRowIDContext) (int64, bool) {
c := ctx.c
d := ctx.d
tbl := ctx.tbl
curVer, err := s.store.CurrentVersion()
c.Assert(err, IsNil)
maxID, emptyTable, err := d.GetTableMaxRowID(curVer.Ver, tbl.(table.PhysicalTable))
c.Assert(err, IsNil)
return maxID, emptyTable
}
func (s *testDBSuite) checkGetMaxTableRowID(ctx *testMaxTableRowIDContext, expectEmpty bool, expectMaxID int64) {
c := ctx.c
maxID, emptyTable := s.getMaxTableRowID(ctx)
c.Assert(emptyTable, Equals, expectEmpty)
c.Assert(maxID, Equals, expectMaxID)
}
func (s *testDBSuite) TestGetTableEndHandle(c *C) {
// TestGetTableEndHandle test ddl.GetTableMaxRowID method, which will return the max row id of the table.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists test_get_endhandle")
tk.MustExec("create database test_get_endhandle")
tk.MustExec("use test_get_endhandle")
// Test PK is handle.
tk.MustExec("create table t(a bigint PRIMARY KEY, b int)")
is := s.dom.InfoSchema()
d := s.dom.DDL()
tbl, err := is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t"))
c.Assert(err, IsNil)
testCtx := newTestMaxTableRowIDContext(c, d, tbl)
// test empty table
s.checkGetMaxTableRowID(testCtx, true, int64(math.MaxInt64))
tk.MustExec("insert into t values(-1, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(-1))
tk.MustExec("insert into t values(9223372036854775806, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775806))
tk.MustExec("insert into t values(9223372036854775807, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775807))
tk.MustExec("insert into t values(10, 1)")
tk.MustExec("insert into t values(102149142, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775807))
tk.MustExec("create table t1(a bigint PRIMARY KEY, b int)")
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t1 values(%v, %v)", i, i))
}
is = s.dom.InfoSchema()
testCtx.tbl, err = is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
s.checkGetMaxTableRowID(testCtx, false, int64(999))
// Test PK is not handle
tk.MustExec("create table t2(a varchar(255))")
is = s.dom.InfoSchema()
testCtx.tbl, err = is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
s.checkGetMaxTableRowID(testCtx, true, int64(math.MaxInt64))
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", i))
}
result := tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable := s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec("insert into t2 values(100000)")
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64-1))
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64))
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec("insert into t2 values(100)")
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
}
func (s *testDBSuite) TestMultiRegionGetTableEndHandle(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists test_get_endhandle")
tk.MustExec("create database test_get_endhandle")
tk.MustExec("use test_get_endhandle")
tk.MustExec("create table t(a bigint PRIMARY KEY, b int)")
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t values(%v, %v)", i, i))
}
// Get table ID for split.
dom := domain.GetDomain(tk.Se)
is := dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tblID := tbl.Meta().ID
d := s.dom.DDL()
testCtx := newTestMaxTableRowIDContext(c, d, tbl)
// Split the table.
s.cluster.SplitTable(s.mvccStore, tblID, 100)
maxID, emptyTable := s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(999))
tk.MustExec("insert into t values(10000, 1000)")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(10000))
tk.MustExec("insert into t values(-1, 1000)")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(10000))
}
func (s *testDBSuite) getHistoryDDLJob(id int64) (*model.Job, error) {
var job *model.Job
err := kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
var err1 error
job, err1 = t.GetHistoryDDLJob(id)
return errors.Trace(err1)
})
return job, errors.Trace(err)
}
func (s *testDBSuite) TestBackwardCompatibility(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists test_backward_compatibility")
defer tk.MustExec("drop database test_backward_compatibility")
tk.MustExec("use test_backward_compatibility")
tk.MustExec("create table t(a int primary key, b int)")
for i := 0; i < 200; i++ {
tk.MustExec(fmt.Sprintf("insert into t values(%v, %v)", i, i))
}
// alter table t add index idx_b(b);
is := s.dom.InfoSchema()
schemaName := model.NewCIStr("test_backward_compatibility")
tableName := model.NewCIStr("t")
schema, ok := is.SchemaByName(schemaName)
c.Assert(ok, IsTrue)
tbl, err := is.TableByName(schemaName, tableName)
c.Assert(err, IsNil)
// Split the table.
s.cluster.SplitTable(s.mvccStore, tbl.Meta().ID, 100)
unique := false
indexName := model.NewCIStr("idx_b")
idxColName := &ast.IndexColName{
Column: &ast.ColumnName{
Schema: schemaName,
Table: tableName,
Name: model.NewCIStr("b"),
},
Length: types.UnspecifiedLength,
}
idxColNames := []*ast.IndexColName{idxColName}
var indexOption *ast.IndexOption
job := &model.Job{
SchemaID: schema.ID,
TableID: tbl.Meta().ID,
Type: model.ActionAddIndex,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{unique, indexName, idxColNames, indexOption},
}
txn, err := s.store.Begin()
c.Assert(err, IsNil)
t := meta.NewMeta(txn)
job.ID, err = t.GenGlobalID()
c.Assert(err, IsNil)
job.Version = 1
job.StartTS = txn.StartTS()
// Simulate old TiDB init the add index job, old TiDB will not init the model.Job.ReorgMeta field,
// if we set job.SnapshotVer here, can simulate the behavior.
job.SnapshotVer = txn.StartTS()
err = t.EnQueueDDLJob(job)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
ticker := time.NewTicker(s.lease)
for range ticker.C {
historyJob, err := s.getHistoryDDLJob(job.ID)
c.Assert(err, IsNil)
if historyJob == nil {
continue
}
c.Assert(historyJob.Error, IsNil)
if historyJob.IsSynced() {
break
}
}
// finished add index
tk.MustExec("admin check index t idx_b")
}
func (s *testDBSuite) TestAlterTableAddPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1")
s.tk.MustExec("drop table if exists employees;")
s.tk.MustExec(`create table employees (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`)
s.tk.MustExec(`alter table employees add partition (
partition p4 values less than (2010),
partition p5 values less than MAXVALUE
);`)
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().Partition, NotNil)
part := tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "year(`hired`)")
c.Assert(part.Definitions, HasLen, 5)
c.Assert(part.Definitions[0].LessThan[0], Equals, "1991")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "1996")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p2"))
c.Assert(part.Definitions[2].LessThan[0], Equals, "2001")
c.Assert(part.Definitions[2].Name, Equals, model.NewCIStr("p3"))
c.Assert(part.Definitions[3].LessThan[0], Equals, "2010")
c.Assert(part.Definitions[3].Name, Equals, model.NewCIStr("p4"))
c.Assert(part.Definitions[4].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[4].Name, Equals, model.NewCIStr("p5"))
s.tk.MustExec("drop table if exists table1;")
s.tk.MustExec("create table table1(a int)")
sql1 := `alter table table1 add partition (
partition p1 values less than (2010),
partition p2 values less than maxvalue
);`
s.testErrorCode(c, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned)
sql2 := "alter table table1 add partition"
s.testErrorCode(c, sql2, tmysql.ErrPartitionsMustBeDefined)
s.tk.MustExec("drop table if exists table2;")
s.tk.MustExec(`create table table2 (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p2 values less than maxvalue
);`)
sql3 := `alter table table2 add partition (
partition p3 values less than (2010)
);`
s.testErrorCode(c, sql3, tmysql.ErrPartitionMaxvalue)
s.tk.MustExec("drop table if exists table3;")
s.tk.MustExec(`create table table3 (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p2 values less than (2001)
);`)
sql4 := `alter table table3 add partition (
partition p3 values less than (1993)
);`
s.testErrorCode(c, sql4, tmysql.ErrRangeNotIncreasing)
sql5 := `alter table table3 add partition (
partition p1 values less than (1993)
);`
s.testErrorCode(c, sql5, tmysql.ErrSameNamePartition)
sql6 := `alter table table3 add partition (
partition p1 values less than (1993),
partition p1 values less than (1995)
);`
s.testErrorCode(c, sql6, tmysql.ErrSameNamePartition)
sql7 := `alter table table3 add partition (
partition p4 values less than (1993),
partition p1 values less than (1995),
partition p5 values less than maxvalue
);`
s.testErrorCode(c, sql7, tmysql.ErrSameNamePartition)
}
func (s *testDBSuite) TestAlterTableDropPartition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1")
s.tk.MustExec("drop table if exists employees")
s.tk.MustExec(`create table employees (
id int not null,
hired int not null
)
partition by range( hired ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`)
s.tk.MustExec("alter table employees drop partition p3;")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("employees"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().GetPartitionInfo(), NotNil)
part := tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`hired`")
c.Assert(part.Definitions, HasLen, 2)
c.Assert(part.Definitions[0].LessThan[0], Equals, "1991")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "1996")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p2"))
s.tk.MustExec("drop table if exists table1;")
s.tk.MustExec("create table table1 (a int);")
sql1 := "alter table table1 drop partition p10;"
s.testErrorCode(c, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned)
s.tk.MustExec("drop table if exists table2;")
s.tk.MustExec(`create table table2 (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p2 values less than (1996),
partition p3 values less than (2001)
);`)
sql2 := "alter table table2 drop partition p10;"
s.testErrorCode(c, sql2, tmysql.ErrDropPartitionNonExistent)
s.tk.MustExec("drop table if exists table3;")
s.tk.MustExec(`create table table3 (
id int not null
)
partition by range( id ) (
partition p1 values less than (1991)
);`)
sql3 := "alter table table3 drop partition p1;"
s.testErrorCode(c, sql3, tmysql.ErrDropLastPartition)
s.tk.MustExec("drop table if exists table4;")
s.tk.MustExec(`create table table4 (
id int not null
)
partition by range( id ) (
partition p1 values less than (10),
partition p2 values less than (20),
partition p3 values less than MAXVALUE
);`)
s.tk.MustExec("alter table table4 drop partition p2;")
is = domain.GetDomain(ctx).InfoSchema()
tbl, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("table4"))
c.Assert(err, IsNil)
c.Assert(tbl.Meta().GetPartitionInfo(), NotNil)
part = tbl.Meta().Partition
c.Assert(part.Type, Equals, model.PartitionTypeRange)
c.Assert(part.Expr, Equals, "`id`")
c.Assert(part.Definitions, HasLen, 2)
c.Assert(part.Definitions[0].LessThan[0], Equals, "10")
c.Assert(part.Definitions[0].Name, Equals, model.NewCIStr("p1"))
c.Assert(part.Definitions[1].LessThan[0], Equals, "MAXVALUE")
c.Assert(part.Definitions[1].Name, Equals, model.NewCIStr("p3"))
s.tk.MustExec("drop table if exists tr;")
s.tk.MustExec(` create table tr(
id int, name varchar(50),
purchased date
)
partition by range( year(purchased) ) (
partition p0 values less than (1990),
partition p1 values less than (1995),
partition p2 values less than (2000),
partition p3 values less than (2005),
partition p4 values less than (2010),
partition p5 values less than (2015)
);`)
s.tk.MustExec(`INSERT INTO tr VALUES
(1, 'desk organiser', '2003-10-15'),
(2, 'alarm clock', '1997-11-05'),
(3, 'chair', '2009-03-10'),
(4, 'bookcase', '1989-01-10'),
(5, 'exercise bike', '2014-05-09'),
(6, 'sofa', '1987-06-05'),
(7, 'espresso maker', '2011-11-22'),
(8, 'aquarium', '1992-08-04'),
(9, 'study desk', '2006-09-16'),
(10, 'lava lamp', '1998-12-25');`)
result := s.tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';")
result.Check(testkit.Rows(`2 alarm clock 1997-11-05`, `10 lava lamp 1998-12-25`))
s.tk.MustExec("alter table tr drop partition p2;")
result = s.tk.MustQuery("select * from tr where purchased between '1995-01-01' and '1999-12-31';")
result.Check(testkit.Rows())
result = s.tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';")
result.Check(testkit.Rows(`5 exercise bike 2014-05-09`, `7 espresso maker 2011-11-22`))
s.tk.MustExec("alter table tr drop partition p5;")
result = s.tk.MustQuery("select * from tr where purchased between '2010-01-01' and '2014-12-31';")
result.Check(testkit.Rows())
s.tk.MustExec("alter table tr drop partition p4;")
result = s.tk.MustQuery("select * from tr where purchased between '2005-01-01' and '2009-12-31';")
result.Check(testkit.Rows())
s.tk.MustExec("drop table if exists table4;")
s.tk.MustExec(`create table table4 (
id int not null
)
partition by range( id ) (
partition Par1 values less than (1991),
partition pAR2 values less than (1992),
partition Par3 values less than (1995),
partition PaR5 values less than (1996)
);`)
s.tk.MustExec("alter table table4 drop partition Par2;")
s.tk.MustExec("alter table table4 drop partition PAR5;")
sql4 := "alter table table4 drop partition PAR0;"
s.testErrorCode(c, sql4, tmysql.ErrDropPartitionNonExistent)
}
func (s *testDBSuite) TestAddPartitionTooManyPartitions(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1")
count := ddl.PartitionCountLimit
s.tk.MustExec("drop table if exists p1;")
sql1 := `create table p1 (
id int not null
)
partition by range( id ) (`
for i := 1; i <= count; i++ {
sql1 += fmt.Sprintf("partition p%d values less than (%d),", i, i)
}
sql1 += "partition p1025 values less than (1025) );"
s.testErrorCode(c, sql1, tmysql.ErrTooManyPartitions)
s.tk.MustExec("drop table if exists p2;")
sql2 := `create table p2 (
id int not null
)
partition by range( id ) (`
for i := 1; i < count; i++ {
sql2 += fmt.Sprintf("partition p%d values less than (%d),", i, i)
}
sql2 += "partition p1024 values less than (1024) );"
s.tk.MustExec(sql2)
sql3 := `alter table p2 add partition (
partition p1025 values less than (1025)
);`
s.testErrorCode(c, sql3, tmysql.ErrTooManyPartitions)
}
func checkPartitionDelRangeDone(c *C, s *testDBSuite, partitionPrefix kv.Key) bool {
hasOldPartitionData := true
for i := 0; i < waitForCleanDataRound; i++ {
err := kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
it, err := txn.Seek(partitionPrefix)
if err != nil {
return err
}
if !it.Valid() {
hasOldPartitionData = false
} else {
hasOldPartitionData = it.Key().HasPrefix(partitionPrefix)
}
it.Close()
return nil
})
c.Assert(err, IsNil)
if !hasOldPartitionData {
break
}
time.Sleep(waitForCleanDataInterval)
}
return hasOldPartitionData
}
func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
// Test truncate common table.
s.tk.MustExec("drop table if exists t1;")
s.tk.MustExec("create table t1 (id int(11));")
for i := 0; i < 100; i++ {
s.mustExec(c, "insert into t1 values (?)", i)
}
result := s.tk.MustQuery("select count(*) from t1;")
result.Check(testkit.Rows("100"))
s.tk.MustExec("truncate table t1;")
result = s.tk.MustQuery("select count(*) from t1")
result.Check(testkit.Rows("0"))
// Test drop common table.
s.tk.MustExec("drop table if exists t2;")
s.tk.MustExec("create table t2 (id int(11));")
for i := 0; i < 100; i++ {
s.mustExec(c, "insert into t2 values (?)", i)
}
result = s.tk.MustQuery("select count(*) from t2;")
result.Check(testkit.Rows("100"))
s.tk.MustExec("drop table t2;")
s.testErrorCode(c, "select * from t2;", tmysql.ErrNoSuchTable)
// Test truncate table partition.
s.tk.MustExec("drop table if exists t3;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1;")
s.tk.MustExec(`create table t3(
id int, name varchar(50),
purchased date
)
partition by range( year(purchased) ) (
partition p0 values less than (1990),
partition p1 values less than (1995),
partition p2 values less than (2000),
partition p3 values less than (2005),
partition p4 values less than (2010),
partition p5 values less than (2015)
);`)
s.tk.MustExec(`insert into t3 values
(1, 'desk organiser', '2003-10-15'),
(2, 'alarm clock', '1997-11-05'),
(3, 'chair', '2009-03-10'),
(4, 'bookcase', '1989-01-10'),
(5, 'exercise bike', '2014-05-09'),
(6, 'sofa', '1987-06-05'),
(7, 'espresso maker', '2011-11-22'),
(8, 'aquarium', '1992-08-04'),
(9, 'study desk', '2006-09-16'),
(10, 'lava lamp', '1998-12-25');`)
result = s.tk.MustQuery("select count(*) from t3;")
result.Check(testkit.Rows("10"))
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t3"))
c.Assert(err, IsNil)
// Only one partition id test is taken here.
oldPID := oldTblInfo.Meta().Partition.Definitions[0].ID
s.tk.MustExec("truncate table t3;")
partitionPrefix := tablecodec.EncodeTablePrefix(oldPID)
hasOldPartitionData := checkPartitionDelRangeDone(c, s, partitionPrefix)
c.Assert(hasOldPartitionData, IsFalse)
// Test drop table partition.
s.tk.MustExec("drop table if exists t4;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1;")
s.tk.MustExec(`create table t4(
id int, name varchar(50),
purchased date
)
partition by range( year(purchased) ) (
partition p0 values less than (1990),
partition p1 values less than (1995),
partition p2 values less than (2000),
partition p3 values less than (2005),
partition p4 values less than (2010),
partition p5 values less than (2015)
);`)
s.tk.MustExec(`insert into t4 values
(1, 'desk organiser', '2003-10-15'),
(2, 'alarm clock', '1997-11-05'),
(3, 'chair', '2009-03-10'),
(4, 'bookcase', '1989-01-10'),
(5, 'exercise bike', '2014-05-09'),
(6, 'sofa', '1987-06-05'),
(7, 'espresso maker', '2011-11-22'),
(8, 'aquarium', '1992-08-04'),
(9, 'study desk', '2006-09-16'),
(10, 'lava lamp', '1998-12-25');`)
result = s.tk.MustQuery("select count(*) from t4; ")
result.Check(testkit.Rows("10"))
is = domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t4"))
c.Assert(err, IsNil)
// Only one partition id test is taken here.
oldPID = oldTblInfo.Meta().Partition.Definitions[1].ID
s.tk.MustExec("drop table t4;")
partitionPrefix = tablecodec.EncodeTablePrefix(oldPID)
hasOldPartitionData = checkPartitionDelRangeDone(c, s, partitionPrefix)
c.Assert(hasOldPartitionData, IsFalse)
s.testErrorCode(c, "select * from t4;", tmysql.ErrNoSuchTable)
}
func (s *testDBSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1;")
s.tk.MustExec("drop table if exists part1;")
s.tk.MustExec(`create table part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1, col2)
)
partition by range( col1 + col2 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`)
s.tk.MustExec("drop table if exists part2;")
s.tk.MustExec(`create table part2 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1, col2, col3),
unique key (col3)
)
partition by range( col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`)
s.tk.MustExec("drop table if exists part3;")
s.tk.MustExec(`create table part3 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col2)
)
partition by range( col1 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`)
s.tk.MustExec("drop table if exists part4;")
s.tk.MustExec(`create table part4 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col2),
unique key(col2)
)
partition by range( year(col2) ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`)
s.tk.MustExec("drop table if exists part5;")
s.tk.MustExec(`create table part5 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col2, col4),
unique key(col2, col1)
)
partition by range( col1 + col2 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`)
s.tk.MustExec("drop table if exists Part1;")
sql1 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1, col2)
)
partition by range( col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql1, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql2 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1),
unique key (col3)
)
partition by range( col1 + col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql2, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql3 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1),
unique key (col3)
)
partition by range( col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql3, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql4 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
unique key (col1, col2, col3),
unique key (col3)
)
partition by range( col1 + col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql4, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql5 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col2)
)
partition by range( col3 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql5, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql6 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col3),
unique key(col2)
)
partition by range( year(col2) ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql6, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists Part1;")
sql7 := `create table Part1 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
primary key(col1, col3, col4),
unique key(col2, col1)
)
partition by range( col1 + col2 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql7, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
s.tk.MustExec("drop table if exists part6;")
sql8 := `create table part6 (
col1 int not null,
col2 date not null,
col3 int not null,
col4 int not null,
col5 int not null,
unique key(col1, col2),
unique key(col1, col3)
)
partition by range( col1 + col2 ) (
partition p1 values less than (11),
partition p2 values less than (15)
);`
s.testErrorCode(c, sql8, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
}
func (s *testDBSuite) TestPartitionDropIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
done := make(chan error, 1)
s.tk.MustExec("use " + s.schemaName)
s.tk.MustExec("set @@session.tidb_enable_table_partition=1;")
s.tk.MustExec("drop table if exists partition_drop_idx;")
s.tk.MustExec(`create table partition_drop_idx (
c1 int, c2 int, c3 int
)
partition by range( c1 ) (
partition p0 values less than (3),
partition p1 values less than (5),
partition p2 values less than (7),
partition p3 values less than (11),
partition p4 values less than (15),
partition p5 values less than (20),
partition p6 values less than (maxvalue)
);`)
num := 20
for i := 0; i < num; i++ {
s.mustExec(c, "insert into partition_drop_idx values (?, ?, ?)", i, i, i)
}
s.tk.MustExec("alter table partition_drop_idx add index idx1 (c1)")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
t, err := is.TableByName(model.NewCIStr(s.schemaName), model.NewCIStr("partition_drop_idx"))
c.Assert(err, IsNil)
var idx1 table.Index
for _, pidx := range t.Indices() {
if pidx.Meta().Name.L == "idx1" {
idx1 = pidx
break
}
}
c.Assert(idx1, NotNil)
sessionExecInGoroutine(c, s.store, "drop index idx1 on partition_drop_idx;", done)
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err = <-done:
if err == nil {
break LOOP
}
c.Assert(err, IsNil, Commentf("err:%v", errors.ErrorStack(err)))
case <-ticker.C:
step := 10
rand.Seed(time.Now().Unix())
for i := num; i < num+step; i++ {
n := rand.Intn(num)
s.mustExec(c, "update partition_drop_idx set c2 = 1 where c1 = ?", n)
s.mustExec(c, "insert into partition_drop_idx values (?, ?, ?)", i, i, i)
}
num += step
}
}
is = domain.GetDomain(ctx).InfoSchema()
t, err = is.TableByName(model.NewCIStr(s.schemaName), model.NewCIStr("partition_drop_idx"))
c.Assert(err, IsNil)
// Only one partition id test is taken here.
pid := t.Meta().Partition.Definitions[0].ID
var idxn table.Index
t.Indices()
for _, idx := range t.Indices() {
if idx.Meta().Name.L == "idx1" {
idxn = idx
break
}
}
c.Assert(idxn, IsNil)
idx := tables.NewIndex(pid, t.Meta(), idx1.Meta())
checkDelRangeDone(c, ctx, idx)
s.tk.MustExec("drop table partition_drop_idx;")
}
func (s *testDBSuite) TestPartitionCancelAddIndex(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.mustExec(c, "use test_db")
s.mustExec(c, "set @@session.tidb_enable_table_partition=1;")
s.mustExec(c, "drop table if exists t1;")
s.mustExec(c, `create table t1 (
c1 int, c2 int, c3 int
)
partition by range( c1 ) (
partition p0 values less than (1024),
partition p1 values less than (2048),
partition p2 values less than (3072),
partition p3 values less than (4096),
partition p4 values less than (maxvalue)
);`)
base := defaultBatchSize * 2
count := base
// add some rows
for i := 0; i < count; i++ {
s.mustExec(c, "insert into t1 values (?, ?, ?)", i, i, i)
}
var checkErr error
var c3IdxInfo *model.IndexInfo
hook := &ddl.TestDDLCallback{}
oldReorgWaitTimeout := ddl.ReorgWaitTimeout
ddl.ReorgWaitTimeout = 10 * time.Millisecond
hook.OnJobUpdatedExported, c3IdxInfo = backgroundExecOnJobUpdatedExported(c, s, hook, checkErr)
s.dom.DDL().(ddl.DDLForTest).SetHook(hook)
done := make(chan error, 1)
go backgroundExec(s.store, "create index c3_index on t1 (c3)", done)
times := 0
ticker := time.NewTicker(s.lease / 2)
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
c.Assert(checkErr, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:12]cancelled DDL job")
break LOOP
case <-ticker.C:
if times >= 10 {
break
}
step := 10
rand.Seed(time.Now().Unix())
// delete some rows, and add some data
for i := count; i < count+step; i++ {
n := rand.Intn(count)
s.mustExec(c, "delete from t1 where c1 = ?", n)
s.mustExec(c, "insert into t1 values (?, ?, ?)", i+10, i, i)
}
count += step
times++
}
}
t := s.testGetTable(c, "t1")
// Only one partition id test is taken here.
pid := t.Meta().Partition.Definitions[0].ID
for _, tidx := range t.Indices() {
c.Assert(strings.EqualFold(tidx.Meta().Name.L, "c3_index"), IsFalse)
}
ctx := s.s.(sessionctx.Context)
idx := tables.NewIndex(pid, t.Meta(), c3IdxInfo)
checkDelRangeDone(c, ctx, idx)
s.mustExec(c, "drop table t1")
ddl.ReorgWaitTimeout = oldReorgWaitTimeout
callback := &ddl.TestDDLCallback{}
s.dom.DDL().(ddl.DDLForTest).SetHook(callback)
}
func backgroundExecOnJobUpdatedExported(c *C, s *testDBSuite, hook *ddl.TestDDLCallback, checkErr error) (func(*model.Job), *model.IndexInfo) {
first := true
ddl.ReorgWaitTimeout = 10 * time.Millisecond
c3IdxInfo := &model.IndexInfo{}
hook.OnJobUpdatedExported = func(job *model.Job) {
addIndexNotFirstReorg := job.Type == model.ActionAddIndex && job.SchemaState == model.StateWriteReorganization && job.SnapshotVer != 0
// If the action is adding index and the state is writing reorganization, it want to test the case of cancelling the job when backfilling indexes.
// When the job satisfies this case of addIndexNotFirstReorg, the worker will start to backfill indexes.
if !addIndexNotFirstReorg {
// Get the index's meta.
if c3IdxInfo != nil {
return
}
t := s.testGetTable(c, "t1")
for _, index := range t.WritableIndices() {
if index.Meta().Name.L == "c3_index" {
c3IdxInfo = index.Meta()
}
}
return
}
// The job satisfies the case of addIndexNotFirst for the first time, the worker hasn't finished a batch of backfill indexes.
if first {
first = false
return
}
if checkErr != nil {
return
}
hookCtx := mock.NewContext()
hookCtx.Store = s.store
var err error
err = hookCtx.NewTxn()
if err != nil {
checkErr = errors.Trace(err)
return
}
jobIDs := []int64{job.ID}
errs, err := admin.CancelJobs(hookCtx.Txn(), jobIDs)
if err != nil {
checkErr = errors.Trace(err)
return
}
// It only tests cancel one DDL job.
if errs[0] != nil {
checkErr = errors.Trace(errs[0])
return
}
err = hookCtx.Txn().Commit(context.Background())
if err != nil {
checkErr = errors.Trace(err)
}
}
return hook.OnJobUpdatedExported, c3IdxInfo
}
func (s *testDBSuite) TestPartitionAddIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@session.tidb_enable_table_partition=1;")
tk.MustExec(`create table partition_add_idx (
id int not null,
hired date not null
)
partition by range( year(hired) ) (
partition p1 values less than (1991),
partition p3 values less than (2001),
partition p4 values less than (2004),
partition p5 values less than (2008),
partition p6 values less than (2012),
partition p7 values less than (2018)
);`)
for i := 0; i < 500; i++ {
tk.MustExec(fmt.Sprintf("insert into partition_add_idx values (%d, '%d-01-01')", i, 1988+rand.Intn(30)))
}
tk.MustExec("alter table partition_add_idx add index idx1 (hired)")
tk.MustExec("alter table partition_add_idx add index idx2 (id, hired)")
ctx := s.tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
t, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("partition_add_idx"))
c.Assert(err, IsNil)
var idx1 table.Index
for _, idx := range t.Indices() {
if idx.Meta().Name.L == "idx1" {
idx1 = idx
break
}
}
c.Assert(idx1, NotNil)
tk.MustQuery("select count(hired) from partition_add_idx use index(idx1)").Check(testkit.Rows("500"))
tk.MustQuery("select count(id) from partition_add_idx use index(idx2)").Check(testkit.Rows("500"))
tk.MustExec("admin check table partition_add_idx")
tk.MustExec("drop table partition_add_idx")
}

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/hanchuanchuan-goInception.git
git@gitlife.ru:oschina-mirror/hanchuanchuan-goInception.git
oschina-mirror
hanchuanchuan-goInception
hanchuanchuan-goInception
v1.2.2