expr.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. )
  167. type Call struct {
  168. Name string
  169. FuncId int
  170. FuncType FuncType
  171. Args []Expr
  172. // This is used for analytic functions.
  173. // In planner, all analytic functions are planned to calculate in analytic_op which produce a new field.
  174. // This cachedField cached the new field name and when evaluating, just returned the field access evaluated value.
  175. CachedField string
  176. Cached bool
  177. Partition *PartitionExpr
  178. WhenExpr Expr
  179. }
  180. func (c *Call) expr() {}
  181. func (c *Call) literal() {}
  182. func (c *Call) node() {}
  183. func (c *Call) String() string {
  184. args := ""
  185. if c.Args != nil {
  186. args = ", args:["
  187. for i, arg := range c.Args {
  188. args += arg.String()
  189. if i != len(c.Args)-1 {
  190. args += ", "
  191. }
  192. }
  193. args += "]"
  194. }
  195. when := ""
  196. if c.WhenExpr != nil {
  197. when += ", when:{ " + c.WhenExpr.String() + " }"
  198. }
  199. return "Call:{ name:" + c.Name + args + when + " }"
  200. }
  201. type PartitionExpr struct {
  202. Exprs []Expr
  203. }
  204. func (pe *PartitionExpr) expr() {}
  205. func (pe *PartitionExpr) node() {}
  206. func (pe *PartitionExpr) String() string {
  207. e := ""
  208. for i, expr := range pe.Exprs {
  209. e += expr.String()
  210. if i != len(pe.Exprs)-1 {
  211. e += ", "
  212. }
  213. }
  214. return "PartitionExpr:[ " + e + " ]"
  215. }
  216. type BinaryExpr struct {
  217. OP Token
  218. LHS Expr
  219. RHS Expr
  220. }
  221. func (be *BinaryExpr) expr() {}
  222. func (be *BinaryExpr) node() {}
  223. func (be *BinaryExpr) String() string {
  224. info := ""
  225. if be.LHS != nil && be.RHS != nil {
  226. t := Tokens[be.OP]
  227. if t == "[]" {
  228. info += "binaryExpr:{ " + be.LHS.String() + "[" + be.RHS.String() + "] }"
  229. return info
  230. }
  231. info += "binaryExpr:{ " + be.LHS.String() + " " + t + " " + be.RHS.String() + " }"
  232. }
  233. return info
  234. }
  235. type WhenClause struct {
  236. // The condition Expression
  237. Expr Expr
  238. Result Expr
  239. }
  240. func (w *WhenClause) expr() {}
  241. func (w *WhenClause) node() {}
  242. func (w *WhenClause) String() string {
  243. e := ""
  244. if w.Expr != nil {
  245. e += w.Expr.String()
  246. }
  247. return "whenClause:{ " + e + " }"
  248. }
  249. type CaseExpr struct {
  250. // The compare value Expression. It can be a value Expression or nil.
  251. // When it is nil, the WhenClause Expr must be a logical(comparison) Expression
  252. Value Expr
  253. WhenClauses []*WhenClause
  254. ElseClause Expr
  255. }
  256. func (c *CaseExpr) expr() {}
  257. func (c *CaseExpr) node() {}
  258. func (c *CaseExpr) String() string {
  259. v := ""
  260. if c.Value != nil {
  261. v += "value:{ " + c.Value.String() + " }"
  262. }
  263. w := ""
  264. if c.WhenClauses != nil && len(c.WhenClauses) != 0 {
  265. if c.Value != nil {
  266. w += ", "
  267. }
  268. w += "whenClauses:["
  269. for i, clause := range c.WhenClauses {
  270. if clause.Expr != nil {
  271. w += "{ " + clause.String() + " }"
  272. if i != len(c.WhenClauses)-1 {
  273. w += ", "
  274. }
  275. }
  276. }
  277. w += "]"
  278. }
  279. return "caseExprValue:{ " + v + w + " }"
  280. }
  281. type ValueSetExpr struct {
  282. LiteralExprs []Expr // ("A", "B", "C") or (1, 2, 3)
  283. ArrayExpr Expr
  284. }
  285. func (c *ValueSetExpr) expr() {}
  286. func (c *ValueSetExpr) node() {}
  287. func (c *ValueSetExpr) String() string {
  288. le := ""
  289. if c.LiteralExprs != nil && len(c.LiteralExprs) != 0 {
  290. le += "literalExprs:["
  291. for i, expr := range c.LiteralExprs {
  292. le += expr.String()
  293. if i != len(c.LiteralExprs)-1 {
  294. le += ", "
  295. }
  296. }
  297. le += "]"
  298. }
  299. a := ""
  300. if c.ArrayExpr != nil {
  301. if c.LiteralExprs != nil && len(c.LiteralExprs) != 0 {
  302. a += ", "
  303. }
  304. a += "arrayExpr:{ " + c.ArrayExpr.String() + " }"
  305. }
  306. return "valueSetExpr:{ " + le + a + " }"
  307. }
  308. type BetweenExpr struct {
  309. Lower Expr
  310. Higher Expr
  311. }
  312. func (b *BetweenExpr) expr() {}
  313. func (b *BetweenExpr) node() {}
  314. func (b *BetweenExpr) String() string {
  315. low := ""
  316. high := ""
  317. if b.Lower != nil {
  318. low += b.Lower.String()
  319. }
  320. if b.Higher != nil {
  321. if b.Lower != nil {
  322. high += ", "
  323. }
  324. high += b.Higher.String()
  325. }
  326. return "betweenExpr:{ " + low + high + " }"
  327. }
  328. type LimitExpr struct {
  329. LimitCount *IntegerLiteral
  330. }
  331. func (l *LimitExpr) expr() {}
  332. func (l *LimitExpr) node() {}
  333. func (l *LimitExpr) String() string {
  334. if l.LimitCount != nil {
  335. return "limitExpr:{ " + l.LimitCount.String() + " }"
  336. }
  337. return ""
  338. }
  339. type StreamName string
  340. func (sn *StreamName) node() {}
  341. const (
  342. DefaultStream = StreamName("$$default")
  343. AliasStream = StreamName("$$alias")
  344. )
  345. type MetaRef struct {
  346. StreamName StreamName
  347. Name string
  348. }
  349. func (fr *MetaRef) expr() {}
  350. func (fr *MetaRef) node() {}
  351. func (fr *MetaRef) String() string {
  352. sn := ""
  353. n := ""
  354. if fr.StreamName != "" {
  355. sn += "streamName:" + string(fr.StreamName)
  356. }
  357. if fr.Name != "" {
  358. if fr.StreamName != "" {
  359. n += ", "
  360. }
  361. n += "fieldName:" + fr.Name
  362. }
  363. return "metaRef:{ " + sn + n + " }"
  364. }
  365. type JsonFieldRef struct {
  366. Name string
  367. }
  368. func (fr *JsonFieldRef) expr() {}
  369. func (fr *JsonFieldRef) node() {}
  370. func (fr *JsonFieldRef) String() string {
  371. return "jsonFieldName:" + fr.Name
  372. }
  373. type ColFuncField struct {
  374. Name string
  375. Expr Expr
  376. }
  377. func (fr *ColFuncField) expr() {}
  378. func (fr *ColFuncField) node() {}
  379. func (fr *ColFuncField) String() string {
  380. e := ""
  381. if fr.Name != "" {
  382. e += "name: " + fr.Name
  383. }
  384. if fr.Expr != nil {
  385. if fr.Name != "" {
  386. e += ", "
  387. }
  388. e += "expr:{ " + fr.Expr.String() + " }"
  389. }
  390. return "colFuncField:{ " + e + " }"
  391. }