sourceStmt.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. type JsonStreamField struct {
  44. Type string `json:"type"`
  45. Items *JsonStreamField `json:"items,omitempty"`
  46. Properties map[string]*JsonStreamField `json:"properties,omitempty"`
  47. }
  48. func (u *StreamField) MarshalJSON() ([]byte, error) {
  49. return json.Marshal(&struct {
  50. FieldType interface{}
  51. Name string
  52. }{
  53. FieldType: printFieldTypeForJson(u.FieldType),
  54. Name: u.Name,
  55. })
  56. }
  57. // UnmarshalJSON The json format follows json schema
  58. func (sf *StreamFields) UnmarshalJSON(data []byte) error {
  59. temp := map[string]*JsonStreamField{}
  60. err := json.Unmarshal(data, &temp)
  61. if err != nil {
  62. return err
  63. }
  64. return sf.UnmarshalFromMap(temp)
  65. }
  66. func (sf *StreamFields) UnmarshalFromMap(data map[string]*JsonStreamField) error {
  67. t, err := fieldsTypeFromSchema(data)
  68. if err != nil {
  69. return err
  70. }
  71. *sf = t
  72. return nil
  73. }
  74. func fieldsTypeFromSchema(mjsf map[string]*JsonStreamField) (StreamFields, error) {
  75. sfs := make(StreamFields, 0, len(mjsf))
  76. for k, v := range mjsf {
  77. ft, err := fieldTypeFromSchema(v)
  78. if err != nil {
  79. return nil, err
  80. }
  81. sfs = append(sfs, StreamField{
  82. Name: k,
  83. FieldType: ft,
  84. })
  85. }
  86. return sfs, nil
  87. }
  88. func fieldTypeFromSchema(v *JsonStreamField) (FieldType, error) {
  89. var ft FieldType
  90. switch v.Type {
  91. case "array":
  92. if v.Items == nil {
  93. return nil, fmt.Errorf("array field type should have items")
  94. }
  95. itemType, err := fieldTypeFromSchema(v.Items)
  96. if err != nil {
  97. return nil, fmt.Errorf("invalid array field type: %v", err)
  98. }
  99. switch t := itemType.(type) {
  100. case *BasicType:
  101. ft = &ArrayType{
  102. Type: t.Type,
  103. }
  104. case *RecType:
  105. ft = &ArrayType{
  106. Type: STRUCT,
  107. FieldType: t,
  108. }
  109. case *ArrayType:
  110. ft = &ArrayType{
  111. Type: ARRAY,
  112. FieldType: t,
  113. }
  114. }
  115. case "struct":
  116. if v.Properties == nil {
  117. return nil, fmt.Errorf("struct field type should have properties")
  118. }
  119. sfs, err := fieldsTypeFromSchema(v.Properties)
  120. if err != nil {
  121. return nil, fmt.Errorf("invalid struct field type: %v", err)
  122. }
  123. ft = &RecType{StreamFields: sfs}
  124. case "bigint":
  125. ft = &BasicType{Type: BIGINT}
  126. case "float":
  127. ft = &BasicType{Type: FLOAT}
  128. case "string":
  129. ft = &BasicType{Type: STRINGS}
  130. case "bytea":
  131. ft = &BasicType{Type: BYTEA}
  132. case "datetime":
  133. ft = &BasicType{Type: DATETIME}
  134. case "boolean":
  135. ft = &BasicType{Type: BOOLEAN}
  136. default:
  137. return nil, fmt.Errorf("unsupported type %s", v.Type)
  138. }
  139. return ft, nil
  140. }
  141. type StreamFields []StreamField
  142. type FieldType interface {
  143. fieldType()
  144. }
  145. type BasicType struct {
  146. Type DataType
  147. FieldType
  148. }
  149. type ArrayType struct {
  150. Type DataType
  151. FieldType
  152. }
  153. type RecType struct {
  154. StreamFields StreamFields
  155. FieldType
  156. }
  157. // Options The stream AST tree
  158. type Options struct {
  159. DATASOURCE string `json:"datasource,omitempty"`
  160. KEY string `json:"key,omitempty"`
  161. FORMAT string `json:"format,omitempty"`
  162. CONF_KEY string `json:"confKey,omitempty"`
  163. TYPE string `json:"type,omitempty"`
  164. STRICT_VALIDATION bool `json:"strictValidation,omitempty"`
  165. TIMESTAMP string `json:"timestamp,omitempty"`
  166. TIMESTAMP_FORMAT string `json:"timestampFormat,omitempty"`
  167. SHARED bool `json:"shared,omitempty"`
  168. SCHEMAID string `json:"schemaid,omitempty"`
  169. // for scan table only
  170. RETAIN_SIZE int `json:"retainSize,omitempty"`
  171. // for table only, to distinguish lookup & scan
  172. KIND string `json:"kind,omitempty"`
  173. }
  174. func (o Options) node() {}
  175. type ShowStreamsStatement struct {
  176. Statement
  177. }
  178. type DescribeStreamStatement struct {
  179. Name string
  180. Statement
  181. }
  182. type ExplainStreamStatement struct {
  183. Name string
  184. Statement
  185. }
  186. type DropStreamStatement struct {
  187. Name string
  188. Statement
  189. }
  190. func (dss *DescribeStreamStatement) GetName() string { return dss.Name }
  191. func (ess *ExplainStreamStatement) GetName() string { return ess.Name }
  192. func (dss *DropStreamStatement) GetName() string { return dss.Name }
  193. type ShowTablesStatement struct {
  194. Statement
  195. }
  196. type DescribeTableStatement struct {
  197. Name string
  198. Statement
  199. }
  200. type ExplainTableStatement struct {
  201. Name string
  202. Statement
  203. }
  204. type DropTableStatement struct {
  205. Name string
  206. Statement
  207. }
  208. func (dss *DescribeTableStatement) GetName() string { return dss.Name }
  209. func (ess *ExplainTableStatement) GetName() string { return ess.Name }
  210. func (dss *DropTableStatement) GetName() string { return dss.Name }
  211. func printFieldTypeForJson(ft FieldType) (result interface{}) {
  212. r, q := doPrintFieldTypeForJson(ft)
  213. if q {
  214. return r
  215. } else {
  216. return json.RawMessage(r)
  217. }
  218. }
  219. func doPrintFieldTypeForJson(ft FieldType) (result string, isLiteral bool) {
  220. switch t := ft.(type) {
  221. case *BasicType:
  222. return t.Type.String(), true
  223. case *ArrayType:
  224. var (
  225. fieldType string
  226. q bool
  227. )
  228. if t.FieldType != nil {
  229. fieldType, q = doPrintFieldTypeForJson(t.FieldType)
  230. } else {
  231. fieldType, q = t.Type.String(), true
  232. }
  233. if q {
  234. result = fmt.Sprintf(`{"Type":"array","ElementType":"%s"}`, fieldType)
  235. } else {
  236. result = fmt.Sprintf(`{"Type":"array","ElementType":%s}`, fieldType)
  237. }
  238. case *RecType:
  239. result = `{"Type":"struct","Fields":[`
  240. isFirst := true
  241. for _, f := range t.StreamFields {
  242. if isFirst {
  243. isFirst = false
  244. } else {
  245. result += ","
  246. }
  247. fieldType, q := doPrintFieldTypeForJson(f.FieldType)
  248. if q {
  249. result = fmt.Sprintf(`%s{"FieldType":"%s","Name":"%s"}`, result, fieldType, f.Name)
  250. } else {
  251. result = fmt.Sprintf(`%s{"FieldType":%s,"Name":"%s"}`, result, fieldType, f.Name)
  252. }
  253. }
  254. result += `]}`
  255. }
  256. return result, false
  257. }