-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
collection.go
174 lines (159 loc) · 4.22 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package dataflow
import (
"fmt"
"sync"
"github.com/stackql/stackql-parser/go/vt/sqlparser"
"github.com/stackql/stackql/internal/stackql/logging"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo"
)
type Unit interface {
iDataFlowUnit()
}
// Collection is the DAG representing.
type Collection interface {
AddOrUpdateEdge(
source Vertex,
dest Vertex,
comparisonExpr *sqlparser.ComparisonExpr,
sourceExpr sqlparser.Expr,
destColumn *sqlparser.ColName) error
AddVertex(v Vertex)
GetAllUnits() ([]Unit, error)
GetNextID() int64
InDegree(v Vertex) int
OutDegree(v Vertex) int
Sort() error
Vertices() []Vertex
}
func NewStandardDataFlowCollection() Collection {
return &standardDataFlowCollection{
idMutex: &sync.Mutex{},
g: simple.NewWeightedDirectedGraph(0.0, 0.0),
vertices: make(map[Vertex]struct{}),
verticesForTableExprs: make(map[sqlparser.TableExpr]struct{}),
}
}
type standardDataFlowCollection struct {
idMutex *sync.Mutex
maxID int64
g *simple.WeightedDirectedGraph
sorted []graph.Node
orphans []Vertex
weaklyConnnectedGraphs []WeaklyConnectedComponent
vertices map[Vertex]struct{}
verticesForTableExprs map[sqlparser.TableExpr]struct{}
edges []Edge
}
func (dc *standardDataFlowCollection) GetNextID() int64 {
defer dc.idMutex.Unlock()
dc.idMutex.Lock()
dc.maxID++
return dc.maxID
}
func (dc *standardDataFlowCollection) AddOrUpdateEdge(
source Vertex,
dest Vertex,
comparisonExpr *sqlparser.ComparisonExpr,
sourceExpr sqlparser.Expr,
destColumn *sqlparser.ColName,
) error {
dc.AddVertex(source)
dc.AddVertex(dest)
existingEdge := dc.g.WeightedEdge(source.ID(), dest.ID())
if existingEdge == nil {
edge := newStandardDataFlowEdge(source, dest, comparisonExpr, sourceExpr, destColumn)
dc.edges = append(dc.edges, edge)
dc.g.SetWeightedEdge(edge)
return nil
}
switch existingEdge := existingEdge.(type) {
case Edge:
existingEdge.AddRelation(NewStandardDataFlowRelation(comparisonExpr, destColumn, sourceExpr))
default:
return fmt.Errorf("cannnot accomodate data flow edge of type: '%T'", existingEdge)
}
return nil
}
func (dc *standardDataFlowCollection) AddVertex(v Vertex) {
_, ok := dc.verticesForTableExprs[v.GetTableExpr()]
if ok {
if v.GetEquivalencyGroup() > 0 {
dc.g.AddNode(v) // TODO: change to acceptable idion
}
return
}
dc.vertices[v] = struct{}{}
dc.verticesForTableExprs[v.GetTableExpr()] = struct{}{}
dc.g.AddNode(v)
}
func (dc *standardDataFlowCollection) Sort() error {
var err error
dc.sorted, err = topo.Sort(dc.g)
if err != nil {
return err
}
err = dc.optimise()
return err
}
func (dc *standardDataFlowCollection) Vertices() []Vertex {
var rv []Vertex
for vert := range dc.vertices {
rv = append(rv, vert)
}
return rv
}
func (dc *standardDataFlowCollection) GetAllUnits() ([]Unit, error) {
var rv []Unit
for _, orphan := range dc.orphans {
rv = append(rv, orphan)
}
for _, component := range dc.weaklyConnnectedGraphs {
rv = append(rv, component)
}
return rv, nil
}
func (dc *standardDataFlowCollection) InDegree(v Vertex) int {
inDegree := 0
for _, e := range dc.edges {
if e.GetDest() == v {
inDegree++
}
}
return inDegree
}
func (dc *standardDataFlowCollection) OutDegree(v Vertex) int {
outDegree := 0
for _, e := range dc.edges {
if e.GetSource() == v {
outDegree++
}
}
return outDegree
}
func (dc *standardDataFlowCollection) optimise() error {
for _, node := range dc.sorted {
switch node := node.(type) {
case Vertex:
logging.GetLogger().Debugf("%v\n", node)
inDegree := dc.g.To(node.ID()).Len()
outDegree := dc.g.From(node.ID()).Len()
if inDegree == 0 && outDegree == 0 {
dc.orphans = append(dc.orphans, node)
continue
}
if inDegree == 0 && outDegree != 0 {
component := NewStandardDataFlowWeaklyConnectedComponent(dc, node)
err := component.Analyze()
if err != nil {
return err
}
dc.weaklyConnnectedGraphs = append(dc.weaklyConnnectedGraphs, component)
}
default:
return fmt.Errorf("cannot accomodate dataflow element of type = '%t'", node)
}
}
return nil
}