github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/rpad_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 multi 16 17 import ( 18 "log" 19 "testing" 20 21 "github.com/matrixorigin/matrixone/pkg/container/nulls" 22 "github.com/stretchr/testify/require" 23 24 "github.com/matrixorigin/matrixone/pkg/container/vector" 25 "github.com/matrixorigin/matrixone/pkg/testutil" 26 "github.com/smartystreets/goconvey/convey" 27 ) 28 29 var strVecBase = []string{"a", "", ""} 30 var int64VecBase = []int64{-1, 0, 4, 8, 0} 31 var int64VecBase2 = []int64{-1, 16, 0} 32 var uint64VecBase = []uint64{0, 1, 4, 16, 0} 33 var uint64VecBase2 = []uint64{0, 16, 0} 34 var float64VecBase = []float64{-1.1, 0.01, 4.2, 8.4, 0} 35 var float64VecBase2 = []float64{0.01, 2.3, 0} 36 37 // var charVecBase = []string{"-1", "0", "4", "8", ""} 38 var charVecBase2 = []string{"a", "12", ""} 39 var compVec []string 40 var compNsp = []int64{0, 1, 2, 5, 8, 11, 12, 13, 14, 15, 16, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 41 var origVecs = make([]*vector.Vector, 3) 42 var proc = testutil.NewProc() 43 44 func Test_varchar1(t *testing.T) { 45 convey.Convey("Test rpad varchar int64 and int64 succ", t, func() { 46 n1, n2, n3 := len(strVecBase), len(int64VecBase), len(int64VecBase2) 47 strVec := make([]string, n1*n2*n3) 48 inputVec := make([]int64, len(strVec)) 49 inputVec2 := make([]int64, len(strVec)) 50 51 nsp1 := make([]uint64, 0) 52 nsp2 := make([]uint64, 0) 53 nsp3 := make([]uint64, 0) 54 for i := 0; i < len(strVec); i++ { 55 strVec[i] = strVecBase[i/(n2*n3)] 56 inputVec[i] = int64VecBase[(i/n3)%n2] 57 inputVec2[i] = int64VecBase2[i%n3] 58 if (i / (n2 * n3)) == (n1 - 1) { 59 nsp1 = append(nsp1, uint64(i)) 60 } 61 if ((i / n3) % n2) == (n2 - 1) { 62 nsp2 = append(nsp2, uint64(i)) 63 } 64 if i%n3 == (n3 - 1) { 65 nsp3 = append(nsp3, uint64(i)) 66 } 67 } 68 69 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 70 origVecs[1] = testutil.MakeInt64Vector(inputVec, nsp2) 71 origVecs[2] = testutil.MakeInt64Vector(inputVec2, nsp3) 72 73 vec, err := Rpad(origVecs, proc) 74 if err != nil { 75 log.Fatal(err) 76 } 77 data := vector.GetStrVectorValues(vec) 78 compVec = []string{"", "", "", "", "", "", "a-1-", "a161", "", "a-1-1-1-", "a1616161", 79 "", "", "", "", "", "", "", "", "", "", "-1-1", "1616", "", "-1-1-1-1", "16161616", "", "", 80 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 81 for i := 0; i < len(data); i++ { 82 convey.So(data[i], convey.ShouldEqual, compVec[i]) 83 } 84 j := 0 85 for i := 0; i < len(compVec); i++ { 86 if j < len(compNsp) { 87 if compNsp[j] == int64(i) { 88 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 89 j++ 90 } else { 91 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 92 } 93 } else { 94 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 95 } 96 } 97 }) 98 } 99 100 func Test_varchar2(t *testing.T) { 101 convey.Convey("Test rpad varchar int64 and uint64 succ", t, func() { 102 n1, n2, n3 := len(strVecBase), len(int64VecBase), len(uint64VecBase2) 103 strVec := make([]string, n1*n2*n3) 104 inputVec := make([]int64, len(strVec)) 105 inputVec2 := make([]uint64, len(strVec)) 106 107 nsp1 := make([]uint64, 0) 108 nsp2 := make([]uint64, 0) 109 nsp3 := make([]uint64, 0) 110 for i := 0; i < len(strVec); i++ { 111 strVec[i] = strVecBase[i/(n2*n3)] 112 inputVec[i] = int64VecBase[(i/n3)%n2] 113 inputVec2[i] = uint64VecBase2[i%n3] 114 if (i / (n2 * n3)) == (n1 - 1) { 115 nsp1 = append(nsp1, uint64(i)) 116 } 117 if ((i / n3) % n2) == (n2 - 1) { 118 nsp2 = append(nsp2, uint64(i)) 119 } 120 if i%n3 == (n3 - 1) { 121 nsp3 = append(nsp3, uint64(i)) 122 } 123 } 124 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 125 origVecs[1] = testutil.MakeInt64Vector(inputVec, nsp2) 126 origVecs[2] = testutil.MakeUint64Vector(inputVec2, nsp3) 127 vec, err := Rpad(origVecs, proc) 128 if err != nil { 129 log.Fatal(err) 130 } 131 data := vector.GetStrVectorValues(vec) 132 compVec = []string{"", "", "", "", "", "", "a000", "a161", "", "a0000000", "a1616161", "", "", "", "", "", "", "", "", "", 133 "", "0000", "1616", "", "00000000", "16161616", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 134 for i := 0; i < len(data); i++ { 135 convey.So(data[i], convey.ShouldEqual, compVec[i]) 136 } 137 j := 0 138 for i := 0; i < len(compVec); i++ { 139 if j < len(compNsp) { 140 if compNsp[j] == int64(i) { 141 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 142 j++ 143 } else { 144 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 145 } 146 } else { 147 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 148 } 149 } 150 }) 151 } 152 153 func Test_varchar3(t *testing.T) { 154 convey.Convey("Test rpad varchar int64 and float64 succ", t, func() { 155 n1, n2, n3 := len(strVecBase), len(int64VecBase), len(float64VecBase2) 156 strVec := make([]string, n1*n2*n3) 157 inputVec := make([]int64, len(strVec)) 158 inputVec2 := make([]float64, len(strVec)) 159 160 nsp1 := make([]uint64, 0) 161 nsp2 := make([]uint64, 0) 162 nsp3 := make([]uint64, 0) 163 for i := 0; i < len(strVec); i++ { 164 strVec[i] = strVecBase[i/(n2*n3)] 165 inputVec[i] = int64VecBase[(i/n3)%n2] 166 inputVec2[i] = float64VecBase2[i%n3] 167 if (i / (n2 * n3)) == (n1 - 1) { 168 nsp1 = append(nsp1, uint64(i)) 169 } 170 if ((i / n3) % n2) == (n2 - 1) { 171 nsp2 = append(nsp2, uint64(i)) 172 } 173 if i%n3 == (n3 - 1) { 174 nsp3 = append(nsp3, uint64(i)) 175 } 176 } 177 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 178 origVecs[1] = testutil.MakeInt64Vector(inputVec, nsp2) 179 origVecs[2] = testutil.MakeFloat64Vector(inputVec2, nsp3) 180 vec, err := Rpad(origVecs, proc) 181 if err != nil { 182 log.Fatal(err) 183 } 184 data := vector.GetStrVectorValues(vec) 185 compVec = []string{"", "", "", "", "", "", "a0.0", "a2.3", "", "a0.010.0", "a2.32.32", "", "", "", "", "", "", "", "", "", "", 186 "0.01", "2.32", "", "0.010.01", "2.32.32.", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 187 for i := 0; i < len(data); i++ { 188 convey.So(data[i], convey.ShouldEqual, compVec[i]) 189 } 190 j := 0 191 for i := 0; i < len(compVec); i++ { 192 if j < len(compNsp) { 193 if compNsp[j] == int64(i) { 194 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 195 j++ 196 } else { 197 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 198 } 199 } else { 200 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 201 } 202 } 203 }) 204 } 205 206 func Test_varchar4(t *testing.T) { 207 convey.Convey("Test rpad varchar int64 and T_varchar succ", t, func() { 208 n1, n2, n3 := len(strVecBase), len(int64VecBase), len(charVecBase2) 209 strVec := make([]string, n1*n2*n3) 210 inputVec := make([]int64, len(strVec)) 211 inputVec2 := make([]string, len(strVec)) 212 213 nsp1 := make([]uint64, 0) 214 nsp2 := make([]uint64, 0) 215 nsp3 := make([]uint64, 0) 216 for i := 0; i < len(strVec); i++ { 217 strVec[i] = strVecBase[i/(n2*n3)] 218 inputVec[i] = int64VecBase[(i/n3)%n2] 219 inputVec2[i] = charVecBase2[i%n3] 220 if (i / (n2 * n3)) == (n1 - 1) { 221 nsp1 = append(nsp1, uint64(i)) 222 } 223 if ((i / n3) % n2) == (n2 - 1) { 224 nsp2 = append(nsp2, uint64(i)) 225 } 226 if i%n3 == (n3 - 1) { 227 nsp3 = append(nsp3, uint64(i)) 228 } 229 } 230 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 231 origVecs[1] = testutil.MakeInt64Vector(inputVec, nsp2) 232 origVecs[2] = testutil.MakeCharVector(inputVec2, nsp3) 233 vec, err := Rpad(origVecs, proc) 234 if err != nil { 235 log.Fatal(err) 236 } 237 data := vector.GetStrVectorValues(vec) 238 compVec = []string{"", "", "", "", "", "", "aaaa", "a121", "", "aaaaaaaa", "a1212121", "", "", "", "", "", "", "", "", "", "", 239 "aaaa", "1212", "", "aaaaaaaa", "12121212", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 240 for i := 0; i < len(data); i++ { 241 convey.So(data[i], convey.ShouldEqual, compVec[i]) 242 } 243 j := 0 244 for i := 0; i < len(compVec); i++ { 245 if j < len(compNsp) { 246 if compNsp[j] == int64(i) { 247 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 248 j++ 249 } else { 250 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 251 } 252 } else { 253 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 254 } 255 } 256 }) 257 } 258 259 func Test_varchar5(t *testing.T) { 260 convey.Convey("Test rpad varchar uint64 and int64 succ", t, func() { 261 n1, n2, n3 := len(strVecBase), len(uint64VecBase), len(int64VecBase2) 262 strVec := make([]string, n1*n2*n3) 263 inputVec := make([]uint64, len(strVec)) 264 inputVec2 := make([]int64, len(strVec)) 265 266 nsp1 := make([]uint64, 0) 267 nsp2 := make([]uint64, 0) 268 nsp3 := make([]uint64, 0) 269 for i := 0; i < len(strVec); i++ { 270 strVec[i] = strVecBase[i/(n2*n3)] 271 inputVec[i] = uint64VecBase[(i/n3)%n2] 272 inputVec2[i] = int64VecBase2[i%n3] 273 if (i / (n2 * n3)) == (n1 - 1) { 274 nsp1 = append(nsp1, uint64(i)) 275 } 276 if ((i / n3) % n2) == (n2 - 1) { 277 nsp2 = append(nsp2, uint64(i)) 278 } 279 if i%n3 == (n3 - 1) { 280 nsp3 = append(nsp3, uint64(i)) 281 } 282 } 283 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 284 origVecs[1] = testutil.MakeUint64Vector(inputVec, nsp2) 285 origVecs[2] = testutil.MakeInt64Vector(inputVec2, nsp3) 286 vec, err := Rpad(origVecs, proc) 287 if err != nil { 288 log.Fatal(err) 289 } 290 data := vector.GetStrVectorValues(vec) 291 compVec = []string{"", "", "", "a", "a", "", "a-1-", "a161", "", "a-1-1-1-1-1-1-1-", "a161616161616161", "", "", "", "", "", 292 "", "", "-", "1", "", "-1-1", "1616", "", "-1-1-1-1-1-1-1-1", "1616161616161616", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 293 compNsp = []int64{2, 5, 8, 11, 12, 13, 14, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 294 for i := 0; i < len(data); i++ { 295 convey.So(data[i], convey.ShouldEqual, compVec[i]) 296 } 297 j := 0 298 for i := 0; i < len(compVec); i++ { 299 if j < len(compNsp) { 300 if compNsp[j] == int64(i) { 301 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 302 j++ 303 } else { 304 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 305 } 306 } else { 307 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 308 } 309 } 310 }) 311 } 312 313 func Test_varchar6(t *testing.T) { 314 convey.Convey("Test rpad varchar uint64 and uint64 succ", t, func() { 315 n1, n2, n3 := len(strVecBase), len(uint64VecBase), len(uint64VecBase2) 316 strVec := make([]string, n1*n2*n3) 317 inputVec := make([]uint64, len(strVec)) 318 inputVec2 := make([]uint64, len(strVec)) 319 320 nsp1 := make([]uint64, 0) 321 nsp2 := make([]uint64, 0) 322 nsp3 := make([]uint64, 0) 323 for i := 0; i < len(strVec); i++ { 324 strVec[i] = strVecBase[i/(n2*n3)] 325 inputVec[i] = uint64VecBase[(i/n3)%n2] 326 inputVec2[i] = uint64VecBase2[i%n3] 327 if (i / (n2 * n3)) == (n1 - 1) { 328 nsp1 = append(nsp1, uint64(i)) 329 } 330 if ((i / n3) % n2) == (n2 - 1) { 331 nsp2 = append(nsp2, uint64(i)) 332 } 333 if i%n3 == (n3 - 1) { 334 nsp3 = append(nsp3, uint64(i)) 335 } 336 } 337 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 338 origVecs[1] = testutil.MakeUint64Vector(inputVec, nsp2) 339 origVecs[2] = testutil.MakeUint64Vector(inputVec2, nsp3) 340 vec, err := Rpad(origVecs, proc) 341 if err != nil { 342 log.Fatal(err) 343 } 344 data := vector.GetStrVectorValues(vec) 345 compVec = []string{"", "", "", "a", "a", "", "a000", "a161", "", "a000000000000000", "a161616161616161", "", "", "", "", "", "", "", 346 "0", "1", "", "0000", "1616", "", "0000000000000000", "1616161616161616", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 347 compNsp = []int64{2, 5, 8, 11, 12, 13, 14, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 348 for i := 0; i < len(data); i++ { 349 convey.So(data[i], convey.ShouldEqual, compVec[i]) 350 } 351 j := 0 352 for i := 0; i < len(compVec); i++ { 353 if j < len(compNsp) { 354 if compNsp[j] == int64(i) { 355 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 356 j++ 357 } else { 358 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 359 } 360 } else { 361 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 362 } 363 } 364 }) 365 } 366 367 func Test_varchar7(t *testing.T) { 368 convey.Convey("Test rpad varchar uint64 and float64 succ", t, func() { 369 n1, n2, n3 := len(strVecBase), len(uint64VecBase), len(float64VecBase2) 370 strVec := make([]string, n1*n2*n3) 371 inputVec := make([]uint64, len(strVec)) 372 inputVec2 := make([]float64, len(strVec)) 373 374 nsp1 := make([]uint64, 0) 375 nsp2 := make([]uint64, 0) 376 nsp3 := make([]uint64, 0) 377 for i := 0; i < len(strVec); i++ { 378 strVec[i] = strVecBase[i/(n2*n3)] 379 inputVec[i] = uint64VecBase[(i/n3)%n2] 380 inputVec2[i] = float64VecBase2[i%n3] 381 if (i / (n2 * n3)) == (n1 - 1) { 382 nsp1 = append(nsp1, uint64(i)) 383 } 384 if ((i / n3) % n2) == (n2 - 1) { 385 nsp2 = append(nsp2, uint64(i)) 386 } 387 if i%n3 == (n3 - 1) { 388 nsp3 = append(nsp3, uint64(i)) 389 } 390 } 391 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 392 origVecs[1] = testutil.MakeUint64Vector(inputVec, nsp2) 393 origVecs[2] = testutil.MakeFloat64Vector(inputVec2, nsp3) 394 vec, err := Rpad(origVecs, proc) 395 if err != nil { 396 log.Fatal(err) 397 } 398 data := vector.GetStrVectorValues(vec) 399 compVec = []string{"", "", "", "a", "a", "", "a0.0", "a2.3", "", "a0.010.010.010.0", "a2.32.32.32.32.3", "", "", "", "", "", 400 "", "", "0", "2", "", "0.01", "2.32", "", "0.010.010.010.01", "2.32.32.32.32.32", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 401 compNsp = []int64{2, 5, 8, 11, 12, 13, 14, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 402 for i := 0; i < len(data); i++ { 403 convey.So(data[i], convey.ShouldEqual, compVec[i]) 404 } 405 j := 0 406 for i := 0; i < len(compVec); i++ { 407 if j < len(compNsp) { 408 if compNsp[j] == int64(i) { 409 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 410 j++ 411 } else { 412 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 413 } 414 } else { 415 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 416 } 417 } 418 }) 419 } 420 421 func Test_varchar8(t *testing.T) { 422 convey.Convey("Test rpad varchar uint64 and T_varchar succ", t, func() { 423 n1, n2, n3 := len(strVecBase), len(uint64VecBase), len(charVecBase2) 424 strVec := make([]string, n1*n2*n3) 425 inputVec := make([]uint64, len(strVec)) 426 inputVec2 := make([]string, len(strVec)) 427 428 nsp1 := make([]uint64, 0) 429 nsp2 := make([]uint64, 0) 430 nsp3 := make([]uint64, 0) 431 for i := 0; i < len(strVec); i++ { 432 strVec[i] = strVecBase[i/(n2*n3)] 433 inputVec[i] = uint64VecBase[(i/n3)%n2] 434 inputVec2[i] = charVecBase2[i%n3] 435 if (i / (n2 * n3)) == (n1 - 1) { 436 nsp1 = append(nsp1, uint64(i)) 437 } 438 if ((i / n3) % n2) == (n2 - 1) { 439 nsp2 = append(nsp2, uint64(i)) 440 } 441 if i%n3 == (n3 - 1) { 442 nsp3 = append(nsp3, uint64(i)) 443 } 444 } 445 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 446 origVecs[1] = testutil.MakeUint64Vector(inputVec, nsp2) 447 origVecs[2] = testutil.MakeCharVector(inputVec2, nsp3) 448 vec, err := Rpad(origVecs, proc) 449 if err != nil { 450 log.Fatal(err) 451 } 452 data := vector.GetStrVectorValues(vec) 453 compVec = []string{"", "", "", "a", "a", "", "aaaa", "a121", "", "aaaaaaaaaaaaaaaa", "a121212121212121", "", "", "", "", "", "", "", 454 "a", "1", "", "aaaa", "1212", "", "aaaaaaaaaaaaaaaa", "1212121212121212", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 455 compNsp = []int64{2, 5, 8, 11, 12, 13, 14, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 456 for i := 0; i < len(data); i++ { 457 convey.So(data[i], convey.ShouldEqual, compVec[i]) 458 } 459 j := 0 460 for i := 0; i < len(compVec); i++ { 461 if j < len(compNsp) { 462 if compNsp[j] == int64(i) { 463 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 464 j++ 465 } else { 466 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 467 } 468 } else { 469 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 470 } 471 } 472 }) 473 } 474 475 func Test_varchar9(t *testing.T) { 476 convey.Convey("Test rpad varchar float64 and int64 succ", t, func() { 477 n1, n2, n3 := len(strVecBase), len(float64VecBase), len(int64VecBase2) 478 strVec := make([]string, n1*n2*n3) 479 inputVec := make([]float64, len(strVec)) 480 inputVec2 := make([]int64, len(strVec)) 481 482 nsp1 := make([]uint64, 0) 483 nsp2 := make([]uint64, 0) 484 nsp3 := make([]uint64, 0) 485 for i := 0; i < len(strVec); i++ { 486 strVec[i] = strVecBase[i/(n2*n3)] 487 inputVec[i] = float64VecBase[(i/n3)%n2] 488 inputVec2[i] = int64VecBase2[i%n3] 489 if (i / (n2 * n3)) == (n1 - 1) { 490 nsp1 = append(nsp1, uint64(i)) 491 } 492 if ((i / n3) % n2) == (n2 - 1) { 493 nsp2 = append(nsp2, uint64(i)) 494 } 495 if i%n3 == (n3 - 1) { 496 nsp3 = append(nsp3, uint64(i)) 497 } 498 } 499 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 500 origVecs[1] = testutil.MakeFloat64Vector(inputVec, nsp2) 501 origVecs[2] = testutil.MakeInt64Vector(inputVec2, nsp3) 502 vec, err := Rpad(origVecs, proc) 503 if err != nil { 504 log.Fatal(err) 505 } 506 data := vector.GetStrVectorValues(vec) 507 compVec = []string{"", "", "", "", "", "", "a-1-", "a161", "", "a-1-1-1-", "a1616161", "", "", "", "", "", "", "", "", "", "", "-1-1", 508 "1616", "", "-1-1-1-1", "16161616", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 509 compNsp = []int64{0, 1, 2, 5, 8, 11, 12, 13, 14, 15, 16, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 510 for i := 0; i < len(data); i++ { 511 convey.So(data[i], convey.ShouldEqual, compVec[i]) 512 } 513 j := 0 514 for i := 0; i < len(compVec); i++ { 515 if j < len(compNsp) { 516 if compNsp[j] == int64(i) { 517 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 518 j++ 519 } else { 520 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 521 } 522 } else { 523 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 524 } 525 } 526 }) 527 } 528 529 func Test_varchar10(t *testing.T) { 530 convey.Convey("Test rpad varchar float64 and uint64 succ", t, func() { 531 n1, n2, n3 := len(strVecBase), len(float64VecBase), len(uint64VecBase2) 532 strVec := make([]string, n1*n2*n3) 533 inputVec := make([]float64, len(strVec)) 534 inputVec2 := make([]uint64, len(strVec)) 535 536 nsp1 := make([]uint64, 0) 537 nsp2 := make([]uint64, 0) 538 nsp3 := make([]uint64, 0) 539 for i := 0; i < len(strVec); i++ { 540 strVec[i] = strVecBase[i/(n2*n3)] 541 inputVec[i] = float64VecBase[(i/n3)%n2] 542 inputVec2[i] = uint64VecBase2[i%n3] 543 if (i / (n2 * n3)) == (n1 - 1) { 544 nsp1 = append(nsp1, uint64(i)) 545 } 546 if ((i / n3) % n2) == (n2 - 1) { 547 nsp2 = append(nsp2, uint64(i)) 548 } 549 if i%n3 == (n3 - 1) { 550 nsp3 = append(nsp3, uint64(i)) 551 } 552 } 553 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 554 origVecs[1] = testutil.MakeFloat64Vector(inputVec, nsp2) 555 origVecs[2] = testutil.MakeUint64Vector(inputVec2, nsp3) 556 vec, err := Rpad(origVecs, proc) 557 if err != nil { 558 log.Fatal(err) 559 } 560 data := vector.GetStrVectorValues(vec) 561 compVec = []string{"", "", "", "", "", "", "a000", "a161", "", "a0000000", "a1616161", "", "", "", "", "", "", "", "", "", "", "0000", "1616", "", 562 "00000000", "16161616", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 563 compNsp = []int64{0, 1, 2, 5, 8, 11, 12, 13, 14, 15, 16, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 564 for i := 0; i < len(data); i++ { 565 convey.So(data[i], convey.ShouldEqual, compVec[i]) 566 } 567 j := 0 568 for i := 0; i < len(compVec); i++ { 569 if j < len(compNsp) { 570 if compNsp[j] == int64(i) { 571 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 572 j++ 573 } else { 574 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 575 } 576 } else { 577 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 578 } 579 } 580 }) 581 } 582 583 func Test_varchar11(t *testing.T) { 584 convey.Convey("Test rpad varchar float64 and float64 succ", t, func() { 585 n1, n2, n3 := len(strVecBase), len(float64VecBase), len(float64VecBase2) 586 strVec := make([]string, n1*n2*n3) 587 inputVec := make([]float64, len(strVec)) 588 inputVec2 := make([]float64, len(strVec)) 589 590 nsp1 := make([]uint64, 0) 591 nsp2 := make([]uint64, 0) 592 nsp3 := make([]uint64, 0) 593 for i := 0; i < len(strVec); i++ { 594 strVec[i] = strVecBase[i/(n2*n3)] 595 inputVec[i] = float64VecBase[(i/n3)%n2] 596 inputVec2[i] = float64VecBase2[i%n3] 597 if (i / (n2 * n3)) == (n1 - 1) { 598 nsp1 = append(nsp1, uint64(i)) 599 } 600 if ((i / n3) % n2) == (n2 - 1) { 601 nsp2 = append(nsp2, uint64(i)) 602 } 603 if i%n3 == (n3 - 1) { 604 nsp3 = append(nsp3, uint64(i)) 605 } 606 } 607 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 608 origVecs[1] = testutil.MakeFloat64Vector(inputVec, nsp2) 609 origVecs[2] = testutil.MakeFloat64Vector(inputVec2, nsp3) 610 vec, err := Rpad(origVecs, proc) 611 if err != nil { 612 log.Fatal(err) 613 } 614 data := vector.GetStrVectorValues(vec) 615 compVec = []string{"", "", "", "", "", "", "a0.0", "a2.3", "", "a0.010.0", "a2.32.32", "", "", "", "", "", "", "", "", "", "", 616 "0.01", "2.32", "", "0.010.01", "2.32.32.", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 617 compNsp = []int64{0, 1, 2, 5, 8, 11, 12, 13, 14, 15, 16, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 618 for i := 0; i < len(data); i++ { 619 convey.So(data[i], convey.ShouldEqual, compVec[i]) 620 } 621 j := 0 622 for i := 0; i < len(compVec); i++ { 623 if j < len(compNsp) { 624 if compNsp[j] == int64(i) { 625 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 626 j++ 627 } else { 628 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 629 } 630 } else { 631 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 632 } 633 } 634 }) 635 } 636 637 func Test_varchar12(t *testing.T) { 638 convey.Convey("Test rpad varchar float64 and T_varchar succ", t, func() { 639 n1, n2, n3 := len(strVecBase), len(float64VecBase), len(charVecBase2) 640 strVec := make([]string, n1*n2*n3) 641 inputVec := make([]float64, len(strVec)) 642 inputVec2 := make([]string, len(strVec)) 643 644 nsp1 := make([]uint64, 0) 645 nsp2 := make([]uint64, 0) 646 nsp3 := make([]uint64, 0) 647 for i := 0; i < len(strVec); i++ { 648 strVec[i] = strVecBase[i/(n2*n3)] 649 inputVec[i] = float64VecBase[(i/n3)%n2] 650 inputVec2[i] = charVecBase2[i%n3] 651 if (i / (n2 * n3)) == (n1 - 1) { 652 nsp1 = append(nsp1, uint64(i)) 653 } 654 if ((i / n3) % n2) == (n2 - 1) { 655 nsp2 = append(nsp2, uint64(i)) 656 } 657 if i%n3 == (n3 - 1) { 658 nsp3 = append(nsp3, uint64(i)) 659 } 660 } 661 origVecs[0] = testutil.MakeCharVector(strVec, nsp1) 662 origVecs[1] = testutil.MakeFloat64Vector(inputVec, nsp2) 663 origVecs[2] = testutil.MakeCharVector(inputVec2, nsp3) 664 vec, err := Rpad(origVecs, proc) 665 if err != nil { 666 log.Fatal(err) 667 } 668 data := vector.GetStrVectorValues(vec) 669 compVec = []string{"", "", "", "", "", "", "aaaa", "a121", "", "aaaaaaaa", "a1212121", "", "", "", "", "", "", "", "", "", "", 670 "aaaa", "1212", "", "aaaaaaaa", "12121212", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""} 671 compNsp = []int64{0, 1, 2, 5, 8, 11, 12, 13, 14, 15, 16, 17, 20, 23, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44} 672 for i := 0; i < len(data); i++ { 673 convey.So(data[i], convey.ShouldEqual, compVec[i]) 674 } 675 j := 0 676 for i := 0; i < len(compVec); i++ { 677 if j < len(compNsp) { 678 if compNsp[j] == int64(i) { 679 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeTrue) 680 j++ 681 } else { 682 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 683 } 684 } else { 685 convey.So(vec.Nsp.Np.Contains(uint64(i)), convey.ShouldBeFalse) 686 } 687 } 688 }) 689 } 690 691 // getBytes converts a string slice to a *types.Bytes 692 func getBytes(s ...string) []string { 693 return s 694 } 695 696 func TestRpadInt(t *testing.T) { 697 // test: no nulls and all args are attribute names 698 isConst := []bool{false, false, false} 699 sizes := []int16{3, 5, 6, 0, 1, 6, 9} 700 padstrs := getBytes("", "", "", "111", "222", "333", "444") 701 strs := getBytes("hello你好", "hello", "hello", "hello", "hello", "hello", "hello你好") 702 expectedStrs := getBytes("hel", "hello", "", "", "h", "hello3", "hello你好44") 703 oriNsps := make([]*nulls.Nulls, 3) 704 for i := 0; i < 3; i++ { 705 oriNsps[i] = new(nulls.Nulls) 706 } 707 oriNsps = append(oriNsps, new(nulls.Nulls)) 708 expectedNsp := new(nulls.Nulls) 709 710 actualStrs, actualNsp, _ := rpad(proc.Ctx, len(sizes), strs, sizes, padstrs, isConst, oriNsps) 711 require.Equal(t, expectedStrs, actualStrs) 712 require.Equal(t, expectedNsp, actualNsp) 713 714 // test: no nulls and the 2nd and 3rd args are constant 715 isConst = []bool{false, true, true} 716 sizes = []int16{3} 717 padstrs = getBytes("111") 718 expectedStrs = getBytes("hel", "hel", "hel", "hel", "hel", "hel", "hel") 719 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(strs), strs, sizes, padstrs, isConst, oriNsps) 720 require.Equal(t, expectedStrs, actualStrs) 721 require.Equal(t, expectedNsp, actualNsp) 722 723 // test: nulls 724 /** 725 Here is a test table like : 726 +------+-----+-------+ 727 |a |b |c | 728 +--------------------+ 729 |NULL |2 |a | 730 |hello |NULL |a | 731 |hello |2 |NULL | 732 |hello |-2 |a | 733 +------+-----+-------+ 734 all results of rpad(a,b,c) should be NULLs 735 */ 736 isConst = []bool{false, false, false} 737 strs = getBytes("", "hello", "hello", "hello") 738 nulls.Add(oriNsps[0], uint64(0)) 739 nulls.Add(oriNsps[1], uint64(1)) 740 nulls.Add(oriNsps[2], uint64(2)) 741 sizes = []int16{2, 2, 2, -2} 742 padstrs = getBytes("a", "a", "", "a") 743 744 expectedStrs = getBytes("", "", "", "") 745 expectedNsp = new(nulls.Nulls) 746 for i := 0; i < len(strs); i++ { 747 nulls.Add(expectedNsp, uint64(i)) // all strings are NULLs 748 749 } 750 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(sizes), strs, sizes, padstrs, isConst, oriNsps) 751 require.Equal(t, expectedStrs, actualStrs) 752 require.Equal(t, expectedNsp, actualNsp) 753 } 754 755 func TestRpadFloat(t *testing.T) { 756 // test float64 757 isConst := []bool{false, false, false} 758 sizes := []float64{0.0, 0.1, -0.1, 8, 8.4, 8.6} 759 padstrs := getBytes("你好", "你好", "你好", "你好", "你好", "你好") 760 strs := getBytes("hello", "hello", "hello", "hello", "hello", "hello") 761 oriNsps := make([]*nulls.Nulls, 3) 762 for i := 0; i < 3; i++ { 763 oriNsps[i] = new(nulls.Nulls) 764 } 765 oriNsps = append(oriNsps, new(nulls.Nulls)) 766 767 expectedStrs := getBytes("", "", "", "hello你好你", "hello你好你", "hello你好你好") 768 expectedNsp := new(nulls.Nulls) 769 actualStrs, actualNsp, _ := rpad(proc.Ctx, len(sizes), strs, sizes, padstrs, isConst, oriNsps) 770 require.Equal(t, expectedStrs, actualStrs) 771 require.Equal(t, expectedNsp, actualNsp) 772 773 // test float32 774 sizes2 := []float32{0.0, 0.1, -0.1, 8, 8.4, 8.6} 775 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(sizes2), strs, sizes2, padstrs, isConst, oriNsps) 776 require.Equal(t, expectedStrs, actualStrs) 777 require.Equal(t, expectedNsp, actualNsp) 778 } 779 780 func TestRpadUint(t *testing.T) { 781 // test: no nulls and all args are attribute names 782 isConst := []bool{false, false, false} 783 sizes := []uint32{3, 5, 6, 0, 1, 6, 9} 784 padstrs := getBytes("", "", "", "111", "222", "333", "444") 785 strs := getBytes("hello你好", "hello", "hello", "hello", "hello", "hello", "hello你好") 786 expectedStrs := getBytes("hel", "hello", "", "", "h", "hello3", "hello你好44") 787 oriNsps := make([]*nulls.Nulls, 3) 788 for i := 0; i < 3; i++ { 789 oriNsps[i] = new(nulls.Nulls) 790 } 791 oriNsps = append(oriNsps, new(nulls.Nulls)) 792 expectedNsp := new(nulls.Nulls) 793 794 actualStrs, actualNsp, _ := rpad(proc.Ctx, len(sizes), strs, sizes, padstrs, isConst, oriNsps) 795 require.Equal(t, expectedStrs, actualStrs) 796 require.Equal(t, expectedNsp, actualNsp) 797 798 // test: no nulls and the 2nd and 3rd args are constant 799 isConst = []bool{false, true, true} 800 sizes = []uint32{3} 801 padstrs = getBytes("111") 802 expectedStrs = getBytes("hel", "hel", "hel", "hel", "hel", "hel", "hel") 803 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(strs), strs, sizes, padstrs, isConst, oriNsps) 804 require.Equal(t, expectedStrs, actualStrs) 805 require.Equal(t, expectedNsp, actualNsp) 806 807 // test: nulls 808 /** 809 Here is a test table like : 810 +------+-----+-------+ 811 |a |b |c | 812 +--------------------+ 813 |NULL |2 |a | 814 |hello |NULL |a | 815 |hello |2 |NULL | 816 +------+-----+-------+ 817 all results of rpad(a,b,c) should be NULLs 818 */ 819 isConst = []bool{false, false, false} 820 strs = getBytes("", "hello", "hello") 821 nulls.Add(oriNsps[0], uint64(0)) 822 nulls.Add(oriNsps[1], uint64(1)) 823 nulls.Add(oriNsps[2], uint64(2)) 824 sizes = []uint32{2, 2, 2} 825 padstrs = getBytes("a", "a", "") 826 827 expectedStrs = getBytes("", "", "") 828 expectedNsp = new(nulls.Nulls) 829 for i := 0; i < len(strs); i++ { 830 nulls.Add(expectedNsp, uint64(i)) // all strings are NULLs 831 832 } 833 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(sizes), strs, sizes, padstrs, isConst, oriNsps) 834 require.Equal(t, expectedStrs, actualStrs) 835 require.Equal(t, expectedNsp, actualNsp) 836 } 837 838 func TestTypes(t *testing.T) { 839 isConst := []bool{false, true, false} 840 841 // test sizes with a non-numerical type 842 sizes := getBytes("aaasdasdsada") 843 padstrs := getBytes("a", "a") 844 strs := getBytes("", "test") 845 oriNsps := []*nulls.Nulls{new(nulls.Nulls), new(nulls.Nulls), new(nulls.Nulls)} 846 // nulls.Add(oriNsps[0], 0) // the first str is NULL 847 848 expectedNsp := new(nulls.Nulls) 849 // nulls.Add(expectedNsp, 0) 850 expectedStrs := getBytes("", "") 851 852 actualStrs, actualNsp, _ := rpad(proc.Ctx, len(strs), strs, sizes, padstrs, isConst, oriNsps) 853 require.Equal(t, expectedStrs, actualStrs) 854 require.Equal(t, expectedNsp, actualNsp) 855 856 // test padstrs with a numerical type 857 isConst = []bool{false, false, false} 858 sizes2 := []int64{-1, 2, 10} 859 padstrs2 := []int32{8, 9, 10} 860 strs2 := getBytes("test", "test", "test") 861 oriNsps2 := []*nulls.Nulls{new(nulls.Nulls), new(nulls.Nulls), new(nulls.Nulls)} 862 863 expectedNsp2 := new(nulls.Nulls) 864 nulls.Add(expectedNsp2, 0) 865 expectedStrs2 := getBytes("", "te", "test101010") 866 actualStrs, actualNsp, _ = rpad(proc.Ctx, len(sizes2), strs2, sizes2, padstrs2, isConst, oriNsps2) 867 require.Equal(t, expectedStrs2, actualStrs) 868 require.Equal(t, expectedNsp2, actualNsp) 869 }