-
Notifications
You must be signed in to change notification settings - Fork 0
/
join_predicate.go
482 lines (426 loc) · 17 KB
/
join_predicate.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// 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.
package sql
import (
"bytes"
"fmt"
"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"
)
// joinPredicate implements the predicate logic for joins.
type joinPredicate struct {
// numLeft/RightCols are the number of columns in the left and right
// operands.
numLeftCols, numRightCols int
// The comparison function to use for each column. We need
// different functions because each USING column may have a different
// type (and they may be heterogeneous between left and right).
cmpFunctions []func(*parser.EvalContext, parser.Datum, parser.Datum) (parser.Datum, error)
// left/rightEqualityIndices give the position of USING columns
// on the left and right input row arrays, respectively.
leftEqualityIndices []int
rightEqualityIndices []int
// The list of names for the columns listed in leftEqualityIndices.
// Used mainly for pretty-printing.
leftColNames parser.NameList
// The list of names for the columns listed in rightEqualityIndices.
// Used mainly for pretty-printing.
rightColNames parser.NameList
// numMergedEqualityColumns specifies how many of the equality
// columns must be merged at the beginning of each result row. This
// is the desired behavior for USING and NATURAL JOIN.
numMergedEqualityColumns int
// For ON predicates or joins with an added filter expression,
// we need an IndexedVarHelper, the dataSourceInfo, a row buffer
// and the expression itself.
iVarHelper parser.IndexedVarHelper
info *dataSourceInfo
curRow parser.Datums
onCond parser.TypedExpr
// This struct must be allocated on the heap and its location stay
// stable after construction because it implements
// IndexedVarContainer and the IndexedVar objects in sub-expressions
// will link to it by reference after checkRenderStar / analyzeExpr.
// Enforce this using NoCopy.
noCopy util.NoCopy
}
// makeCrossPredicate constructs a joinPredicate object for joins with a ON clause.
func makeCrossPredicate(left, right *dataSourceInfo) (*joinPredicate, *dataSourceInfo, error) {
return makeEqualityPredicate(left, right, nil, nil, 0, nil)
}
// tryAddEqualityFilter attempts to turn the given filter expression into
// an equality predicate. It returns true iff the transformation succeeds.
func (p *joinPredicate) tryAddEqualityFilter(filter parser.Expr, left, right *dataSourceInfo) bool {
c, ok := filter.(*parser.ComparisonExpr)
if !ok || c.Operator != parser.EQ {
return false
}
lhs, ok := c.Left.(*parser.IndexedVar)
if !ok {
return false
}
rhs, ok := c.Right.(*parser.IndexedVar)
if !ok {
return false
}
sourceBoundary := p.numMergedEqualityColumns + len(left.sourceColumns)
if (lhs.Idx >= sourceBoundary && rhs.Idx >= sourceBoundary) ||
(lhs.Idx < sourceBoundary && rhs.Idx < sourceBoundary) {
// Both variables are on the same side of the join (e.g. `a JOIN b ON a.x = a.y`).
return false
}
if lhs.Idx > rhs.Idx {
lhs, rhs = rhs, lhs
}
// At this point we have an equality, so we can add it to the list
// of equality columns.
// To do this we must be a bit careful: the expression contains
// IndexedVars, and the column indices at this point will refer to
// the full column set of the joinPredicate, including the
// merged columns.
leftColIdx := lhs.Idx - p.numMergedEqualityColumns
rightColIdx := rhs.Idx - len(left.sourceColumns) - p.numMergedEqualityColumns
// Also, we will want to avoid redundant equality checks.
for i := range p.leftEqualityIndices {
if p.leftEqualityIndices[i] == leftColIdx && p.rightEqualityIndices[i] == rightColIdx {
// The filter is already there; simply absorb it and say we succeeded.
return true
}
}
// First resolve the comparison function. We can't use the
// ComparisonExpr's memoized comparison directly, because we may
// have swapped the operands above.
fn, found := parser.FindEqualComparisonFunction(lhs.ResolvedType(), rhs.ResolvedType())
if !found {
// This is ... unexpected. This means we have a valid ON
// expression of the form "a = b" but the expression "b = a" is
// invalid. We could simply avoid the optimization but this is
// really a bug in the built-in semantics so we want to complain
// loudly.
panic(fmt.Errorf("predicate %s is valid, but '%T = %T' cannot be type checked", c, lhs, rhs))
}
p.cmpFunctions = append(p.cmpFunctions, fn)
p.leftEqualityIndices = append(p.leftEqualityIndices, leftColIdx)
p.rightEqualityIndices = append(p.rightEqualityIndices, rightColIdx)
p.leftColNames = append(p.leftColNames, parser.Name(left.sourceColumns[leftColIdx].Name))
p.rightColNames = append(p.rightColNames, parser.Name(right.sourceColumns[rightColIdx].Name))
return true
}
// makeOnPredicate constructs a joinPredicate object for joins with a ON clause.
func (p *planner) makeOnPredicate(
ctx context.Context, left, right *dataSourceInfo, expr parser.Expr,
) (*joinPredicate, *dataSourceInfo, error) {
pred, info, err := makeEqualityPredicate(left, right, nil, nil, 0, nil)
if err != nil {
return nil, nil, err
}
// Determine the on condition expression.
onCond, err := p.analyzeExpr(ctx, expr, multiSourceInfo{info}, pred.iVarHelper, parser.TypeBool, true, "ON")
if err != nil {
return nil, nil, err
}
pred.onCond = onCond
return pred, info, nil
}
// makeUsingPredicate constructs a joinPredicate object for joins with
// a USING clause.
func makeUsingPredicate(
left, right *dataSourceInfo, colNames parser.NameList,
) (*joinPredicate, *dataSourceInfo, error) {
seenNames := make(map[string]struct{})
for _, syntaxColName := range colNames {
colName := string(syntaxColName)
// Check for USING(x,x)
if _, ok := seenNames[colName]; ok {
return nil, nil, fmt.Errorf("column %q appears more than once in USING clause", colName)
}
seenNames[colName] = struct{}{}
}
return makeEqualityPredicate(left, right, colNames, colNames, len(colNames), nil)
}
// makeEqualityPredicate constructs a joinPredicate object for joins. The join
// condition includes equality between numMergedEqualityColumns columns,
// specified by leftColNames and rightColNames.
func makeEqualityPredicate(
left, right *dataSourceInfo,
leftColNames, rightColNames parser.NameList,
numMergedEqualityColumns int,
concatInfos *dataSourceInfo,
) (resPred *joinPredicate, info *dataSourceInfo, err error) {
if len(leftColNames) != len(rightColNames) {
panic(fmt.Errorf("left columns' length %q doesn't match right columns' length %q in EqualityPredicate",
len(leftColNames), len(rightColNames)))
}
if len(leftColNames) < numMergedEqualityColumns {
panic(fmt.Errorf("cannot merge %d columns, only %d columns to compare", numMergedEqualityColumns, len(leftColNames)))
}
// Prepare the arrays populated below.
cmpOps := make([]func(*parser.EvalContext, parser.Datum, parser.Datum) (parser.Datum, error), len(leftColNames))
leftEqualityIndices := make([]int, len(leftColNames))
rightEqualityIndices := make([]int, len(rightColNames))
// usedLeft represents the list of indices that participate in the
// equality predicate. They are collected in order to determine
// below which columns remain after the equality; this is used
// only when merging result columns.
var usedLeft, usedRight []int
var columns sqlbase.ResultColumns
if numMergedEqualityColumns > 0 {
usedLeft = make([]int, len(left.sourceColumns))
for i := range usedLeft {
usedLeft[i] = invalidColIdx
}
usedRight = make([]int, len(right.sourceColumns))
for i := range usedRight {
usedRight[i] = invalidColIdx
}
nResultColumns := len(left.sourceColumns) + len(right.sourceColumns) - numMergedEqualityColumns
columns = make(sqlbase.ResultColumns, 0, nResultColumns)
}
// Find out which columns are involved in EqualityPredicate.
for i := range leftColNames {
leftColName := string(leftColNames[i])
rightColName := string(rightColNames[i])
// Find the column name on the left.
leftIdx, leftType, err := pickUsingColumn(left.sourceColumns, leftColName, "left")
if err != nil {
return nil, nil, err
}
// Find the column name on the right.
rightIdx, rightType, err := pickUsingColumn(right.sourceColumns, rightColName, "right")
if err != nil {
return nil, nil, err
}
// Remember the indices.
leftEqualityIndices[i] = leftIdx
rightEqualityIndices[i] = rightIdx
// Memoize the comparison function.
fn, found := parser.FindEqualComparisonFunction(leftType, rightType)
if !found {
return nil, nil, fmt.Errorf("JOIN/USING types %s for left column %s and %s for right column %s cannot be matched",
leftType, leftColName, rightType, rightColName)
}
cmpOps[i] = fn
if i < numMergedEqualityColumns {
usedLeft[leftIdx] = i
usedRight[rightIdx] = i
// Merged columns come first in the results.
columns = append(columns, left.sourceColumns[leftIdx])
}
}
// Now, prepare/complete the metadata for the result columns.
// The structure of the join data source results is like this:
// - first, all the equality/USING columns;
// - then all the left columns,
// - then all the right columns,
// The duplicate columns appended after the equality/USING columns
// are hidden so that they are invisible to star expansion, but
// not omitted so that they can still be selected separately.
// Finish collecting the column definitions from the left and
// right data sources.
for i, c := range left.sourceColumns {
if usedLeft != nil && usedLeft[i] != invalidColIdx {
c.Hidden = true
}
columns = append(columns, c)
}
for i, c := range right.sourceColumns {
if usedRight != nil && usedRight[i] != invalidColIdx {
c.Hidden = true
}
columns = append(columns, c)
}
// Compute the mappings from table aliases to column sets from
// both sides into a new alias-columnset mapping for the result
// rows. We need to be extra careful about the aliases
// for the anonymous table, which needs to be merged.
aliases := make(sourceAliases, 0, len(left.sourceAliases)+len(right.sourceAliases))
collectAliases := func(sourceAliases sourceAliases, offset int) {
for _, alias := range sourceAliases {
if alias.name == anonymousTable {
continue
}
newRange := make([]int, len(alias.columnRange))
for i, colIdx := range alias.columnRange {
newRange[i] = colIdx + offset
}
aliases = append(aliases, sourceAlias{name: alias.name, columnRange: newRange})
}
}
collectAliases(left.sourceAliases, numMergedEqualityColumns)
collectAliases(right.sourceAliases, numMergedEqualityColumns+len(left.sourceColumns))
anonymousAlias := sourceAlias{name: anonymousTable, columnRange: nil}
var hiddenLeftNames, hiddenRightNames []string
// All the merged columns at the beginning belong to the
// anonymous data source.
for i := 0; i < numMergedEqualityColumns; i++ {
anonymousAlias.columnRange = append(anonymousAlias.columnRange, i)
hiddenLeftNames = append(hiddenLeftNames, left.sourceColumns[i].Name)
hiddenRightNames = append(hiddenRightNames, right.sourceColumns[i].Name)
}
// Now collect the other table-less columns into the anonymous data
// source, but hide (skip) those that are already merged.
collectAnonymousAliases := func(
sourceAliases sourceAliases, hiddenNames []string, cols sqlbase.ResultColumns, offset int,
) {
for _, alias := range sourceAliases {
if alias.name != anonymousTable {
continue
}
for _, colIdx := range alias.columnRange {
isHidden := false
for _, hiddenName := range hiddenNames {
if cols[colIdx].Name == hiddenName {
isHidden = true
break
}
}
if !isHidden {
anonymousAlias.columnRange = append(anonymousAlias.columnRange, colIdx+offset)
}
}
}
}
collectAnonymousAliases(left.sourceAliases, hiddenLeftNames, left.sourceColumns,
numMergedEqualityColumns)
collectAnonymousAliases(right.sourceAliases, hiddenRightNames, right.sourceColumns,
numMergedEqualityColumns+len(left.sourceColumns))
if anonymousAlias.columnRange != nil {
aliases = append(aliases, anonymousAlias)
}
info = &dataSourceInfo{
sourceColumns: columns,
sourceAliases: aliases,
}
pred := &joinPredicate{
numLeftCols: len(left.sourceColumns),
numRightCols: len(right.sourceColumns),
leftColNames: leftColNames,
rightColNames: rightColNames,
numMergedEqualityColumns: numMergedEqualityColumns,
cmpFunctions: cmpOps,
leftEqualityIndices: leftEqualityIndices,
rightEqualityIndices: rightEqualityIndices,
info: info,
}
// We must initialize the indexed var helper in all cases, even when
// there is no on condition, so that getNeededColumns() does not get
// confused.
pred.iVarHelper = parser.MakeIndexedVarHelper(pred, len(columns))
return pred, info, nil
}
// IndexedVarEval implements the parser.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarEval(idx int, ctx *parser.EvalContext) (parser.Datum, error) {
return p.curRow[idx].Eval(ctx)
}
// IndexedVarResolvedType implements the parser.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarResolvedType(idx int) parser.Type {
return p.info.sourceColumns[idx].Typ
}
// IndexedVarFormat implements the parser.IndexedVarContainer interface.
func (p *joinPredicate) IndexedVarFormat(buf *bytes.Buffer, f parser.FmtFlags, idx int) {
p.info.FormatVar(buf, f, idx)
}
// eval for joinPredicate runs the on condition across the columns that do
// not participate in the equality (the equality columns are checked
// in the join algorithm already).
// Returns true if there is no on condition or the on condition accepts the
// row.
func (p *joinPredicate) eval(
ctx *parser.EvalContext, result, leftRow, rightRow parser.Datums,
) (bool, error) {
if p.onCond != nil {
p.curRow = result
copy(p.curRow[p.numMergedEqualityColumns:p.numMergedEqualityColumns+len(leftRow)], leftRow)
copy(p.curRow[p.numMergedEqualityColumns+len(leftRow):], rightRow)
return sqlbase.RunFilter(p.onCond, ctx)
}
return true, nil
}
// getNeededColumns figures out the columns needed for the two
// sources. This takes into account both the equality columns and the
// predicate expression.
func (p *joinPredicate) getNeededColumns(neededJoined []bool) ([]bool, []bool) {
// Reset the helper and rebind the variable to detect which columns
// are effectively needed.
p.onCond = p.iVarHelper.Rebind(p.onCond, true, false)
// The columns that are part of the expression are always needed.
neededJoined = append([]bool(nil), neededJoined...)
for i := range neededJoined {
if p.iVarHelper.IndexedVarUsed(i) {
neededJoined[i] = true
}
}
leftNeeded := neededJoined[p.numMergedEqualityColumns : p.numMergedEqualityColumns+p.numLeftCols]
rightNeeded := neededJoined[p.numMergedEqualityColumns+p.numLeftCols:]
// The equality columns are always needed.
for i := range p.leftEqualityIndices {
leftNeeded[p.leftEqualityIndices[i]] = true
rightNeeded[p.rightEqualityIndices[i]] = true
}
return leftNeeded, rightNeeded
}
// prepareRow prepares the output row by combining values from the
// input data sources.
func (p *joinPredicate) prepareRow(result, leftRow, rightRow parser.Datums) {
var offset int
for offset = 0; offset < p.numMergedEqualityColumns; offset++ {
// The result for merged columns must be computed as per COALESCE().
leftIdx := p.leftEqualityIndices[offset]
if leftRow[leftIdx] != parser.DNull {
result[offset] = leftRow[leftIdx]
} else {
result[offset] = rightRow[p.rightEqualityIndices[offset]]
}
}
copy(result[offset:offset+len(leftRow)], leftRow)
copy(result[offset+len(leftRow):], rightRow)
}
// encode returns the encoding of a row from a given side (left or right),
// according to the columns specified by the equality constraints.
func (p *joinPredicate) encode(b []byte, row parser.Datums, cols []int) ([]byte, bool, error) {
var err error
containsNull := false
for _, colIdx := range cols {
if row[colIdx] == parser.DNull {
containsNull = true
}
b, err = sqlbase.EncodeDatum(b, row[colIdx])
if err != nil {
return nil, false, err
}
}
return b, containsNull, nil
}
// pickUsingColumn searches for a column whose name matches colName.
// The column index and type are returned if found, otherwise an error
// is reported.
func pickUsingColumn(
cols sqlbase.ResultColumns, colName string, context string,
) (int, parser.Type, error) {
idx := invalidColIdx
for j, col := range cols {
if col.Hidden {
continue
}
if col.Name == colName {
idx = j
}
}
if idx == invalidColIdx {
return idx, nil, fmt.Errorf("column \"%s\" specified in USING clause does not exist in %s table", colName, context)
}
return idx, cols[idx].Typ, nil
}