Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
39133: exec: minor cleanup of planning of hash and merge joiners r=yuzefovich a=yuzefovich

A minor bug with setting up output column types with hash joiner is
fixed. Also, in some cases, we need to use a join hint to force
planning of hash joiner in the test.

Release note: None

Co-authored-by: Yahor Yuzefovich <yahor@cockroachlabs.com>
  • Loading branch information
craig[bot] and yuzefovich committed Aug 2, 2019
2 parents 175c5ad + 7369f85 commit ca9184d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
33 changes: 20 additions & 13 deletions pkg/sql/distsqlrun/column_exec_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,16 @@ func newColOperator(
return result, err
}

columnTypes = make([]semtypes.T, len(leftTypes)+len(rightTypes))
copy(columnTypes, spec.Input[0].ColumnTypes)
copy(columnTypes[len(leftTypes):], spec.Input[1].ColumnTypes)

nLeftCols := uint32(len(leftTypes))
nRightCols := uint32(len(rightTypes))

leftOutCols := make([]uint32, 0)
rightOutCols := make([]uint32, 0)

// Note that we do not need a special treatment in case of LEFT SEMI and
// LEFT ANTI joins when setting up outCols because in such cases there will
// be a projection with post.OutputColumns already projecting out the right
// side.
if post.Projection {
for _, col := range post.OutputColumns {
if col < nLeftCols {
Expand All @@ -322,7 +322,6 @@ func newColOperator(
for i := uint32(0); i < nLeftCols; i++ {
leftOutCols = append(leftOutCols, i)
}

for i := uint32(0); i < nRightCols; i++ {
rightOutCols = append(rightOutCols, i)
}
Expand All @@ -345,6 +344,15 @@ func newColOperator(
return result, err
}

columnTypes = make([]semtypes.T, nLeftCols+nRightCols)
copy(columnTypes, spec.Input[0].ColumnTypes)
if core.HashJoiner.Type != sqlbase.JoinType_LEFT_SEMI {
// TODO(yuzefovich): update this conditional once LEFT ANTI is supported.
copy(columnTypes[nLeftCols:], spec.Input[1].ColumnTypes)
} else {
columnTypes = columnTypes[:nLeftCols]
}

if !core.HashJoiner.OnExpr.Empty() {
if core.HashJoiner.Type != sqlbase.JoinType_INNER {
return result, errors.Newf("can't plan non-inner hash join with on expressions")
Expand Down Expand Up @@ -377,25 +385,24 @@ func newColOperator(
leftOutCols := make([]uint32, 0, nLeftCols)
rightOutCols := make([]uint32, 0, nRightCols)

// Note that we do not need a special treatment in case of LEFT SEMI and
// LEFT ANTI joins when setting up outCols because in such cases there will
// be a projection with post.OutputColumns already projecting out the right
// side.
if post.Projection {
for _, col := range post.OutputColumns {
if col < nLeftCols {
leftOutCols = append(leftOutCols, col)
} else if core.MergeJoiner.Type != sqlbase.JoinType_LEFT_SEMI &&
core.MergeJoiner.Type != sqlbase.JoinType_LEFT_ANTI {
} else {
rightOutCols = append(rightOutCols, col-nLeftCols)
}
}
} else {
for i := uint32(0); i < nLeftCols; i++ {
leftOutCols = append(leftOutCols, i)
}

if core.MergeJoiner.Type != sqlbase.JoinType_LEFT_SEMI &&
core.MergeJoiner.Type != sqlbase.JoinType_LEFT_ANTI {
for i := uint32(0); i < nRightCols; i++ {
rightOutCols = append(rightOutCols, i)
}
for i := uint32(0); i < nRightCols; i++ {
rightOutCols = append(rightOutCols, i)
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/distsqlrun/columnar_operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ func generateColumnOrdering(
// comparison which can be either comparing a column from the left against a
// column from the right or comparing a column from either side against a
// constant.
// TODO(yuzefovich): update this once LEFT SEMI or LEFT ANTI is supported with
// ON expression.
func generateOnExpr(rng *rand.Rand, nCols int, nEqCols int, maxNum int) distsqlpb.Expression {
var comparison string
r := rng.Float64()
Expand Down
28 changes: 14 additions & 14 deletions pkg/sql/logictest/testdata/logic_test/exec_hash_join
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LogicTest: local-vec

# Test that the exec HashJoiner follows SQL NULL semantics for ON predicate
# equivalence. The use of sorts here force the planning of merge join.
# equivalence.

statement ok
CREATE TABLE t1 (k INT PRIMARY KEY, v INT)
Expand Down Expand Up @@ -34,41 +34,41 @@ statement ok
INSERT INTO c VALUES (1, 'a'), (1, 'b'), (2, 'c')

query IIII
SELECT * FROM t1 JOIN t2 ON t1.k = t2.x
SELECT * FROM t1 INNER HASH JOIN t2 ON t1.k = t2.x ORDER BY 1
----
0 4 0 5
3 4 3 2

query IIII rowsort
SELECT * FROM a AS a1 JOIN a AS a2 ON a1.k = a2.v
query IIII
SELECT * FROM a AS a1 JOIN a AS a2 ON a1.k = a2.v ORDER BY 1
----
0 1 2 0
1 2 0 1
2 0 1 2

query IIII rowsort
SELECT * FROM a AS a2 JOIN a AS a1 ON a1.k = a2.v
query IIII
SELECT * FROM a AS a2 JOIN a AS a1 ON a1.k = a2.v ORDER BY 1
----
0 1 1 2
1 2 2 0
2 0 0 1

query II
SELECT t2.y, t1.v FROM t1 JOIN t2 ON t1.k = t2.x
SELECT t2.y, t1.v FROM t1 INNER HASH JOIN t2 ON t1.k = t2.x ORDER BY 1 DESC
----
5 4
2 4

query IIII rowsort
SELECT * FROM t1 JOIN t2 ON t1.v = t2.x
query IIII
SELECT * FROM t1 JOIN t2 ON t1.v = t2.x ORDER BY 1
----
0 4 4 6
2 1 1 3
3 4 4 6
5 4 4 6

query IIII rowsort
SELECT * FROM t1 LEFT JOIN t2 ON t1.v = t2.x
query IIII
SELECT * FROM t1 LEFT JOIN t2 ON t1.v = t2.x ORDER BY 1
----
-1 -1 NULL NULL
0 4 4 6
Expand Down Expand Up @@ -97,14 +97,14 @@ SELECT * FROM t1 FULL JOIN t2 ON t1.v = t2.x
NULL NULL 3 2
NULL NULL 0 5

query IIT rowsort
SELECT b.a, b.b, b.c FROM b JOIN a ON b.a = a.k AND a.v = b.b
query IIT
SELECT b.a, b.b, b.c FROM b JOIN a ON b.a = a.k AND a.v = b.b ORDER BY 3
----
0 1 a
0 1 d

query ITI
SELECT b.a, b.c, c.a FROM b JOIN c ON b.b = c.a AND b.c = c.b
SELECT b.a, b.c, c.a FROM b JOIN c ON b.b = c.a AND b.c = c.b ORDER BY 2
----
0 a 1
2 b 1
Expand Down

0 comments on commit ca9184d

Please sign in to comment.