io.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 graph
  15. import "fmt"
  16. type (
  17. IoInputType uint8
  18. IoRowType uint8
  19. IoCollectionType uint8
  20. )
  21. const (
  22. IOINPUT_TYPE_SAME IoInputType = iota
  23. IOINPUT_TYPE_ROW // 0b01
  24. IOINPUT_TYPE_COLLECTION // 0b10
  25. IOINPUT_TYPE_ANY // 0b11
  26. )
  27. var inputTypes = map[IoInputType]string{
  28. IOINPUT_TYPE_ROW: "row",
  29. IOINPUT_TYPE_COLLECTION: "collection",
  30. IOINPUT_TYPE_ANY: "any",
  31. IOINPUT_TYPE_SAME: "same",
  32. }
  33. const (
  34. IOROW_TYPE_SAME IoRowType = iota
  35. IOROW_TYPE_SINGLE // 0b01
  36. IOROW_TYPE_MERGED // 0b10
  37. IOROW_TYPE_ANY // 0b11
  38. )
  39. var rowTypes = map[IoRowType]string{
  40. IOROW_TYPE_SINGLE: "single emitter row",
  41. IOROW_TYPE_MERGED: "merged row",
  42. IOROW_TYPE_ANY: "any",
  43. IOROW_TYPE_SAME: "same",
  44. }
  45. const (
  46. IOCOLLECTION_TYPE_SAME IoCollectionType = iota
  47. IOCOLLECTION_TYPE_SINGLE
  48. IOCOLLECTION_TYPE_GROUPED
  49. IOCOLLECTION_TYPE_ANY
  50. )
  51. var collectionsTypes = map[IoCollectionType]string{
  52. IOCOLLECTION_TYPE_SINGLE: "non-grouped collection",
  53. IOCOLLECTION_TYPE_GROUPED: "grouped collection",
  54. IOCOLLECTION_TYPE_ANY: "any",
  55. IOCOLLECTION_TYPE_SAME: "same",
  56. }
  57. // IOType is the type of input/output
  58. // all fields are default to any
  59. type IOType struct {
  60. Type IoInputType `json:"type"`
  61. RowType IoRowType `json:"rowType"`
  62. CollectionType IoCollectionType `json:"collectionType"`
  63. AllowMulti bool `json:"allowMulti"`
  64. }
  65. // NewIOType creates a new IOType
  66. func NewIOType() *IOType {
  67. return &IOType{
  68. Type: IOINPUT_TYPE_ANY,
  69. RowType: IOROW_TYPE_ANY,
  70. CollectionType: IOCOLLECTION_TYPE_ANY,
  71. }
  72. }
  73. func Fit(value, condition *IOType) (bool, error) {
  74. if value.Type&condition.Type == 0 {
  75. return false, fmt.Errorf("input type mismatch, expect %s, got %s", inputTypes[condition.Type], inputTypes[value.Type])
  76. }
  77. if value.RowType&condition.RowType == 0 {
  78. return false, fmt.Errorf("row type mismatch, expect %s, got %s", rowTypes[condition.RowType], rowTypes[value.RowType])
  79. }
  80. if value.CollectionType&condition.CollectionType == 0 {
  81. return false, fmt.Errorf("collection type mismatch, expect %s, got %s", collectionsTypes[condition.CollectionType], collectionsTypes[value.CollectionType])
  82. }
  83. return true, nil
  84. }
  85. func MapOut(previous, origin *IOType) (result *IOType) {
  86. result = NewIOType()
  87. if origin.Type == IOINPUT_TYPE_SAME {
  88. result.Type = previous.Type
  89. result.RowType = previous.RowType
  90. result.CollectionType = previous.CollectionType
  91. } else {
  92. result.Type = origin.Type
  93. if origin.RowType == IOROW_TYPE_SAME {
  94. result.RowType = previous.RowType
  95. } else {
  96. result.RowType = origin.RowType
  97. }
  98. if origin.CollectionType == IOCOLLECTION_TYPE_SAME {
  99. result.CollectionType = previous.CollectionType
  100. } else {
  101. result.CollectionType = origin.CollectionType
  102. }
  103. }
  104. return
  105. }
  106. // OpIO The io constraints for a node
  107. var OpIO = map[string][]*IOType{
  108. "aggfunc": {
  109. {Type: IOINPUT_TYPE_COLLECTION, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  110. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_GROUPED},
  111. },
  112. "filter": {
  113. {Type: IOINPUT_TYPE_ANY, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  114. {Type: IOINPUT_TYPE_SAME},
  115. },
  116. "function": {
  117. {Type: IOINPUT_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_SINGLE, RowType: IOROW_TYPE_ANY},
  118. {Type: IOINPUT_TYPE_SAME},
  119. },
  120. "groupby": {
  121. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_SINGLE, RowType: IOROW_TYPE_ANY},
  122. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_GROUPED},
  123. },
  124. "join": {
  125. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_SINGLE, RowType: IOROW_TYPE_SINGLE},
  126. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_SINGLE, RowType: IOROW_TYPE_MERGED},
  127. },
  128. "orderby": {
  129. {Type: IOINPUT_TYPE_COLLECTION, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  130. {Type: IOINPUT_TYPE_SAME},
  131. },
  132. "pick": {
  133. {Type: IOINPUT_TYPE_ANY, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  134. {Type: IOINPUT_TYPE_SAME},
  135. },
  136. "window": {
  137. {Type: IOINPUT_TYPE_ROW, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY, AllowMulti: true},
  138. {Type: IOINPUT_TYPE_COLLECTION, CollectionType: IOCOLLECTION_TYPE_SINGLE, RowType: IOROW_TYPE_SINGLE},
  139. },
  140. "switch": {
  141. {Type: IOINPUT_TYPE_ANY, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  142. {Type: IOINPUT_TYPE_SAME},
  143. },
  144. "script": {
  145. {Type: IOINPUT_TYPE_ROW, RowType: IOROW_TYPE_ANY, CollectionType: IOCOLLECTION_TYPE_ANY},
  146. {Type: IOINPUT_TYPE_SAME},
  147. },
  148. }