sourceStmt.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Copyright 2021-2022 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. "encoding/json"
  17. "fmt"
  18. )
  19. const (
  20. TypeStream StreamType = iota
  21. TypeTable
  22. )
  23. var StreamTypeMap = map[StreamType]string{
  24. TypeStream: "stream",
  25. TypeTable: "table",
  26. }
  27. const (
  28. StreamKindLookup = "lookup"
  29. StreamKindScan = "scan"
  30. )
  31. type StreamType int
  32. type StreamStmt struct {
  33. Name StreamName
  34. StreamFields StreamFields
  35. Options *Options
  36. StreamType StreamType //default to TypeStream
  37. Statement
  38. }
  39. type StreamField struct {
  40. Name string
  41. FieldType
  42. }
  43. func (u *StreamField) MarshalJSON() ([]byte, error) {
  44. return json.Marshal(&struct {
  45. FieldType interface{}
  46. Name string
  47. }{
  48. FieldType: printFieldTypeForJson(u.FieldType),
  49. Name: u.Name,
  50. })
  51. }
  52. type StreamFields []StreamField
  53. type FieldType interface {
  54. fieldType()
  55. }
  56. type BasicType struct {
  57. Type DataType
  58. FieldType
  59. }
  60. type ArrayType struct {
  61. Type DataType
  62. FieldType
  63. }
  64. type RecType struct {
  65. StreamFields StreamFields
  66. FieldType
  67. }
  68. // Options The stream AST tree
  69. type Options struct {
  70. DATASOURCE string `json:"datasource,omitempty"`
  71. KEY string `json:"key,omitempty"`
  72. FORMAT string `json:"format,omitempty"`
  73. CONF_KEY string `json:"confKey,omitempty"`
  74. TYPE string `json:"type,omitempty"`
  75. STRICT_VALIDATION bool `json:"strictValidation,omitempty"`
  76. TIMESTAMP string `json:"timestamp,omitempty"`
  77. TIMESTAMP_FORMAT string `json:"timestampFormat,omitempty"`
  78. SHARED bool `json:"shared,omitempty"`
  79. SCHEMAID string `json:"schemaid,omitempty"`
  80. // for scan table only
  81. RETAIN_SIZE int `json:"retainSize,omitempty"`
  82. // for table only, to distinguish lookup & scan
  83. KIND string `json:"kind,omitempty"`
  84. }
  85. func (o Options) node() {}
  86. type ShowStreamsStatement struct {
  87. Statement
  88. }
  89. type DescribeStreamStatement struct {
  90. Name string
  91. Statement
  92. }
  93. type ExplainStreamStatement struct {
  94. Name string
  95. Statement
  96. }
  97. type DropStreamStatement struct {
  98. Name string
  99. Statement
  100. }
  101. func (dss *DescribeStreamStatement) GetName() string { return dss.Name }
  102. func (ess *ExplainStreamStatement) GetName() string { return ess.Name }
  103. func (dss *DropStreamStatement) GetName() string { return dss.Name }
  104. type ShowTablesStatement struct {
  105. Statement
  106. }
  107. type DescribeTableStatement struct {
  108. Name string
  109. Statement
  110. }
  111. type ExplainTableStatement struct {
  112. Name string
  113. Statement
  114. }
  115. type DropTableStatement struct {
  116. Name string
  117. Statement
  118. }
  119. func (dss *DescribeTableStatement) GetName() string { return dss.Name }
  120. func (ess *ExplainTableStatement) GetName() string { return ess.Name }
  121. func (dss *DropTableStatement) GetName() string { return dss.Name }
  122. func printFieldTypeForJson(ft FieldType) (result interface{}) {
  123. r, q := doPrintFieldTypeForJson(ft)
  124. if q {
  125. return r
  126. } else {
  127. return json.RawMessage(r)
  128. }
  129. }
  130. func doPrintFieldTypeForJson(ft FieldType) (result string, isLiteral bool) {
  131. switch t := ft.(type) {
  132. case *BasicType:
  133. return t.Type.String(), true
  134. case *ArrayType:
  135. var (
  136. fieldType string
  137. q bool
  138. )
  139. if t.FieldType != nil {
  140. fieldType, q = doPrintFieldTypeForJson(t.FieldType)
  141. } else {
  142. fieldType, q = t.Type.String(), true
  143. }
  144. if q {
  145. result = fmt.Sprintf(`{"Type":"array","ElementType":"%s"}`, fieldType)
  146. } else {
  147. result = fmt.Sprintf(`{"Type":"array","ElementType":%s}`, fieldType)
  148. }
  149. case *RecType:
  150. result = `{"Type":"struct","Fields":[`
  151. isFirst := true
  152. for _, f := range t.StreamFields {
  153. if isFirst {
  154. isFirst = false
  155. } else {
  156. result += ","
  157. }
  158. fieldType, q := doPrintFieldTypeForJson(f.FieldType)
  159. if q {
  160. result = fmt.Sprintf(`%s{"FieldType":"%s","Name":"%s"}`, result, fieldType, f.Name)
  161. } else {
  162. result = fmt.Sprintf(`%s{"FieldType":%s,"Name":"%s"}`, result, fieldType, f.Name)
  163. }
  164. }
  165. result += `]}`
  166. }
  167. return result, false
  168. }