io.go 4.8 KB

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