Skip to content

Commit 65ddf9f

Browse files
authored
Merge pull request #11700 from RaduBerinde/hashjoiner-switcheroo
distsql: swap left and right in hashjoiner
2 parents a770f71 + 69f60ec commit 65ddf9f

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

pkg/sql/distsql/hashjoiner.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ type bucket struct {
3434

3535
// HashJoiner performs hash join, it has two input streams and one output.
3636
//
37-
// It works by reading the entire left stream and putting it in a hash
37+
// It works by reading the entire right stream and putting it in a hash
3838
// table. Thus, there is no guarantee on the ordering of results that stem only
39-
// from the left input (in the case of LEFT OUTER, FULL OUTER). However, it is
40-
// guaranteed that results that involve the right stream preserve the ordering;
41-
// i.e. all results that stem from right row (i) precede results that stem from
42-
// right row (i+1).
39+
// from the right input (in the case of RIGHT OUTER, FULL OUTER). However, it is
40+
// guaranteed that results that involve the left stream preserve the ordering;
41+
// i.e. all results that stem from left row (i) precede results that stem from
42+
// left row (i+1).
4343
type hashJoiner struct {
4444
joinerBase
4545

@@ -94,53 +94,53 @@ func (h *hashJoiner) Run(wg *sync.WaitGroup) {
9494
}
9595

9696
// buildPhase constructs our internal hash map of rows seen, this is done
97-
// entirely from the left stream with the encoding/group key generated using the
97+
// entirely from the right stream with the encoding/group key generated using the
9898
// left equality columns.
9999
func (h *hashJoiner) buildPhase() error {
100100
var scratch []byte
101101
for {
102-
lrow, err := h.inputs[0].NextRow()
103-
if err != nil || lrow == nil {
102+
rrow, err := h.inputs[1].NextRow()
103+
if err != nil || rrow == nil {
104104
return err
105105
}
106106

107-
encoded, err := h.encode(scratch, lrow, h.leftEqCols)
107+
encoded, err := h.encode(scratch, rrow, h.rightEqCols)
108108
if err != nil {
109109
return err
110110
}
111111

112112
b, _ := h.buckets[string(encoded)]
113-
b.rows = append(b.rows, lrow)
113+
b.rows = append(b.rows, rrow)
114114
h.buckets[string(encoded)] = b
115115

116116
scratch = encoded[:0]
117117
}
118118
}
119119

120-
// probePhase uses our constructed hash map of rows seen from the left stream,
121-
// we probe the map for each row retrieved from the right stream outputting the
122-
// merging of the two rows if matched. Behaviour for outer joins also behave as
123-
// expected, i.e. for RIGHT OUTER joins if no corresponding left row is seen an
124-
// empty DNull row is emitted instead.
120+
// probePhase uses our constructed hash map of rows seen from the right stream,
121+
// we probe the map for each row retrieved from the left stream outputting the
122+
// merging of the two rows if matched. Behaviour for outer joins is as expected,
123+
// i.e. for RIGHT OUTER joins if no corresponding left row is seen an empty
124+
// DNull row is emitted instead.
125125
func (h *hashJoiner) probePhase() error {
126126
var scratch []byte
127127
for {
128-
rrow, err := h.inputs[1].NextRow()
128+
lrow, err := h.inputs[0].NextRow()
129129
if err != nil {
130130
return err
131131
}
132-
if rrow == nil {
132+
if lrow == nil {
133133
break
134134
}
135135

136-
encoded, err := h.encode(scratch, rrow, h.rightEqCols)
136+
encoded, err := h.encode(scratch, lrow, h.leftEqCols)
137137
if err != nil {
138138
return err
139139
}
140140

141141
b, ok := h.buckets[string(encoded)]
142142
if !ok {
143-
row, err := h.render(nil, rrow)
143+
row, err := h.render(lrow, nil)
144144
if err != nil {
145145
return err
146146
}
@@ -150,7 +150,7 @@ func (h *hashJoiner) probePhase() error {
150150
} else {
151151
b.seen = true
152152
h.buckets[string(encoded)] = b
153-
for _, lrow := range b.rows {
153+
for _, rrow := range b.rows {
154154
row, err := h.render(lrow, rrow)
155155
if err != nil {
156156
return err
@@ -163,22 +163,22 @@ func (h *hashJoiner) probePhase() error {
163163
scratch = encoded[:0]
164164
}
165165

166-
if h.joinType == innerJoin || h.joinType == rightOuter {
166+
if h.joinType == innerJoin || h.joinType == leftOuter {
167167
return nil
168168
}
169169

170+
// Produce results for unmatched right rows (for RIGHT OUTER or FULL OUTER).
170171
for _, b := range h.buckets {
171172
if !b.seen {
172-
for _, lrow := range b.rows {
173-
row, err := h.render(lrow, nil)
173+
for _, rrow := range b.rows {
174+
row, err := h.render(nil, rrow)
174175
if err != nil {
175176
return err
176177
}
177178
if row != nil && !h.output.PushRow(row) {
178179
return nil
179180
}
180181
}
181-
182182
}
183183
}
184184

pkg/sql/distsql/processors.pb.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/distsql/processors.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ message MergeJoinerSpec {
180180
// HashJoinerSpec is the specification for a hash join processor. The processor
181181
// has two inputs and one output.
182182
//
183-
// The processor works by reading the entire left input and putting it in a hash
183+
// The processor works by reading the entire right input and putting it in a hash
184184
// table. Thus, there is no guarantee on the ordering of results that stem only
185-
// from the left input (in the case of LEFT_OUTER, FULL_OUTER). However, it is
186-
// guaranteed that results that involve the right stream preserve the ordering;
187-
// i.e. all results that stem from right row (i) precede results that stem from
188-
// right row (i+1).
185+
// from the right input (in the case of RIGHT_OUTER, FULL_OUTER). However, it is
186+
// guaranteed that results that involve the left stream preserve the ordering;
187+
// i.e. all results that stem from left row (i) precede results that stem from
188+
// left row (i+1).
189189
message HashJoinerSpec {
190190
// The join constraints certain columns from the left stream to equal
191191
// corresponding columns on the right stream. These must have the same length.

0 commit comments

Comments
 (0)