vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/hash_join_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package engine 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/stretchr/testify/require" 24 25 "vitess.io/vitess/go/sqltypes" 26 querypb "vitess.io/vitess/go/vt/proto/query" 27 ) 28 29 func TestHashJoinExecuteSameType(t *testing.T) { 30 leftPrim := &fakePrimitive{ 31 results: []*sqltypes.Result{ 32 sqltypes.MakeTestResult( 33 sqltypes.MakeTestFields( 34 "col1|col2|col3", 35 "int64|varchar|varchar", 36 ), 37 "1|a|aa", 38 "2|b|bb", 39 "3|c|cc", 40 ), 41 }, 42 } 43 rightPrim := &fakePrimitive{ 44 results: []*sqltypes.Result{ 45 sqltypes.MakeTestResult( 46 sqltypes.MakeTestFields( 47 "col4|col5|col6", 48 "int64|varchar|varchar", 49 ), 50 "1|d|dd", 51 "3|e|ee", 52 "4|f|ff", 53 "3|g|gg", 54 ), 55 }, 56 } 57 58 // Normal join 59 jn := &HashJoin{ 60 Opcode: InnerJoin, 61 Left: leftPrim, 62 Right: rightPrim, 63 Cols: []int{-1, -2, 1, 2}, 64 LHSKey: 0, 65 RHSKey: 0, 66 } 67 r, err := jn.TryExecute(context.Background(), &noopVCursor{}, map[string]*querypb.BindVariable{}, true) 68 require.NoError(t, err) 69 leftPrim.ExpectLog(t, []string{ 70 `Execute true`, 71 }) 72 rightPrim.ExpectLog(t, []string{ 73 `Execute true`, 74 }) 75 expectResult(t, "jn.Execute", r, sqltypes.MakeTestResult( 76 sqltypes.MakeTestFields( 77 "col1|col2|col4|col5", 78 "int64|varchar|int64|varchar", 79 ), 80 "1|a|1|d", 81 "3|c|3|e", 82 "3|c|3|g", 83 )) 84 } 85 86 func TestHashJoinExecuteDifferentType(t *testing.T) { 87 leftPrim := &fakePrimitive{ 88 results: []*sqltypes.Result{ 89 sqltypes.MakeTestResult( 90 sqltypes.MakeTestFields( 91 "col1|col2|col3", 92 "int64|varchar|varchar", 93 ), 94 "1|a|aa", 95 "2|b|bb", 96 "3|c|cc", 97 "5|c|cc", 98 ), 99 }, 100 } 101 rightPrim := &fakePrimitive{ 102 results: []*sqltypes.Result{ 103 sqltypes.MakeTestResult( 104 sqltypes.MakeTestFields( 105 "col4|col5|col6", 106 "varchar|varchar|varchar", 107 ), 108 "1.00|d|dd", 109 "3|e|ee", 110 "2.89|z|zz", 111 "4|f|ff", 112 "3|g|gg", 113 " 5.0toto|g|gg", 114 "w|ww|www", 115 ), 116 }, 117 } 118 119 // Normal join 120 jn := &HashJoin{ 121 Opcode: InnerJoin, 122 Left: leftPrim, 123 Right: rightPrim, 124 Cols: []int{-1, -2, 1, 2}, 125 LHSKey: 0, 126 RHSKey: 0, 127 ComparisonType: querypb.Type_FLOAT64, 128 } 129 r, err := jn.TryExecute(context.Background(), &noopVCursor{}, map[string]*querypb.BindVariable{}, true) 130 require.NoError(t, err) 131 leftPrim.ExpectLog(t, []string{ 132 `Execute true`, 133 }) 134 rightPrim.ExpectLog(t, []string{ 135 `Execute true`, 136 }) 137 expectResult(t, "jn.Execute", r, sqltypes.MakeTestResult( 138 sqltypes.MakeTestFields( 139 "col1|col2|col4|col5", 140 "int64|varchar|varchar|varchar", 141 ), 142 "1|a|1.00|d", 143 "3|c|3|e", 144 "3|c|3|g", 145 "5|c| 5.0toto|g", 146 )) 147 }