vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/projection_test.go (about) 1 /* 2 Copyright 2022 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 "fmt" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/sqltypes" 28 querypb "vitess.io/vitess/go/vt/proto/query" 29 "vitess.io/vitess/go/vt/sqlparser" 30 "vitess.io/vitess/go/vt/vtgate/evalengine" 31 ) 32 33 func TestMultiply(t *testing.T) { 34 expr := &sqlparser.BinaryExpr{ 35 Operator: sqlparser.MultOp, 36 Left: &sqlparser.Offset{V: 0}, 37 Right: &sqlparser.Offset{V: 1}, 38 } 39 evalExpr, err := evalengine.Translate(expr, nil) 40 require.NoError(t, err) 41 fp := &fakePrimitive{ 42 results: []*sqltypes.Result{sqltypes.MakeTestResult( 43 sqltypes.MakeTestFields("a|b", "uint64|uint64"), 44 "3|2", 45 "1|0", 46 "1|2", 47 )}, 48 } 49 proj := &Projection{ 50 Cols: []string{"apa"}, 51 Exprs: []evalengine.Expr{evalExpr}, 52 Input: fp, 53 noTxNeeded: noTxNeeded{}, 54 } 55 qr, err := proj.TryExecute(context.Background(), &noopVCursor{}, map[string]*querypb.BindVariable{}, false) 56 require.NoError(t, err) 57 assert.Equal(t, "[[UINT64(6)] [UINT64(0)] [UINT64(2)]]", fmt.Sprintf("%v", qr.Rows)) 58 59 fp = &fakePrimitive{ 60 results: []*sqltypes.Result{sqltypes.MakeTestResult( 61 sqltypes.MakeTestFields("a|b", "uint64|uint64"), 62 "3|2", 63 "1|0", 64 "1|2", 65 )}, 66 } 67 proj.Input = fp 68 qr, err = wrapStreamExecute(proj, &noopVCursor{}, nil, true) 69 require.NoError(t, err) 70 assert.Equal(t, "[[UINT64(6)] [UINT64(0)] [UINT64(2)]]", fmt.Sprintf("%v", qr.Rows)) 71 } 72 73 func TestEmptyInput(t *testing.T) { 74 expr := &sqlparser.BinaryExpr{ 75 Operator: sqlparser.MultOp, 76 Left: &sqlparser.Offset{V: 0}, 77 Right: &sqlparser.Offset{V: 1}, 78 } 79 evalExpr, err := evalengine.Translate(expr, nil) 80 require.NoError(t, err) 81 fp := &fakePrimitive{ 82 results: []*sqltypes.Result{sqltypes.MakeTestResult(sqltypes.MakeTestFields("a|b", "uint64|uint64"))}, 83 } 84 proj := &Projection{ 85 Cols: []string{"count(*)"}, 86 Exprs: []evalengine.Expr{evalExpr}, 87 Input: fp, 88 noTxNeeded: noTxNeeded{}, 89 } 90 qr, err := proj.TryExecute(context.Background(), &noopVCursor{}, map[string]*querypb.BindVariable{}, false) 91 require.NoError(t, err) 92 assert.Equal(t, "[]", fmt.Sprintf("%v", qr.Rows)) 93 94 //fp = &fakePrimitive{ 95 // results: []*sqltypes.Result{sqltypes.MakeTestResult( 96 // sqltypes.MakeTestFields("a|b", "uint64|uint64"), 97 // "3|2", 98 // "1|0", 99 // "1|2", 100 // )}, 101 //} 102 //proj.Input = fp 103 //qr, err = wrapStreamExecute(proj, newNoopVCursor(context.Background()), nil, true) 104 //require.NoError(t, err) 105 //assert.Equal(t, "[[UINT64(6)] [UINT64(0)] [UINT64(2)]]", fmt.Sprintf("%v", qr.Rows)) 106 } 107 108 func TestHexAndBinaryArgument(t *testing.T) { 109 hexExpr, err := evalengine.Translate(sqlparser.Argument("vtg1"), nil) 110 require.NoError(t, err) 111 proj := &Projection{ 112 Cols: []string{"hex"}, 113 Exprs: []evalengine.Expr{hexExpr}, 114 Input: &SingleRow{}, 115 noTxNeeded: noTxNeeded{}, 116 } 117 qr, err := proj.TryExecute(context.Background(), &noopVCursor{}, map[string]*querypb.BindVariable{ 118 "vtg1": sqltypes.HexNumBindVariable([]byte("0x9")), 119 }, false) 120 require.NoError(t, err) 121 assert.Equal(t, `[[VARBINARY("\t")]]`, fmt.Sprintf("%v", qr.Rows)) 122 }