1// Licensed to the Apache Software Foundation (ASF) under one 2// or more contributor license agreements. See the NOTICE file 3// distributed with this work for additional information 4// regarding copyright ownership. The ASF licenses this file 5// to you under the Apache License, Version 2.0 (the 6// "License"); you may not use this file except in compliance 7// with the License. You may obtain a copy of the License at 8// 9// http://www.apache.org/licenses/LICENSE-2.0 10// 11// Unless required by applicable law or agreed to in writing, software 12// distributed under the License is distributed on an "AS IS" BASIS, 13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14// See the License for the specific language governing permissions and 15// limitations under the License. 16 17package array_test 18 19import ( 20 "reflect" 21 "testing" 22 23 "github.com/apache/arrow/go/v6/arrow" 24 "github.com/apache/arrow/go/v6/arrow/array" 25 "github.com/apache/arrow/go/v6/arrow/memory" 26 "github.com/stretchr/testify/assert" 27) 28 29func TestNewFloat64Data(t *testing.T) { 30 exp := []float64{1.0, 2.0, 4.0, 8.0, 16.0} 31 32 ad := array.NewData( 33 arrow.PrimitiveTypes.Float64, len(exp), 34 []*memory.Buffer{nil, memory.NewBufferBytes(arrow.Float64Traits.CastToBytes(exp))}, 35 nil, 0, 0, 36 ) 37 fa := array.NewFloat64Data(ad) 38 39 assert.Equal(t, len(exp), fa.Len(), "unexpected Len()") 40 assert.Equal(t, exp, fa.Float64Values(), "unexpected Float64Values()") 41} 42 43func TestFloat64SliceData(t *testing.T) { 44 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 45 defer pool.AssertSize(t, 0) 46 47 const ( 48 beg = 2 49 end = 4 50 ) 51 52 var ( 53 vs = []float64{1, 2, 3, 4, 5} 54 sub = vs[beg:end] 55 ) 56 57 b := array.NewFloat64Builder(pool) 58 defer b.Release() 59 60 for _, v := range vs { 61 b.Append(v) 62 } 63 64 arr := b.NewArray().(*array.Float64) 65 defer arr.Release() 66 67 if got, want := arr.Len(), len(vs); got != want { 68 t.Fatalf("got=%d, want=%d", got, want) 69 } 70 71 if got, want := arr.Float64Values(), vs; !reflect.DeepEqual(got, want) { 72 t.Fatalf("got=%v, want=%v", got, want) 73 } 74 75 slice := array.NewSlice(arr, beg, end).(*array.Float64) 76 defer slice.Release() 77 78 if got, want := slice.Len(), len(sub); got != want { 79 t.Fatalf("got=%d, want=%d", got, want) 80 } 81 82 if got, want := slice.Float64Values(), sub; !reflect.DeepEqual(got, want) { 83 t.Fatalf("got=%v, want=%v", got, want) 84 } 85} 86 87func TestFloat64SliceDataWithNull(t *testing.T) { 88 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 89 defer pool.AssertSize(t, 0) 90 91 const ( 92 beg = 2 93 end = 5 94 ) 95 96 var ( 97 valids = []bool{true, true, true, false, true, true} 98 vs = []float64{1, 2, 3, 0, 4, 5} 99 sub = vs[beg:end] 100 ) 101 102 b := array.NewFloat64Builder(pool) 103 defer b.Release() 104 105 b.AppendValues(vs, valids) 106 107 arr := b.NewArray().(*array.Float64) 108 defer arr.Release() 109 110 if got, want := arr.Len(), len(valids); got != want { 111 t.Fatalf("got=%d, want=%d", got, want) 112 } 113 114 if got, want := arr.NullN(), 1; got != want { 115 t.Fatalf("got=%d, want=%d", got, want) 116 } 117 118 if got, want := arr.Float64Values(), vs; !reflect.DeepEqual(got, want) { 119 t.Fatalf("got=%v, want=%v", got, want) 120 } 121 122 slice := array.NewSlice(arr, beg, end).(*array.Float64) 123 defer slice.Release() 124 125 if got, want := slice.NullN(), 1; got != want { 126 t.Errorf("got=%d, want=%d", got, want) 127 } 128 129 if got, want := slice.Len(), len(sub); got != want { 130 t.Fatalf("got=%d, want=%d", got, want) 131 } 132 133 if got, want := slice.Float64Values(), sub; !reflect.DeepEqual(got, want) { 134 t.Fatalf("got=%v, want=%v", got, want) 135 } 136} 137 138func TestNewTime32Data(t *testing.T) { 139 data := []arrow.Time32{ 140 arrow.Time32(1), 141 arrow.Time32(2), 142 arrow.Time32(4), 143 arrow.Time32(8), 144 arrow.Time32(16), 145 } 146 147 dtype := arrow.FixedWidthTypes.Time32s 148 ad := array.NewData(dtype, len(data), 149 []*memory.Buffer{nil, memory.NewBufferBytes(arrow.Time32Traits.CastToBytes(data))}, 150 nil, 0, 0, 151 ) 152 t32a := array.NewTime32Data(ad) 153 154 assert.Equal(t, len(data), t32a.Len(), "unexpected Len()") 155 assert.Equal(t, data, t32a.Time32Values(), "unexpected Float64Values()") 156} 157 158func TestTime32SliceData(t *testing.T) { 159 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 160 defer pool.AssertSize(t, 0) 161 162 const ( 163 beg = 2 164 end = 4 165 ) 166 167 var ( 168 vs = []arrow.Time32{ 169 arrow.Time32(1), 170 arrow.Time32(2), 171 arrow.Time32(4), 172 arrow.Time32(8), 173 arrow.Time32(16), 174 } 175 sub = vs[beg:end] 176 ) 177 178 dtype := arrow.FixedWidthTypes.Time32s 179 b := array.NewTime32Builder(pool, dtype.(*arrow.Time32Type)) 180 defer b.Release() 181 182 for _, v := range vs { 183 b.Append(v) 184 } 185 186 arr := b.NewArray().(*array.Time32) 187 defer arr.Release() 188 189 if got, want := arr.Len(), len(vs); got != want { 190 t.Fatalf("got=%d, want=%d", got, want) 191 } 192 193 if got, want := arr.Time32Values(), vs; !reflect.DeepEqual(got, want) { 194 t.Fatalf("got=%v, want=%v", got, want) 195 } 196 197 slice := array.NewSlice(arr, beg, end).(*array.Time32) 198 defer slice.Release() 199 200 if got, want := slice.Len(), len(sub); got != want { 201 t.Fatalf("got=%d, want=%d", got, want) 202 } 203 204 if got, want := slice.Time32Values(), sub; !reflect.DeepEqual(got, want) { 205 t.Fatalf("got=%v, want=%v", got, want) 206 } 207} 208 209func TestTime32SliceDataWithNull(t *testing.T) { 210 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 211 defer pool.AssertSize(t, 0) 212 213 const ( 214 beg = 2 215 end = 5 216 ) 217 218 var ( 219 valids = []bool{true, true, true, false, true, true} 220 vs = []arrow.Time32{ 221 arrow.Time32(1), 222 arrow.Time32(2), 223 arrow.Time32(3), 224 arrow.Time32(0), 225 arrow.Time32(4), 226 arrow.Time32(5), 227 } 228 sub = vs[beg:end] 229 ) 230 231 dtype := arrow.FixedWidthTypes.Time32s 232 b := array.NewTime32Builder(pool, dtype.(*arrow.Time32Type)) 233 defer b.Release() 234 235 b.AppendValues(vs, valids) 236 237 arr := b.NewArray().(*array.Time32) 238 defer arr.Release() 239 240 if got, want := arr.Len(), len(valids); got != want { 241 t.Fatalf("got=%d, want=%d", got, want) 242 } 243 244 if got, want := arr.NullN(), 1; got != want { 245 t.Fatalf("got=%d, want=%d", got, want) 246 } 247 248 if got, want := arr.Time32Values(), vs; !reflect.DeepEqual(got, want) { 249 t.Fatalf("got=%v, want=%v", got, want) 250 } 251 252 slice := array.NewSlice(arr, beg, end).(*array.Time32) 253 defer slice.Release() 254 255 if got, want := slice.NullN(), 1; got != want { 256 t.Errorf("got=%d, want=%d", got, want) 257 } 258 259 if got, want := slice.Len(), len(sub); got != want { 260 t.Fatalf("got=%d, want=%d", got, want) 261 } 262 263 if got, want := slice.Time32Values(), sub; !reflect.DeepEqual(got, want) { 264 t.Fatalf("got=%v, want=%v", got, want) 265 } 266} 267 268func TestNewTime64Data(t *testing.T) { 269 data := []arrow.Time64{ 270 arrow.Time64(1), 271 arrow.Time64(2), 272 arrow.Time64(4), 273 arrow.Time64(8), 274 arrow.Time64(16), 275 } 276 277 dtype := arrow.FixedWidthTypes.Time64us 278 ad := array.NewData(dtype, len(data), 279 []*memory.Buffer{nil, memory.NewBufferBytes(arrow.Time64Traits.CastToBytes(data))}, 280 nil, 0, 0, 281 ) 282 t64a := array.NewTime64Data(ad) 283 284 assert.Equal(t, len(data), t64a.Len(), "unexpected Len()") 285 assert.Equal(t, data, t64a.Time64Values(), "unexpected Float64Values()") 286} 287 288func TestTime64SliceData(t *testing.T) { 289 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 290 defer pool.AssertSize(t, 0) 291 292 const ( 293 beg = 2 294 end = 4 295 ) 296 297 var ( 298 vs = []arrow.Time64{ 299 arrow.Time64(1), 300 arrow.Time64(2), 301 arrow.Time64(4), 302 arrow.Time64(8), 303 arrow.Time64(16), 304 } 305 sub = vs[beg:end] 306 ) 307 308 dtype := arrow.FixedWidthTypes.Time64us 309 b := array.NewTime64Builder(pool, dtype.(*arrow.Time64Type)) 310 defer b.Release() 311 312 for _, v := range vs { 313 b.Append(v) 314 } 315 316 arr := b.NewArray().(*array.Time64) 317 defer arr.Release() 318 319 if got, want := arr.Len(), len(vs); got != want { 320 t.Fatalf("got=%d, want=%d", got, want) 321 } 322 323 if got, want := arr.Time64Values(), vs; !reflect.DeepEqual(got, want) { 324 t.Fatalf("got=%v, want=%v", got, want) 325 } 326 327 slice := array.NewSlice(arr, beg, end).(*array.Time64) 328 defer slice.Release() 329 330 if got, want := slice.Len(), len(sub); got != want { 331 t.Fatalf("got=%d, want=%d", got, want) 332 } 333 334 if got, want := slice.Time64Values(), sub; !reflect.DeepEqual(got, want) { 335 t.Fatalf("got=%v, want=%v", got, want) 336 } 337} 338 339func TestTime64SliceDataWithNull(t *testing.T) { 340 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 341 defer pool.AssertSize(t, 0) 342 343 const ( 344 beg = 2 345 end = 5 346 ) 347 348 var ( 349 valids = []bool{true, true, true, false, true, true} 350 vs = []arrow.Time64{ 351 arrow.Time64(1), 352 arrow.Time64(2), 353 arrow.Time64(3), 354 arrow.Time64(0), 355 arrow.Time64(4), 356 arrow.Time64(5), 357 } 358 sub = vs[beg:end] 359 ) 360 361 dtype := arrow.FixedWidthTypes.Time64us 362 b := array.NewTime64Builder(pool, dtype.(*arrow.Time64Type)) 363 defer b.Release() 364 365 b.AppendValues(vs, valids) 366 367 arr := b.NewArray().(*array.Time64) 368 defer arr.Release() 369 370 if got, want := arr.Len(), len(valids); got != want { 371 t.Fatalf("got=%d, want=%d", got, want) 372 } 373 374 if got, want := arr.NullN(), 1; got != want { 375 t.Fatalf("got=%d, want=%d", got, want) 376 } 377 378 if got, want := arr.Time64Values(), vs; !reflect.DeepEqual(got, want) { 379 t.Fatalf("got=%v, want=%v", got, want) 380 } 381 382 slice := array.NewSlice(arr, beg, end).(*array.Time64) 383 defer slice.Release() 384 385 if got, want := slice.NullN(), 1; got != want { 386 t.Errorf("got=%d, want=%d", got, want) 387 } 388 389 if got, want := slice.Len(), len(sub); got != want { 390 t.Fatalf("got=%d, want=%d", got, want) 391 } 392 393 if got, want := slice.Time64Values(), sub; !reflect.DeepEqual(got, want) { 394 t.Fatalf("got=%v, want=%v", got, want) 395 } 396} 397 398func TestNewDate32Data(t *testing.T) { 399 exp := []arrow.Date32{1, 2, 4, 8, 16} 400 401 dtype := &arrow.Date32Type{} 402 ad := array.NewData( 403 dtype, len(exp), 404 []*memory.Buffer{nil, memory.NewBufferBytes(arrow.Date32Traits.CastToBytes(exp))}, 405 nil, 0, 0, 406 ) 407 fa := array.NewDate32Data(ad) 408 409 assert.Equal(t, len(exp), fa.Len(), "unexpected Len()") 410 assert.Equal(t, exp, fa.Date32Values(), "unexpected Date32Values()") 411} 412 413func TestDate32SliceData(t *testing.T) { 414 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 415 defer pool.AssertSize(t, 0) 416 417 const ( 418 beg = 2 419 end = 4 420 ) 421 422 var ( 423 vs = []arrow.Date32{1, 2, 3, 4, 5} 424 sub = vs[beg:end] 425 ) 426 427 b := array.NewDate32Builder(pool) 428 defer b.Release() 429 430 for _, v := range vs { 431 b.Append(v) 432 } 433 434 arr := b.NewArray().(*array.Date32) 435 defer arr.Release() 436 437 if got, want := arr.Len(), len(vs); got != want { 438 t.Fatalf("got=%d, want=%d", got, want) 439 } 440 441 if got, want := arr.Date32Values(), vs; !reflect.DeepEqual(got, want) { 442 t.Fatalf("got=%v, want=%v", got, want) 443 } 444 445 slice := array.NewSlice(arr, beg, end).(*array.Date32) 446 defer slice.Release() 447 448 if got, want := slice.Len(), len(sub); got != want { 449 t.Fatalf("got=%d, want=%d", got, want) 450 } 451 452 if got, want := slice.Date32Values(), sub; !reflect.DeepEqual(got, want) { 453 t.Fatalf("got=%v, want=%v", got, want) 454 } 455} 456 457func TestDate32SliceDataWithNull(t *testing.T) { 458 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 459 defer pool.AssertSize(t, 0) 460 461 const ( 462 beg = 2 463 end = 5 464 ) 465 466 var ( 467 valids = []bool{true, true, true, false, true, true} 468 vs = []arrow.Date32{1, 2, 3, 0, 4, 5} 469 sub = vs[beg:end] 470 ) 471 472 b := array.NewDate32Builder(pool) 473 defer b.Release() 474 475 b.AppendValues(vs, valids) 476 477 arr := b.NewArray().(*array.Date32) 478 defer arr.Release() 479 480 if got, want := arr.Len(), len(valids); got != want { 481 t.Fatalf("got=%d, want=%d", got, want) 482 } 483 484 if got, want := arr.NullN(), 1; got != want { 485 t.Fatalf("got=%d, want=%d", got, want) 486 } 487 488 if got, want := arr.Date32Values(), vs; !reflect.DeepEqual(got, want) { 489 t.Fatalf("got=%v, want=%v", got, want) 490 } 491 492 slice := array.NewSlice(arr, beg, end).(*array.Date32) 493 defer slice.Release() 494 495 if got, want := slice.NullN(), 1; got != want { 496 t.Errorf("got=%d, want=%d", got, want) 497 } 498 499 if got, want := slice.Len(), len(sub); got != want { 500 t.Fatalf("got=%d, want=%d", got, want) 501 } 502 503 if got, want := slice.Date32Values(), sub; !reflect.DeepEqual(got, want) { 504 t.Fatalf("got=%v, want=%v", got, want) 505 } 506} 507 508func TestNewDate64Data(t *testing.T) { 509 exp := []arrow.Date64{1, 2, 4, 8, 16} 510 511 dtype := &arrow.Date64Type{} 512 ad := array.NewData( 513 dtype, len(exp), 514 []*memory.Buffer{nil, memory.NewBufferBytes(arrow.Date64Traits.CastToBytes(exp))}, 515 nil, 0, 0, 516 ) 517 fa := array.NewDate64Data(ad) 518 519 assert.Equal(t, len(exp), fa.Len(), "unexpected Len()") 520 assert.Equal(t, exp, fa.Date64Values(), "unexpected Date64Values()") 521} 522 523func TestDate64SliceData(t *testing.T) { 524 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 525 defer pool.AssertSize(t, 0) 526 527 const ( 528 beg = 2 529 end = 4 530 ) 531 532 var ( 533 vs = []arrow.Date64{1, 2, 3, 4, 5} 534 sub = vs[beg:end] 535 ) 536 537 b := array.NewDate64Builder(pool) 538 defer b.Release() 539 540 for _, v := range vs { 541 b.Append(v) 542 } 543 544 arr := b.NewArray().(*array.Date64) 545 defer arr.Release() 546 547 if got, want := arr.Len(), len(vs); got != want { 548 t.Fatalf("got=%d, want=%d", got, want) 549 } 550 551 if got, want := arr.Date64Values(), vs; !reflect.DeepEqual(got, want) { 552 t.Fatalf("got=%v, want=%v", got, want) 553 } 554 555 slice := array.NewSlice(arr, beg, end).(*array.Date64) 556 defer slice.Release() 557 558 if got, want := slice.Len(), len(sub); got != want { 559 t.Fatalf("got=%d, want=%d", got, want) 560 } 561 562 if got, want := slice.Date64Values(), sub; !reflect.DeepEqual(got, want) { 563 t.Fatalf("got=%v, want=%v", got, want) 564 } 565} 566 567func TestDate64SliceDataWithNull(t *testing.T) { 568 pool := memory.NewCheckedAllocator(memory.NewGoAllocator()) 569 defer pool.AssertSize(t, 0) 570 571 const ( 572 beg = 2 573 end = 5 574 ) 575 576 var ( 577 valids = []bool{true, true, true, false, true, true} 578 vs = []arrow.Date64{1, 2, 3, 0, 4, 5} 579 sub = vs[beg:end] 580 ) 581 582 b := array.NewDate64Builder(pool) 583 defer b.Release() 584 585 b.AppendValues(vs, valids) 586 587 arr := b.NewArray().(*array.Date64) 588 defer arr.Release() 589 590 if got, want := arr.Len(), len(valids); got != want { 591 t.Fatalf("got=%d, want=%d", got, want) 592 } 593 594 if got, want := arr.NullN(), 1; got != want { 595 t.Fatalf("got=%d, want=%d", got, want) 596 } 597 598 if got, want := arr.Date64Values(), vs; !reflect.DeepEqual(got, want) { 599 t.Fatalf("got=%v, want=%v", got, want) 600 } 601 602 slice := array.NewSlice(arr, beg, end).(*array.Date64) 603 defer slice.Release() 604 605 if got, want := slice.NullN(), 1; got != want { 606 t.Errorf("got=%d, want=%d", got, want) 607 } 608 609 if got, want := slice.Len(), len(sub); got != want { 610 t.Fatalf("got=%d, want=%d", got, want) 611 } 612 613 if got, want := slice.Date64Values(), sub; !reflect.DeepEqual(got, want) { 614 t.Fatalf("got=%v, want=%v", got, want) 615 } 616} 617