dml.go 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283
  1. // Copyright 2015 PingCAP, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package ast
  14. import (
  15. "github.com/pingcap/errors"
  16. "github.com/pingcap/parser/auth"
  17. "github.com/pingcap/parser/format"
  18. "github.com/pingcap/parser/model"
  19. "github.com/pingcap/parser/mysql"
  20. )
  21. var (
  22. _ DMLNode = &DeleteStmt{}
  23. _ DMLNode = &InsertStmt{}
  24. _ DMLNode = &SetOprStmt{}
  25. _ DMLNode = &UpdateStmt{}
  26. _ DMLNode = &SelectStmt{}
  27. _ DMLNode = &CallStmt{}
  28. _ DMLNode = &ShowStmt{}
  29. _ DMLNode = &LoadDataStmt{}
  30. _ DMLNode = &SplitRegionStmt{}
  31. _ Node = &Assignment{}
  32. _ Node = &ByItem{}
  33. _ Node = &FieldList{}
  34. _ Node = &GroupByClause{}
  35. _ Node = &HavingClause{}
  36. _ Node = &Join{}
  37. _ Node = &Limit{}
  38. _ Node = &OnCondition{}
  39. _ Node = &OrderByClause{}
  40. _ Node = &SelectField{}
  41. _ Node = &TableName{}
  42. _ Node = &TableRefsClause{}
  43. _ Node = &TableSource{}
  44. _ Node = &SetOprSelectList{}
  45. _ Node = &WildCardField{}
  46. _ Node = &WindowSpec{}
  47. _ Node = &PartitionByClause{}
  48. _ Node = &FrameClause{}
  49. _ Node = &FrameBound{}
  50. )
  51. // JoinType is join type, including cross/left/right/full.
  52. type JoinType int
  53. const (
  54. // CrossJoin is cross join type.
  55. CrossJoin JoinType = iota + 1
  56. // LeftJoin is left Join type.
  57. LeftJoin
  58. // RightJoin is right Join type.
  59. RightJoin
  60. )
  61. // Join represents table join.
  62. type Join struct {
  63. node
  64. // Left table can be TableSource or JoinNode.
  65. Left ResultSetNode
  66. // Right table can be TableSource or JoinNode or nil.
  67. Right ResultSetNode
  68. // Tp represents join type.
  69. Tp JoinType
  70. // On represents join on condition.
  71. On *OnCondition
  72. // Using represents join using clause.
  73. Using []*ColumnName
  74. // NaturalJoin represents join is natural join.
  75. NaturalJoin bool
  76. // StraightJoin represents a straight join.
  77. StraightJoin bool
  78. ExplicitParens bool
  79. }
  80. // NewCrossJoin builds a cross join without `on` or `using` clause.
  81. // If the right child is a join tree, we need to handle it differently to make the precedence get right.
  82. // Here is the example: t1 join t2 join t3
  83. // JOIN ON t2.a = t3.a
  84. // t1 join / \
  85. // t2 t3
  86. // (left) (right)
  87. //
  88. // We can not build it directly to:
  89. // JOIN
  90. // / \
  91. // t1 JOIN ON t2.a = t3.a
  92. // / \
  93. // t2 t3
  94. // The precedence would be t1 join (t2 join t3 on t2.a=t3.a), not (t1 join t2) join t3 on t2.a=t3.a
  95. // We need to find the left-most child of the right child, and build a cross join of the left-hand side
  96. // of the left child(t1), and the right hand side with the original left-most child of the right child(t2).
  97. // JOIN t2.a = t3.a
  98. // / \
  99. // JOIN t3
  100. // / \
  101. // t1 t2
  102. // Besides, if the right handle side join tree's join type is right join and has explicit parentheses, we need to rewrite it to left join.
  103. // So t1 join t2 right join t3 would be rewrite to t1 join t3 left join t2.
  104. // If not, t1 join (t2 right join t3) would be (t1 join t2) right join t3. After rewrite the right join to left join.
  105. // We get (t1 join t3) left join t2, the semantics is correct.
  106. func NewCrossJoin(left, right ResultSetNode) (n *Join) {
  107. rj, ok := right.(*Join)
  108. if !ok || rj.Right == nil {
  109. return &Join{Left: left, Right: right, Tp: CrossJoin}
  110. }
  111. var leftMostLeafFatherOfRight = rj
  112. // Walk down the right hand side.
  113. for {
  114. if leftMostLeafFatherOfRight.Tp == RightJoin && leftMostLeafFatherOfRight.ExplicitParens {
  115. // Rewrite right join to left join.
  116. tmpChild := leftMostLeafFatherOfRight.Right
  117. leftMostLeafFatherOfRight.Right = leftMostLeafFatherOfRight.Left
  118. leftMostLeafFatherOfRight.Left = tmpChild
  119. leftMostLeafFatherOfRight.Tp = LeftJoin
  120. }
  121. leftChild := leftMostLeafFatherOfRight.Left
  122. if join, ok := leftChild.(*Join); ok && join.Right != nil {
  123. leftMostLeafFatherOfRight = join
  124. } else {
  125. break
  126. }
  127. }
  128. newCrossJoin := &Join{Left: left, Right: leftMostLeafFatherOfRight.Left, Tp: CrossJoin}
  129. leftMostLeafFatherOfRight.Left = newCrossJoin
  130. return rj
  131. }
  132. // Restore implements Node interface.
  133. func (n *Join) Restore(ctx *format.RestoreCtx) error {
  134. useCommaJoin := false
  135. _, leftIsJoin := n.Left.(*Join)
  136. if leftIsJoin && n.Left.(*Join).Right == nil {
  137. if ts, ok := n.Left.(*Join).Left.(*TableSource); ok {
  138. switch ts.Source.(type) {
  139. case *SelectStmt, *SetOprStmt:
  140. useCommaJoin = true
  141. }
  142. }
  143. }
  144. if leftIsJoin && !useCommaJoin {
  145. ctx.WritePlain("(")
  146. }
  147. if err := n.Left.Restore(ctx); err != nil {
  148. return errors.Annotate(err, "An error occurred while restore Join.Left")
  149. }
  150. if leftIsJoin && !useCommaJoin {
  151. ctx.WritePlain(")")
  152. }
  153. if n.Right == nil {
  154. return nil
  155. }
  156. if n.NaturalJoin {
  157. ctx.WriteKeyWord(" NATURAL")
  158. }
  159. switch n.Tp {
  160. case LeftJoin:
  161. ctx.WriteKeyWord(" LEFT")
  162. case RightJoin:
  163. ctx.WriteKeyWord(" RIGHT")
  164. }
  165. if n.StraightJoin {
  166. ctx.WriteKeyWord(" STRAIGHT_JOIN ")
  167. } else {
  168. if useCommaJoin {
  169. ctx.WritePlain(", ")
  170. } else {
  171. ctx.WriteKeyWord(" JOIN ")
  172. }
  173. }
  174. _, rightIsJoin := n.Right.(*Join)
  175. if rightIsJoin {
  176. ctx.WritePlain("(")
  177. }
  178. if err := n.Right.Restore(ctx); err != nil {
  179. return errors.Annotate(err, "An error occurred while restore Join.Right")
  180. }
  181. if rightIsJoin {
  182. ctx.WritePlain(")")
  183. }
  184. if n.On != nil {
  185. ctx.WritePlain(" ")
  186. if err := n.On.Restore(ctx); err != nil {
  187. return errors.Annotate(err, "An error occurred while restore Join.On")
  188. }
  189. }
  190. if len(n.Using) != 0 {
  191. ctx.WriteKeyWord(" USING ")
  192. ctx.WritePlain("(")
  193. for i, v := range n.Using {
  194. if i != 0 {
  195. ctx.WritePlain(",")
  196. }
  197. if err := v.Restore(ctx); err != nil {
  198. return errors.Annotate(err, "An error occurred while restore Join.Using")
  199. }
  200. }
  201. ctx.WritePlain(")")
  202. }
  203. return nil
  204. }
  205. // Accept implements Node Accept interface.
  206. func (n *Join) Accept(v Visitor) (Node, bool) {
  207. newNode, skipChildren := v.Enter(n)
  208. if skipChildren {
  209. return v.Leave(newNode)
  210. }
  211. n = newNode.(*Join)
  212. node, ok := n.Left.Accept(v)
  213. if !ok {
  214. return n, false
  215. }
  216. n.Left = node.(ResultSetNode)
  217. if n.Right != nil {
  218. node, ok = n.Right.Accept(v)
  219. if !ok {
  220. return n, false
  221. }
  222. n.Right = node.(ResultSetNode)
  223. }
  224. if n.On != nil {
  225. node, ok = n.On.Accept(v)
  226. if !ok {
  227. return n, false
  228. }
  229. n.On = node.(*OnCondition)
  230. }
  231. return v.Leave(n)
  232. }
  233. // TableName represents a table name.
  234. type TableName struct {
  235. node
  236. Schema model.CIStr
  237. Name model.CIStr
  238. DBInfo *model.DBInfo
  239. TableInfo *model.TableInfo
  240. IndexHints []*IndexHint
  241. PartitionNames []model.CIStr
  242. TableSample *TableSample
  243. }
  244. // Restore implements Node interface.
  245. func (n *TableName) restoreName(ctx *format.RestoreCtx) {
  246. if n.Schema.String() != "" {
  247. ctx.WriteName(n.Schema.String())
  248. ctx.WritePlain(".")
  249. } else if ctx.DefaultDB != "" {
  250. ctx.WriteName(ctx.DefaultDB)
  251. ctx.WritePlain(".")
  252. }
  253. ctx.WriteName(n.Name.String())
  254. }
  255. func (n *TableName) restorePartitions(ctx *format.RestoreCtx) {
  256. if len(n.PartitionNames) > 0 {
  257. ctx.WriteKeyWord(" PARTITION")
  258. ctx.WritePlain("(")
  259. for i, v := range n.PartitionNames {
  260. if i != 0 {
  261. ctx.WritePlain(", ")
  262. }
  263. ctx.WriteName(v.String())
  264. }
  265. ctx.WritePlain(")")
  266. }
  267. }
  268. func (n *TableName) restoreIndexHints(ctx *format.RestoreCtx) error {
  269. for _, value := range n.IndexHints {
  270. ctx.WritePlain(" ")
  271. if err := value.Restore(ctx); err != nil {
  272. return errors.Annotate(err, "An error occurred while splicing IndexHints")
  273. }
  274. }
  275. return nil
  276. }
  277. func (n *TableName) Restore(ctx *format.RestoreCtx) error {
  278. n.restoreName(ctx)
  279. n.restorePartitions(ctx)
  280. if err := n.restoreIndexHints(ctx); err != nil {
  281. return err
  282. }
  283. if n.TableSample != nil {
  284. ctx.WritePlain(" ")
  285. if err := n.TableSample.Restore(ctx); err != nil {
  286. return errors.Annotate(err, "An error occurred while splicing TableName.TableSample")
  287. }
  288. }
  289. return nil
  290. }
  291. // IndexHintType is the type for index hint use, ignore or force.
  292. type IndexHintType int
  293. // IndexHintUseType values.
  294. const (
  295. HintUse IndexHintType = iota + 1
  296. HintIgnore
  297. HintForce
  298. )
  299. // IndexHintScope is the type for index hint for join, order by or group by.
  300. type IndexHintScope int
  301. // Index hint scopes.
  302. const (
  303. HintForScan IndexHintScope = iota + 1
  304. HintForJoin
  305. HintForOrderBy
  306. HintForGroupBy
  307. )
  308. // IndexHint represents a hint for optimizer to use/ignore/force for join/order by/group by.
  309. type IndexHint struct {
  310. IndexNames []model.CIStr
  311. HintType IndexHintType
  312. HintScope IndexHintScope
  313. }
  314. // IndexHint Restore (The const field uses switch to facilitate understanding)
  315. func (n *IndexHint) Restore(ctx *format.RestoreCtx) error {
  316. indexHintType := ""
  317. switch n.HintType {
  318. case HintUse:
  319. indexHintType = "USE INDEX"
  320. case HintIgnore:
  321. indexHintType = "IGNORE INDEX"
  322. case HintForce:
  323. indexHintType = "FORCE INDEX"
  324. default: // Prevent accidents
  325. return errors.New("IndexHintType has an error while matching")
  326. }
  327. indexHintScope := ""
  328. switch n.HintScope {
  329. case HintForScan:
  330. indexHintScope = ""
  331. case HintForJoin:
  332. indexHintScope = " FOR JOIN"
  333. case HintForOrderBy:
  334. indexHintScope = " FOR ORDER BY"
  335. case HintForGroupBy:
  336. indexHintScope = " FOR GROUP BY"
  337. default: // Prevent accidents
  338. return errors.New("IndexHintScope has an error while matching")
  339. }
  340. ctx.WriteKeyWord(indexHintType)
  341. ctx.WriteKeyWord(indexHintScope)
  342. ctx.WritePlain(" (")
  343. for i, value := range n.IndexNames {
  344. if i > 0 {
  345. ctx.WritePlain(", ")
  346. }
  347. ctx.WriteName(value.O)
  348. }
  349. ctx.WritePlain(")")
  350. return nil
  351. }
  352. // Accept implements Node Accept interface.
  353. func (n *TableName) Accept(v Visitor) (Node, bool) {
  354. newNode, skipChildren := v.Enter(n)
  355. if skipChildren {
  356. return v.Leave(newNode)
  357. }
  358. n = newNode.(*TableName)
  359. if n.TableSample != nil {
  360. newTs, ok := n.TableSample.Accept(v)
  361. if !ok {
  362. return n, false
  363. }
  364. n.TableSample = newTs.(*TableSample)
  365. }
  366. return v.Leave(n)
  367. }
  368. // DeleteTableList is the tablelist used in delete statement multi-table mode.
  369. type DeleteTableList struct {
  370. node
  371. Tables []*TableName
  372. }
  373. // Restore implements Node interface.
  374. func (n *DeleteTableList) Restore(ctx *format.RestoreCtx) error {
  375. for i, t := range n.Tables {
  376. if i != 0 {
  377. ctx.WritePlain(",")
  378. }
  379. if err := t.Restore(ctx); err != nil {
  380. return errors.Annotatef(err, "An error occurred while restore DeleteTableList.Tables[%v]", i)
  381. }
  382. }
  383. return nil
  384. }
  385. // Accept implements Node Accept interface.
  386. func (n *DeleteTableList) Accept(v Visitor) (Node, bool) {
  387. newNode, skipChildren := v.Enter(n)
  388. if skipChildren {
  389. return v.Leave(newNode)
  390. }
  391. n = newNode.(*DeleteTableList)
  392. if n != nil {
  393. for i, t := range n.Tables {
  394. node, ok := t.Accept(v)
  395. if !ok {
  396. return n, false
  397. }
  398. n.Tables[i] = node.(*TableName)
  399. }
  400. }
  401. return v.Leave(n)
  402. }
  403. // OnCondition represents JOIN on condition.
  404. type OnCondition struct {
  405. node
  406. Expr ExprNode
  407. }
  408. // Restore implements Node interface.
  409. func (n *OnCondition) Restore(ctx *format.RestoreCtx) error {
  410. ctx.WriteKeyWord("ON ")
  411. if err := n.Expr.Restore(ctx); err != nil {
  412. return errors.Annotate(err, "An error occurred while restore OnCondition.Expr")
  413. }
  414. return nil
  415. }
  416. // Accept implements Node Accept interface.
  417. func (n *OnCondition) Accept(v Visitor) (Node, bool) {
  418. newNode, skipChildren := v.Enter(n)
  419. if skipChildren {
  420. return v.Leave(newNode)
  421. }
  422. n = newNode.(*OnCondition)
  423. node, ok := n.Expr.Accept(v)
  424. if !ok {
  425. return n, false
  426. }
  427. n.Expr = node.(ExprNode)
  428. return v.Leave(n)
  429. }
  430. // TableSource represents table source with a name.
  431. type TableSource struct {
  432. node
  433. // Source is the source of the data, can be a TableName,
  434. // a SelectStmt, a SetOprStmt, or a JoinNode.
  435. Source ResultSetNode
  436. // AsName is the alias name of the table source.
  437. AsName model.CIStr
  438. }
  439. // Restore implements Node interface.
  440. func (n *TableSource) Restore(ctx *format.RestoreCtx) error {
  441. needParen := false
  442. switch n.Source.(type) {
  443. case *SelectStmt, *SetOprStmt:
  444. needParen = true
  445. }
  446. if tn, tnCase := n.Source.(*TableName); tnCase {
  447. if needParen {
  448. ctx.WritePlain("(")
  449. }
  450. tn.restoreName(ctx)
  451. tn.restorePartitions(ctx)
  452. if asName := n.AsName.String(); asName != "" {
  453. ctx.WriteKeyWord(" AS ")
  454. ctx.WriteName(asName)
  455. }
  456. if err := tn.restoreIndexHints(ctx); err != nil {
  457. return errors.Annotate(err, "An error occurred while restore TableSource.Source.(*TableName).IndexHints")
  458. }
  459. if tn.TableSample != nil {
  460. ctx.WritePlain(" ")
  461. if err := tn.TableSample.Restore(ctx); err != nil {
  462. return errors.Annotate(err, "An error occurred while splicing TableName.TableSample")
  463. }
  464. }
  465. if needParen {
  466. ctx.WritePlain(")")
  467. }
  468. } else {
  469. if needParen {
  470. ctx.WritePlain("(")
  471. }
  472. if err := n.Source.Restore(ctx); err != nil {
  473. return errors.Annotate(err, "An error occurred while restore TableSource.Source")
  474. }
  475. if needParen {
  476. ctx.WritePlain(")")
  477. }
  478. if asName := n.AsName.String(); asName != "" {
  479. ctx.WriteKeyWord(" AS ")
  480. ctx.WriteName(asName)
  481. }
  482. }
  483. return nil
  484. }
  485. // Accept implements Node Accept interface.
  486. func (n *TableSource) Accept(v Visitor) (Node, bool) {
  487. newNode, skipChildren := v.Enter(n)
  488. if skipChildren {
  489. return v.Leave(newNode)
  490. }
  491. n = newNode.(*TableSource)
  492. node, ok := n.Source.Accept(v)
  493. if !ok {
  494. return n, false
  495. }
  496. n.Source = node.(ResultSetNode)
  497. return v.Leave(n)
  498. }
  499. // SelectLockType is the lock type for SelectStmt.
  500. type SelectLockType int
  501. // Select lock types.
  502. const (
  503. SelectLockNone SelectLockType = iota
  504. SelectLockForUpdate
  505. SelectLockForShare
  506. SelectLockForUpdateNoWait
  507. SelectLockForUpdateWaitN
  508. SelectLockForShareNoWait
  509. SelectLockForUpdateSkipLocked
  510. SelectLockForShareSkipLocked
  511. )
  512. type SelectLockInfo struct {
  513. LockType SelectLockType
  514. WaitSec uint64
  515. }
  516. // String implements fmt.Stringer.
  517. func (n SelectLockType) String() string {
  518. switch n {
  519. case SelectLockNone:
  520. return "none"
  521. case SelectLockForUpdate:
  522. return "for update"
  523. case SelectLockForShare:
  524. return "for share"
  525. case SelectLockForUpdateNoWait:
  526. return "for update nowait"
  527. case SelectLockForUpdateWaitN:
  528. return "for update wait"
  529. case SelectLockForShareNoWait:
  530. return "for share nowait"
  531. case SelectLockForUpdateSkipLocked:
  532. return "for update skip locked"
  533. case SelectLockForShareSkipLocked:
  534. return "for share skip locked"
  535. }
  536. return "unsupported select lock type"
  537. }
  538. // WildCardField is a special type of select field content.
  539. type WildCardField struct {
  540. node
  541. Table model.CIStr
  542. Schema model.CIStr
  543. }
  544. // Restore implements Node interface.
  545. func (n *WildCardField) Restore(ctx *format.RestoreCtx) error {
  546. if schema := n.Schema.String(); schema != "" {
  547. ctx.WriteName(schema)
  548. ctx.WritePlain(".")
  549. }
  550. if table := n.Table.String(); table != "" {
  551. ctx.WriteName(table)
  552. ctx.WritePlain(".")
  553. }
  554. ctx.WritePlain("*")
  555. return nil
  556. }
  557. // Accept implements Node Accept interface.
  558. func (n *WildCardField) Accept(v Visitor) (Node, bool) {
  559. newNode, skipChildren := v.Enter(n)
  560. if skipChildren {
  561. return v.Leave(newNode)
  562. }
  563. n = newNode.(*WildCardField)
  564. return v.Leave(n)
  565. }
  566. // SelectField represents fields in select statement.
  567. // There are two type of select field: wildcard
  568. // and expression with optional alias name.
  569. type SelectField struct {
  570. node
  571. // Offset is used to get original text.
  572. Offset int
  573. // WildCard is not nil, Expr will be nil.
  574. WildCard *WildCardField
  575. // Expr is not nil, WildCard will be nil.
  576. Expr ExprNode
  577. // AsName is alias name for Expr.
  578. AsName model.CIStr
  579. // Auxiliary stands for if this field is auxiliary.
  580. // When we add a Field into SelectField list which is used for having/orderby clause but the field is not in select clause,
  581. // we should set its Auxiliary to true. Then the TrimExec will trim the field.
  582. Auxiliary bool
  583. }
  584. // Restore implements Node interface.
  585. func (n *SelectField) Restore(ctx *format.RestoreCtx) error {
  586. if n.WildCard != nil {
  587. if err := n.WildCard.Restore(ctx); err != nil {
  588. return errors.Annotate(err, "An error occurred while restore SelectField.WildCard")
  589. }
  590. }
  591. if n.Expr != nil {
  592. if err := n.Expr.Restore(ctx); err != nil {
  593. return errors.Annotate(err, "An error occurred while restore SelectField.Expr")
  594. }
  595. }
  596. if asName := n.AsName.String(); asName != "" {
  597. ctx.WriteKeyWord(" AS ")
  598. ctx.WriteName(asName)
  599. }
  600. return nil
  601. }
  602. // Accept implements Node Accept interface.
  603. func (n *SelectField) Accept(v Visitor) (Node, bool) {
  604. newNode, skipChildren := v.Enter(n)
  605. if skipChildren {
  606. return v.Leave(newNode)
  607. }
  608. n = newNode.(*SelectField)
  609. if n.Expr != nil {
  610. node, ok := n.Expr.Accept(v)
  611. if !ok {
  612. return n, false
  613. }
  614. n.Expr = node.(ExprNode)
  615. }
  616. return v.Leave(n)
  617. }
  618. // FieldList represents field list in select statement.
  619. type FieldList struct {
  620. node
  621. Fields []*SelectField
  622. }
  623. // Restore implements Node interface.
  624. func (n *FieldList) Restore(ctx *format.RestoreCtx) error {
  625. for i, v := range n.Fields {
  626. if i != 0 {
  627. ctx.WritePlain(", ")
  628. }
  629. if err := v.Restore(ctx); err != nil {
  630. return errors.Annotatef(err, "An error occurred while restore FieldList.Fields[%d]", i)
  631. }
  632. }
  633. return nil
  634. }
  635. // Accept implements Node Accept interface.
  636. func (n *FieldList) Accept(v Visitor) (Node, bool) {
  637. newNode, skipChildren := v.Enter(n)
  638. if skipChildren {
  639. return v.Leave(newNode)
  640. }
  641. n = newNode.(*FieldList)
  642. for i, val := range n.Fields {
  643. node, ok := val.Accept(v)
  644. if !ok {
  645. return n, false
  646. }
  647. n.Fields[i] = node.(*SelectField)
  648. }
  649. return v.Leave(n)
  650. }
  651. // TableRefsClause represents table references clause in dml statement.
  652. type TableRefsClause struct {
  653. node
  654. TableRefs *Join
  655. }
  656. // Restore implements Node interface.
  657. func (n *TableRefsClause) Restore(ctx *format.RestoreCtx) error {
  658. if err := n.TableRefs.Restore(ctx); err != nil {
  659. return errors.Annotate(err, "An error occurred while restore TableRefsClause.TableRefs")
  660. }
  661. return nil
  662. }
  663. // Accept implements Node Accept interface.
  664. func (n *TableRefsClause) Accept(v Visitor) (Node, bool) {
  665. newNode, skipChildren := v.Enter(n)
  666. if skipChildren {
  667. return v.Leave(newNode)
  668. }
  669. n = newNode.(*TableRefsClause)
  670. node, ok := n.TableRefs.Accept(v)
  671. if !ok {
  672. return n, false
  673. }
  674. n.TableRefs = node.(*Join)
  675. return v.Leave(n)
  676. }
  677. // ByItem represents an item in order by or group by.
  678. type ByItem struct {
  679. node
  680. Expr ExprNode
  681. Desc bool
  682. NullOrder bool
  683. }
  684. // Restore implements Node interface.
  685. func (n *ByItem) Restore(ctx *format.RestoreCtx) error {
  686. if err := n.Expr.Restore(ctx); err != nil {
  687. return errors.Annotate(err, "An error occurred while restore ByItem.Expr")
  688. }
  689. if n.Desc {
  690. ctx.WriteKeyWord(" DESC")
  691. }
  692. return nil
  693. }
  694. // Accept implements Node Accept interface.
  695. func (n *ByItem) Accept(v Visitor) (Node, bool) {
  696. newNode, skipChildren := v.Enter(n)
  697. if skipChildren {
  698. return v.Leave(newNode)
  699. }
  700. n = newNode.(*ByItem)
  701. node, ok := n.Expr.Accept(v)
  702. if !ok {
  703. return n, false
  704. }
  705. n.Expr = node.(ExprNode)
  706. return v.Leave(n)
  707. }
  708. // GroupByClause represents group by clause.
  709. type GroupByClause struct {
  710. node
  711. Items []*ByItem
  712. }
  713. // Restore implements Node interface.
  714. func (n *GroupByClause) Restore(ctx *format.RestoreCtx) error {
  715. ctx.WriteKeyWord("GROUP BY ")
  716. for i, v := range n.Items {
  717. if i != 0 {
  718. ctx.WritePlain(",")
  719. }
  720. if err := v.Restore(ctx); err != nil {
  721. return errors.Annotatef(err, "An error occurred while restore GroupByClause.Items[%d]", i)
  722. }
  723. }
  724. return nil
  725. }
  726. // Accept implements Node Accept interface.
  727. func (n *GroupByClause) Accept(v Visitor) (Node, bool) {
  728. newNode, skipChildren := v.Enter(n)
  729. if skipChildren {
  730. return v.Leave(newNode)
  731. }
  732. n = newNode.(*GroupByClause)
  733. for i, val := range n.Items {
  734. node, ok := val.Accept(v)
  735. if !ok {
  736. return n, false
  737. }
  738. n.Items[i] = node.(*ByItem)
  739. }
  740. return v.Leave(n)
  741. }
  742. // HavingClause represents having clause.
  743. type HavingClause struct {
  744. node
  745. Expr ExprNode
  746. }
  747. // Restore implements Node interface.
  748. func (n *HavingClause) Restore(ctx *format.RestoreCtx) error {
  749. ctx.WriteKeyWord("HAVING ")
  750. if err := n.Expr.Restore(ctx); err != nil {
  751. return errors.Annotate(err, "An error occurred while restore HavingClause.Expr")
  752. }
  753. return nil
  754. }
  755. // Accept implements Node Accept interface.
  756. func (n *HavingClause) Accept(v Visitor) (Node, bool) {
  757. newNode, skipChildren := v.Enter(n)
  758. if skipChildren {
  759. return v.Leave(newNode)
  760. }
  761. n = newNode.(*HavingClause)
  762. node, ok := n.Expr.Accept(v)
  763. if !ok {
  764. return n, false
  765. }
  766. n.Expr = node.(ExprNode)
  767. return v.Leave(n)
  768. }
  769. // OrderByClause represents order by clause.
  770. type OrderByClause struct {
  771. node
  772. Items []*ByItem
  773. ForUnion bool
  774. }
  775. // Restore implements Node interface.
  776. func (n *OrderByClause) Restore(ctx *format.RestoreCtx) error {
  777. ctx.WriteKeyWord("ORDER BY ")
  778. for i, item := range n.Items {
  779. if i != 0 {
  780. ctx.WritePlain(",")
  781. }
  782. if err := item.Restore(ctx); err != nil {
  783. return errors.Annotatef(err, "An error occurred while restore OrderByClause.Items[%d]", i)
  784. }
  785. }
  786. return nil
  787. }
  788. // Accept implements Node Accept interface.
  789. func (n *OrderByClause) Accept(v Visitor) (Node, bool) {
  790. newNode, skipChildren := v.Enter(n)
  791. if skipChildren {
  792. return v.Leave(newNode)
  793. }
  794. n = newNode.(*OrderByClause)
  795. for i, val := range n.Items {
  796. node, ok := val.Accept(v)
  797. if !ok {
  798. return n, false
  799. }
  800. n.Items[i] = node.(*ByItem)
  801. }
  802. return v.Leave(n)
  803. }
  804. type SampleMethodType int8
  805. const (
  806. SampleMethodTypeNone SampleMethodType = iota
  807. SampleMethodTypeSystem
  808. SampleMethodTypeBernoulli
  809. SampleMethodTypeTiDBRegion
  810. )
  811. type SampleClauseUnitType int8
  812. const (
  813. SampleClauseUnitTypeDefault SampleClauseUnitType = iota
  814. SampleClauseUnitTypeRow
  815. SampleClauseUnitTypePercent
  816. )
  817. type TableSample struct {
  818. node
  819. SampleMethod SampleMethodType
  820. Expr ExprNode
  821. SampleClauseUnit SampleClauseUnitType
  822. RepeatableSeed ExprNode
  823. }
  824. func (s *TableSample) Restore(ctx *format.RestoreCtx) error {
  825. ctx.WriteKeyWord("TABLESAMPLE ")
  826. switch s.SampleMethod {
  827. case SampleMethodTypeBernoulli:
  828. ctx.WriteKeyWord("BERNOULLI ")
  829. case SampleMethodTypeSystem:
  830. ctx.WriteKeyWord("SYSTEM ")
  831. case SampleMethodTypeTiDBRegion:
  832. ctx.WriteKeyWord("REGION ")
  833. }
  834. ctx.WritePlain("(")
  835. if s.Expr != nil {
  836. if err := s.Expr.Restore(ctx); err != nil {
  837. return errors.Annotate(err, "An error occurred while restore TableSample.Expr")
  838. }
  839. }
  840. switch s.SampleClauseUnit {
  841. case SampleClauseUnitTypeDefault:
  842. case SampleClauseUnitTypePercent:
  843. ctx.WriteKeyWord(" PERCENT")
  844. case SampleClauseUnitTypeRow:
  845. ctx.WriteKeyWord(" ROWS")
  846. }
  847. ctx.WritePlain(")")
  848. if s.RepeatableSeed != nil {
  849. ctx.WriteKeyWord(" REPEATABLE")
  850. ctx.WritePlain("(")
  851. if err := s.RepeatableSeed.Restore(ctx); err != nil {
  852. return errors.Annotate(err, "An error occurred while restore TableSample.Expr")
  853. }
  854. ctx.WritePlain(")")
  855. }
  856. return nil
  857. }
  858. func (s *TableSample) Accept(v Visitor) (node Node, ok bool) {
  859. newNode, skipChildren := v.Enter(s)
  860. if skipChildren {
  861. return v.Leave(newNode)
  862. }
  863. s = newNode.(*TableSample)
  864. if s.Expr != nil {
  865. node, ok = s.Expr.Accept(v)
  866. if !ok {
  867. return s, false
  868. }
  869. s.Expr = node.(ExprNode)
  870. }
  871. if s.RepeatableSeed != nil {
  872. node, ok = s.RepeatableSeed.Accept(v)
  873. if !ok {
  874. return s, false
  875. }
  876. s.RepeatableSeed = node.(ExprNode)
  877. }
  878. return v.Leave(s)
  879. }
  880. type SelectStmtKind uint8
  881. const (
  882. SelectStmtKindSelect SelectStmtKind = iota
  883. SelectStmtKindTable
  884. SelectStmtKindValues
  885. )
  886. func (s *SelectStmtKind) String() string {
  887. switch *s {
  888. case SelectStmtKindSelect:
  889. return "SELECT"
  890. case SelectStmtKindTable:
  891. return "TABLE"
  892. case SelectStmtKindValues:
  893. return "VALUES"
  894. }
  895. return ""
  896. }
  897. type CommonTableExpression struct {
  898. node
  899. Name model.CIStr
  900. Query *SubqueryExpr
  901. ColNameList []model.CIStr
  902. }
  903. type WithClause struct {
  904. node
  905. IsRecursive bool
  906. CTEs []*CommonTableExpression
  907. }
  908. // SelectStmt represents the select query node.
  909. // See https://dev.mysql.com/doc/refman/5.7/en/select.html
  910. type SelectStmt struct {
  911. dmlNode
  912. // SelectStmtOpts wraps around select hints and switches.
  913. *SelectStmtOpts
  914. // Distinct represents whether the select has distinct option.
  915. Distinct bool
  916. // From is the from clause of the query.
  917. From *TableRefsClause
  918. // Where is the where clause in select statement.
  919. Where ExprNode
  920. // Fields is the select expression list.
  921. Fields *FieldList
  922. // GroupBy is the group by expression list.
  923. GroupBy *GroupByClause
  924. // Having is the having condition.
  925. Having *HavingClause
  926. // WindowSpecs is the window specification list.
  927. WindowSpecs []WindowSpec
  928. // OrderBy is the ordering expression list.
  929. OrderBy *OrderByClause
  930. // Limit is the limit clause.
  931. Limit *Limit
  932. // LockInfo is the lock type
  933. LockInfo *SelectLockInfo
  934. // TableHints represents the table level Optimizer Hint for join type
  935. TableHints []*TableOptimizerHint
  936. // IsInBraces indicates whether it's a stmt in brace.
  937. IsInBraces bool
  938. // QueryBlockOffset indicates the order of this SelectStmt if counted from left to right in the sql text.
  939. QueryBlockOffset int
  940. // SelectIntoOpt is the select-into option.
  941. SelectIntoOpt *SelectIntoOption
  942. // AfterSetOperator indicates the SelectStmt after which type of set operator
  943. AfterSetOperator *SetOprType
  944. // Kind refer to three kind of statement: SelectStmt, TableStmt and ValuesStmt
  945. Kind SelectStmtKind
  946. // Lists is filled only when Kind == SelectStmtKindValues
  947. Lists []*RowExpr
  948. With *WithClause
  949. }
  950. func (n *WithClause) Restore(ctx *format.RestoreCtx) error {
  951. ctx.WriteKeyWord("WITH ")
  952. if n.IsRecursive {
  953. ctx.WriteKeyWord("RECURSIVE ")
  954. }
  955. for i, cte := range n.CTEs {
  956. if i != 0 {
  957. ctx.WritePlain(", ")
  958. }
  959. ctx.WriteName(cte.Name.String())
  960. if len(cte.ColNameList) > 0 {
  961. ctx.WritePlain(" (")
  962. for j, name := range cte.ColNameList {
  963. if j != 0 {
  964. ctx.WritePlain(", ")
  965. }
  966. ctx.WriteName(name.String())
  967. }
  968. ctx.WritePlain(")")
  969. }
  970. ctx.WriteKeyWord(" AS ")
  971. err := cte.Query.Restore(ctx)
  972. if err != nil {
  973. return err
  974. }
  975. }
  976. ctx.WritePlain(" ")
  977. return nil
  978. }
  979. func (n *WithClause) Accept(v Visitor) (Node, bool) {
  980. newNode, skipChildren := v.Enter(n)
  981. if skipChildren {
  982. return v.Leave(newNode)
  983. }
  984. for _, cte := range n.CTEs {
  985. node, ok := cte.Query.Accept(v)
  986. if !ok {
  987. return n, false
  988. }
  989. cte.Query = node.(*SubqueryExpr)
  990. }
  991. return v.Leave(n)
  992. }
  993. // Restore implements Node interface.
  994. func (n *SelectStmt) Restore(ctx *format.RestoreCtx) error {
  995. if n.IsInBraces {
  996. ctx.WritePlain("(")
  997. defer func() {
  998. ctx.WritePlain(")")
  999. }()
  1000. }
  1001. if n.With != nil {
  1002. err := n.With.Restore(ctx)
  1003. if err != nil {
  1004. return err
  1005. }
  1006. }
  1007. ctx.WriteKeyWord(n.Kind.String())
  1008. ctx.WritePlain(" ")
  1009. switch n.Kind {
  1010. case SelectStmtKindSelect:
  1011. if n.SelectStmtOpts.Priority > 0 {
  1012. ctx.WriteKeyWord(mysql.Priority2Str[n.SelectStmtOpts.Priority])
  1013. ctx.WritePlain(" ")
  1014. }
  1015. if n.SelectStmtOpts.SQLSmallResult {
  1016. ctx.WriteKeyWord("SQL_SMALL_RESULT ")
  1017. }
  1018. if n.SelectStmtOpts.SQLBigResult {
  1019. ctx.WriteKeyWord("SQL_BIG_RESULT ")
  1020. }
  1021. if n.SelectStmtOpts.SQLBufferResult {
  1022. ctx.WriteKeyWord("SQL_BUFFER_RESULT ")
  1023. }
  1024. if !n.SelectStmtOpts.SQLCache {
  1025. ctx.WriteKeyWord("SQL_NO_CACHE ")
  1026. }
  1027. if n.SelectStmtOpts.CalcFoundRows {
  1028. ctx.WriteKeyWord("SQL_CALC_FOUND_ROWS ")
  1029. }
  1030. if n.TableHints != nil && len(n.TableHints) != 0 {
  1031. ctx.WritePlain("/*+ ")
  1032. for i, tableHint := range n.TableHints {
  1033. if i != 0 {
  1034. ctx.WritePlain(" ")
  1035. }
  1036. if err := tableHint.Restore(ctx); err != nil {
  1037. return errors.Annotatef(err, "An error occurred while restore SelectStmt.TableHints[%d]", i)
  1038. }
  1039. }
  1040. ctx.WritePlain("*/ ")
  1041. }
  1042. if n.Distinct {
  1043. ctx.WriteKeyWord("DISTINCT ")
  1044. } else if n.SelectStmtOpts.ExplicitAll {
  1045. ctx.WriteKeyWord("ALL ")
  1046. }
  1047. if n.SelectStmtOpts.StraightJoin {
  1048. ctx.WriteKeyWord("STRAIGHT_JOIN ")
  1049. }
  1050. if n.Fields != nil {
  1051. for i, field := range n.Fields.Fields {
  1052. if i != 0 {
  1053. ctx.WritePlain(",")
  1054. }
  1055. if err := field.Restore(ctx); err != nil {
  1056. return errors.Annotatef(err, "An error occurred while restore SelectStmt.Fields[%d]", i)
  1057. }
  1058. }
  1059. }
  1060. if n.From != nil {
  1061. ctx.WriteKeyWord(" FROM ")
  1062. if err := n.From.Restore(ctx); err != nil {
  1063. return errors.Annotate(err, "An error occurred while restore SelectStmt.From")
  1064. }
  1065. }
  1066. if n.From == nil && n.Where != nil {
  1067. ctx.WriteKeyWord(" FROM DUAL")
  1068. }
  1069. if n.Where != nil {
  1070. ctx.WriteKeyWord(" WHERE ")
  1071. if err := n.Where.Restore(ctx); err != nil {
  1072. return errors.Annotate(err, "An error occurred while restore SelectStmt.Where")
  1073. }
  1074. }
  1075. if n.GroupBy != nil {
  1076. ctx.WritePlain(" ")
  1077. if err := n.GroupBy.Restore(ctx); err != nil {
  1078. return errors.Annotate(err, "An error occurred while restore SelectStmt.GroupBy")
  1079. }
  1080. }
  1081. if n.Having != nil {
  1082. ctx.WritePlain(" ")
  1083. if err := n.Having.Restore(ctx); err != nil {
  1084. return errors.Annotate(err, "An error occurred while restore SelectStmt.Having")
  1085. }
  1086. }
  1087. if n.WindowSpecs != nil {
  1088. ctx.WriteKeyWord(" WINDOW ")
  1089. for i, windowsSpec := range n.WindowSpecs {
  1090. if i != 0 {
  1091. ctx.WritePlain(",")
  1092. }
  1093. if err := windowsSpec.Restore(ctx); err != nil {
  1094. return errors.Annotatef(err, "An error occurred while restore SelectStmt.WindowSpec[%d]", i)
  1095. }
  1096. }
  1097. }
  1098. case SelectStmtKindTable:
  1099. if err := n.From.Restore(ctx); err != nil {
  1100. return errors.Annotate(err, "An error occurred while restore SelectStmt.From")
  1101. }
  1102. case SelectStmtKindValues:
  1103. for i, v := range n.Lists {
  1104. if err := v.Restore(ctx); err != nil {
  1105. return errors.Annotatef(err, "An error occurred while restore SelectStmt.Lists[%d]", i)
  1106. }
  1107. if i != len(n.Lists)-1 {
  1108. ctx.WritePlain(", ")
  1109. }
  1110. }
  1111. }
  1112. if n.OrderBy != nil {
  1113. ctx.WritePlain(" ")
  1114. if err := n.OrderBy.Restore(ctx); err != nil {
  1115. return errors.Annotate(err, "An error occurred while restore SelectStmt.OrderBy")
  1116. }
  1117. }
  1118. if n.Limit != nil {
  1119. ctx.WritePlain(" ")
  1120. if err := n.Limit.Restore(ctx); err != nil {
  1121. return errors.Annotate(err, "An error occurred while restore SelectStmt.Limit")
  1122. }
  1123. }
  1124. if n.LockInfo != nil {
  1125. ctx.WritePlain(" ")
  1126. switch n.LockInfo.LockType {
  1127. case SelectLockNone:
  1128. case SelectLockForUpdateWaitN:
  1129. ctx.WriteKeyWord(n.LockInfo.LockType.String())
  1130. ctx.WritePlainf(" %d", n.LockInfo.WaitSec)
  1131. default:
  1132. ctx.WriteKeyWord(n.LockInfo.LockType.String())
  1133. }
  1134. }
  1135. if n.SelectIntoOpt != nil {
  1136. ctx.WritePlain(" ")
  1137. if err := n.SelectIntoOpt.Restore(ctx); err != nil {
  1138. return errors.Annotate(err, "An error occurred while restore SelectStmt.SelectIntoOpt")
  1139. }
  1140. }
  1141. return nil
  1142. }
  1143. // Accept implements Node Accept interface.
  1144. func (n *SelectStmt) Accept(v Visitor) (Node, bool) {
  1145. newNode, skipChildren := v.Enter(n)
  1146. if skipChildren {
  1147. return v.Leave(newNode)
  1148. }
  1149. n = newNode.(*SelectStmt)
  1150. if n.With != nil {
  1151. node, ok := n.With.Accept(v)
  1152. if !ok {
  1153. return n, false
  1154. }
  1155. n.With = node.(*WithClause)
  1156. }
  1157. if n.TableHints != nil && len(n.TableHints) != 0 {
  1158. newHints := make([]*TableOptimizerHint, len(n.TableHints))
  1159. for i, hint := range n.TableHints {
  1160. node, ok := hint.Accept(v)
  1161. if !ok {
  1162. return n, false
  1163. }
  1164. newHints[i] = node.(*TableOptimizerHint)
  1165. }
  1166. n.TableHints = newHints
  1167. }
  1168. if n.Fields != nil {
  1169. node, ok := n.Fields.Accept(v)
  1170. if !ok {
  1171. return n, false
  1172. }
  1173. n.Fields = node.(*FieldList)
  1174. }
  1175. if n.From != nil {
  1176. node, ok := n.From.Accept(v)
  1177. if !ok {
  1178. return n, false
  1179. }
  1180. n.From = node.(*TableRefsClause)
  1181. }
  1182. if n.Where != nil {
  1183. node, ok := n.Where.Accept(v)
  1184. if !ok {
  1185. return n, false
  1186. }
  1187. n.Where = node.(ExprNode)
  1188. }
  1189. if n.GroupBy != nil {
  1190. node, ok := n.GroupBy.Accept(v)
  1191. if !ok {
  1192. return n, false
  1193. }
  1194. n.GroupBy = node.(*GroupByClause)
  1195. }
  1196. if n.Having != nil {
  1197. node, ok := n.Having.Accept(v)
  1198. if !ok {
  1199. return n, false
  1200. }
  1201. n.Having = node.(*HavingClause)
  1202. }
  1203. for i, list := range n.Lists {
  1204. node, ok := list.Accept(v)
  1205. if !ok {
  1206. return n, false
  1207. }
  1208. n.Lists[i] = node.(*RowExpr)
  1209. }
  1210. for i, spec := range n.WindowSpecs {
  1211. node, ok := spec.Accept(v)
  1212. if !ok {
  1213. return n, false
  1214. }
  1215. n.WindowSpecs[i] = *node.(*WindowSpec)
  1216. }
  1217. if n.OrderBy != nil {
  1218. node, ok := n.OrderBy.Accept(v)
  1219. if !ok {
  1220. return n, false
  1221. }
  1222. n.OrderBy = node.(*OrderByClause)
  1223. }
  1224. if n.Limit != nil {
  1225. node, ok := n.Limit.Accept(v)
  1226. if !ok {
  1227. return n, false
  1228. }
  1229. n.Limit = node.(*Limit)
  1230. }
  1231. return v.Leave(n)
  1232. }
  1233. // SetOprSelectList represents the SelectStmt/TableStmt/ValuesStmt list in a union statement.
  1234. type SetOprSelectList struct {
  1235. node
  1236. AfterSetOperator *SetOprType
  1237. Selects []Node
  1238. }
  1239. // Restore implements Node interface.
  1240. func (n *SetOprSelectList) Restore(ctx *format.RestoreCtx) error {
  1241. for i, stmt := range n.Selects {
  1242. switch selectStmt := stmt.(type) {
  1243. case *SelectStmt:
  1244. if i != 0 {
  1245. ctx.WriteKeyWord(" " + selectStmt.AfterSetOperator.String() + " ")
  1246. }
  1247. if err := selectStmt.Restore(ctx); err != nil {
  1248. return errors.Annotate(err, "An error occurred while restore SetOprSelectList.SelectStmt")
  1249. }
  1250. case *SetOprSelectList:
  1251. if i != 0 {
  1252. ctx.WriteKeyWord(" " + selectStmt.AfterSetOperator.String() + " ")
  1253. }
  1254. ctx.WritePlain("(")
  1255. err := selectStmt.Restore(ctx)
  1256. if err != nil {
  1257. return err
  1258. }
  1259. ctx.WritePlain(")")
  1260. }
  1261. }
  1262. return nil
  1263. }
  1264. // Accept implements Node Accept interface.
  1265. func (n *SetOprSelectList) Accept(v Visitor) (Node, bool) {
  1266. newNode, skipChildren := v.Enter(n)
  1267. if skipChildren {
  1268. return v.Leave(newNode)
  1269. }
  1270. n = newNode.(*SetOprSelectList)
  1271. for i, sel := range n.Selects {
  1272. node, ok := sel.Accept(v)
  1273. if !ok {
  1274. return n, false
  1275. }
  1276. n.Selects[i] = node
  1277. }
  1278. return v.Leave(n)
  1279. }
  1280. type SetOprType uint8
  1281. const (
  1282. Union SetOprType = iota
  1283. UnionAll
  1284. Except
  1285. ExceptAll
  1286. Intersect
  1287. IntersectAll
  1288. )
  1289. func (s *SetOprType) String() string {
  1290. switch *s {
  1291. case Union:
  1292. return "UNION"
  1293. case UnionAll:
  1294. return "UNION ALL"
  1295. case Except:
  1296. return "EXCEPT"
  1297. case ExceptAll:
  1298. return "EXCEPT ALL"
  1299. case Intersect:
  1300. return "INTERSECT"
  1301. case IntersectAll:
  1302. return "INTERSECT ALL"
  1303. }
  1304. return ""
  1305. }
  1306. // SetOprStmt represents "union/except/intersect statement"
  1307. // See https://dev.mysql.com/doc/refman/5.7/en/union.html
  1308. // See https://mariadb.com/kb/en/intersect/
  1309. // See https://mariadb.com/kb/en/except/
  1310. type SetOprStmt struct {
  1311. dmlNode
  1312. SelectList *SetOprSelectList
  1313. OrderBy *OrderByClause
  1314. Limit *Limit
  1315. With *WithClause
  1316. }
  1317. // Restore implements Node interface.
  1318. func (n *SetOprStmt) Restore(ctx *format.RestoreCtx) error {
  1319. if n.With != nil {
  1320. if err := n.With.Restore(ctx); err != nil {
  1321. return errors.Annotate(err, "An error occurred while restore UnionStmt.With")
  1322. }
  1323. }
  1324. if err := n.SelectList.Restore(ctx); err != nil {
  1325. return errors.Annotate(err, "An error occurred while restore SetOprStmt.SelectList")
  1326. }
  1327. if n.OrderBy != nil {
  1328. ctx.WritePlain(" ")
  1329. if err := n.OrderBy.Restore(ctx); err != nil {
  1330. return errors.Annotate(err, "An error occurred while restore SetOprStmt.OrderBy")
  1331. }
  1332. }
  1333. if n.Limit != nil {
  1334. ctx.WritePlain(" ")
  1335. if err := n.Limit.Restore(ctx); err != nil {
  1336. return errors.Annotate(err, "An error occurred while restore SetOprStmt.Limit")
  1337. }
  1338. }
  1339. return nil
  1340. }
  1341. // Accept implements Node Accept interface.
  1342. func (n *SetOprStmt) Accept(v Visitor) (Node, bool) {
  1343. newNode, skipChildren := v.Enter(n)
  1344. if skipChildren {
  1345. return v.Leave(newNode)
  1346. }
  1347. if n.With != nil {
  1348. node, ok := n.With.Accept(v)
  1349. if !ok {
  1350. return n, false
  1351. }
  1352. n.With = node.(*WithClause)
  1353. }
  1354. if n.SelectList != nil {
  1355. node, ok := n.SelectList.Accept(v)
  1356. if !ok {
  1357. return n, false
  1358. }
  1359. n.SelectList = node.(*SetOprSelectList)
  1360. }
  1361. if n.OrderBy != nil {
  1362. node, ok := n.OrderBy.Accept(v)
  1363. if !ok {
  1364. return n, false
  1365. }
  1366. n.OrderBy = node.(*OrderByClause)
  1367. }
  1368. if n.Limit != nil {
  1369. node, ok := n.Limit.Accept(v)
  1370. if !ok {
  1371. return n, false
  1372. }
  1373. n.Limit = node.(*Limit)
  1374. }
  1375. return v.Leave(n)
  1376. }
  1377. // Assignment is the expression for assignment, like a = 1.
  1378. type Assignment struct {
  1379. node
  1380. // Column is the column name to be assigned.
  1381. Column *ColumnName
  1382. // Expr is the expression assigning to ColName.
  1383. Expr ExprNode
  1384. }
  1385. // Restore implements Node interface.
  1386. func (n *Assignment) Restore(ctx *format.RestoreCtx) error {
  1387. if err := n.Column.Restore(ctx); err != nil {
  1388. return errors.Annotate(err, "An error occurred while restore Assignment.Column")
  1389. }
  1390. ctx.WritePlain("=")
  1391. if err := n.Expr.Restore(ctx); err != nil {
  1392. return errors.Annotate(err, "An error occurred while restore Assignment.Expr")
  1393. }
  1394. return nil
  1395. }
  1396. // Accept implements Node Accept interface.
  1397. func (n *Assignment) Accept(v Visitor) (Node, bool) {
  1398. newNode, skipChildren := v.Enter(n)
  1399. if skipChildren {
  1400. return v.Leave(newNode)
  1401. }
  1402. n = newNode.(*Assignment)
  1403. node, ok := n.Column.Accept(v)
  1404. if !ok {
  1405. return n, false
  1406. }
  1407. n.Column = node.(*ColumnName)
  1408. node, ok = n.Expr.Accept(v)
  1409. if !ok {
  1410. return n, false
  1411. }
  1412. n.Expr = node.(ExprNode)
  1413. return v.Leave(n)
  1414. }
  1415. type ColumnNameOrUserVar struct {
  1416. node
  1417. ColumnName *ColumnName
  1418. UserVar *VariableExpr
  1419. }
  1420. func (n *ColumnNameOrUserVar) Restore(ctx *format.RestoreCtx) error {
  1421. if n.ColumnName != nil {
  1422. if err := n.ColumnName.Restore(ctx); err != nil {
  1423. return errors.Annotate(err, "An error occurred while restore ColumnNameOrUserVar.ColumnName")
  1424. }
  1425. }
  1426. if n.UserVar != nil {
  1427. if err := n.UserVar.Restore(ctx); err != nil {
  1428. return errors.Annotate(err, "An error occurred while restore ColumnNameOrUserVar.UserVar")
  1429. }
  1430. }
  1431. return nil
  1432. }
  1433. func (n *ColumnNameOrUserVar) Accept(v Visitor) (node Node, ok bool) {
  1434. newNode, skipChild := v.Enter(n)
  1435. if skipChild {
  1436. return v.Leave(newNode)
  1437. }
  1438. n = newNode.(*ColumnNameOrUserVar)
  1439. if n.ColumnName != nil {
  1440. node, ok = n.ColumnName.Accept(v)
  1441. if !ok {
  1442. return node, false
  1443. }
  1444. n.ColumnName = node.(*ColumnName)
  1445. }
  1446. if n.UserVar != nil {
  1447. node, ok = n.UserVar.Accept(v)
  1448. if !ok {
  1449. return node, false
  1450. }
  1451. n.UserVar = node.(*VariableExpr)
  1452. }
  1453. return v.Leave(n)
  1454. }
  1455. // LoadDataStmt is a statement to load data from a specified file, then insert this rows into an existing table.
  1456. // See https://dev.mysql.com/doc/refman/5.7/en/load-data.html
  1457. type LoadDataStmt struct {
  1458. dmlNode
  1459. IsLocal bool
  1460. Path string
  1461. OnDuplicate OnDuplicateKeyHandlingType
  1462. Table *TableName
  1463. Columns []*ColumnName
  1464. FieldsInfo *FieldsClause
  1465. LinesInfo *LinesClause
  1466. IgnoreLines uint64
  1467. ColumnAssignments []*Assignment
  1468. ColumnsAndUserVars []*ColumnNameOrUserVar
  1469. }
  1470. // Restore implements Node interface.
  1471. func (n *LoadDataStmt) Restore(ctx *format.RestoreCtx) error {
  1472. ctx.WriteKeyWord("LOAD DATA ")
  1473. if n.IsLocal {
  1474. ctx.WriteKeyWord("LOCAL ")
  1475. }
  1476. ctx.WriteKeyWord("INFILE ")
  1477. ctx.WriteString(n.Path)
  1478. if n.OnDuplicate == OnDuplicateKeyHandlingReplace {
  1479. ctx.WriteKeyWord(" REPLACE")
  1480. } else if n.OnDuplicate == OnDuplicateKeyHandlingIgnore {
  1481. ctx.WriteKeyWord(" IGNORE")
  1482. }
  1483. ctx.WriteKeyWord(" INTO TABLE ")
  1484. if err := n.Table.Restore(ctx); err != nil {
  1485. return errors.Annotate(err, "An error occurred while restore LoadDataStmt.Table")
  1486. }
  1487. n.FieldsInfo.Restore(ctx)
  1488. n.LinesInfo.Restore(ctx)
  1489. if n.IgnoreLines != 0 {
  1490. ctx.WriteKeyWord(" IGNORE ")
  1491. ctx.WritePlainf("%d", n.IgnoreLines)
  1492. ctx.WriteKeyWord(" LINES")
  1493. }
  1494. if len(n.ColumnsAndUserVars) != 0 {
  1495. ctx.WritePlain(" (")
  1496. for i, c := range n.ColumnsAndUserVars {
  1497. if i != 0 {
  1498. ctx.WritePlain(",")
  1499. }
  1500. if err := c.Restore(ctx); err != nil {
  1501. return errors.Annotate(err, "An error occurred while restore LoadDataStmt.ColumnsAndUserVars")
  1502. }
  1503. }
  1504. ctx.WritePlain(")")
  1505. }
  1506. if n.ColumnAssignments != nil {
  1507. ctx.WriteKeyWord(" SET")
  1508. for i, assign := range n.ColumnAssignments {
  1509. if i != 0 {
  1510. ctx.WritePlain(",")
  1511. }
  1512. ctx.WritePlain(" ")
  1513. if err := assign.Restore(ctx); err != nil {
  1514. return errors.Annotate(err, "An error occurred while restore LoadDataStmt.ColumnAssignments")
  1515. }
  1516. }
  1517. }
  1518. return nil
  1519. }
  1520. // Accept implements Node Accept interface.
  1521. func (n *LoadDataStmt) Accept(v Visitor) (Node, bool) {
  1522. newNode, skipChildren := v.Enter(n)
  1523. if skipChildren {
  1524. return v.Leave(newNode)
  1525. }
  1526. n = newNode.(*LoadDataStmt)
  1527. if n.Table != nil {
  1528. node, ok := n.Table.Accept(v)
  1529. if !ok {
  1530. return n, false
  1531. }
  1532. n.Table = node.(*TableName)
  1533. }
  1534. for i, val := range n.Columns {
  1535. node, ok := val.Accept(v)
  1536. if !ok {
  1537. return n, false
  1538. }
  1539. n.Columns[i] = node.(*ColumnName)
  1540. }
  1541. for i, assignment := range n.ColumnAssignments {
  1542. node, ok := assignment.Accept(v)
  1543. if !ok {
  1544. return n, false
  1545. }
  1546. n.ColumnAssignments[i] = node.(*Assignment)
  1547. }
  1548. for i, cuVars := range n.ColumnsAndUserVars {
  1549. node, ok := cuVars.Accept(v)
  1550. if !ok {
  1551. return n, false
  1552. }
  1553. n.ColumnsAndUserVars[i] = node.(*ColumnNameOrUserVar)
  1554. }
  1555. return v.Leave(n)
  1556. }
  1557. const (
  1558. Terminated = iota
  1559. Enclosed
  1560. Escaped
  1561. )
  1562. type FieldItem struct {
  1563. Type int
  1564. Value string
  1565. OptEnclosed bool
  1566. }
  1567. // FieldsClause represents fields references clause in load data statement.
  1568. type FieldsClause struct {
  1569. Terminated string
  1570. Enclosed byte
  1571. Escaped byte
  1572. OptEnclosed bool
  1573. }
  1574. // Restore for FieldsClause
  1575. func (n *FieldsClause) Restore(ctx *format.RestoreCtx) error {
  1576. if n.Terminated != "\t" || n.Escaped != '\\' {
  1577. ctx.WriteKeyWord(" FIELDS")
  1578. if n.Terminated != "\t" {
  1579. ctx.WriteKeyWord(" TERMINATED BY ")
  1580. ctx.WriteString(n.Terminated)
  1581. }
  1582. if n.Enclosed != 0 {
  1583. if n.OptEnclosed {
  1584. ctx.WriteKeyWord(" OPTIONALLY")
  1585. }
  1586. ctx.WriteKeyWord(" ENCLOSED BY ")
  1587. ctx.WriteString(string(n.Enclosed))
  1588. }
  1589. if n.Escaped != '\\' {
  1590. ctx.WriteKeyWord(" ESCAPED BY ")
  1591. if n.Escaped == 0 {
  1592. ctx.WritePlain("''")
  1593. } else {
  1594. ctx.WriteString(string(n.Escaped))
  1595. }
  1596. }
  1597. }
  1598. return nil
  1599. }
  1600. // LinesClause represents lines references clause in load data statement.
  1601. type LinesClause struct {
  1602. Starting string
  1603. Terminated string
  1604. }
  1605. // Restore for LinesClause
  1606. func (n *LinesClause) Restore(ctx *format.RestoreCtx) error {
  1607. if n.Starting != "" || n.Terminated != "\n" {
  1608. ctx.WriteKeyWord(" LINES")
  1609. if n.Starting != "" {
  1610. ctx.WriteKeyWord(" STARTING BY ")
  1611. ctx.WriteString(n.Starting)
  1612. }
  1613. if n.Terminated != "\n" {
  1614. ctx.WriteKeyWord(" TERMINATED BY ")
  1615. ctx.WriteString(n.Terminated)
  1616. }
  1617. }
  1618. return nil
  1619. }
  1620. // CallStmt represents a call procedure query node.
  1621. // See https://dev.mysql.com/doc/refman/5.7/en/call.html
  1622. type CallStmt struct {
  1623. dmlNode
  1624. Procedure *FuncCallExpr
  1625. }
  1626. // Restore implements Node interface.
  1627. func (n *CallStmt) Restore(ctx *format.RestoreCtx) error {
  1628. ctx.WriteKeyWord("CALL ")
  1629. if err := n.Procedure.Restore(ctx); err != nil {
  1630. return errors.Annotate(err, "An error occurred while restore CallStmt.Procedure")
  1631. }
  1632. return nil
  1633. }
  1634. // Accept implements Node Accept interface.
  1635. func (n *CallStmt) Accept(v Visitor) (Node, bool) {
  1636. newNode, skipChildren := v.Enter(n)
  1637. if skipChildren {
  1638. return v.Leave(newNode)
  1639. }
  1640. n = newNode.(*CallStmt)
  1641. if n.Procedure != nil {
  1642. node, ok := n.Procedure.Accept(v)
  1643. if !ok {
  1644. return n, false
  1645. }
  1646. n.Procedure = node.(*FuncCallExpr)
  1647. }
  1648. return v.Leave(n)
  1649. }
  1650. // InsertStmt is a statement to insert new rows into an existing table.
  1651. // See https://dev.mysql.com/doc/refman/5.7/en/insert.html
  1652. type InsertStmt struct {
  1653. dmlNode
  1654. IsReplace bool
  1655. IgnoreErr bool
  1656. Table *TableRefsClause
  1657. Columns []*ColumnName
  1658. Lists [][]ExprNode
  1659. Setlist []*Assignment
  1660. Priority mysql.PriorityEnum
  1661. OnDuplicate []*Assignment
  1662. Select ResultSetNode
  1663. // TableHints represents the table level Optimizer Hint for join type.
  1664. TableHints []*TableOptimizerHint
  1665. PartitionNames []model.CIStr
  1666. }
  1667. // Restore implements Node interface.
  1668. func (n *InsertStmt) Restore(ctx *format.RestoreCtx) error {
  1669. if n.IsReplace {
  1670. ctx.WriteKeyWord("REPLACE ")
  1671. } else {
  1672. ctx.WriteKeyWord("INSERT ")
  1673. }
  1674. if n.TableHints != nil && len(n.TableHints) != 0 {
  1675. ctx.WritePlain("/*+ ")
  1676. for i, tableHint := range n.TableHints {
  1677. if i != 0 {
  1678. ctx.WritePlain(" ")
  1679. }
  1680. if err := tableHint.Restore(ctx); err != nil {
  1681. return errors.Annotatef(err, "An error occurred while restore InsertStmt.TableHints[%d]", i)
  1682. }
  1683. }
  1684. ctx.WritePlain("*/ ")
  1685. }
  1686. if err := n.Priority.Restore(ctx); err != nil {
  1687. return errors.Trace(err)
  1688. }
  1689. if n.Priority != mysql.NoPriority {
  1690. ctx.WritePlain(" ")
  1691. }
  1692. if n.IgnoreErr {
  1693. ctx.WriteKeyWord("IGNORE ")
  1694. }
  1695. ctx.WriteKeyWord("INTO ")
  1696. if err := n.Table.Restore(ctx); err != nil {
  1697. return errors.Annotate(err, "An error occurred while restore InsertStmt.Table")
  1698. }
  1699. if len(n.PartitionNames) != 0 {
  1700. ctx.WriteKeyWord(" PARTITION")
  1701. ctx.WritePlain("(")
  1702. for i := 0; i < len(n.PartitionNames); i++ {
  1703. if i != 0 {
  1704. ctx.WritePlain(", ")
  1705. }
  1706. ctx.WriteName(n.PartitionNames[i].String())
  1707. }
  1708. ctx.WritePlain(")")
  1709. }
  1710. if n.Columns != nil {
  1711. ctx.WritePlain(" (")
  1712. for i, v := range n.Columns {
  1713. if i != 0 {
  1714. ctx.WritePlain(",")
  1715. }
  1716. if err := v.Restore(ctx); err != nil {
  1717. return errors.Annotatef(err, "An error occurred while restore InsertStmt.Columns[%d]", i)
  1718. }
  1719. }
  1720. ctx.WritePlain(")")
  1721. }
  1722. if n.Lists != nil {
  1723. ctx.WriteKeyWord(" VALUES ")
  1724. for i, row := range n.Lists {
  1725. if i != 0 {
  1726. ctx.WritePlain(",")
  1727. }
  1728. ctx.WritePlain("(")
  1729. for j, v := range row {
  1730. if j != 0 {
  1731. ctx.WritePlain(",")
  1732. }
  1733. if err := v.Restore(ctx); err != nil {
  1734. return errors.Annotatef(err, "An error occurred while restore InsertStmt.Lists[%d][%d]", i, j)
  1735. }
  1736. }
  1737. ctx.WritePlain(")")
  1738. }
  1739. }
  1740. if n.Select != nil {
  1741. ctx.WritePlain(" ")
  1742. switch v := n.Select.(type) {
  1743. case *SelectStmt, *SetOprStmt:
  1744. if err := v.Restore(ctx); err != nil {
  1745. return errors.Annotate(err, "An error occurred while restore InsertStmt.Select")
  1746. }
  1747. default:
  1748. return errors.Errorf("Incorrect type for InsertStmt.Select: %T", v)
  1749. }
  1750. }
  1751. if n.Setlist != nil {
  1752. ctx.WriteKeyWord(" SET ")
  1753. for i, v := range n.Setlist {
  1754. if i != 0 {
  1755. ctx.WritePlain(",")
  1756. }
  1757. if err := v.Restore(ctx); err != nil {
  1758. return errors.Annotatef(err, "An error occurred while restore InsertStmt.Setlist[%d]", i)
  1759. }
  1760. }
  1761. }
  1762. if n.OnDuplicate != nil {
  1763. ctx.WriteKeyWord(" ON DUPLICATE KEY UPDATE ")
  1764. for i, v := range n.OnDuplicate {
  1765. if i != 0 {
  1766. ctx.WritePlain(",")
  1767. }
  1768. if err := v.Restore(ctx); err != nil {
  1769. return errors.Annotatef(err, "An error occurred while restore InsertStmt.OnDuplicate[%d]", i)
  1770. }
  1771. }
  1772. }
  1773. return nil
  1774. }
  1775. // Accept implements Node Accept interface.
  1776. func (n *InsertStmt) Accept(v Visitor) (Node, bool) {
  1777. newNode, skipChildren := v.Enter(n)
  1778. if skipChildren {
  1779. return v.Leave(newNode)
  1780. }
  1781. n = newNode.(*InsertStmt)
  1782. if n.Select != nil {
  1783. node, ok := n.Select.Accept(v)
  1784. if !ok {
  1785. return n, false
  1786. }
  1787. n.Select = node.(ResultSetNode)
  1788. }
  1789. node, ok := n.Table.Accept(v)
  1790. if !ok {
  1791. return n, false
  1792. }
  1793. n.Table = node.(*TableRefsClause)
  1794. for i, val := range n.Columns {
  1795. node, ok := val.Accept(v)
  1796. if !ok {
  1797. return n, false
  1798. }
  1799. n.Columns[i] = node.(*ColumnName)
  1800. }
  1801. for i, list := range n.Lists {
  1802. for j, val := range list {
  1803. node, ok := val.Accept(v)
  1804. if !ok {
  1805. return n, false
  1806. }
  1807. n.Lists[i][j] = node.(ExprNode)
  1808. }
  1809. }
  1810. for i, val := range n.Setlist {
  1811. node, ok := val.Accept(v)
  1812. if !ok {
  1813. return n, false
  1814. }
  1815. n.Setlist[i] = node.(*Assignment)
  1816. }
  1817. for i, val := range n.OnDuplicate {
  1818. node, ok := val.Accept(v)
  1819. if !ok {
  1820. return n, false
  1821. }
  1822. n.OnDuplicate[i] = node.(*Assignment)
  1823. }
  1824. return v.Leave(n)
  1825. }
  1826. // DeleteStmt is a statement to delete rows from table.
  1827. // See https://dev.mysql.com/doc/refman/5.7/en/delete.html
  1828. type DeleteStmt struct {
  1829. dmlNode
  1830. // TableRefs is used in both single table and multiple table delete statement.
  1831. TableRefs *TableRefsClause
  1832. // Tables is only used in multiple table delete statement.
  1833. Tables *DeleteTableList
  1834. Where ExprNode
  1835. Order *OrderByClause
  1836. Limit *Limit
  1837. Priority mysql.PriorityEnum
  1838. IgnoreErr bool
  1839. Quick bool
  1840. IsMultiTable bool
  1841. BeforeFrom bool
  1842. // TableHints represents the table level Optimizer Hint for join type.
  1843. TableHints []*TableOptimizerHint
  1844. With *WithClause
  1845. }
  1846. // Restore implements Node interface.
  1847. func (n *DeleteStmt) Restore(ctx *format.RestoreCtx) error {
  1848. if n.With != nil {
  1849. err := n.With.Restore(ctx)
  1850. if err != nil {
  1851. return err
  1852. }
  1853. }
  1854. ctx.WriteKeyWord("DELETE ")
  1855. if n.TableHints != nil && len(n.TableHints) != 0 {
  1856. ctx.WritePlain("/*+ ")
  1857. for i, tableHint := range n.TableHints {
  1858. if i != 0 {
  1859. ctx.WritePlain(" ")
  1860. }
  1861. if err := tableHint.Restore(ctx); err != nil {
  1862. return errors.Annotatef(err, "An error occurred while restore UpdateStmt.TableHints[%d]", i)
  1863. }
  1864. }
  1865. ctx.WritePlain("*/ ")
  1866. }
  1867. if err := n.Priority.Restore(ctx); err != nil {
  1868. return errors.Trace(err)
  1869. }
  1870. if n.Priority != mysql.NoPriority {
  1871. ctx.WritePlain(" ")
  1872. }
  1873. if n.Quick {
  1874. ctx.WriteKeyWord("QUICK ")
  1875. }
  1876. if n.IgnoreErr {
  1877. ctx.WriteKeyWord("IGNORE ")
  1878. }
  1879. if n.IsMultiTable { // Multiple-Table Syntax
  1880. if n.BeforeFrom {
  1881. if err := n.Tables.Restore(ctx); err != nil {
  1882. return errors.Annotate(err, "An error occurred while restore DeleteStmt.Tables")
  1883. }
  1884. ctx.WriteKeyWord(" FROM ")
  1885. if err := n.TableRefs.Restore(ctx); err != nil {
  1886. return errors.Annotate(err, "An error occurred while restore DeleteStmt.TableRefs")
  1887. }
  1888. } else {
  1889. ctx.WriteKeyWord("FROM ")
  1890. if err := n.Tables.Restore(ctx); err != nil {
  1891. return errors.Annotate(err, "An error occurred while restore DeleteStmt.Tables")
  1892. }
  1893. ctx.WriteKeyWord(" USING ")
  1894. if err := n.TableRefs.Restore(ctx); err != nil {
  1895. return errors.Annotate(err, "An error occurred while restore DeleteStmt.TableRefs")
  1896. }
  1897. }
  1898. } else { // Single-Table Syntax
  1899. ctx.WriteKeyWord("FROM ")
  1900. if err := n.TableRefs.Restore(ctx); err != nil {
  1901. return errors.Annotate(err, "An error occurred while restore DeleteStmt.TableRefs")
  1902. }
  1903. }
  1904. if n.Where != nil {
  1905. ctx.WriteKeyWord(" WHERE ")
  1906. if err := n.Where.Restore(ctx); err != nil {
  1907. return errors.Annotate(err, "An error occurred while restore DeleteStmt.Where")
  1908. }
  1909. }
  1910. if n.Order != nil {
  1911. ctx.WritePlain(" ")
  1912. if err := n.Order.Restore(ctx); err != nil {
  1913. return errors.Annotate(err, "An error occurred while restore DeleteStmt.Order")
  1914. }
  1915. }
  1916. if n.Limit != nil {
  1917. ctx.WritePlain(" ")
  1918. if err := n.Limit.Restore(ctx); err != nil {
  1919. return errors.Annotate(err, "An error occurred while restore DeleteStmt.Limit")
  1920. }
  1921. }
  1922. return nil
  1923. }
  1924. // Accept implements Node Accept interface.
  1925. func (n *DeleteStmt) Accept(v Visitor) (Node, bool) {
  1926. newNode, skipChildren := v.Enter(n)
  1927. if skipChildren {
  1928. return v.Leave(newNode)
  1929. }
  1930. n = newNode.(*DeleteStmt)
  1931. if n.With != nil {
  1932. node, ok := n.With.Accept(v)
  1933. if !ok {
  1934. return n, false
  1935. }
  1936. n.With = node.(*WithClause)
  1937. }
  1938. node, ok := n.TableRefs.Accept(v)
  1939. if !ok {
  1940. return n, false
  1941. }
  1942. n.TableRefs = node.(*TableRefsClause)
  1943. if n.Tables != nil {
  1944. node, ok = n.Tables.Accept(v)
  1945. if !ok {
  1946. return n, false
  1947. }
  1948. n.Tables = node.(*DeleteTableList)
  1949. }
  1950. if n.Where != nil {
  1951. node, ok = n.Where.Accept(v)
  1952. if !ok {
  1953. return n, false
  1954. }
  1955. n.Where = node.(ExprNode)
  1956. }
  1957. if n.Order != nil {
  1958. node, ok = n.Order.Accept(v)
  1959. if !ok {
  1960. return n, false
  1961. }
  1962. n.Order = node.(*OrderByClause)
  1963. }
  1964. if n.Limit != nil {
  1965. node, ok = n.Limit.Accept(v)
  1966. if !ok {
  1967. return n, false
  1968. }
  1969. n.Limit = node.(*Limit)
  1970. }
  1971. return v.Leave(n)
  1972. }
  1973. // UpdateStmt is a statement to update columns of existing rows in tables with new values.
  1974. // See https://dev.mysql.com/doc/refman/5.7/en/update.html
  1975. type UpdateStmt struct {
  1976. dmlNode
  1977. TableRefs *TableRefsClause
  1978. List []*Assignment
  1979. Where ExprNode
  1980. Order *OrderByClause
  1981. Limit *Limit
  1982. Priority mysql.PriorityEnum
  1983. IgnoreErr bool
  1984. MultipleTable bool
  1985. TableHints []*TableOptimizerHint
  1986. With *WithClause
  1987. }
  1988. // Restore implements Node interface.
  1989. func (n *UpdateStmt) Restore(ctx *format.RestoreCtx) error {
  1990. if n.With != nil {
  1991. err := n.With.Restore(ctx)
  1992. if err != nil {
  1993. return err
  1994. }
  1995. }
  1996. ctx.WriteKeyWord("UPDATE ")
  1997. if n.TableHints != nil && len(n.TableHints) != 0 {
  1998. ctx.WritePlain("/*+ ")
  1999. for i, tableHint := range n.TableHints {
  2000. if i != 0 {
  2001. ctx.WritePlain(" ")
  2002. }
  2003. if err := tableHint.Restore(ctx); err != nil {
  2004. return errors.Annotatef(err, "An error occurred while restore UpdateStmt.TableHints[%d]", i)
  2005. }
  2006. }
  2007. ctx.WritePlain("*/ ")
  2008. }
  2009. if err := n.Priority.Restore(ctx); err != nil {
  2010. return errors.Trace(err)
  2011. }
  2012. if n.Priority != mysql.NoPriority {
  2013. ctx.WritePlain(" ")
  2014. }
  2015. if n.IgnoreErr {
  2016. ctx.WriteKeyWord("IGNORE ")
  2017. }
  2018. if err := n.TableRefs.Restore(ctx); err != nil {
  2019. return errors.Annotate(err, "An error occur while restore UpdateStmt.TableRefs")
  2020. }
  2021. ctx.WriteKeyWord(" SET ")
  2022. for i, assignment := range n.List {
  2023. if i != 0 {
  2024. ctx.WritePlain(", ")
  2025. }
  2026. if err := assignment.Column.Restore(ctx); err != nil {
  2027. return errors.Annotatef(err, "An error occur while restore UpdateStmt.List[%d].Column", i)
  2028. }
  2029. ctx.WritePlain("=")
  2030. if err := assignment.Expr.Restore(ctx); err != nil {
  2031. return errors.Annotatef(err, "An error occur while restore UpdateStmt.List[%d].Expr", i)
  2032. }
  2033. }
  2034. if n.Where != nil {
  2035. ctx.WriteKeyWord(" WHERE ")
  2036. if err := n.Where.Restore(ctx); err != nil {
  2037. return errors.Annotate(err, "An error occur while restore UpdateStmt.Where")
  2038. }
  2039. }
  2040. if n.Order != nil {
  2041. ctx.WritePlain(" ")
  2042. if err := n.Order.Restore(ctx); err != nil {
  2043. return errors.Annotate(err, "An error occur while restore UpdateStmt.Order")
  2044. }
  2045. }
  2046. if n.Limit != nil {
  2047. ctx.WritePlain(" ")
  2048. if err := n.Limit.Restore(ctx); err != nil {
  2049. return errors.Annotate(err, "An error occur while restore UpdateStmt.Limit")
  2050. }
  2051. }
  2052. return nil
  2053. }
  2054. // Accept implements Node Accept interface.
  2055. func (n *UpdateStmt) Accept(v Visitor) (Node, bool) {
  2056. newNode, skipChildren := v.Enter(n)
  2057. if skipChildren {
  2058. return v.Leave(newNode)
  2059. }
  2060. n = newNode.(*UpdateStmt)
  2061. if n.With != nil {
  2062. node, ok := n.With.Accept(v)
  2063. if !ok {
  2064. return n, false
  2065. }
  2066. n.With = node.(*WithClause)
  2067. }
  2068. node, ok := n.TableRefs.Accept(v)
  2069. if !ok {
  2070. return n, false
  2071. }
  2072. n.TableRefs = node.(*TableRefsClause)
  2073. for i, val := range n.List {
  2074. node, ok = val.Accept(v)
  2075. if !ok {
  2076. return n, false
  2077. }
  2078. n.List[i] = node.(*Assignment)
  2079. }
  2080. if n.Where != nil {
  2081. node, ok = n.Where.Accept(v)
  2082. if !ok {
  2083. return n, false
  2084. }
  2085. n.Where = node.(ExprNode)
  2086. }
  2087. if n.Order != nil {
  2088. node, ok = n.Order.Accept(v)
  2089. if !ok {
  2090. return n, false
  2091. }
  2092. n.Order = node.(*OrderByClause)
  2093. }
  2094. if n.Limit != nil {
  2095. node, ok = n.Limit.Accept(v)
  2096. if !ok {
  2097. return n, false
  2098. }
  2099. n.Limit = node.(*Limit)
  2100. }
  2101. return v.Leave(n)
  2102. }
  2103. // Limit is the limit clause.
  2104. type Limit struct {
  2105. node
  2106. Count ExprNode
  2107. Offset ExprNode
  2108. }
  2109. // Restore implements Node interface.
  2110. func (n *Limit) Restore(ctx *format.RestoreCtx) error {
  2111. ctx.WriteKeyWord("LIMIT ")
  2112. if n.Offset != nil {
  2113. if err := n.Offset.Restore(ctx); err != nil {
  2114. return errors.Annotate(err, "An error occurred while restore Limit.Offset")
  2115. }
  2116. ctx.WritePlain(",")
  2117. }
  2118. if err := n.Count.Restore(ctx); err != nil {
  2119. return errors.Annotate(err, "An error occurred while restore Limit.Count")
  2120. }
  2121. return nil
  2122. }
  2123. // Accept implements Node Accept interface.
  2124. func (n *Limit) Accept(v Visitor) (Node, bool) {
  2125. newNode, skipChildren := v.Enter(n)
  2126. if skipChildren {
  2127. return v.Leave(newNode)
  2128. }
  2129. if n.Count != nil {
  2130. node, ok := n.Count.Accept(v)
  2131. if !ok {
  2132. return n, false
  2133. }
  2134. n.Count = node.(ExprNode)
  2135. }
  2136. if n.Offset != nil {
  2137. node, ok := n.Offset.Accept(v)
  2138. if !ok {
  2139. return n, false
  2140. }
  2141. n.Offset = node.(ExprNode)
  2142. }
  2143. n = newNode.(*Limit)
  2144. return v.Leave(n)
  2145. }
  2146. // ShowStmtType is the type for SHOW statement.
  2147. type ShowStmtType int
  2148. // Show statement types.
  2149. const (
  2150. ShowNone = iota
  2151. ShowEngines
  2152. ShowDatabases
  2153. ShowTables
  2154. ShowTableStatus
  2155. ShowColumns
  2156. ShowWarnings
  2157. ShowCharset
  2158. ShowVariables
  2159. ShowStatus
  2160. ShowCollation
  2161. ShowCreateTable
  2162. ShowCreateView
  2163. ShowCreateUser
  2164. ShowCreateSequence
  2165. ShowGrants
  2166. ShowTriggers
  2167. ShowProcedureStatus
  2168. ShowIndex
  2169. ShowProcessList
  2170. ShowCreateDatabase
  2171. ShowConfig
  2172. ShowEvents
  2173. ShowStatsExtended
  2174. ShowStatsMeta
  2175. ShowStatsHistograms
  2176. ShowStatsTopN
  2177. ShowStatsBuckets
  2178. ShowStatsHealthy
  2179. ShowPlugins
  2180. ShowProfile
  2181. ShowProfiles
  2182. ShowMasterStatus
  2183. ShowPrivileges
  2184. ShowErrors
  2185. ShowBindings
  2186. ShowPumpStatus
  2187. ShowDrainerStatus
  2188. ShowOpenTables
  2189. ShowAnalyzeStatus
  2190. ShowRegions
  2191. ShowBuiltins
  2192. ShowTableNextRowId
  2193. ShowBackups
  2194. ShowRestores
  2195. ShowImports
  2196. ShowCreateImport
  2197. )
  2198. const (
  2199. ProfileTypeInvalid = iota
  2200. ProfileTypeCPU
  2201. ProfileTypeMemory
  2202. ProfileTypeBlockIo
  2203. ProfileTypeContextSwitch
  2204. ProfileTypePageFaults
  2205. ProfileTypeIpc
  2206. ProfileTypeSwaps
  2207. ProfileTypeSource
  2208. ProfileTypeAll
  2209. )
  2210. // ShowStmt is a statement to provide information about databases, tables, columns and so on.
  2211. // See https://dev.mysql.com/doc/refman/5.7/en/show.html
  2212. type ShowStmt struct {
  2213. dmlNode
  2214. Tp ShowStmtType // Databases/Tables/Columns/....
  2215. DBName string
  2216. Table *TableName // Used for showing columns.
  2217. Column *ColumnName // Used for `desc table column`.
  2218. IndexName model.CIStr
  2219. Flag int // Some flag parsed from sql, such as FULL.
  2220. Full bool
  2221. User *auth.UserIdentity // Used for show grants/create user.
  2222. Roles []*auth.RoleIdentity // Used for show grants .. using
  2223. IfNotExists bool // Used for `show create database if not exists`
  2224. Extended bool // Used for `show extended columns from ...`
  2225. // GlobalScope is used by `show variables` and `show bindings`
  2226. GlobalScope bool
  2227. Pattern *PatternLikeExpr
  2228. Where ExprNode
  2229. ShowProfileTypes []int // Used for `SHOW PROFILE` syntax
  2230. ShowProfileArgs *int64 // Used for `SHOW PROFILE` syntax
  2231. ShowProfileLimit *Limit // Used for `SHOW PROFILE` syntax
  2232. }
  2233. // Restore implements Node interface.
  2234. func (n *ShowStmt) Restore(ctx *format.RestoreCtx) error {
  2235. restoreOptFull := func() {
  2236. if n.Full {
  2237. ctx.WriteKeyWord("FULL ")
  2238. }
  2239. }
  2240. restoreShowDatabaseNameOpt := func() {
  2241. if n.DBName != "" {
  2242. // FROM OR IN
  2243. ctx.WriteKeyWord(" IN ")
  2244. ctx.WriteName(n.DBName)
  2245. }
  2246. }
  2247. restoreGlobalScope := func() {
  2248. if n.GlobalScope {
  2249. ctx.WriteKeyWord("GLOBAL ")
  2250. } else {
  2251. ctx.WriteKeyWord("SESSION ")
  2252. }
  2253. }
  2254. restoreShowLikeOrWhereOpt := func() error {
  2255. if n.Pattern != nil && n.Pattern.Pattern != nil {
  2256. ctx.WriteKeyWord(" LIKE ")
  2257. if err := n.Pattern.Pattern.Restore(ctx); err != nil {
  2258. return errors.Annotate(err, "An error occurred while restore ShowStmt.Pattern")
  2259. }
  2260. } else if n.Where != nil {
  2261. ctx.WriteKeyWord(" WHERE ")
  2262. if err := n.Where.Restore(ctx); err != nil {
  2263. return errors.Annotate(err, "An error occurred while restore ShowStmt.Where")
  2264. }
  2265. }
  2266. return nil
  2267. }
  2268. ctx.WriteKeyWord("SHOW ")
  2269. switch n.Tp {
  2270. case ShowCreateTable:
  2271. ctx.WriteKeyWord("CREATE TABLE ")
  2272. if err := n.Table.Restore(ctx); err != nil {
  2273. return errors.Annotate(err, "An error occurred while restore ShowStmt.Table")
  2274. }
  2275. case ShowCreateView:
  2276. ctx.WriteKeyWord("CREATE VIEW ")
  2277. if err := n.Table.Restore(ctx); err != nil {
  2278. return errors.Annotate(err, "An error occurred while restore ShowStmt.VIEW")
  2279. }
  2280. case ShowCreateDatabase:
  2281. ctx.WriteKeyWord("CREATE DATABASE ")
  2282. if n.IfNotExists {
  2283. ctx.WriteKeyWord("IF NOT EXISTS ")
  2284. }
  2285. ctx.WriteName(n.DBName)
  2286. case ShowCreateSequence:
  2287. ctx.WriteKeyWord("CREATE SEQUENCE ")
  2288. if err := n.Table.Restore(ctx); err != nil {
  2289. return errors.Annotate(err, "An error occurred while restore ShowStmt.SEQUENCE")
  2290. }
  2291. case ShowCreateUser:
  2292. ctx.WriteKeyWord("CREATE USER ")
  2293. if err := n.User.Restore(ctx); err != nil {
  2294. return errors.Annotate(err, "An error occurred while restore ShowStmt.User")
  2295. }
  2296. case ShowGrants:
  2297. ctx.WriteKeyWord("GRANTS")
  2298. if n.User != nil {
  2299. ctx.WriteKeyWord(" FOR ")
  2300. if err := n.User.Restore(ctx); err != nil {
  2301. return errors.Annotate(err, "An error occurred while restore ShowStmt.User")
  2302. }
  2303. }
  2304. if n.Roles != nil {
  2305. ctx.WriteKeyWord(" USING ")
  2306. for i, r := range n.Roles {
  2307. if err := r.Restore(ctx); err != nil {
  2308. return errors.Annotate(err, "An error occurred while restore ShowStmt.User")
  2309. }
  2310. if i != len(n.Roles)-1 {
  2311. ctx.WritePlain(", ")
  2312. }
  2313. }
  2314. }
  2315. case ShowMasterStatus:
  2316. ctx.WriteKeyWord("MASTER STATUS")
  2317. case ShowProcessList:
  2318. restoreOptFull()
  2319. ctx.WriteKeyWord("PROCESSLIST")
  2320. case ShowStatsExtended:
  2321. ctx.WriteKeyWord("STATS_EXTENDED")
  2322. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2323. return err
  2324. }
  2325. case ShowStatsMeta:
  2326. ctx.WriteKeyWord("STATS_META")
  2327. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2328. return err
  2329. }
  2330. case ShowStatsHistograms:
  2331. ctx.WriteKeyWord("STATS_HISTOGRAMS")
  2332. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2333. return err
  2334. }
  2335. case ShowStatsTopN:
  2336. ctx.WriteKeyWord("STATS_TOPN")
  2337. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2338. return err
  2339. }
  2340. case ShowStatsBuckets:
  2341. ctx.WriteKeyWord("STATS_BUCKETS")
  2342. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2343. return err
  2344. }
  2345. case ShowStatsHealthy:
  2346. ctx.WriteKeyWord("STATS_HEALTHY")
  2347. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2348. return err
  2349. }
  2350. case ShowProfiles:
  2351. ctx.WriteKeyWord("PROFILES")
  2352. case ShowProfile:
  2353. ctx.WriteKeyWord("PROFILE")
  2354. if len(n.ShowProfileTypes) > 0 {
  2355. for i, tp := range n.ShowProfileTypes {
  2356. if i != 0 {
  2357. ctx.WritePlain(",")
  2358. }
  2359. ctx.WritePlain(" ")
  2360. switch tp {
  2361. case ProfileTypeCPU:
  2362. ctx.WriteKeyWord("CPU")
  2363. case ProfileTypeMemory:
  2364. ctx.WriteKeyWord("MEMORY")
  2365. case ProfileTypeBlockIo:
  2366. ctx.WriteKeyWord("BLOCK IO")
  2367. case ProfileTypeContextSwitch:
  2368. ctx.WriteKeyWord("CONTEXT SWITCHES")
  2369. case ProfileTypeIpc:
  2370. ctx.WriteKeyWord("IPC")
  2371. case ProfileTypePageFaults:
  2372. ctx.WriteKeyWord("PAGE FAULTS")
  2373. case ProfileTypeSource:
  2374. ctx.WriteKeyWord("SOURCE")
  2375. case ProfileTypeSwaps:
  2376. ctx.WriteKeyWord("SWAPS")
  2377. case ProfileTypeAll:
  2378. ctx.WriteKeyWord("ALL")
  2379. }
  2380. }
  2381. }
  2382. if n.ShowProfileArgs != nil {
  2383. ctx.WriteKeyWord(" FOR QUERY ")
  2384. ctx.WritePlainf("%d", *n.ShowProfileArgs)
  2385. }
  2386. if n.ShowProfileLimit != nil {
  2387. ctx.WritePlain(" ")
  2388. if err := n.ShowProfileLimit.Restore(ctx); err != nil {
  2389. return errors.Annotate(err, "An error occurred while restore ShowStmt.WritePlain")
  2390. }
  2391. }
  2392. case ShowPrivileges:
  2393. ctx.WriteKeyWord("PRIVILEGES")
  2394. case ShowBuiltins:
  2395. ctx.WriteKeyWord("BUILTINS")
  2396. case ShowCreateImport:
  2397. ctx.WriteKeyWord("CREATE IMPORT ")
  2398. ctx.WriteName(n.DBName)
  2399. // ShowTargetFilterable
  2400. default:
  2401. switch n.Tp {
  2402. case ShowEngines:
  2403. ctx.WriteKeyWord("ENGINES")
  2404. case ShowConfig:
  2405. ctx.WriteKeyWord("CONFIG")
  2406. case ShowDatabases:
  2407. ctx.WriteKeyWord("DATABASES")
  2408. case ShowCharset:
  2409. ctx.WriteKeyWord("CHARSET")
  2410. case ShowTables:
  2411. restoreOptFull()
  2412. ctx.WriteKeyWord("TABLES")
  2413. restoreShowDatabaseNameOpt()
  2414. case ShowOpenTables:
  2415. ctx.WriteKeyWord("OPEN TABLES")
  2416. restoreShowDatabaseNameOpt()
  2417. case ShowTableStatus:
  2418. ctx.WriteKeyWord("TABLE STATUS")
  2419. restoreShowDatabaseNameOpt()
  2420. case ShowIndex:
  2421. // here can be INDEX INDEXES KEYS
  2422. // FROM or IN
  2423. ctx.WriteKeyWord("INDEX IN ")
  2424. if err := n.Table.Restore(ctx); err != nil {
  2425. return errors.Annotate(err, "An error occurred while resotre ShowStmt.Table")
  2426. } // TODO: remember to check this case
  2427. case ShowColumns: // equivalent to SHOW FIELDS
  2428. if n.Extended {
  2429. ctx.WriteKeyWord("EXTENDED ")
  2430. }
  2431. restoreOptFull()
  2432. ctx.WriteKeyWord("COLUMNS")
  2433. if n.Table != nil {
  2434. // FROM or IN
  2435. ctx.WriteKeyWord(" IN ")
  2436. if err := n.Table.Restore(ctx); err != nil {
  2437. return errors.Annotate(err, "An error occurred while resotre ShowStmt.Table")
  2438. }
  2439. }
  2440. restoreShowDatabaseNameOpt()
  2441. case ShowWarnings:
  2442. ctx.WriteKeyWord("WARNINGS")
  2443. case ShowErrors:
  2444. ctx.WriteKeyWord("ERRORS")
  2445. case ShowVariables:
  2446. restoreGlobalScope()
  2447. ctx.WriteKeyWord("VARIABLES")
  2448. case ShowStatus:
  2449. restoreGlobalScope()
  2450. ctx.WriteKeyWord("STATUS")
  2451. case ShowCollation:
  2452. ctx.WriteKeyWord("COLLATION")
  2453. case ShowTriggers:
  2454. ctx.WriteKeyWord("TRIGGERS")
  2455. restoreShowDatabaseNameOpt()
  2456. case ShowProcedureStatus:
  2457. ctx.WriteKeyWord("PROCEDURE STATUS")
  2458. case ShowEvents:
  2459. ctx.WriteKeyWord("EVENTS")
  2460. restoreShowDatabaseNameOpt()
  2461. case ShowPlugins:
  2462. ctx.WriteKeyWord("PLUGINS")
  2463. case ShowBindings:
  2464. if n.GlobalScope {
  2465. ctx.WriteKeyWord("GLOBAL ")
  2466. } else {
  2467. ctx.WriteKeyWord("SESSION ")
  2468. }
  2469. ctx.WriteKeyWord("BINDINGS")
  2470. case ShowPumpStatus:
  2471. ctx.WriteKeyWord("PUMP STATUS")
  2472. case ShowDrainerStatus:
  2473. ctx.WriteKeyWord("DRAINER STATUS")
  2474. case ShowAnalyzeStatus:
  2475. ctx.WriteKeyWord("ANALYZE STATUS")
  2476. case ShowRegions:
  2477. ctx.WriteKeyWord("TABLE ")
  2478. if err := n.Table.Restore(ctx); err != nil {
  2479. return errors.Annotate(err, "An error occurred while restore SplitIndexRegionStmt.Table")
  2480. }
  2481. if len(n.IndexName.L) > 0 {
  2482. ctx.WriteKeyWord(" INDEX ")
  2483. ctx.WriteName(n.IndexName.String())
  2484. }
  2485. ctx.WriteKeyWord(" REGIONS")
  2486. if err := restoreShowLikeOrWhereOpt(); err != nil {
  2487. return err
  2488. }
  2489. return nil
  2490. case ShowTableNextRowId:
  2491. ctx.WriteKeyWord("TABLE ")
  2492. if err := n.Table.Restore(ctx); err != nil {
  2493. return errors.Annotate(err, "An error occurred while restore SplitIndexRegionStmt.Table")
  2494. }
  2495. ctx.WriteKeyWord(" NEXT_ROW_ID")
  2496. return nil
  2497. case ShowBackups:
  2498. ctx.WriteKeyWord("BACKUPS")
  2499. case ShowRestores:
  2500. ctx.WriteKeyWord("RESTORES")
  2501. case ShowImports:
  2502. ctx.WriteKeyWord("IMPORTS")
  2503. default:
  2504. return errors.New("Unknown ShowStmt type")
  2505. }
  2506. restoreShowLikeOrWhereOpt()
  2507. }
  2508. return nil
  2509. }
  2510. // Accept implements Node Accept interface.
  2511. func (n *ShowStmt) Accept(v Visitor) (Node, bool) {
  2512. newNode, skipChildren := v.Enter(n)
  2513. if skipChildren {
  2514. return v.Leave(newNode)
  2515. }
  2516. n = newNode.(*ShowStmt)
  2517. if n.Table != nil {
  2518. node, ok := n.Table.Accept(v)
  2519. if !ok {
  2520. return n, false
  2521. }
  2522. n.Table = node.(*TableName)
  2523. }
  2524. if n.Column != nil {
  2525. node, ok := n.Column.Accept(v)
  2526. if !ok {
  2527. return n, false
  2528. }
  2529. n.Column = node.(*ColumnName)
  2530. }
  2531. if n.Pattern != nil {
  2532. node, ok := n.Pattern.Accept(v)
  2533. if !ok {
  2534. return n, false
  2535. }
  2536. n.Pattern = node.(*PatternLikeExpr)
  2537. }
  2538. if n.Where != nil {
  2539. node, ok := n.Where.Accept(v)
  2540. if !ok {
  2541. return n, false
  2542. }
  2543. n.Where = node.(ExprNode)
  2544. }
  2545. return v.Leave(n)
  2546. }
  2547. // WindowSpec is the specification of a window.
  2548. type WindowSpec struct {
  2549. node
  2550. Name model.CIStr
  2551. // Ref is the reference window of this specification. For example, in `w2 as (w1 order by a)`,
  2552. // the definition of `w2` references `w1`.
  2553. Ref model.CIStr
  2554. PartitionBy *PartitionByClause
  2555. OrderBy *OrderByClause
  2556. Frame *FrameClause
  2557. // OnlyAlias will set to true of the first following case.
  2558. // To make compatible with MySQL, we need to distinguish `select func over w` from `select func over (w)`.
  2559. OnlyAlias bool
  2560. }
  2561. // Restore implements Node interface.
  2562. func (n *WindowSpec) Restore(ctx *format.RestoreCtx) error {
  2563. if name := n.Name.String(); name != "" {
  2564. ctx.WriteName(name)
  2565. if n.OnlyAlias {
  2566. return nil
  2567. }
  2568. ctx.WriteKeyWord(" AS ")
  2569. }
  2570. ctx.WritePlain("(")
  2571. sep := ""
  2572. if refName := n.Ref.String(); refName != "" {
  2573. ctx.WriteName(refName)
  2574. sep = " "
  2575. }
  2576. if n.PartitionBy != nil {
  2577. ctx.WritePlain(sep)
  2578. if err := n.PartitionBy.Restore(ctx); err != nil {
  2579. return errors.Annotate(err, "An error occurred while restore WindowSpec.PartitionBy")
  2580. }
  2581. sep = " "
  2582. }
  2583. if n.OrderBy != nil {
  2584. ctx.WritePlain(sep)
  2585. if err := n.OrderBy.Restore(ctx); err != nil {
  2586. return errors.Annotate(err, "An error occurred while restore WindowSpec.OrderBy")
  2587. }
  2588. sep = " "
  2589. }
  2590. if n.Frame != nil {
  2591. ctx.WritePlain(sep)
  2592. if err := n.Frame.Restore(ctx); err != nil {
  2593. return errors.Annotate(err, "An error occurred while restore WindowSpec.Frame")
  2594. }
  2595. }
  2596. ctx.WritePlain(")")
  2597. return nil
  2598. }
  2599. // Accept implements Node Accept interface.
  2600. func (n *WindowSpec) Accept(v Visitor) (Node, bool) {
  2601. newNode, skipChildren := v.Enter(n)
  2602. if skipChildren {
  2603. return v.Leave(newNode)
  2604. }
  2605. n = newNode.(*WindowSpec)
  2606. if n.PartitionBy != nil {
  2607. node, ok := n.PartitionBy.Accept(v)
  2608. if !ok {
  2609. return n, false
  2610. }
  2611. n.PartitionBy = node.(*PartitionByClause)
  2612. }
  2613. if n.OrderBy != nil {
  2614. node, ok := n.OrderBy.Accept(v)
  2615. if !ok {
  2616. return n, false
  2617. }
  2618. n.OrderBy = node.(*OrderByClause)
  2619. }
  2620. if n.Frame != nil {
  2621. node, ok := n.Frame.Accept(v)
  2622. if !ok {
  2623. return n, false
  2624. }
  2625. n.Frame = node.(*FrameClause)
  2626. }
  2627. return v.Leave(n)
  2628. }
  2629. type SelectIntoType int
  2630. const (
  2631. SelectIntoOutfile SelectIntoType = iota + 1
  2632. SelectIntoDumpfile
  2633. SelectIntoVars
  2634. )
  2635. type SelectIntoOption struct {
  2636. node
  2637. Tp SelectIntoType
  2638. FileName string
  2639. FieldsInfo *FieldsClause
  2640. LinesInfo *LinesClause
  2641. }
  2642. // Restore implements Node interface.
  2643. func (n *SelectIntoOption) Restore(ctx *format.RestoreCtx) error {
  2644. if n.Tp != SelectIntoOutfile {
  2645. // only support SELECT/TABLE/VALUES ... INTO OUTFILE statement now
  2646. return errors.New("Unsupported SelectionInto type")
  2647. }
  2648. ctx.WriteKeyWord("INTO OUTFILE ")
  2649. ctx.WriteString(n.FileName)
  2650. if n.FieldsInfo != nil {
  2651. if err := n.FieldsInfo.Restore(ctx); err != nil {
  2652. return errors.Annotate(err, "An error occurred while restore SelectInto.FieldsInfo")
  2653. }
  2654. }
  2655. if n.LinesInfo != nil {
  2656. if err := n.LinesInfo.Restore(ctx); err != nil {
  2657. return errors.Annotate(err, "An error occurred while restore SelectInto.LinesInfo")
  2658. }
  2659. }
  2660. return nil
  2661. }
  2662. // Accept implements Node Accept interface.
  2663. func (n *SelectIntoOption) Accept(v Visitor) (Node, bool) {
  2664. newNode, skipChildren := v.Enter(n)
  2665. if skipChildren {
  2666. return v.Leave(newNode)
  2667. }
  2668. return v.Leave(n)
  2669. }
  2670. // PartitionByClause represents partition by clause.
  2671. type PartitionByClause struct {
  2672. node
  2673. Items []*ByItem
  2674. }
  2675. // Restore implements Node interface.
  2676. func (n *PartitionByClause) Restore(ctx *format.RestoreCtx) error {
  2677. ctx.WriteKeyWord("PARTITION BY ")
  2678. for i, v := range n.Items {
  2679. if i != 0 {
  2680. ctx.WritePlain(", ")
  2681. }
  2682. if err := v.Restore(ctx); err != nil {
  2683. return errors.Annotatef(err, "An error occurred while restore PartitionByClause.Items[%d]", i)
  2684. }
  2685. }
  2686. return nil
  2687. }
  2688. // Accept implements Node Accept interface.
  2689. func (n *PartitionByClause) Accept(v Visitor) (Node, bool) {
  2690. newNode, skipChildren := v.Enter(n)
  2691. if skipChildren {
  2692. return v.Leave(newNode)
  2693. }
  2694. n = newNode.(*PartitionByClause)
  2695. for i, val := range n.Items {
  2696. node, ok := val.Accept(v)
  2697. if !ok {
  2698. return n, false
  2699. }
  2700. n.Items[i] = node.(*ByItem)
  2701. }
  2702. return v.Leave(n)
  2703. }
  2704. // FrameType is the type of window function frame.
  2705. type FrameType int
  2706. // Window function frame types.
  2707. // MySQL only supports `ROWS` and `RANGES`.
  2708. const (
  2709. Rows = iota
  2710. Ranges
  2711. Groups
  2712. )
  2713. // FrameClause represents frame clause.
  2714. type FrameClause struct {
  2715. node
  2716. Type FrameType
  2717. Extent FrameExtent
  2718. }
  2719. // Restore implements Node interface.
  2720. func (n *FrameClause) Restore(ctx *format.RestoreCtx) error {
  2721. switch n.Type {
  2722. case Rows:
  2723. ctx.WriteKeyWord("ROWS")
  2724. case Ranges:
  2725. ctx.WriteKeyWord("RANGE")
  2726. default:
  2727. return errors.New("Unsupported window function frame type")
  2728. }
  2729. ctx.WriteKeyWord(" BETWEEN ")
  2730. if err := n.Extent.Start.Restore(ctx); err != nil {
  2731. return errors.Annotate(err, "An error occurred while restore FrameClause.Extent.Start")
  2732. }
  2733. ctx.WriteKeyWord(" AND ")
  2734. if err := n.Extent.End.Restore(ctx); err != nil {
  2735. return errors.Annotate(err, "An error occurred while restore FrameClause.Extent.End")
  2736. }
  2737. return nil
  2738. }
  2739. // Accept implements Node Accept interface.
  2740. func (n *FrameClause) Accept(v Visitor) (Node, bool) {
  2741. newNode, skipChildren := v.Enter(n)
  2742. if skipChildren {
  2743. return v.Leave(newNode)
  2744. }
  2745. n = newNode.(*FrameClause)
  2746. node, ok := n.Extent.Start.Accept(v)
  2747. if !ok {
  2748. return n, false
  2749. }
  2750. n.Extent.Start = *node.(*FrameBound)
  2751. node, ok = n.Extent.End.Accept(v)
  2752. if !ok {
  2753. return n, false
  2754. }
  2755. n.Extent.End = *node.(*FrameBound)
  2756. return v.Leave(n)
  2757. }
  2758. // FrameExtent represents frame extent.
  2759. type FrameExtent struct {
  2760. Start FrameBound
  2761. End FrameBound
  2762. }
  2763. // FrameType is the type of window function frame bound.
  2764. type BoundType int
  2765. // Frame bound types.
  2766. const (
  2767. Following = iota
  2768. Preceding
  2769. CurrentRow
  2770. )
  2771. // FrameBound represents frame bound.
  2772. type FrameBound struct {
  2773. node
  2774. Type BoundType
  2775. UnBounded bool
  2776. Expr ExprNode
  2777. // `Unit` is used to indicate the units in which the `Expr` should be interpreted.
  2778. // For example: '2:30' MINUTE_SECOND.
  2779. Unit TimeUnitType
  2780. }
  2781. // Restore implements Node interface.
  2782. func (n *FrameBound) Restore(ctx *format.RestoreCtx) error {
  2783. if n.UnBounded {
  2784. ctx.WriteKeyWord("UNBOUNDED")
  2785. }
  2786. switch n.Type {
  2787. case CurrentRow:
  2788. ctx.WriteKeyWord("CURRENT ROW")
  2789. case Preceding, Following:
  2790. if n.Unit != TimeUnitInvalid {
  2791. ctx.WriteKeyWord("INTERVAL ")
  2792. }
  2793. if n.Expr != nil {
  2794. if err := n.Expr.Restore(ctx); err != nil {
  2795. return errors.Annotate(err, "An error occurred while restore FrameBound.Expr")
  2796. }
  2797. }
  2798. if n.Unit != TimeUnitInvalid {
  2799. ctx.WritePlain(" ")
  2800. ctx.WriteKeyWord(n.Unit.String())
  2801. }
  2802. if n.Type == Preceding {
  2803. ctx.WriteKeyWord(" PRECEDING")
  2804. } else {
  2805. ctx.WriteKeyWord(" FOLLOWING")
  2806. }
  2807. }
  2808. return nil
  2809. }
  2810. // Accept implements Node Accept interface.
  2811. func (n *FrameBound) Accept(v Visitor) (Node, bool) {
  2812. newNode, skipChildren := v.Enter(n)
  2813. if skipChildren {
  2814. return v.Leave(newNode)
  2815. }
  2816. n = newNode.(*FrameBound)
  2817. if n.Expr != nil {
  2818. node, ok := n.Expr.Accept(v)
  2819. if !ok {
  2820. return n, false
  2821. }
  2822. n.Expr = node.(ExprNode)
  2823. }
  2824. return v.Leave(n)
  2825. }
  2826. type SplitRegionStmt struct {
  2827. dmlNode
  2828. Table *TableName
  2829. IndexName model.CIStr
  2830. PartitionNames []model.CIStr
  2831. SplitSyntaxOpt *SplitSyntaxOption
  2832. SplitOpt *SplitOption
  2833. }
  2834. type SplitOption struct {
  2835. Lower []ExprNode
  2836. Upper []ExprNode
  2837. Num int64
  2838. ValueLists [][]ExprNode
  2839. }
  2840. type SplitSyntaxOption struct {
  2841. HasRegionFor bool
  2842. HasPartition bool
  2843. }
  2844. func (n *SplitRegionStmt) Restore(ctx *format.RestoreCtx) error {
  2845. ctx.WriteKeyWord("SPLIT ")
  2846. if n.SplitSyntaxOpt != nil {
  2847. if n.SplitSyntaxOpt.HasRegionFor {
  2848. ctx.WriteKeyWord("REGION FOR ")
  2849. }
  2850. if n.SplitSyntaxOpt.HasPartition {
  2851. ctx.WriteKeyWord("PARTITION ")
  2852. }
  2853. }
  2854. ctx.WriteKeyWord("TABLE ")
  2855. if err := n.Table.Restore(ctx); err != nil {
  2856. return errors.Annotate(err, "An error occurred while restore SplitIndexRegionStmt.Table")
  2857. }
  2858. if len(n.PartitionNames) > 0 {
  2859. ctx.WriteKeyWord(" PARTITION")
  2860. ctx.WritePlain("(")
  2861. for i, v := range n.PartitionNames {
  2862. if i != 0 {
  2863. ctx.WritePlain(", ")
  2864. }
  2865. ctx.WriteName(v.String())
  2866. }
  2867. ctx.WritePlain(")")
  2868. }
  2869. if len(n.IndexName.L) > 0 {
  2870. ctx.WriteKeyWord(" INDEX ")
  2871. ctx.WriteName(n.IndexName.String())
  2872. }
  2873. ctx.WritePlain(" ")
  2874. err := n.SplitOpt.Restore(ctx)
  2875. return err
  2876. }
  2877. func (n *SplitRegionStmt) Accept(v Visitor) (Node, bool) {
  2878. newNode, skipChildren := v.Enter(n)
  2879. if skipChildren {
  2880. return v.Leave(newNode)
  2881. }
  2882. n = newNode.(*SplitRegionStmt)
  2883. node, ok := n.Table.Accept(v)
  2884. if !ok {
  2885. return n, false
  2886. }
  2887. n.Table = node.(*TableName)
  2888. for i, val := range n.SplitOpt.Lower {
  2889. node, ok := val.Accept(v)
  2890. if !ok {
  2891. return n, false
  2892. }
  2893. n.SplitOpt.Lower[i] = node.(ExprNode)
  2894. }
  2895. for i, val := range n.SplitOpt.Upper {
  2896. node, ok := val.Accept(v)
  2897. if !ok {
  2898. return n, false
  2899. }
  2900. n.SplitOpt.Upper[i] = node.(ExprNode)
  2901. }
  2902. for i, list := range n.SplitOpt.ValueLists {
  2903. for j, val := range list {
  2904. node, ok := val.Accept(v)
  2905. if !ok {
  2906. return n, false
  2907. }
  2908. n.SplitOpt.ValueLists[i][j] = node.(ExprNode)
  2909. }
  2910. }
  2911. return v.Leave(n)
  2912. }
  2913. func (n *SplitOption) Restore(ctx *format.RestoreCtx) error {
  2914. if len(n.ValueLists) == 0 {
  2915. ctx.WriteKeyWord("BETWEEN ")
  2916. ctx.WritePlain("(")
  2917. for j, v := range n.Lower {
  2918. if j != 0 {
  2919. ctx.WritePlain(",")
  2920. }
  2921. if err := v.Restore(ctx); err != nil {
  2922. return errors.Annotatef(err, "An error occurred while restore SplitOption Lower")
  2923. }
  2924. }
  2925. ctx.WritePlain(")")
  2926. ctx.WriteKeyWord(" AND ")
  2927. ctx.WritePlain("(")
  2928. for j, v := range n.Upper {
  2929. if j != 0 {
  2930. ctx.WritePlain(",")
  2931. }
  2932. if err := v.Restore(ctx); err != nil {
  2933. return errors.Annotatef(err, "An error occurred while restore SplitOption Upper")
  2934. }
  2935. }
  2936. ctx.WritePlain(")")
  2937. ctx.WriteKeyWord(" REGIONS")
  2938. ctx.WritePlainf(" %d", n.Num)
  2939. return nil
  2940. }
  2941. ctx.WriteKeyWord("BY ")
  2942. for i, row := range n.ValueLists {
  2943. if i != 0 {
  2944. ctx.WritePlain(",")
  2945. }
  2946. ctx.WritePlain("(")
  2947. for j, v := range row {
  2948. if j != 0 {
  2949. ctx.WritePlain(",")
  2950. }
  2951. if err := v.Restore(ctx); err != nil {
  2952. return errors.Annotatef(err, "An error occurred while restore SplitOption.ValueLists[%d][%d]", i, j)
  2953. }
  2954. }
  2955. ctx.WritePlain(")")
  2956. }
  2957. return nil
  2958. }
  2959. type FulltextSearchModifier int
  2960. const (
  2961. FulltextSearchModifierNaturalLanguageMode = 0
  2962. FulltextSearchModifierBooleanMode = 1
  2963. FulltextSearchModifierModeMask = 0xF
  2964. FulltextSearchModifierWithQueryExpansion = 1 << 4
  2965. )
  2966. func (m FulltextSearchModifier) IsBooleanMode() bool {
  2967. return m&FulltextSearchModifierModeMask == FulltextSearchModifierBooleanMode
  2968. }
  2969. func (m FulltextSearchModifier) IsNaturalLanguageMode() bool {
  2970. return m&FulltextSearchModifierModeMask == FulltextSearchModifierNaturalLanguageMode
  2971. }
  2972. func (m FulltextSearchModifier) WithQueryExpansion() bool {
  2973. return m&FulltextSearchModifierWithQueryExpansion == FulltextSearchModifierWithQueryExpansion
  2974. }
  2975. type TimestampBound struct {
  2976. Mode TimestampBoundMode
  2977. Timestamp ExprNode
  2978. }
  2979. type TimestampBoundMode int
  2980. const (
  2981. TimestampBoundStrong TimestampBoundMode = iota
  2982. TimestampBoundMaxStaleness
  2983. TimestampBoundExactStaleness
  2984. TimestampBoundReadTimestamp
  2985. TimestampBoundMinReadTimestamp
  2986. )