expr.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  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. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ast
  15. import (
  16. "fmt"
  17. "strconv"
  18. )
  19. type Node interface {
  20. node()
  21. }
  22. type NameNode interface {
  23. Node
  24. GetName() string
  25. }
  26. type Expr interface {
  27. Node
  28. expr()
  29. // String function for the explain grammar, convert Expr to String
  30. String() string
  31. }
  32. type Literal interface {
  33. Expr
  34. literal()
  35. }
  36. type ParenExpr struct {
  37. Expr Expr
  38. }
  39. type ArrowExpr struct {
  40. Expr Expr
  41. }
  42. type BracketExpr struct {
  43. Expr Expr
  44. }
  45. type ColonExpr struct {
  46. Start Expr
  47. End Expr
  48. }
  49. type IndexExpr struct {
  50. Index Expr
  51. }
  52. type BooleanLiteral struct {
  53. Val bool
  54. }
  55. type TimeLiteral struct {
  56. Val Token
  57. }
  58. type IntegerLiteral struct {
  59. Val int
  60. }
  61. type StringLiteral struct {
  62. Val string
  63. }
  64. type NumberLiteral struct {
  65. Val float64
  66. }
  67. type Wildcard struct {
  68. Token Token
  69. Replace []Field
  70. Except []string
  71. }
  72. func (pe *ParenExpr) expr() {}
  73. func (pe *ParenExpr) node() {}
  74. func (pe *ParenExpr) String() string {
  75. e := ""
  76. if pe.Expr != nil {
  77. e += pe.Expr.String()
  78. }
  79. return "parenExpr:{ " + e + " }"
  80. }
  81. func (ae *ArrowExpr) expr() {}
  82. func (ae *ArrowExpr) node() {}
  83. func (ae *ArrowExpr) String() string {
  84. e := ""
  85. if ae.Expr != nil {
  86. e += ae.Expr.String()
  87. }
  88. return "arrowExpr:{ " + e + " }"
  89. }
  90. func (be *BracketExpr) expr() {}
  91. func (be *BracketExpr) node() {}
  92. func (be *BracketExpr) String() string {
  93. e := ""
  94. if be.Expr != nil {
  95. e += be.Expr.String()
  96. }
  97. return "bracketExpr:{ " + e + " }"
  98. }
  99. func (be *ColonExpr) expr() {}
  100. func (be *ColonExpr) node() {}
  101. func (be *ColonExpr) String() string {
  102. s := ""
  103. e := ""
  104. if be.Start != nil {
  105. s += "start:{ " + be.Start.String() + " }"
  106. }
  107. if be.End != nil {
  108. if be.Start != nil {
  109. e += ", "
  110. }
  111. e += "end:{ " + be.End.String() + " }"
  112. }
  113. return "ColonExpr:{ " + s + e + " }"
  114. }
  115. func (be *IndexExpr) expr() {}
  116. func (be *IndexExpr) node() {}
  117. func (be *IndexExpr) String() string {
  118. i := ""
  119. if be.Index != nil {
  120. i += be.Index.String()
  121. }
  122. return i
  123. }
  124. func (w *Wildcard) expr() {}
  125. func (w *Wildcard) node() {}
  126. func (w *Wildcard) String() string {
  127. return Tokens[w.Token]
  128. }
  129. func (bl *BooleanLiteral) expr() {}
  130. func (bl *BooleanLiteral) literal() {}
  131. func (bl *BooleanLiteral) node() {}
  132. func (bl *BooleanLiteral) String() string {
  133. return strconv.FormatBool(bl.Val)
  134. }
  135. func (tl *TimeLiteral) expr() {}
  136. func (tl *TimeLiteral) literal() {}
  137. func (tl *TimeLiteral) node() {}
  138. func (tl *TimeLiteral) String() string {
  139. return Tokens[tl.Val]
  140. }
  141. func (il *IntegerLiteral) expr() {}
  142. func (il *IntegerLiteral) literal() {}
  143. func (il *IntegerLiteral) node() {}
  144. func (il *IntegerLiteral) String() string {
  145. return strconv.Itoa(il.Val)
  146. }
  147. func (nl *NumberLiteral) expr() {}
  148. func (nl *NumberLiteral) literal() {}
  149. func (nl *NumberLiteral) node() {}
  150. func (nl *NumberLiteral) String() string {
  151. return fmt.Sprintf("%f", nl.Val)
  152. }
  153. func (sl *StringLiteral) expr() {}
  154. func (sl *StringLiteral) literal() {}
  155. func (sl *StringLiteral) node() {}
  156. func (sl *StringLiteral) String() string {
  157. return sl.Val
  158. }
  159. type FuncType int
  160. const (
  161. FuncTypeUnknown FuncType = iota - 1
  162. FuncTypeScalar
  163. FuncTypeAgg
  164. FuncTypeCols
  165. FuncTypeSrf
  166. FuncTypeWindow
  167. )
  168. type Call struct {
  169. Name string
  170. FuncId int
  171. FuncType FuncType
  172. Args []Expr
  173. // This is used for analytic functions.
  174. // In planner, all analytic functions are planned to calculate in analytic_op which produce a new field.
  175. // This cachedField cached the new field name and when evaluating, just returned the field access evaluated value.
  176. CachedField string
  177. Cached bool
  178. Partition *PartitionExpr
  179. WhenExpr Expr
  180. }
  181. func (c *Call) expr() {}
  182. func (c *Call) literal() {}
  183. func (c *Call) node() {}
  184. func (c *Call) String() string {
  185. args := ""
  186. if c.Args != nil {
  187. args = ", args:["
  188. for i, arg := range c.Args {
  189. args += arg.String()
  190. if i != len(c.Args)-1 {
  191. args += ", "
  192. }
  193. }
  194. args += "]"
  195. }
  196. when := ""
  197. if c.WhenExpr != nil {
  198. when += ", when:{ " + c.WhenExpr.String() + " }"
  199. }
  200. return "Call:{ name:" + c.Name + args + when + " }"
  201. }
  202. type PartitionExpr struct {
  203. Exprs []Expr
  204. }
  205. func (pe *PartitionExpr) expr() {}
  206. func (pe *PartitionExpr) node() {}
  207. func (pe *PartitionExpr) String() string {
  208. e := ""
  209. for i, expr := range pe.Exprs {
  210. e += expr.String()
  211. if i != len(pe.Exprs)-1 {
  212. e += ", "
  213. }
  214. }
  215. return "PartitionExpr:[ " + e + " ]"
  216. }
  217. type BinaryExpr struct {
  218. OP Token
  219. LHS Expr
  220. RHS Expr
  221. }
  222. func (be *BinaryExpr) expr() {}
  223. func (be *BinaryExpr) node() {}
  224. func (be *BinaryExpr) String() string {
  225. info := ""
  226. if be.LHS != nil && be.RHS != nil {
  227. t := Tokens[be.OP]
  228. if t == "[]" {
  229. info += "binaryExpr:{ " + be.LHS.String() + "[" + be.RHS.String() + "] }"
  230. return info
  231. }
  232. info += "binaryExpr:{ " + be.LHS.String() + " " + t + " " + be.RHS.String() + " }"
  233. }
  234. return info
  235. }
  236. type WhenClause struct {
  237. // The condition Expression
  238. Expr Expr
  239. Result Expr
  240. }
  241. func (w *WhenClause) expr() {}
  242. func (w *WhenClause) node() {}
  243. func (w *WhenClause) String() string {
  244. e := ""
  245. if w.Expr != nil {
  246. e += w.Expr.String()
  247. }
  248. return "whenClause:{ " + e + " }"
  249. }
  250. type CaseExpr struct {
  251. // The compare value Expression. It can be a value Expression or nil.
  252. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  253. Value Expr
  254. WhenClauses []*WhenClause
  255. ElseClause Expr
  256. }
  257. func (c *CaseExpr) expr() {}
  258. func (c *CaseExpr) node() {}
  259. func (c *CaseExpr) String() string {
  260. v := ""
  261. if c.Value != nil {
  262. v += "value:{ " + c.Value.String() + " }"
  263. }
  264. w := ""
  265. if c.WhenClauses != nil && len(c.WhenClauses) != 0 {
  266. if c.Value != nil {
  267. w += ", "
  268. }
  269. w += "whenClauses:["
  270. for i, clause := range c.WhenClauses {
  271. if clause.Expr != nil {
  272. w += "{ " + clause.String() + " }"
  273. if i != len(c.WhenClauses)-1 {
  274. w += ", "
  275. }
  276. }
  277. }
  278. w += "]"
  279. }
  280. return "caseExprValue:{ " + v + w + " }"
  281. }
  282. type ValueSetExpr struct {
  283. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  284. ArrayExpr Expr
  285. }
  286. func (c *ValueSetExpr) expr() {}
  287. func (c *ValueSetExpr) node() {}
  288. func (c *ValueSetExpr) String() string {
  289. le := ""
  290. if c.LiteralExprs != nil && len(c.LiteralExprs) != 0 {
  291. le += "literalExprs:["
  292. for i, expr := range c.LiteralExprs {
  293. le += expr.String()
  294. if i != len(c.LiteralExprs)-1 {
  295. le += ", "
  296. }
  297. }
  298. le += "]"
  299. }
  300. a := ""
  301. if c.ArrayExpr != nil {
  302. if c.LiteralExprs != nil && len(c.LiteralExprs) != 0 {
  303. a += ", "
  304. }
  305. a += "arrayExpr:{ " + c.ArrayExpr.String() + " }"
  306. }
  307. return "valueSetExpr:{ " + le + a + " }"
  308. }
  309. type BetweenExpr struct {
  310. Lower Expr
  311. Higher Expr
  312. }
  313. func (b *BetweenExpr) expr() {}
  314. func (b *BetweenExpr) node() {}
  315. func (b *BetweenExpr) String() string {
  316. low := ""
  317. high := ""
  318. if b.Lower != nil {
  319. low += b.Lower.String()
  320. }
  321. if b.Higher != nil {
  322. if b.Lower != nil {
  323. high += ", "
  324. }
  325. high += b.Higher.String()
  326. }
  327. return "betweenExpr:{ " + low + high + " }"
  328. }
  329. type LimitExpr struct {
  330. LimitCount *IntegerLiteral
  331. }
  332. func (l *LimitExpr) expr() {}
  333. func (l *LimitExpr) node() {}
  334. func (l *LimitExpr) String() string {
  335. if l.LimitCount != nil {
  336. return "limitExpr:{ " + l.LimitCount.String() + " }"
  337. }
  338. return ""
  339. }
  340. type StreamName string
  341. func (sn *StreamName) node() {}
  342. const (
  343. DefaultStream = StreamName("$$default")
  344. AliasStream = StreamName("$$alias")
  345. )
  346. type MetaRef struct {
  347. StreamName StreamName
  348. Name string
  349. }
  350. func (fr *MetaRef) expr() {}
  351. func (fr *MetaRef) node() {}
  352. func (fr *MetaRef) String() string {
  353. sn := ""
  354. n := ""
  355. if fr.StreamName != "" {
  356. sn += "streamName:" + string(fr.StreamName)
  357. }
  358. if fr.Name != "" {
  359. if fr.StreamName != "" {
  360. n += ", "
  361. }
  362. n += "fieldName:" + fr.Name
  363. }
  364. return "metaRef:{ " + sn + n + " }"
  365. }
  366. type JsonFieldRef struct {
  367. Name string
  368. }
  369. func (fr *JsonFieldRef) expr() {}
  370. func (fr *JsonFieldRef) node() {}
  371. func (fr *JsonFieldRef) String() string {
  372. return "jsonFieldName:" + fr.Name
  373. }
  374. type ColFuncField struct {
  375. Name string
  376. Expr Expr
  377. }
  378. func (fr *ColFuncField) expr() {}
  379. func (fr *ColFuncField) node() {}
  380. func (fr *ColFuncField) String() string {
  381. e := ""
  382. if fr.Name != "" {
  383. e += "name: " + fr.Name
  384. }
  385. if fr.Expr != nil {
  386. if fr.Name != "" {
  387. e += ", "
  388. }
  389. e += "expr:{ " + fr.Expr.String() + " }"
  390. }
  391. return "colFuncField:{ " + e + " }"
  392. }