expressions.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. "fmt"
  16. "io"
  17. "reflect"
  18. "regexp"
  19. "strings"
  20. "github.com/pingcap/errors"
  21. "github.com/pingcap/parser/format"
  22. "github.com/pingcap/parser/model"
  23. "github.com/pingcap/parser/opcode"
  24. )
  25. var (
  26. _ ExprNode = &BetweenExpr{}
  27. _ ExprNode = &BinaryOperationExpr{}
  28. _ ExprNode = &CaseExpr{}
  29. _ ExprNode = &ColumnNameExpr{}
  30. _ ExprNode = &TableNameExpr{}
  31. _ ExprNode = &CompareSubqueryExpr{}
  32. _ ExprNode = &DefaultExpr{}
  33. _ ExprNode = &ExistsSubqueryExpr{}
  34. _ ExprNode = &IsNullExpr{}
  35. _ ExprNode = &IsTruthExpr{}
  36. _ ExprNode = &ParenthesesExpr{}
  37. _ ExprNode = &PatternInExpr{}
  38. _ ExprNode = &PatternLikeExpr{}
  39. _ ExprNode = &PatternRegexpExpr{}
  40. _ ExprNode = &PositionExpr{}
  41. _ ExprNode = &RowExpr{}
  42. _ ExprNode = &SubqueryExpr{}
  43. _ ExprNode = &UnaryOperationExpr{}
  44. _ ExprNode = &ValuesExpr{}
  45. _ ExprNode = &VariableExpr{}
  46. _ ExprNode = &MatchAgainst{}
  47. _ ExprNode = &SetCollationExpr{}
  48. _ Node = &ColumnName{}
  49. _ Node = &WhenClause{}
  50. )
  51. // ValueExpr define a interface for ValueExpr.
  52. type ValueExpr interface {
  53. ExprNode
  54. SetValue(val interface{})
  55. GetValue() interface{}
  56. GetDatumString() string
  57. GetString() string
  58. GetProjectionOffset() int
  59. SetProjectionOffset(offset int)
  60. }
  61. // NewValueExpr creates a ValueExpr with value, and sets default field type.
  62. var NewValueExpr func(value interface{}, charset string, collate string) ValueExpr
  63. // NewParamMarkerExpr creates a ParamMarkerExpr.
  64. var NewParamMarkerExpr func(offset int) ParamMarkerExpr
  65. // BetweenExpr is for "between and" or "not between and" expression.
  66. type BetweenExpr struct {
  67. exprNode
  68. // Expr is the expression to be checked.
  69. Expr ExprNode
  70. // Left is the expression for minimal value in the range.
  71. Left ExprNode
  72. // Right is the expression for maximum value in the range.
  73. Right ExprNode
  74. // Not is true, the expression is "not between and".
  75. Not bool
  76. }
  77. // Restore implements Node interface.
  78. func (n *BetweenExpr) Restore(ctx *format.RestoreCtx) error {
  79. if err := n.Expr.Restore(ctx); err != nil {
  80. return errors.Annotate(err, "An error occurred while restore BetweenExpr.Expr")
  81. }
  82. if n.Not {
  83. ctx.WriteKeyWord(" NOT BETWEEN ")
  84. } else {
  85. ctx.WriteKeyWord(" BETWEEN ")
  86. }
  87. if err := n.Left.Restore(ctx); err != nil {
  88. return errors.Annotate(err, "An error occurred while restore BetweenExpr.Left")
  89. }
  90. ctx.WriteKeyWord(" AND ")
  91. if err := n.Right.Restore(ctx); err != nil {
  92. return errors.Annotate(err, "An error occurred while restore BetweenExpr.Right ")
  93. }
  94. return nil
  95. }
  96. // Format the ExprNode into a Writer.
  97. func (n *BetweenExpr) Format(w io.Writer) {
  98. n.Expr.Format(w)
  99. if n.Not {
  100. fmt.Fprint(w, " NOT BETWEEN ")
  101. } else {
  102. fmt.Fprint(w, " BETWEEN ")
  103. }
  104. n.Left.Format(w)
  105. fmt.Fprint(w, " AND ")
  106. n.Right.Format(w)
  107. }
  108. // Accept implements Node interface.
  109. func (n *BetweenExpr) Accept(v Visitor) (Node, bool) {
  110. newNode, skipChildren := v.Enter(n)
  111. if skipChildren {
  112. return v.Leave(newNode)
  113. }
  114. n = newNode.(*BetweenExpr)
  115. node, ok := n.Expr.Accept(v)
  116. if !ok {
  117. return n, false
  118. }
  119. n.Expr = node.(ExprNode)
  120. node, ok = n.Left.Accept(v)
  121. if !ok {
  122. return n, false
  123. }
  124. n.Left = node.(ExprNode)
  125. node, ok = n.Right.Accept(v)
  126. if !ok {
  127. return n, false
  128. }
  129. n.Right = node.(ExprNode)
  130. return v.Leave(n)
  131. }
  132. // BinaryOperationExpr is for binary operation like `1 + 1`, `1 - 1`, etc.
  133. type BinaryOperationExpr struct {
  134. exprNode
  135. // Op is the operator code for BinaryOperation.
  136. Op opcode.Op
  137. // L is the left expression in BinaryOperation.
  138. L ExprNode
  139. // R is the right expression in BinaryOperation.
  140. R ExprNode
  141. }
  142. func restoreBinaryOpWithSpacesAround(ctx *format.RestoreCtx, op opcode.Op) error {
  143. shouldInsertSpace := ctx.Flags.HasSpacesAroundBinaryOperationFlag() || op.IsKeyword()
  144. if shouldInsertSpace {
  145. ctx.WritePlain(" ")
  146. }
  147. if err := op.Restore(ctx); err != nil {
  148. return err // no need to annotate, the caller will annotate.
  149. }
  150. if shouldInsertSpace {
  151. ctx.WritePlain(" ")
  152. }
  153. return nil
  154. }
  155. // Restore implements Node interface.
  156. func (n *BinaryOperationExpr) Restore(ctx *format.RestoreCtx) error {
  157. if err := n.L.Restore(ctx); err != nil {
  158. return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.L")
  159. }
  160. if err := restoreBinaryOpWithSpacesAround(ctx, n.Op); err != nil {
  161. return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.Op")
  162. }
  163. if err := n.R.Restore(ctx); err != nil {
  164. return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.R")
  165. }
  166. return nil
  167. }
  168. // Format the ExprNode into a Writer.
  169. func (n *BinaryOperationExpr) Format(w io.Writer) {
  170. n.L.Format(w)
  171. fmt.Fprint(w, " ")
  172. n.Op.Format(w)
  173. fmt.Fprint(w, " ")
  174. n.R.Format(w)
  175. }
  176. // Accept implements Node interface.
  177. func (n *BinaryOperationExpr) Accept(v Visitor) (Node, bool) {
  178. newNode, skipChildren := v.Enter(n)
  179. if skipChildren {
  180. return v.Leave(newNode)
  181. }
  182. n = newNode.(*BinaryOperationExpr)
  183. node, ok := n.L.Accept(v)
  184. if !ok {
  185. return n, false
  186. }
  187. n.L = node.(ExprNode)
  188. node, ok = n.R.Accept(v)
  189. if !ok {
  190. return n, false
  191. }
  192. n.R = node.(ExprNode)
  193. return v.Leave(n)
  194. }
  195. // WhenClause is the when clause in Case expression for "when condition then result".
  196. type WhenClause struct {
  197. node
  198. // Expr is the condition expression in WhenClause.
  199. Expr ExprNode
  200. // Result is the result expression in WhenClause.
  201. Result ExprNode
  202. }
  203. // Restore implements Node interface.
  204. func (n *WhenClause) Restore(ctx *format.RestoreCtx) error {
  205. ctx.WriteKeyWord("WHEN ")
  206. if err := n.Expr.Restore(ctx); err != nil {
  207. return errors.Annotate(err, "An error occurred while restore WhenClauses.Expr")
  208. }
  209. ctx.WriteKeyWord(" THEN ")
  210. if err := n.Result.Restore(ctx); err != nil {
  211. return errors.Annotate(err, "An error occurred while restore WhenClauses.Result")
  212. }
  213. return nil
  214. }
  215. // Accept implements Node Accept interface.
  216. func (n *WhenClause) Accept(v Visitor) (Node, bool) {
  217. newNode, skipChildren := v.Enter(n)
  218. if skipChildren {
  219. return v.Leave(newNode)
  220. }
  221. n = newNode.(*WhenClause)
  222. node, ok := n.Expr.Accept(v)
  223. if !ok {
  224. return n, false
  225. }
  226. n.Expr = node.(ExprNode)
  227. node, ok = n.Result.Accept(v)
  228. if !ok {
  229. return n, false
  230. }
  231. n.Result = node.(ExprNode)
  232. return v.Leave(n)
  233. }
  234. // CaseExpr is the case expression.
  235. type CaseExpr struct {
  236. exprNode
  237. // Value is the compare value expression.
  238. Value ExprNode
  239. // WhenClauses is the condition check expression.
  240. WhenClauses []*WhenClause
  241. // ElseClause is the else result expression.
  242. ElseClause ExprNode
  243. }
  244. // Restore implements Node interface.
  245. func (n *CaseExpr) Restore(ctx *format.RestoreCtx) error {
  246. ctx.WriteKeyWord("CASE")
  247. if n.Value != nil {
  248. ctx.WritePlain(" ")
  249. if err := n.Value.Restore(ctx); err != nil {
  250. return errors.Annotate(err, "An error occurred while restore CaseExpr.Value")
  251. }
  252. }
  253. for _, clause := range n.WhenClauses {
  254. ctx.WritePlain(" ")
  255. if err := clause.Restore(ctx); err != nil {
  256. return errors.Annotate(err, "An error occurred while restore CaseExpr.WhenClauses")
  257. }
  258. }
  259. if n.ElseClause != nil {
  260. ctx.WriteKeyWord(" ELSE ")
  261. if err := n.ElseClause.Restore(ctx); err != nil {
  262. return errors.Annotate(err, "An error occurred while restore CaseExpr.ElseClause")
  263. }
  264. }
  265. ctx.WriteKeyWord(" END")
  266. return nil
  267. }
  268. // Format the ExprNode into a Writer.
  269. func (n *CaseExpr) Format(w io.Writer) {
  270. fmt.Fprint(w, "CASE")
  271. // Because the presence of `case when` syntax, `Value` could be nil and we need check this.
  272. if n.Value != nil {
  273. fmt.Fprint(w, " ")
  274. n.Value.Format(w)
  275. }
  276. for _, clause := range n.WhenClauses {
  277. fmt.Fprint(w, " ")
  278. fmt.Fprint(w, "WHEN ")
  279. clause.Expr.Format(w)
  280. fmt.Fprint(w, " THEN ")
  281. clause.Result.Format(w)
  282. }
  283. if n.ElseClause != nil {
  284. fmt.Fprint(w, " ELSE ")
  285. n.ElseClause.Format(w)
  286. }
  287. fmt.Fprint(w, " END")
  288. }
  289. // Accept implements Node Accept interface.
  290. func (n *CaseExpr) Accept(v Visitor) (Node, bool) {
  291. newNode, skipChildren := v.Enter(n)
  292. if skipChildren {
  293. return v.Leave(newNode)
  294. }
  295. n = newNode.(*CaseExpr)
  296. if n.Value != nil {
  297. node, ok := n.Value.Accept(v)
  298. if !ok {
  299. return n, false
  300. }
  301. n.Value = node.(ExprNode)
  302. }
  303. for i, val := range n.WhenClauses {
  304. node, ok := val.Accept(v)
  305. if !ok {
  306. return n, false
  307. }
  308. n.WhenClauses[i] = node.(*WhenClause)
  309. }
  310. if n.ElseClause != nil {
  311. node, ok := n.ElseClause.Accept(v)
  312. if !ok {
  313. return n, false
  314. }
  315. n.ElseClause = node.(ExprNode)
  316. }
  317. return v.Leave(n)
  318. }
  319. // SubqueryExpr represents a subquery.
  320. type SubqueryExpr struct {
  321. exprNode
  322. // Query is the query SelectNode.
  323. Query ResultSetNode
  324. Evaluated bool
  325. Correlated bool
  326. MultiRows bool
  327. Exists bool
  328. }
  329. // Restore implements Node interface.
  330. func (n *SubqueryExpr) Restore(ctx *format.RestoreCtx) error {
  331. ctx.WritePlain("(")
  332. if err := n.Query.Restore(ctx); err != nil {
  333. return errors.Annotate(err, "An error occurred while restore SubqueryExpr.Query")
  334. }
  335. ctx.WritePlain(")")
  336. return nil
  337. }
  338. // Format the ExprNode into a Writer.
  339. func (n *SubqueryExpr) Format(w io.Writer) {
  340. panic("Not implemented")
  341. }
  342. // Accept implements Node Accept interface.
  343. func (n *SubqueryExpr) Accept(v Visitor) (Node, bool) {
  344. newNode, skipChildren := v.Enter(n)
  345. if skipChildren {
  346. return v.Leave(newNode)
  347. }
  348. n = newNode.(*SubqueryExpr)
  349. node, ok := n.Query.Accept(v)
  350. if !ok {
  351. return n, false
  352. }
  353. n.Query = node.(ResultSetNode)
  354. return v.Leave(n)
  355. }
  356. // CompareSubqueryExpr is the expression for "expr cmp (select ...)".
  357. // See https://dev.mysql.com/doc/refman/5.7/en/comparisons-using-subqueries.html
  358. // See https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html
  359. // See https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html
  360. type CompareSubqueryExpr struct {
  361. exprNode
  362. // L is the left expression
  363. L ExprNode
  364. // Op is the comparison opcode.
  365. Op opcode.Op
  366. // R is the subquery for right expression, may be rewritten to other type of expression.
  367. R ExprNode
  368. // All is true, we should compare all records in subquery.
  369. All bool
  370. }
  371. // Restore implements Node interface.
  372. func (n *CompareSubqueryExpr) Restore(ctx *format.RestoreCtx) error {
  373. if err := n.L.Restore(ctx); err != nil {
  374. return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.L")
  375. }
  376. if err := restoreBinaryOpWithSpacesAround(ctx, n.Op); err != nil {
  377. return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.Op")
  378. }
  379. if n.All {
  380. ctx.WriteKeyWord("ALL ")
  381. } else {
  382. ctx.WriteKeyWord("ANY ")
  383. }
  384. if err := n.R.Restore(ctx); err != nil {
  385. return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.R")
  386. }
  387. return nil
  388. }
  389. // Format the ExprNode into a Writer.
  390. func (n *CompareSubqueryExpr) Format(w io.Writer) {
  391. panic("Not implemented")
  392. }
  393. // Accept implements Node Accept interface.
  394. func (n *CompareSubqueryExpr) Accept(v Visitor) (Node, bool) {
  395. newNode, skipChildren := v.Enter(n)
  396. if skipChildren {
  397. return v.Leave(newNode)
  398. }
  399. n = newNode.(*CompareSubqueryExpr)
  400. node, ok := n.L.Accept(v)
  401. if !ok {
  402. return n, false
  403. }
  404. n.L = node.(ExprNode)
  405. node, ok = n.R.Accept(v)
  406. if !ok {
  407. return n, false
  408. }
  409. n.R = node.(ExprNode)
  410. return v.Leave(n)
  411. }
  412. // TableNameExpr represents a table-level object name expression, such as sequence/table/view etc.
  413. type TableNameExpr struct {
  414. exprNode
  415. // Name is the referenced object name expression.
  416. Name *TableName
  417. }
  418. // Restore implements Node interface.
  419. func (n *TableNameExpr) Restore(ctx *format.RestoreCtx) error {
  420. if err := n.Name.Restore(ctx); err != nil {
  421. return errors.Trace(err)
  422. }
  423. return nil
  424. }
  425. // Format the ExprNode into a Writer.
  426. func (n *TableNameExpr) Format(w io.Writer) {
  427. dbName, tbName := n.Name.Schema.L, n.Name.Name.L
  428. if dbName == "" {
  429. fmt.Fprintf(w, "`%s`", tbName)
  430. } else {
  431. fmt.Fprintf(w, "`%s`.`%s`", dbName, tbName)
  432. }
  433. }
  434. // Accept implements Node Accept interface.
  435. func (n *TableNameExpr) Accept(v Visitor) (Node, bool) {
  436. newNode, skipChildren := v.Enter(n)
  437. if skipChildren {
  438. return v.Leave(newNode)
  439. }
  440. n = newNode.(*TableNameExpr)
  441. node, ok := n.Name.Accept(v)
  442. if !ok {
  443. return n, false
  444. }
  445. n.Name = node.(*TableName)
  446. return v.Leave(n)
  447. }
  448. // ColumnName represents column name.
  449. type ColumnName struct {
  450. node
  451. Schema model.CIStr
  452. Table model.CIStr
  453. Name model.CIStr
  454. }
  455. // Restore implements Node interface.
  456. func (n *ColumnName) Restore(ctx *format.RestoreCtx) error {
  457. if n.Schema.O != "" {
  458. ctx.WriteName(n.Schema.O)
  459. ctx.WritePlain(".")
  460. }
  461. if n.Table.O != "" {
  462. ctx.WriteName(n.Table.O)
  463. ctx.WritePlain(".")
  464. }
  465. ctx.WriteName(n.Name.O)
  466. return nil
  467. }
  468. // Accept implements Node Accept interface.
  469. func (n *ColumnName) Accept(v Visitor) (Node, bool) {
  470. newNode, skipChildren := v.Enter(n)
  471. if skipChildren {
  472. return v.Leave(newNode)
  473. }
  474. n = newNode.(*ColumnName)
  475. return v.Leave(n)
  476. }
  477. // String implements Stringer interface.
  478. func (n *ColumnName) String() string {
  479. result := n.Name.L
  480. if n.Table.L != "" {
  481. result = n.Table.L + "." + result
  482. }
  483. if n.Schema.L != "" {
  484. result = n.Schema.L + "." + result
  485. }
  486. return result
  487. }
  488. // OrigColName returns the full original column name.
  489. func (n *ColumnName) OrigColName() (ret string) {
  490. ret = n.Name.O
  491. if n.Table.O == "" {
  492. return
  493. }
  494. ret = n.Table.O + "." + ret
  495. if n.Schema.O == "" {
  496. return
  497. }
  498. ret = n.Schema.O + "." + ret
  499. return
  500. }
  501. // ColumnNameExpr represents a column name expression.
  502. type ColumnNameExpr struct {
  503. exprNode
  504. // Name is the referenced column name.
  505. Name *ColumnName
  506. // Refer is the result field the column name refers to.
  507. // The value of Refer.Expr is used as the value of the expression.
  508. Refer *ResultField
  509. }
  510. // Restore implements Node interface.
  511. func (n *ColumnNameExpr) Restore(ctx *format.RestoreCtx) error {
  512. if err := n.Name.Restore(ctx); err != nil {
  513. return errors.Trace(err)
  514. }
  515. return nil
  516. }
  517. // Format the ExprNode into a Writer.
  518. func (n *ColumnNameExpr) Format(w io.Writer) {
  519. name := strings.Replace(n.Name.String(), ".", "`.`", -1)
  520. fmt.Fprintf(w, "`%s`", name)
  521. }
  522. // Accept implements Node Accept interface.
  523. func (n *ColumnNameExpr) Accept(v Visitor) (Node, bool) {
  524. newNode, skipChildren := v.Enter(n)
  525. if skipChildren {
  526. return v.Leave(newNode)
  527. }
  528. n = newNode.(*ColumnNameExpr)
  529. node, ok := n.Name.Accept(v)
  530. if !ok {
  531. return n, false
  532. }
  533. n.Name = node.(*ColumnName)
  534. return v.Leave(n)
  535. }
  536. // DefaultExpr is the default expression using default value for a column.
  537. type DefaultExpr struct {
  538. exprNode
  539. // Name is the column name.
  540. Name *ColumnName
  541. }
  542. // Restore implements Node interface.
  543. func (n *DefaultExpr) Restore(ctx *format.RestoreCtx) error {
  544. ctx.WriteKeyWord("DEFAULT")
  545. if n.Name != nil {
  546. ctx.WritePlain("(")
  547. if err := n.Name.Restore(ctx); err != nil {
  548. return errors.Annotate(err, "An error occurred while restore DefaultExpr.Name")
  549. }
  550. ctx.WritePlain(")")
  551. }
  552. return nil
  553. }
  554. // Format the ExprNode into a Writer.
  555. func (n *DefaultExpr) Format(w io.Writer) {
  556. panic("Not implemented")
  557. }
  558. // Accept implements Node Accept interface.
  559. func (n *DefaultExpr) Accept(v Visitor) (Node, bool) {
  560. newNode, skipChildren := v.Enter(n)
  561. if skipChildren {
  562. return v.Leave(newNode)
  563. }
  564. n = newNode.(*DefaultExpr)
  565. return v.Leave(n)
  566. }
  567. // ExistsSubqueryExpr is the expression for "exists (select ...)".
  568. // See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
  569. type ExistsSubqueryExpr struct {
  570. exprNode
  571. // Sel is the subquery, may be rewritten to other type of expression.
  572. Sel ExprNode
  573. // Not is true, the expression is "not exists".
  574. Not bool
  575. }
  576. // Restore implements Node interface.
  577. func (n *ExistsSubqueryExpr) Restore(ctx *format.RestoreCtx) error {
  578. if n.Not {
  579. ctx.WriteKeyWord("NOT EXISTS ")
  580. } else {
  581. ctx.WriteKeyWord("EXISTS ")
  582. }
  583. if err := n.Sel.Restore(ctx); err != nil {
  584. return errors.Annotate(err, "An error occurred while restore ExistsSubqueryExpr.Sel")
  585. }
  586. return nil
  587. }
  588. // Format the ExprNode into a Writer.
  589. func (n *ExistsSubqueryExpr) Format(w io.Writer) {
  590. panic("Not implemented")
  591. }
  592. // Accept implements Node Accept interface.
  593. func (n *ExistsSubqueryExpr) Accept(v Visitor) (Node, bool) {
  594. newNode, skipChildren := v.Enter(n)
  595. if skipChildren {
  596. return v.Leave(newNode)
  597. }
  598. n = newNode.(*ExistsSubqueryExpr)
  599. node, ok := n.Sel.Accept(v)
  600. if !ok {
  601. return n, false
  602. }
  603. n.Sel = node.(ExprNode)
  604. return v.Leave(n)
  605. }
  606. // PatternInExpr is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)".
  607. type PatternInExpr struct {
  608. exprNode
  609. // Expr is the value expression to be compared.
  610. Expr ExprNode
  611. // List is the list expression in compare list.
  612. List []ExprNode
  613. // Not is true, the expression is "not in".
  614. Not bool
  615. // Sel is the subquery, may be rewritten to other type of expression.
  616. Sel ExprNode
  617. }
  618. // Restore implements Node interface.
  619. func (n *PatternInExpr) Restore(ctx *format.RestoreCtx) error {
  620. if err := n.Expr.Restore(ctx); err != nil {
  621. return errors.Annotate(err, "An error occurred while restore PatternInExpr.Expr")
  622. }
  623. if n.Not {
  624. ctx.WriteKeyWord(" NOT IN ")
  625. } else {
  626. ctx.WriteKeyWord(" IN ")
  627. }
  628. if n.Sel != nil {
  629. if err := n.Sel.Restore(ctx); err != nil {
  630. return errors.Annotate(err, "An error occurred while restore PatternInExpr.Sel")
  631. }
  632. } else {
  633. ctx.WritePlain("(")
  634. for i, expr := range n.List {
  635. if i != 0 {
  636. ctx.WritePlain(",")
  637. }
  638. if err := expr.Restore(ctx); err != nil {
  639. return errors.Annotatef(err, "An error occurred while restore PatternInExpr.List[%d]", i)
  640. }
  641. }
  642. ctx.WritePlain(")")
  643. }
  644. return nil
  645. }
  646. // Format the ExprNode into a Writer.
  647. func (n *PatternInExpr) Format(w io.Writer) {
  648. n.Expr.Format(w)
  649. if n.Not {
  650. fmt.Fprint(w, " NOT IN (")
  651. } else {
  652. fmt.Fprint(w, " IN (")
  653. }
  654. for i, expr := range n.List {
  655. if i != 0 {
  656. fmt.Fprint(w, ",")
  657. }
  658. expr.Format(w)
  659. }
  660. fmt.Fprint(w, ")")
  661. }
  662. // Accept implements Node Accept interface.
  663. func (n *PatternInExpr) Accept(v Visitor) (Node, bool) {
  664. newNode, skipChildren := v.Enter(n)
  665. if skipChildren {
  666. return v.Leave(newNode)
  667. }
  668. n = newNode.(*PatternInExpr)
  669. node, ok := n.Expr.Accept(v)
  670. if !ok {
  671. return n, false
  672. }
  673. n.Expr = node.(ExprNode)
  674. for i, val := range n.List {
  675. node, ok = val.Accept(v)
  676. if !ok {
  677. return n, false
  678. }
  679. n.List[i] = node.(ExprNode)
  680. }
  681. if n.Sel != nil {
  682. node, ok = n.Sel.Accept(v)
  683. if !ok {
  684. return n, false
  685. }
  686. n.Sel = node.(ExprNode)
  687. }
  688. return v.Leave(n)
  689. }
  690. // IsNullExpr is the expression for null check.
  691. type IsNullExpr struct {
  692. exprNode
  693. // Expr is the expression to be checked.
  694. Expr ExprNode
  695. // Not is true, the expression is "is not null".
  696. Not bool
  697. }
  698. // Restore implements Node interface.
  699. func (n *IsNullExpr) Restore(ctx *format.RestoreCtx) error {
  700. if err := n.Expr.Restore(ctx); err != nil {
  701. return errors.Trace(err)
  702. }
  703. if n.Not {
  704. ctx.WriteKeyWord(" IS NOT NULL")
  705. } else {
  706. ctx.WriteKeyWord(" IS NULL")
  707. }
  708. return nil
  709. }
  710. // Format the ExprNode into a Writer.
  711. func (n *IsNullExpr) Format(w io.Writer) {
  712. n.Expr.Format(w)
  713. if n.Not {
  714. fmt.Fprint(w, " IS NOT NULL")
  715. return
  716. }
  717. fmt.Fprint(w, " IS NULL")
  718. }
  719. // Accept implements Node Accept interface.
  720. func (n *IsNullExpr) Accept(v Visitor) (Node, bool) {
  721. newNode, skipChildren := v.Enter(n)
  722. if skipChildren {
  723. return v.Leave(newNode)
  724. }
  725. n = newNode.(*IsNullExpr)
  726. node, ok := n.Expr.Accept(v)
  727. if !ok {
  728. return n, false
  729. }
  730. n.Expr = node.(ExprNode)
  731. return v.Leave(n)
  732. }
  733. // IsTruthExpr is the expression for true/false check.
  734. type IsTruthExpr struct {
  735. exprNode
  736. // Expr is the expression to be checked.
  737. Expr ExprNode
  738. // Not is true, the expression is "is not true/false".
  739. Not bool
  740. // True indicates checking true or false.
  741. True int64
  742. }
  743. // Restore implements Node interface.
  744. func (n *IsTruthExpr) Restore(ctx *format.RestoreCtx) error {
  745. if err := n.Expr.Restore(ctx); err != nil {
  746. return errors.Trace(err)
  747. }
  748. if n.Not {
  749. ctx.WriteKeyWord(" IS NOT")
  750. } else {
  751. ctx.WriteKeyWord(" IS")
  752. }
  753. if n.True > 0 {
  754. ctx.WriteKeyWord(" TRUE")
  755. } else {
  756. ctx.WriteKeyWord(" FALSE")
  757. }
  758. return nil
  759. }
  760. // Format the ExprNode into a Writer.
  761. func (n *IsTruthExpr) Format(w io.Writer) {
  762. n.Expr.Format(w)
  763. if n.Not {
  764. fmt.Fprint(w, " IS NOT")
  765. } else {
  766. fmt.Fprint(w, " IS")
  767. }
  768. if n.True > 0 {
  769. fmt.Fprint(w, " TRUE")
  770. } else {
  771. fmt.Fprint(w, " FALSE")
  772. }
  773. }
  774. // Accept implements Node Accept interface.
  775. func (n *IsTruthExpr) Accept(v Visitor) (Node, bool) {
  776. newNode, skipChildren := v.Enter(n)
  777. if skipChildren {
  778. return v.Leave(newNode)
  779. }
  780. n = newNode.(*IsTruthExpr)
  781. node, ok := n.Expr.Accept(v)
  782. if !ok {
  783. return n, false
  784. }
  785. n.Expr = node.(ExprNode)
  786. return v.Leave(n)
  787. }
  788. // PatternLikeExpr is the expression for like operator, e.g, expr like "%123%"
  789. type PatternLikeExpr struct {
  790. exprNode
  791. // Expr is the expression to be checked.
  792. Expr ExprNode
  793. // Pattern is the like expression.
  794. Pattern ExprNode
  795. // Not is true, the expression is "not like".
  796. Not bool
  797. Escape byte
  798. PatChars []byte
  799. PatTypes []byte
  800. }
  801. // Restore implements Node interface.
  802. func (n *PatternLikeExpr) Restore(ctx *format.RestoreCtx) error {
  803. if err := n.Expr.Restore(ctx); err != nil {
  804. return errors.Annotate(err, "An error occurred while restore PatternLikeExpr.Expr")
  805. }
  806. if n.Not {
  807. ctx.WriteKeyWord(" NOT LIKE ")
  808. } else {
  809. ctx.WriteKeyWord(" LIKE ")
  810. }
  811. if err := n.Pattern.Restore(ctx); err != nil {
  812. return errors.Annotate(err, "An error occurred while restore PatternLikeExpr.Pattern")
  813. }
  814. escape := string(n.Escape)
  815. if escape != "\\" {
  816. ctx.WriteKeyWord(" ESCAPE ")
  817. ctx.WriteString(escape)
  818. }
  819. return nil
  820. }
  821. // Format the ExprNode into a Writer.
  822. func (n *PatternLikeExpr) Format(w io.Writer) {
  823. n.Expr.Format(w)
  824. if n.Not {
  825. fmt.Fprint(w, " NOT LIKE ")
  826. } else {
  827. fmt.Fprint(w, " LIKE ")
  828. }
  829. n.Pattern.Format(w)
  830. if n.Escape != '\\' {
  831. fmt.Fprint(w, " ESCAPE ")
  832. fmt.Fprintf(w, "'%c'", n.Escape)
  833. }
  834. }
  835. // Accept implements Node Accept interface.
  836. func (n *PatternLikeExpr) Accept(v Visitor) (Node, bool) {
  837. newNode, skipChildren := v.Enter(n)
  838. if skipChildren {
  839. return v.Leave(newNode)
  840. }
  841. n = newNode.(*PatternLikeExpr)
  842. if n.Expr != nil {
  843. node, ok := n.Expr.Accept(v)
  844. if !ok {
  845. return n, false
  846. }
  847. n.Expr = node.(ExprNode)
  848. }
  849. if n.Pattern != nil {
  850. node, ok := n.Pattern.Accept(v)
  851. if !ok {
  852. return n, false
  853. }
  854. n.Pattern = node.(ExprNode)
  855. }
  856. return v.Leave(n)
  857. }
  858. // ParamMarkerExpr expression holds a place for another expression.
  859. // Used in parsing prepare statement.
  860. type ParamMarkerExpr interface {
  861. ValueExpr
  862. SetOrder(int)
  863. }
  864. // ParenthesesExpr is the parentheses expression.
  865. type ParenthesesExpr struct {
  866. exprNode
  867. // Expr is the expression in parentheses.
  868. Expr ExprNode
  869. }
  870. // Restore implements Node interface.
  871. func (n *ParenthesesExpr) Restore(ctx *format.RestoreCtx) error {
  872. ctx.WritePlain("(")
  873. if err := n.Expr.Restore(ctx); err != nil {
  874. return errors.Annotate(err, "An error occurred when restore ParenthesesExpr.Expr")
  875. }
  876. ctx.WritePlain(")")
  877. return nil
  878. }
  879. // Format the ExprNode into a Writer.
  880. func (n *ParenthesesExpr) Format(w io.Writer) {
  881. fmt.Fprint(w, "(")
  882. n.Expr.Format(w)
  883. fmt.Fprint(w, ")")
  884. }
  885. // Accept implements Node Accept interface.
  886. func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) {
  887. newNode, skipChildren := v.Enter(n)
  888. if skipChildren {
  889. return v.Leave(newNode)
  890. }
  891. n = newNode.(*ParenthesesExpr)
  892. if n.Expr != nil {
  893. node, ok := n.Expr.Accept(v)
  894. if !ok {
  895. return n, false
  896. }
  897. n.Expr = node.(ExprNode)
  898. }
  899. return v.Leave(n)
  900. }
  901. // PositionExpr is the expression for order by and group by position.
  902. // MySQL use position expression started from 1, it looks a little confused inner.
  903. // maybe later we will use 0 at first.
  904. type PositionExpr struct {
  905. exprNode
  906. // N is the position, started from 1 now.
  907. N int
  908. // P is the parameterized position.
  909. P ExprNode
  910. // Refer is the result field the position refers to.
  911. Refer *ResultField
  912. }
  913. // Restore implements Node interface.
  914. func (n *PositionExpr) Restore(ctx *format.RestoreCtx) error {
  915. ctx.WritePlainf("%d", n.N)
  916. return nil
  917. }
  918. // Format the ExprNode into a Writer.
  919. func (n *PositionExpr) Format(w io.Writer) {
  920. panic("Not implemented")
  921. }
  922. // Accept implements Node Accept interface.
  923. func (n *PositionExpr) Accept(v Visitor) (Node, bool) {
  924. newNode, skipChildren := v.Enter(n)
  925. if skipChildren {
  926. return v.Leave(newNode)
  927. }
  928. n = newNode.(*PositionExpr)
  929. if n.P != nil {
  930. node, ok := n.P.Accept(v)
  931. if !ok {
  932. return n, false
  933. }
  934. n.P = node.(ExprNode)
  935. }
  936. return v.Leave(n)
  937. }
  938. // PatternRegexpExpr is the pattern expression for pattern match.
  939. type PatternRegexpExpr struct {
  940. exprNode
  941. // Expr is the expression to be checked.
  942. Expr ExprNode
  943. // Pattern is the expression for pattern.
  944. Pattern ExprNode
  945. // Not is true, the expression is "not rlike",
  946. Not bool
  947. // Re is the compiled regexp.
  948. Re *regexp.Regexp
  949. // Sexpr is the string for Expr expression.
  950. Sexpr *string
  951. }
  952. // Restore implements Node interface.
  953. func (n *PatternRegexpExpr) Restore(ctx *format.RestoreCtx) error {
  954. if err := n.Expr.Restore(ctx); err != nil {
  955. return errors.Annotate(err, "An error occurred while restore PatternRegexpExpr.Expr")
  956. }
  957. if n.Not {
  958. ctx.WriteKeyWord(" NOT REGEXP ")
  959. } else {
  960. ctx.WriteKeyWord(" REGEXP ")
  961. }
  962. if err := n.Pattern.Restore(ctx); err != nil {
  963. return errors.Annotate(err, "An error occurred while restore PatternRegexpExpr.Pattern")
  964. }
  965. return nil
  966. }
  967. // Format the ExprNode into a Writer.
  968. func (n *PatternRegexpExpr) Format(w io.Writer) {
  969. n.Expr.Format(w)
  970. if n.Not {
  971. fmt.Fprint(w, " NOT REGEXP ")
  972. } else {
  973. fmt.Fprint(w, " REGEXP ")
  974. }
  975. n.Pattern.Format(w)
  976. }
  977. // Accept implements Node Accept interface.
  978. func (n *PatternRegexpExpr) Accept(v Visitor) (Node, bool) {
  979. newNode, skipChildren := v.Enter(n)
  980. if skipChildren {
  981. return v.Leave(newNode)
  982. }
  983. n = newNode.(*PatternRegexpExpr)
  984. node, ok := n.Expr.Accept(v)
  985. if !ok {
  986. return n, false
  987. }
  988. n.Expr = node.(ExprNode)
  989. node, ok = n.Pattern.Accept(v)
  990. if !ok {
  991. return n, false
  992. }
  993. n.Pattern = node.(ExprNode)
  994. return v.Leave(n)
  995. }
  996. // RowExpr is the expression for row constructor.
  997. // See https://dev.mysql.com/doc/refman/5.7/en/row-subqueries.html
  998. type RowExpr struct {
  999. exprNode
  1000. Values []ExprNode
  1001. }
  1002. // Restore implements Node interface.
  1003. func (n *RowExpr) Restore(ctx *format.RestoreCtx) error {
  1004. ctx.WriteKeyWord("ROW")
  1005. ctx.WritePlain("(")
  1006. for i, v := range n.Values {
  1007. if i != 0 {
  1008. ctx.WritePlain(",")
  1009. }
  1010. if err := v.Restore(ctx); err != nil {
  1011. return errors.Annotatef(err, "An error occurred when restore RowExpr.Values[%v]", i)
  1012. }
  1013. }
  1014. ctx.WritePlain(")")
  1015. return nil
  1016. }
  1017. // Format the ExprNode into a Writer.
  1018. func (n *RowExpr) Format(w io.Writer) {
  1019. panic("Not implemented")
  1020. }
  1021. // Accept implements Node Accept interface.
  1022. func (n *RowExpr) Accept(v Visitor) (Node, bool) {
  1023. newNode, skipChildren := v.Enter(n)
  1024. if skipChildren {
  1025. return v.Leave(newNode)
  1026. }
  1027. n = newNode.(*RowExpr)
  1028. for i, val := range n.Values {
  1029. node, ok := val.Accept(v)
  1030. if !ok {
  1031. return n, false
  1032. }
  1033. n.Values[i] = node.(ExprNode)
  1034. }
  1035. return v.Leave(n)
  1036. }
  1037. // UnaryOperationExpr is the expression for unary operator.
  1038. type UnaryOperationExpr struct {
  1039. exprNode
  1040. // Op is the operator opcode.
  1041. Op opcode.Op
  1042. // V is the unary expression.
  1043. V ExprNode
  1044. }
  1045. // Restore implements Node interface.
  1046. func (n *UnaryOperationExpr) Restore(ctx *format.RestoreCtx) error {
  1047. if err := n.Op.Restore(ctx); err != nil {
  1048. return errors.Trace(err)
  1049. }
  1050. if err := n.V.Restore(ctx); err != nil {
  1051. return errors.Trace(err)
  1052. }
  1053. return nil
  1054. }
  1055. // Format the ExprNode into a Writer.
  1056. func (n *UnaryOperationExpr) Format(w io.Writer) {
  1057. n.Op.Format(w)
  1058. n.V.Format(w)
  1059. }
  1060. // Accept implements Node Accept interface.
  1061. func (n *UnaryOperationExpr) Accept(v Visitor) (Node, bool) {
  1062. newNode, skipChildren := v.Enter(n)
  1063. if skipChildren {
  1064. return v.Leave(newNode)
  1065. }
  1066. n = newNode.(*UnaryOperationExpr)
  1067. node, ok := n.V.Accept(v)
  1068. if !ok {
  1069. return n, false
  1070. }
  1071. n.V = node.(ExprNode)
  1072. return v.Leave(n)
  1073. }
  1074. // ValuesExpr is the expression used in INSERT VALUES.
  1075. type ValuesExpr struct {
  1076. exprNode
  1077. // Column is column name.
  1078. Column *ColumnNameExpr
  1079. }
  1080. // Restore implements Node interface.
  1081. func (n *ValuesExpr) Restore(ctx *format.RestoreCtx) error {
  1082. ctx.WriteKeyWord("VALUES")
  1083. ctx.WritePlain("(")
  1084. if err := n.Column.Restore(ctx); err != nil {
  1085. return errors.Annotate(err, "An error occurred while restore ValuesExpr.Column")
  1086. }
  1087. ctx.WritePlain(")")
  1088. return nil
  1089. }
  1090. // Format the ExprNode into a Writer.
  1091. func (n *ValuesExpr) Format(w io.Writer) {
  1092. panic("Not implemented")
  1093. }
  1094. // Accept implements Node Accept interface.
  1095. func (n *ValuesExpr) Accept(v Visitor) (Node, bool) {
  1096. newNode, skipChildren := v.Enter(n)
  1097. if skipChildren {
  1098. return v.Leave(newNode)
  1099. }
  1100. n = newNode.(*ValuesExpr)
  1101. node, ok := n.Column.Accept(v)
  1102. if !ok {
  1103. return n, false
  1104. }
  1105. if col, ok := node.(*ColumnNameExpr); ok {
  1106. // `node` may be *ast.ValueExpr
  1107. n.Column = col
  1108. }
  1109. return v.Leave(n)
  1110. }
  1111. // VariableExpr is the expression for variable.
  1112. type VariableExpr struct {
  1113. exprNode
  1114. // Name is the variable name.
  1115. Name string
  1116. // IsGlobal indicates whether this variable is global.
  1117. IsGlobal bool
  1118. // IsSystem indicates whether this variable is a system variable in current session.
  1119. IsSystem bool
  1120. // ExplicitScope indicates whether this variable scope is set explicitly.
  1121. ExplicitScope bool
  1122. // Value is the variable value.
  1123. Value ExprNode
  1124. }
  1125. // Restore implements Node interface.
  1126. func (n *VariableExpr) Restore(ctx *format.RestoreCtx) error {
  1127. if n.IsSystem {
  1128. ctx.WritePlain("@@")
  1129. if n.ExplicitScope {
  1130. if n.IsGlobal {
  1131. ctx.WriteKeyWord("GLOBAL")
  1132. } else {
  1133. ctx.WriteKeyWord("SESSION")
  1134. }
  1135. ctx.WritePlain(".")
  1136. }
  1137. } else {
  1138. ctx.WritePlain("@")
  1139. }
  1140. ctx.WriteName(n.Name)
  1141. if n.Value != nil {
  1142. ctx.WritePlain(":=")
  1143. if err := n.Value.Restore(ctx); err != nil {
  1144. return errors.Annotate(err, "An error occurred while restore VariableExpr.Value")
  1145. }
  1146. }
  1147. return nil
  1148. }
  1149. // Format the ExprNode into a Writer.
  1150. func (n *VariableExpr) Format(w io.Writer) {
  1151. panic("Not implemented")
  1152. }
  1153. // Accept implements Node Accept interface.
  1154. func (n *VariableExpr) Accept(v Visitor) (Node, bool) {
  1155. newNode, skipChildren := v.Enter(n)
  1156. if skipChildren {
  1157. return v.Leave(newNode)
  1158. }
  1159. n = newNode.(*VariableExpr)
  1160. if n.Value == nil {
  1161. return v.Leave(n)
  1162. }
  1163. node, ok := n.Value.Accept(v)
  1164. if !ok {
  1165. return n, false
  1166. }
  1167. n.Value = node.(ExprNode)
  1168. return v.Leave(n)
  1169. }
  1170. // MaxValueExpr is the expression for "maxvalue" used in partition.
  1171. type MaxValueExpr struct {
  1172. exprNode
  1173. }
  1174. // Restore implements Node interface.
  1175. func (n *MaxValueExpr) Restore(ctx *format.RestoreCtx) error {
  1176. ctx.WriteKeyWord("MAXVALUE")
  1177. return nil
  1178. }
  1179. // Format the ExprNode into a Writer.
  1180. func (n *MaxValueExpr) Format(w io.Writer) {
  1181. fmt.Fprint(w, "MAXVALUE")
  1182. }
  1183. // Accept implements Node Accept interface.
  1184. func (n *MaxValueExpr) Accept(v Visitor) (Node, bool) {
  1185. newNode, skipChildren := v.Enter(n)
  1186. if skipChildren {
  1187. return v.Leave(newNode)
  1188. }
  1189. return v.Leave(n)
  1190. }
  1191. // MatchAgainst is the expression for matching against fulltext index.
  1192. type MatchAgainst struct {
  1193. exprNode
  1194. // ColumnNames are the columns to match.
  1195. ColumnNames []*ColumnName
  1196. // Against
  1197. Against ExprNode
  1198. // Modifier
  1199. Modifier FulltextSearchModifier
  1200. }
  1201. func (n *MatchAgainst) Restore(ctx *format.RestoreCtx) error {
  1202. ctx.WriteKeyWord("MATCH")
  1203. ctx.WritePlain(" (")
  1204. for i, v := range n.ColumnNames {
  1205. if i != 0 {
  1206. ctx.WritePlain(",")
  1207. }
  1208. if err := v.Restore(ctx); err != nil {
  1209. return errors.Annotatef(err, "An error occurred while restore MatchAgainst.ColumnNames[%d]", i)
  1210. }
  1211. }
  1212. ctx.WritePlain(") ")
  1213. ctx.WriteKeyWord("AGAINST")
  1214. ctx.WritePlain(" (")
  1215. if err := n.Against.Restore(ctx); err != nil {
  1216. return errors.Annotate(err, "An error occurred while restore MatchAgainst.Against")
  1217. }
  1218. if n.Modifier.IsBooleanMode() {
  1219. ctx.WritePlain(" IN BOOLEAN MODE")
  1220. if n.Modifier.WithQueryExpansion() {
  1221. return errors.New("BOOLEAN MODE doesn't support QUERY EXPANSION")
  1222. }
  1223. } else if n.Modifier.WithQueryExpansion() {
  1224. ctx.WritePlain(" WITH QUERY EXPANSION")
  1225. }
  1226. ctx.WritePlain(")")
  1227. return nil
  1228. }
  1229. func (n *MatchAgainst) Format(w io.Writer) {
  1230. fmt.Fprint(w, "MATCH(")
  1231. for i, v := range n.ColumnNames {
  1232. if i != 0 {
  1233. fmt.Fprintf(w, ",%s", v.String())
  1234. } else {
  1235. fmt.Fprint(w, v.String())
  1236. }
  1237. }
  1238. fmt.Fprint(w, ") AGAINST(")
  1239. n.Against.Format(w)
  1240. if n.Modifier.IsBooleanMode() {
  1241. fmt.Fprint(w, " IN BOOLEAN MODE")
  1242. } else if n.Modifier.WithQueryExpansion() {
  1243. fmt.Fprint(w, " WITH QUERY EXPANSION")
  1244. }
  1245. fmt.Fprint(w, ")")
  1246. }
  1247. func (n *MatchAgainst) Accept(v Visitor) (Node, bool) {
  1248. newNode, skipChildren := v.Enter(n)
  1249. if skipChildren {
  1250. return v.Leave(newNode)
  1251. }
  1252. n = newNode.(*MatchAgainst)
  1253. for i, colName := range n.ColumnNames {
  1254. newColName, ok := colName.Accept(v)
  1255. if !ok {
  1256. return n, false
  1257. }
  1258. n.ColumnNames[i] = newColName.(*ColumnName)
  1259. }
  1260. newAgainst, ok := n.Against.Accept(v)
  1261. if !ok {
  1262. return n, false
  1263. }
  1264. n.Against = newAgainst.(ExprNode)
  1265. return v.Leave(n)
  1266. }
  1267. // SetCollationExpr is the expression for the `COLLATE collation_name` clause.
  1268. type SetCollationExpr struct {
  1269. exprNode
  1270. // Expr is the expression to be set.
  1271. Expr ExprNode
  1272. // Collate is the name of collation to set.
  1273. Collate string
  1274. }
  1275. // Restore implements Node interface.
  1276. func (n *SetCollationExpr) Restore(ctx *format.RestoreCtx) error {
  1277. if err := n.Expr.Restore(ctx); err != nil {
  1278. return errors.Trace(err)
  1279. }
  1280. ctx.WriteKeyWord(" COLLATE ")
  1281. ctx.WritePlain(n.Collate)
  1282. return nil
  1283. }
  1284. // Format the ExprNode into a Writer.
  1285. func (n *SetCollationExpr) Format(w io.Writer) {
  1286. n.Expr.Format(w)
  1287. fmt.Fprintf(w, " COLLATE %s", n.Collate)
  1288. }
  1289. // Accept implements Node Accept interface.
  1290. func (n *SetCollationExpr) Accept(v Visitor) (Node, bool) {
  1291. newNode, skipChildren := v.Enter(n)
  1292. if skipChildren {
  1293. return v.Leave(newNode)
  1294. }
  1295. n = newNode.(*SetCollationExpr)
  1296. node, ok := n.Expr.Accept(v)
  1297. if !ok {
  1298. return n, false
  1299. }
  1300. n.Expr = node.(ExprNode)
  1301. return v.Leave(n)
  1302. }
  1303. type exprTextPositionCleaner struct {
  1304. oldTextPos []int
  1305. restore bool
  1306. }
  1307. func (e *exprTextPositionCleaner) BeginRestore() {
  1308. e.restore = true
  1309. }
  1310. func (e *exprTextPositionCleaner) Enter(n Node) (node Node, skipChildren bool) {
  1311. if e.restore {
  1312. n.SetOriginTextPosition(e.oldTextPos[0])
  1313. e.oldTextPos = e.oldTextPos[1:]
  1314. return n, false
  1315. }
  1316. e.oldTextPos = append(e.oldTextPos, n.OriginTextPosition())
  1317. n.SetOriginTextPosition(0)
  1318. return n, false
  1319. }
  1320. func (e *exprTextPositionCleaner) Leave(n Node) (node Node, ok bool) {
  1321. return n, true
  1322. }
  1323. // ExpressionDeepEqual compares the equivalence of two expressions.
  1324. func ExpressionDeepEqual(a ExprNode, b ExprNode) bool {
  1325. cleanerA := &exprTextPositionCleaner{}
  1326. cleanerB := &exprTextPositionCleaner{}
  1327. a.Accept(cleanerA)
  1328. b.Accept(cleanerB)
  1329. result := reflect.DeepEqual(a, b)
  1330. cleanerA.BeginRestore()
  1331. cleanerB.BeginRestore()
  1332. a.Accept(cleanerA)
  1333. b.Accept(cleanerB)
  1334. return result
  1335. }