sourceStmt.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 (sf *StreamFields) ToJsonSchema() map[string]*JsonStreamField {
  75. return convertSchema(*sf)
  76. }
  77. func convertSchema(sfs StreamFields) map[string]*JsonStreamField {
  78. result := make(map[string]*JsonStreamField, len(sfs))
  79. for _, sf := range sfs {
  80. result[sf.Name] = convertFieldType(sf.FieldType)
  81. }
  82. return result
  83. }
  84. func convertFieldType(sf FieldType) *JsonStreamField {
  85. switch t := sf.(type) {
  86. case *BasicType:
  87. return &JsonStreamField{
  88. Type: t.Type.String(),
  89. }
  90. case *ArrayType:
  91. var items *JsonStreamField
  92. switch t.Type {
  93. case ARRAY, STRUCT:
  94. items = convertFieldType(t.FieldType)
  95. default:
  96. items = &JsonStreamField{
  97. Type: t.Type.String(),
  98. }
  99. }
  100. return &JsonStreamField{
  101. Type: "array",
  102. Items: items,
  103. }
  104. case *RecType:
  105. return &JsonStreamField{
  106. Type: "struct",
  107. Properties: convertSchema(t.StreamFields),
  108. }
  109. default: // should never happen
  110. return nil
  111. }
  112. }
  113. func fieldsTypeFromSchema(mjsf map[string]*JsonStreamField) (StreamFields, error) {
  114. sfs := make(StreamFields, 0, len(mjsf))
  115. for k, v := range mjsf {
  116. ft, err := fieldTypeFromSchema(v)
  117. if err != nil {
  118. return nil, err
  119. }
  120. sfs = append(sfs, StreamField{
  121. Name: k,
  122. FieldType: ft,
  123. })
  124. }
  125. return sfs, nil
  126. }
  127. func fieldTypeFromSchema(v *JsonStreamField) (FieldType, error) {
  128. var ft FieldType
  129. switch v.Type {
  130. case "array":
  131. if v.Items == nil {
  132. return nil, fmt.Errorf("array field type should have items")
  133. }
  134. itemType, err := fieldTypeFromSchema(v.Items)
  135. if err != nil {
  136. return nil, fmt.Errorf("invalid array field type: %v", err)
  137. }
  138. switch t := itemType.(type) {
  139. case *BasicType:
  140. ft = &ArrayType{
  141. Type: t.Type,
  142. }
  143. case *RecType:
  144. ft = &ArrayType{
  145. Type: STRUCT,
  146. FieldType: t,
  147. }
  148. case *ArrayType:
  149. ft = &ArrayType{
  150. Type: ARRAY,
  151. FieldType: t,
  152. }
  153. }
  154. case "struct":
  155. if v.Properties == nil {
  156. return nil, fmt.Errorf("struct field type should have properties")
  157. }
  158. sfs, err := fieldsTypeFromSchema(v.Properties)
  159. if err != nil {
  160. return nil, fmt.Errorf("invalid struct field type: %v", err)
  161. }
  162. ft = &RecType{StreamFields: sfs}
  163. case "bigint":
  164. ft = &BasicType{Type: BIGINT}
  165. case "float":
  166. ft = &BasicType{Type: FLOAT}
  167. case "string":
  168. ft = &BasicType{Type: STRINGS}
  169. case "bytea":
  170. ft = &BasicType{Type: BYTEA}
  171. case "datetime":
  172. ft = &BasicType{Type: DATETIME}
  173. case "boolean":
  174. ft = &BasicType{Type: BOOLEAN}
  175. default:
  176. return nil, fmt.Errorf("unsupported type %s", v.Type)
  177. }
  178. return ft, nil
  179. }
  180. type StreamFields []StreamField
  181. type FieldType interface {
  182. fieldType()
  183. }
  184. type BasicType struct {
  185. Type DataType
  186. FieldType
  187. }
  188. type ArrayType struct {
  189. Type DataType
  190. FieldType
  191. }
  192. type RecType struct {
  193. StreamFields StreamFields
  194. FieldType
  195. }
  196. // Options The stream AST tree
  197. type Options struct {
  198. DATASOURCE string `json:"datasource,omitempty"`
  199. KEY string `json:"key,omitempty"`
  200. FORMAT string `json:"format,omitempty"`
  201. CONF_KEY string `json:"confKey,omitempty"`
  202. TYPE string `json:"type,omitempty"`
  203. STRICT_VALIDATION bool `json:"strictValidation,omitempty"`
  204. TIMESTAMP string `json:"timestamp,omitempty"`
  205. TIMESTAMP_FORMAT string `json:"timestampFormat,omitempty"`
  206. SHARED bool `json:"shared,omitempty"`
  207. SCHEMAID string `json:"schemaid,omitempty"`
  208. // for scan table only
  209. RETAIN_SIZE int `json:"retainSize,omitempty"`
  210. // for table only, to distinguish lookup & scan
  211. KIND string `json:"kind,omitempty"`
  212. // for delimited format only
  213. DELIMITER string `json:"delimiter,omitempty"`
  214. }
  215. func (o Options) node() {}
  216. type ShowStreamsStatement struct {
  217. Statement
  218. }
  219. type DescribeStreamStatement struct {
  220. Name string
  221. Statement
  222. }
  223. type ExplainStreamStatement struct {
  224. Name string
  225. Statement
  226. }
  227. type DropStreamStatement struct {
  228. Name string
  229. Statement
  230. }
  231. func (dss *DescribeStreamStatement) GetName() string { return dss.Name }
  232. func (ess *ExplainStreamStatement) GetName() string { return ess.Name }
  233. func (dss *DropStreamStatement) GetName() string { return dss.Name }
  234. type ShowTablesStatement struct {
  235. Statement
  236. }
  237. type DescribeTableStatement struct {
  238. Name string
  239. Statement
  240. }
  241. type ExplainTableStatement struct {
  242. Name string
  243. Statement
  244. }
  245. type DropTableStatement struct {
  246. Name string
  247. Statement
  248. }
  249. func (dss *DescribeTableStatement) GetName() string { return dss.Name }
  250. func (ess *ExplainTableStatement) GetName() string { return ess.Name }
  251. func (dss *DropTableStatement) GetName() string { return dss.Name }
  252. func printFieldTypeForJson(ft FieldType) (result interface{}) {
  253. r, q := doPrintFieldTypeForJson(ft)
  254. if q {
  255. return r
  256. } else {
  257. return json.RawMessage(r)
  258. }
  259. }
  260. func doPrintFieldTypeForJson(ft FieldType) (result string, isLiteral bool) {
  261. switch t := ft.(type) {
  262. case *BasicType:
  263. return t.Type.String(), true
  264. case *ArrayType:
  265. var (
  266. fieldType string
  267. q bool
  268. )
  269. if t.FieldType != nil {
  270. fieldType, q = doPrintFieldTypeForJson(t.FieldType)
  271. } else {
  272. fieldType, q = t.Type.String(), true
  273. }
  274. if q {
  275. result = fmt.Sprintf(`{"Type":"array","ElementType":"%s"}`, fieldType)
  276. } else {
  277. result = fmt.Sprintf(`{"Type":"array","ElementType":%s}`, fieldType)
  278. }
  279. case *RecType:
  280. result = `{"Type":"struct","Fields":[`
  281. isFirst := true
  282. for _, f := range t.StreamFields {
  283. if isFirst {
  284. isFirst = false
  285. } else {
  286. result += ","
  287. }
  288. fieldType, q := doPrintFieldTypeForJson(f.FieldType)
  289. if q {
  290. result = fmt.Sprintf(`%s{"FieldType":"%s","Name":"%s"}`, result, fieldType, f.Name)
  291. } else {
  292. result = fmt.Sprintf(`%s{"FieldType":%s,"Name":"%s"}`, result, fieldType, f.Name)
  293. }
  294. }
  295. result += `]}`
  296. }
  297. return result, false
  298. }