github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/length_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package unary 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/testutil" 23 "github.com/matrixorigin/matrixone/pkg/vm/process" 24 "github.com/smartystreets/goconvey/convey" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestLength(t *testing.T) { 29 makeTempVector := func(src string, t types.T, srcIsScalar bool) []*vector.Vector { 30 vectors := make([]*vector.Vector, 1) 31 if srcIsScalar { 32 vectors[0] = vector.NewConstString(t.ToType(), 1, src, testutil.TestUtilMp) 33 } else { 34 vectors[0] = vector.NewWithStrings(t.ToType(), []string{src}, nil, testutil.TestUtilMp) 35 } 36 return vectors 37 } 38 39 procs := testutil.NewProcess() 40 41 cases := []struct { 42 name string 43 vecs []*vector.Vector 44 proc *process.Process 45 wantBytes []int64 46 wantScalar bool 47 }{ 48 { 49 name: "Test01", 50 vecs: makeTempVector("abcdefghijklm", types.T_varchar, true), 51 proc: procs, 52 wantBytes: []int64{13}, 53 wantScalar: true, 54 }, 55 { 56 name: "Test02", 57 vecs: makeTempVector("abcdefghijklm", types.T_char, true), 58 proc: procs, 59 wantBytes: []int64{13}, 60 wantScalar: true, 61 }, 62 { 63 name: "Test03", 64 vecs: makeTempVector("abcdefghijklm", types.T_varchar, false), 65 proc: procs, 66 wantBytes: []int64{13}, 67 wantScalar: false, 68 }, 69 { 70 name: "Test04", 71 vecs: makeTempVector("abcdefghijklm", types.T_char, false), 72 proc: procs, 73 wantBytes: []int64{13}, 74 wantScalar: false, 75 }, 76 } 77 78 for _, c := range cases { 79 t.Run(c.name, func(t *testing.T) { 80 lengthRes, err := Length(c.vecs, c.proc) 81 if err != nil { 82 t.Fatal(err) 83 } 84 require.Equal(t, c.wantBytes, lengthRes.Col) 85 require.Equal(t, c.wantScalar, lengthRes.IsScalar()) 86 87 }) 88 } 89 } 90 91 func TestBlobLength(t *testing.T) { 92 makeBlobVector := func(src []byte, srcIsScalar bool, procs *process.Process) *vector.Vector { 93 inputType := types.New(types.T_blob, 0, 0, 0) 94 var inputVector *vector.Vector 95 if srcIsScalar { 96 inputVector = vector.NewConst(inputType, 1) 97 } else { 98 inputVector = vector.New(inputType) 99 } 100 err := inputVector.Append(src, false, procs.Mp()) 101 if err != nil { 102 t.Fatal(err) 103 } 104 return inputVector 105 } 106 107 procs := testutil.NewProcess() 108 cases := []struct { 109 name string 110 ctx []byte 111 want []int64 112 isScalar bool 113 }{ 114 { 115 name: "Normal Case", 116 ctx: []byte("12345678"), 117 want: []int64{8}, 118 isScalar: false, 119 }, 120 { 121 name: "Empty Case", 122 ctx: []byte(""), 123 want: []int64{0}, 124 isScalar: false, 125 }, 126 { 127 name: "Scalar Case", 128 ctx: []byte("12345678"), 129 want: []int64{8}, 130 isScalar: true, 131 }, 132 } 133 for _, c := range cases { 134 convey.Convey(c.name, t, func() { 135 res, err := Length([]*vector.Vector{makeBlobVector(c.ctx, c.isScalar, procs)}, procs) 136 convey.So(err, convey.ShouldBeNil) 137 convey.So(res.Col, convey.ShouldResemble, c.want) 138 convey.So(res.IsScalar(), convey.ShouldEqual, c.isScalar) 139 }) 140 } 141 142 }