forked from cockroachdb/cockroach
/
joinerbase.go
110 lines (100 loc) · 3.17 KB
/
joinerbase.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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Irfan Sharif (irfansharif@cockroachlabs.com)
package distsql
import (
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
type joinerBase struct {
inputs []RowSource
output RowReceiver
ctx context.Context
joinType joinType
outputCols columns
filter exprHelper
rowAlloc sqlbase.EncDatumRowAlloc
emptyLeft sqlbase.EncDatumRow
emptyRight sqlbase.EncDatumRow
combinedRow sqlbase.EncDatumRow
}
// err := init(flowCtx, inputs, output, spec.OutputColumns,
// spec.Type, spec.LeftTypes, spec.RightTypes, spec.Expr)
func (jb *joinerBase) init(
flowCtx *FlowCtx,
inputs []RowSource,
output RowReceiver,
outputCols []uint32,
jType JoinType,
leftTypes []sqlbase.ColumnType_Kind,
rightTypes []sqlbase.ColumnType_Kind,
expr Expression,
) error {
jb.inputs = inputs
jb.output = output
jb.ctx = log.WithLogTag(flowCtx.Context, "Joiner", nil)
jb.outputCols = columns(outputCols)
jb.joinType = joinType(jType)
jb.emptyLeft = make(sqlbase.EncDatumRow, len(leftTypes))
for i := range jb.emptyLeft {
jb.emptyLeft[i].Datum = parser.DNull
}
jb.emptyRight = make(sqlbase.EncDatumRow, len(rightTypes))
for i := range jb.emptyRight {
jb.emptyRight[i].Datum = parser.DNull
}
return jb.filter.init(expr, append(leftTypes, rightTypes...), flowCtx.evalCtx)
}
// render evaluates the provided filter and constructs a row with columns from
// both rows as specified by the provided output columns. We expect left or
// right to be nil if there was no explicit "join" match, the filter is then
// evaluated on a combinedRow with null values for the columns of the nil row.
// render returns a nil row if no row is to be emitted (eg. if join type is
// inner join and one of the given rows is nil).
func (jb *joinerBase) render(lrow, rrow sqlbase.EncDatumRow) (sqlbase.EncDatumRow, error) {
switch jb.joinType {
case innerJoin:
if lrow == nil || rrow == nil {
return nil, nil
}
case fullOuter:
if lrow == nil {
lrow = jb.emptyLeft
} else if rrow == nil {
rrow = jb.emptyRight
}
case leftOuter:
if rrow == nil {
rrow = jb.emptyRight
}
case rightOuter:
if lrow == nil {
lrow = jb.emptyLeft
}
}
jb.combinedRow = append(jb.combinedRow[:0], lrow...)
jb.combinedRow = append(jb.combinedRow, rrow...)
res, err := jb.filter.evalFilter(jb.combinedRow)
if !res || err != nil {
return nil, err
}
row := jb.rowAlloc.AllocRow(len(jb.outputCols))
for i, col := range jb.outputCols {
row[i] = jb.combinedRow[col]
}
return row, nil
}