vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/semi_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  	"vitess.io/vitess/go/test/utils"
    24  
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"vitess.io/vitess/go/sqltypes"
    28  
    29  	querypb "vitess.io/vitess/go/vt/proto/query"
    30  )
    31  
    32  func TestSemiJoinExecute(t *testing.T) {
    33  	leftPrim := &fakePrimitive{
    34  		results: []*sqltypes.Result{
    35  			sqltypes.MakeTestResult(
    36  				sqltypes.MakeTestFields(
    37  					"col1|col2|col3",
    38  					"int64|varchar|varchar",
    39  				),
    40  				"1|a|aa",
    41  				"2|b|bb",
    42  				"3|c|cc",
    43  			),
    44  		},
    45  	}
    46  	rightFields := sqltypes.MakeTestFields(
    47  		"col4|col5|col6",
    48  		"int64|varchar|varchar",
    49  	)
    50  	rightPrim := &fakePrimitive{
    51  		results: []*sqltypes.Result{
    52  			sqltypes.MakeTestResult(
    53  				rightFields,
    54  				"4|d|dd",
    55  			),
    56  			sqltypes.MakeTestResult(
    57  				rightFields,
    58  			),
    59  			sqltypes.MakeTestResult(
    60  				rightFields,
    61  				"5|e|ee",
    62  				"6|f|ff",
    63  				"7|g|gg",
    64  			),
    65  		},
    66  	}
    67  	bv := map[string]*querypb.BindVariable{
    68  		"a": sqltypes.Int64BindVariable(10),
    69  	}
    70  
    71  	jn := &SemiJoin{
    72  		Left:  leftPrim,
    73  		Right: rightPrim,
    74  		Vars: map[string]int{
    75  			"bv": 1,
    76  		},
    77  		Cols: []int{-1, -2, -3},
    78  	}
    79  	r, err := jn.TryExecute(context.Background(), &noopVCursor{}, bv, true)
    80  	require.NoError(t, err)
    81  	leftPrim.ExpectLog(t, []string{
    82  		`Execute a: type:INT64 value:"10" true`,
    83  	})
    84  	rightPrim.ExpectLog(t, []string{
    85  		`Execute a: type:INT64 value:"10" bv: type:VARCHAR value:"a" false`,
    86  		`Execute a: type:INT64 value:"10" bv: type:VARCHAR value:"b" false`,
    87  		`Execute a: type:INT64 value:"10" bv: type:VARCHAR value:"c" false`,
    88  	})
    89  	utils.MustMatch(t, sqltypes.MakeTestResult(
    90  		sqltypes.MakeTestFields(
    91  			"col1|col2|col3",
    92  			"int64|varchar|varchar",
    93  		),
    94  		"1|a|aa",
    95  		"3|c|cc",
    96  	), r)
    97  }
    98  
    99  func TestSemiJoinStreamExecute(t *testing.T) {
   100  	leftPrim := &fakePrimitive{
   101  		results: []*sqltypes.Result{
   102  			sqltypes.MakeTestResult(
   103  				sqltypes.MakeTestFields(
   104  					"col1|col2|col3",
   105  					"int64|varchar|varchar",
   106  				),
   107  				"1|a|aa",
   108  				"2|b|bb",
   109  			), sqltypes.MakeTestResult(
   110  				sqltypes.MakeTestFields(
   111  					"col1|col2|col3",
   112  					"int64|varchar|varchar",
   113  				),
   114  				"3|c|cc",
   115  				"4|d|dd",
   116  			),
   117  		},
   118  		allResultsInOneCall: true,
   119  	}
   120  	rightFields := sqltypes.MakeTestFields(
   121  		"col4|col5|col6",
   122  		"int64|varchar|varchar",
   123  	)
   124  	rightPrim := &fakePrimitive{
   125  		// we'll return non-empty results for rows 2 and 4
   126  		results: sqltypes.MakeTestStreamingResults(rightFields,
   127  			"4|d|dd",
   128  			"---",
   129  			"---",
   130  			"5|e|ee",
   131  			"6|f|ff",
   132  			"7|g|gg",
   133  		),
   134  	}
   135  
   136  	jn := &SemiJoin{
   137  		Left:  leftPrim,
   138  		Right: rightPrim,
   139  		Vars: map[string]int{
   140  			"bv": 1,
   141  		},
   142  		Cols: []int{-1, -2, -3},
   143  	}
   144  	r, err := wrapStreamExecute(jn, &noopVCursor{}, map[string]*querypb.BindVariable{}, true)
   145  	require.NoError(t, err)
   146  	leftPrim.ExpectLog(t, []string{
   147  		`StreamExecute  true`,
   148  	})
   149  	rightPrim.ExpectLog(t, []string{
   150  		`StreamExecute bv: type:VARCHAR value:"a" false`,
   151  		`StreamExecute bv: type:VARCHAR value:"b" false`,
   152  		`StreamExecute bv: type:VARCHAR value:"c" false`,
   153  		`StreamExecute bv: type:VARCHAR value:"d" false`,
   154  	})
   155  	expectResult(t, "jn.Execute", r, sqltypes.MakeTestResult(
   156  		sqltypes.MakeTestFields(
   157  			"col1|col2|col3",
   158  			"int64|varchar|varchar",
   159  		),
   160  		"2|b|bb",
   161  		"4|d|dd",
   162  	))
   163  }