/
default_joinable.go
131 lines (108 loc) · 2.94 KB
/
default_joinable.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
package dataframe
import (
"github.com/AdikaStyle/go-df/backend"
"github.com/AdikaStyle/go-df/conds"
"github.com/AdikaStyle/go-df/types"
)
type defaultJoinable struct {
df Dataframe
}
func newDefaultJoinable(df Dataframe) *defaultJoinable {
return &defaultJoinable{df: df}
}
func (this *defaultJoinable) LeftJoin(with Dataframe, on conds.JoinCondition) Dataframe {
newHeaders := combineHeaders(this.df, with)
joinedDf := this.df.constructNew(newHeaders)
index := make(map[string][]int)
with.VisitRows(func(id int, row backend.Row) {
indexKey := on.ColumnsHint(row)
index[indexKey] = append(index[indexKey], id)
})
this.df.VisitRows(func(id int, left backend.Row) {
indexKey := on.ColumnsHint(left)
matches, found := index[indexKey]
if found {
for _, match := range matches {
right := with.getBackend().GetRow(match)
if on.Match(left, right) {
joinedRow := combineRows(left, right, false)
joinedDf.getBackend().AppendRow(joinedRow)
}
}
} else {
row := combineRows(left, with.getBackend().GetRow(0), true)
joinedDf.getBackend().AppendRow(row)
}
})
return joinedDf
}
func (this *defaultJoinable) RightJoin(with Dataframe, on conds.JoinCondition) Dataframe {
return with.LeftJoin(this.df, on)
}
func (this *defaultJoinable) InnerJoin(with Dataframe, on conds.JoinCondition) Dataframe {
newHeaders := combineHeaders(this.df, with)
joinedDf := this.df.constructNew(newHeaders)
var small, big Dataframe
if this.df.GetRowCount() > with.GetRowCount() {
big = this.df
small = with
} else {
big = with
small = this.df
}
index := make(map[string][]int)
small.VisitRows(func(id int, row backend.Row) {
indexKey := on.ColumnsHint(row)
index[indexKey] = append(index[indexKey], id)
})
big.VisitRows(func(id int, row backend.Row) {
indexKey := on.ColumnsHint(row)
matches, found := index[indexKey]
if found {
for _, match := range matches {
row2 := small.getBackend().GetRow(match)
if on.Match(row, row2) {
joinedRow := combineRows(row, row2, false)
joinedDf.getBackend().AppendRow(joinedRow)
}
}
}
})
return joinedDf
}
func (this *defaultJoinable) OuterJoin(with Dataframe, on conds.JoinCondition) Dataframe {
panic("unimplemented")
}
func combineHeaders(left Dataframe, right Dataframe) backend.Headers {
var newHeaders backend.Headers
dup := make(map[string]bool)
for _, h := range left.GetHeaders() {
newHeaders = append(newHeaders, h)
dup[h.Name] = true
}
for _, h := range right.GetHeaders() {
if _, found := dup[h.Name]; !found {
newHeaders = append(newHeaders, h)
}
}
return newHeaders
}
func combineRows(left backend.Row, right backend.Row, rightMissing bool) backend.Row {
dup := make(map[string]bool)
row := make(backend.Row)
for k, v := range left {
dup[k] = true
row[k] = v
}
for k, v := range right {
if _, found := dup[k]; found {
continue
}
if rightMissing {
row[k] = types.Missing
} else {
row[k] = v
}
}
return row
}