github.com/apache/arrow/go/v16@v16.1.0/arrow/array/numericbuilder.gen.go (about) 1 // Code generated by array/numericbuilder.gen.go.tmpl. DO NOT EDIT. 2 3 // Licensed to the Apache Software Foundation (ASF) under one 4 // or more contributor license agreements. See the NOTICE file 5 // distributed with this work for additional information 6 // regarding copyright ownership. The ASF licenses this file 7 // to you under the Apache License, Version 2.0 (the 8 // "License"); you may not use this file except in compliance 9 // with the License. You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 19 package array 20 21 import ( 22 "bytes" 23 "fmt" 24 "reflect" 25 "strconv" 26 "strings" 27 "sync/atomic" 28 "time" 29 30 "github.com/apache/arrow/go/v16/arrow" 31 "github.com/apache/arrow/go/v16/arrow/bitutil" 32 "github.com/apache/arrow/go/v16/arrow/internal/debug" 33 "github.com/apache/arrow/go/v16/arrow/memory" 34 "github.com/apache/arrow/go/v16/internal/json" 35 ) 36 37 type Int64Builder struct { 38 builder 39 40 data *memory.Buffer 41 rawData []int64 42 } 43 44 func NewInt64Builder(mem memory.Allocator) *Int64Builder { 45 return &Int64Builder{builder: builder{refCount: 1, mem: mem}} 46 } 47 48 func (b *Int64Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Int64 } 49 50 // Release decreases the reference count by 1. 51 // When the reference count goes to zero, the memory is freed. 52 func (b *Int64Builder) Release() { 53 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 54 55 if atomic.AddInt64(&b.refCount, -1) == 0 { 56 if b.nullBitmap != nil { 57 b.nullBitmap.Release() 58 b.nullBitmap = nil 59 } 60 if b.data != nil { 61 b.data.Release() 62 b.data = nil 63 b.rawData = nil 64 } 65 } 66 } 67 68 func (b *Int64Builder) Append(v int64) { 69 b.Reserve(1) 70 b.UnsafeAppend(v) 71 } 72 73 func (b *Int64Builder) AppendNull() { 74 b.Reserve(1) 75 b.UnsafeAppendBoolToBitmap(false) 76 } 77 78 func (b *Int64Builder) AppendNulls(n int) { 79 for i := 0; i < n; i++ { 80 b.AppendNull() 81 } 82 } 83 84 func (b *Int64Builder) AppendEmptyValue() { 85 b.Append(0) 86 } 87 88 func (b *Int64Builder) AppendEmptyValues(n int) { 89 for i := 0; i < n; i++ { 90 b.AppendEmptyValue() 91 } 92 } 93 94 func (b *Int64Builder) UnsafeAppend(v int64) { 95 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 96 b.rawData[b.length] = v 97 b.length++ 98 } 99 100 func (b *Int64Builder) UnsafeAppendBoolToBitmap(isValid bool) { 101 if isValid { 102 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 103 } else { 104 b.nulls++ 105 } 106 b.length++ 107 } 108 109 // AppendValues will append the values in the v slice. The valid slice determines which values 110 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 111 // all values in v are appended and considered valid. 112 func (b *Int64Builder) AppendValues(v []int64, valid []bool) { 113 if len(v) != len(valid) && len(valid) != 0 { 114 panic("len(v) != len(valid) && len(valid) != 0") 115 } 116 117 if len(v) == 0 { 118 return 119 } 120 121 b.Reserve(len(v)) 122 arrow.Int64Traits.Copy(b.rawData[b.length:], v) 123 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 124 } 125 126 func (b *Int64Builder) init(capacity int) { 127 b.builder.init(capacity) 128 129 b.data = memory.NewResizableBuffer(b.mem) 130 bytesN := arrow.Int64Traits.BytesRequired(capacity) 131 b.data.Resize(bytesN) 132 b.rawData = arrow.Int64Traits.CastFromBytes(b.data.Bytes()) 133 } 134 135 // Reserve ensures there is enough space for appending n elements 136 // by checking the capacity and calling Resize if necessary. 137 func (b *Int64Builder) Reserve(n int) { 138 b.builder.reserve(n, b.Resize) 139 } 140 141 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 142 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 143 func (b *Int64Builder) Resize(n int) { 144 nBuilder := n 145 if n < minBuilderCapacity { 146 n = minBuilderCapacity 147 } 148 149 if b.capacity == 0 { 150 b.init(n) 151 } else { 152 b.builder.resize(nBuilder, b.init) 153 b.data.Resize(arrow.Int64Traits.BytesRequired(n)) 154 b.rawData = arrow.Int64Traits.CastFromBytes(b.data.Bytes()) 155 } 156 } 157 158 func (b *Int64Builder) Value(i int) int64 { 159 return b.rawData[i] 160 } 161 162 // NewArray creates a Int64 array from the memory buffers used by the builder and resets the Int64Builder 163 // so it can be used to build a new array. 164 func (b *Int64Builder) NewArray() arrow.Array { 165 return b.NewInt64Array() 166 } 167 168 // NewInt64Array creates a Int64 array from the memory buffers used by the builder and resets the Int64Builder 169 // so it can be used to build a new array. 170 func (b *Int64Builder) NewInt64Array() (a *Int64) { 171 data := b.newData() 172 a = NewInt64Data(data) 173 data.Release() 174 return 175 } 176 177 func (b *Int64Builder) newData() (data *Data) { 178 bytesRequired := arrow.Int64Traits.BytesRequired(b.length) 179 if bytesRequired > 0 && bytesRequired < b.data.Len() { 180 // trim buffers 181 b.data.Resize(bytesRequired) 182 } 183 data = NewData(arrow.PrimitiveTypes.Int64, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 184 b.reset() 185 186 if b.data != nil { 187 b.data.Release() 188 b.data = nil 189 b.rawData = nil 190 } 191 192 return 193 } 194 195 func (b *Int64Builder) AppendValueFromString(s string) error { 196 if s == NullValueStr { 197 b.AppendNull() 198 return nil 199 } 200 v, err := strconv.ParseInt(s, 10, 8*8) 201 if err != nil { 202 b.AppendNull() 203 return err 204 } 205 b.Append(int64(v)) 206 return nil 207 } 208 209 func (b *Int64Builder) UnmarshalOne(dec *json.Decoder) error { 210 t, err := dec.Token() 211 if err != nil { 212 return err 213 } 214 215 switch v := t.(type) { 216 case nil: 217 b.AppendNull() 218 219 case string: 220 f, err := strconv.ParseInt(v, 10, 8*8) 221 if err != nil { 222 return &json.UnmarshalTypeError{ 223 Value: v, 224 Type: reflect.TypeOf(int64(0)), 225 Offset: dec.InputOffset(), 226 } 227 } 228 b.Append(int64(f)) 229 case float64: 230 b.Append(int64(v)) 231 case json.Number: 232 f, err := strconv.ParseInt(v.String(), 10, 8*8) 233 if err != nil { 234 return &json.UnmarshalTypeError{ 235 Value: v.String(), 236 Type: reflect.TypeOf(int64(0)), 237 Offset: dec.InputOffset(), 238 } 239 } 240 b.Append(int64(f)) 241 242 default: 243 return &json.UnmarshalTypeError{ 244 Value: fmt.Sprint(t), 245 Type: reflect.TypeOf(int64(0)), 246 Offset: dec.InputOffset(), 247 } 248 } 249 250 return nil 251 } 252 253 func (b *Int64Builder) Unmarshal(dec *json.Decoder) error { 254 for dec.More() { 255 if err := b.UnmarshalOne(dec); err != nil { 256 return err 257 } 258 } 259 return nil 260 } 261 262 func (b *Int64Builder) UnmarshalJSON(data []byte) error { 263 dec := json.NewDecoder(bytes.NewReader(data)) 264 t, err := dec.Token() 265 if err != nil { 266 return err 267 } 268 269 if delim, ok := t.(json.Delim); !ok || delim != '[' { 270 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 271 } 272 273 return b.Unmarshal(dec) 274 } 275 276 type Uint64Builder struct { 277 builder 278 279 data *memory.Buffer 280 rawData []uint64 281 } 282 283 func NewUint64Builder(mem memory.Allocator) *Uint64Builder { 284 return &Uint64Builder{builder: builder{refCount: 1, mem: mem}} 285 } 286 287 func (b *Uint64Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Uint64 } 288 289 // Release decreases the reference count by 1. 290 // When the reference count goes to zero, the memory is freed. 291 func (b *Uint64Builder) Release() { 292 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 293 294 if atomic.AddInt64(&b.refCount, -1) == 0 { 295 if b.nullBitmap != nil { 296 b.nullBitmap.Release() 297 b.nullBitmap = nil 298 } 299 if b.data != nil { 300 b.data.Release() 301 b.data = nil 302 b.rawData = nil 303 } 304 } 305 } 306 307 func (b *Uint64Builder) Append(v uint64) { 308 b.Reserve(1) 309 b.UnsafeAppend(v) 310 } 311 312 func (b *Uint64Builder) AppendNull() { 313 b.Reserve(1) 314 b.UnsafeAppendBoolToBitmap(false) 315 } 316 317 func (b *Uint64Builder) AppendNulls(n int) { 318 for i := 0; i < n; i++ { 319 b.AppendNull() 320 } 321 } 322 323 func (b *Uint64Builder) AppendEmptyValue() { 324 b.Append(0) 325 } 326 327 func (b *Uint64Builder) AppendEmptyValues(n int) { 328 for i := 0; i < n; i++ { 329 b.AppendEmptyValue() 330 } 331 } 332 333 func (b *Uint64Builder) UnsafeAppend(v uint64) { 334 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 335 b.rawData[b.length] = v 336 b.length++ 337 } 338 339 func (b *Uint64Builder) UnsafeAppendBoolToBitmap(isValid bool) { 340 if isValid { 341 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 342 } else { 343 b.nulls++ 344 } 345 b.length++ 346 } 347 348 // AppendValues will append the values in the v slice. The valid slice determines which values 349 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 350 // all values in v are appended and considered valid. 351 func (b *Uint64Builder) AppendValues(v []uint64, valid []bool) { 352 if len(v) != len(valid) && len(valid) != 0 { 353 panic("len(v) != len(valid) && len(valid) != 0") 354 } 355 356 if len(v) == 0 { 357 return 358 } 359 360 b.Reserve(len(v)) 361 arrow.Uint64Traits.Copy(b.rawData[b.length:], v) 362 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 363 } 364 365 func (b *Uint64Builder) init(capacity int) { 366 b.builder.init(capacity) 367 368 b.data = memory.NewResizableBuffer(b.mem) 369 bytesN := arrow.Uint64Traits.BytesRequired(capacity) 370 b.data.Resize(bytesN) 371 b.rawData = arrow.Uint64Traits.CastFromBytes(b.data.Bytes()) 372 } 373 374 // Reserve ensures there is enough space for appending n elements 375 // by checking the capacity and calling Resize if necessary. 376 func (b *Uint64Builder) Reserve(n int) { 377 b.builder.reserve(n, b.Resize) 378 } 379 380 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 381 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 382 func (b *Uint64Builder) Resize(n int) { 383 nBuilder := n 384 if n < minBuilderCapacity { 385 n = minBuilderCapacity 386 } 387 388 if b.capacity == 0 { 389 b.init(n) 390 } else { 391 b.builder.resize(nBuilder, b.init) 392 b.data.Resize(arrow.Uint64Traits.BytesRequired(n)) 393 b.rawData = arrow.Uint64Traits.CastFromBytes(b.data.Bytes()) 394 } 395 } 396 397 func (b *Uint64Builder) Value(i int) uint64 { 398 return b.rawData[i] 399 } 400 401 // NewArray creates a Uint64 array from the memory buffers used by the builder and resets the Uint64Builder 402 // so it can be used to build a new array. 403 func (b *Uint64Builder) NewArray() arrow.Array { 404 return b.NewUint64Array() 405 } 406 407 // NewUint64Array creates a Uint64 array from the memory buffers used by the builder and resets the Uint64Builder 408 // so it can be used to build a new array. 409 func (b *Uint64Builder) NewUint64Array() (a *Uint64) { 410 data := b.newData() 411 a = NewUint64Data(data) 412 data.Release() 413 return 414 } 415 416 func (b *Uint64Builder) newData() (data *Data) { 417 bytesRequired := arrow.Uint64Traits.BytesRequired(b.length) 418 if bytesRequired > 0 && bytesRequired < b.data.Len() { 419 // trim buffers 420 b.data.Resize(bytesRequired) 421 } 422 data = NewData(arrow.PrimitiveTypes.Uint64, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 423 b.reset() 424 425 if b.data != nil { 426 b.data.Release() 427 b.data = nil 428 b.rawData = nil 429 } 430 431 return 432 } 433 434 func (b *Uint64Builder) AppendValueFromString(s string) error { 435 if s == NullValueStr { 436 b.AppendNull() 437 return nil 438 } 439 v, err := strconv.ParseUint(s, 10, 8*8) 440 if err != nil { 441 b.AppendNull() 442 return err 443 } 444 b.Append(uint64(v)) 445 return nil 446 } 447 448 func (b *Uint64Builder) UnmarshalOne(dec *json.Decoder) error { 449 t, err := dec.Token() 450 if err != nil { 451 return err 452 } 453 454 switch v := t.(type) { 455 case nil: 456 b.AppendNull() 457 458 case string: 459 f, err := strconv.ParseUint(v, 10, 8*8) 460 if err != nil { 461 return &json.UnmarshalTypeError{ 462 Value: v, 463 Type: reflect.TypeOf(uint64(0)), 464 Offset: dec.InputOffset(), 465 } 466 } 467 b.Append(uint64(f)) 468 case float64: 469 b.Append(uint64(v)) 470 case json.Number: 471 f, err := strconv.ParseUint(v.String(), 10, 8*8) 472 if err != nil { 473 return &json.UnmarshalTypeError{ 474 Value: v.String(), 475 Type: reflect.TypeOf(uint64(0)), 476 Offset: dec.InputOffset(), 477 } 478 } 479 b.Append(uint64(f)) 480 481 default: 482 return &json.UnmarshalTypeError{ 483 Value: fmt.Sprint(t), 484 Type: reflect.TypeOf(uint64(0)), 485 Offset: dec.InputOffset(), 486 } 487 } 488 489 return nil 490 } 491 492 func (b *Uint64Builder) Unmarshal(dec *json.Decoder) error { 493 for dec.More() { 494 if err := b.UnmarshalOne(dec); err != nil { 495 return err 496 } 497 } 498 return nil 499 } 500 501 func (b *Uint64Builder) UnmarshalJSON(data []byte) error { 502 dec := json.NewDecoder(bytes.NewReader(data)) 503 t, err := dec.Token() 504 if err != nil { 505 return err 506 } 507 508 if delim, ok := t.(json.Delim); !ok || delim != '[' { 509 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 510 } 511 512 return b.Unmarshal(dec) 513 } 514 515 type Float64Builder struct { 516 builder 517 518 data *memory.Buffer 519 rawData []float64 520 } 521 522 func NewFloat64Builder(mem memory.Allocator) *Float64Builder { 523 return &Float64Builder{builder: builder{refCount: 1, mem: mem}} 524 } 525 526 func (b *Float64Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Float64 } 527 528 // Release decreases the reference count by 1. 529 // When the reference count goes to zero, the memory is freed. 530 func (b *Float64Builder) Release() { 531 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 532 533 if atomic.AddInt64(&b.refCount, -1) == 0 { 534 if b.nullBitmap != nil { 535 b.nullBitmap.Release() 536 b.nullBitmap = nil 537 } 538 if b.data != nil { 539 b.data.Release() 540 b.data = nil 541 b.rawData = nil 542 } 543 } 544 } 545 546 func (b *Float64Builder) Append(v float64) { 547 b.Reserve(1) 548 b.UnsafeAppend(v) 549 } 550 551 func (b *Float64Builder) AppendNull() { 552 b.Reserve(1) 553 b.UnsafeAppendBoolToBitmap(false) 554 } 555 556 func (b *Float64Builder) AppendNulls(n int) { 557 for i := 0; i < n; i++ { 558 b.AppendNull() 559 } 560 } 561 562 func (b *Float64Builder) AppendEmptyValue() { 563 b.Append(0) 564 } 565 566 func (b *Float64Builder) AppendEmptyValues(n int) { 567 for i := 0; i < n; i++ { 568 b.AppendEmptyValue() 569 } 570 } 571 572 func (b *Float64Builder) UnsafeAppend(v float64) { 573 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 574 b.rawData[b.length] = v 575 b.length++ 576 } 577 578 func (b *Float64Builder) UnsafeAppendBoolToBitmap(isValid bool) { 579 if isValid { 580 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 581 } else { 582 b.nulls++ 583 } 584 b.length++ 585 } 586 587 // AppendValues will append the values in the v slice. The valid slice determines which values 588 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 589 // all values in v are appended and considered valid. 590 func (b *Float64Builder) AppendValues(v []float64, valid []bool) { 591 if len(v) != len(valid) && len(valid) != 0 { 592 panic("len(v) != len(valid) && len(valid) != 0") 593 } 594 595 if len(v) == 0 { 596 return 597 } 598 599 b.Reserve(len(v)) 600 arrow.Float64Traits.Copy(b.rawData[b.length:], v) 601 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 602 } 603 604 func (b *Float64Builder) init(capacity int) { 605 b.builder.init(capacity) 606 607 b.data = memory.NewResizableBuffer(b.mem) 608 bytesN := arrow.Float64Traits.BytesRequired(capacity) 609 b.data.Resize(bytesN) 610 b.rawData = arrow.Float64Traits.CastFromBytes(b.data.Bytes()) 611 } 612 613 // Reserve ensures there is enough space for appending n elements 614 // by checking the capacity and calling Resize if necessary. 615 func (b *Float64Builder) Reserve(n int) { 616 b.builder.reserve(n, b.Resize) 617 } 618 619 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 620 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 621 func (b *Float64Builder) Resize(n int) { 622 nBuilder := n 623 if n < minBuilderCapacity { 624 n = minBuilderCapacity 625 } 626 627 if b.capacity == 0 { 628 b.init(n) 629 } else { 630 b.builder.resize(nBuilder, b.init) 631 b.data.Resize(arrow.Float64Traits.BytesRequired(n)) 632 b.rawData = arrow.Float64Traits.CastFromBytes(b.data.Bytes()) 633 } 634 } 635 636 func (b *Float64Builder) Value(i int) float64 { 637 return b.rawData[i] 638 } 639 640 // NewArray creates a Float64 array from the memory buffers used by the builder and resets the Float64Builder 641 // so it can be used to build a new array. 642 func (b *Float64Builder) NewArray() arrow.Array { 643 return b.NewFloat64Array() 644 } 645 646 // NewFloat64Array creates a Float64 array from the memory buffers used by the builder and resets the Float64Builder 647 // so it can be used to build a new array. 648 func (b *Float64Builder) NewFloat64Array() (a *Float64) { 649 data := b.newData() 650 a = NewFloat64Data(data) 651 data.Release() 652 return 653 } 654 655 func (b *Float64Builder) newData() (data *Data) { 656 bytesRequired := arrow.Float64Traits.BytesRequired(b.length) 657 if bytesRequired > 0 && bytesRequired < b.data.Len() { 658 // trim buffers 659 b.data.Resize(bytesRequired) 660 } 661 data = NewData(arrow.PrimitiveTypes.Float64, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 662 b.reset() 663 664 if b.data != nil { 665 b.data.Release() 666 b.data = nil 667 b.rawData = nil 668 } 669 670 return 671 } 672 673 func (b *Float64Builder) AppendValueFromString(s string) error { 674 if s == NullValueStr { 675 b.AppendNull() 676 return nil 677 } 678 v, err := strconv.ParseFloat(s, 8*8) 679 if err != nil { 680 b.AppendNull() 681 return err 682 } 683 b.Append(float64(v)) 684 return nil 685 } 686 687 func (b *Float64Builder) UnmarshalOne(dec *json.Decoder) error { 688 t, err := dec.Token() 689 if err != nil { 690 return err 691 } 692 693 switch v := t.(type) { 694 case nil: 695 b.AppendNull() 696 697 case string: 698 f, err := strconv.ParseFloat(v, 8*8) 699 if err != nil { 700 return &json.UnmarshalTypeError{ 701 Value: v, 702 Type: reflect.TypeOf(float64(0)), 703 Offset: dec.InputOffset(), 704 } 705 } 706 b.Append(float64(f)) 707 case float64: 708 b.Append(float64(v)) 709 case json.Number: 710 f, err := strconv.ParseFloat(v.String(), 8*8) 711 if err != nil { 712 return &json.UnmarshalTypeError{ 713 Value: v.String(), 714 Type: reflect.TypeOf(float64(0)), 715 Offset: dec.InputOffset(), 716 } 717 } 718 b.Append(float64(f)) 719 720 default: 721 return &json.UnmarshalTypeError{ 722 Value: fmt.Sprint(t), 723 Type: reflect.TypeOf(float64(0)), 724 Offset: dec.InputOffset(), 725 } 726 } 727 728 return nil 729 } 730 731 func (b *Float64Builder) Unmarshal(dec *json.Decoder) error { 732 for dec.More() { 733 if err := b.UnmarshalOne(dec); err != nil { 734 return err 735 } 736 } 737 return nil 738 } 739 740 func (b *Float64Builder) UnmarshalJSON(data []byte) error { 741 dec := json.NewDecoder(bytes.NewReader(data)) 742 t, err := dec.Token() 743 if err != nil { 744 return err 745 } 746 747 if delim, ok := t.(json.Delim); !ok || delim != '[' { 748 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 749 } 750 751 return b.Unmarshal(dec) 752 } 753 754 type Int32Builder struct { 755 builder 756 757 data *memory.Buffer 758 rawData []int32 759 } 760 761 func NewInt32Builder(mem memory.Allocator) *Int32Builder { 762 return &Int32Builder{builder: builder{refCount: 1, mem: mem}} 763 } 764 765 func (b *Int32Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Int32 } 766 767 // Release decreases the reference count by 1. 768 // When the reference count goes to zero, the memory is freed. 769 func (b *Int32Builder) Release() { 770 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 771 772 if atomic.AddInt64(&b.refCount, -1) == 0 { 773 if b.nullBitmap != nil { 774 b.nullBitmap.Release() 775 b.nullBitmap = nil 776 } 777 if b.data != nil { 778 b.data.Release() 779 b.data = nil 780 b.rawData = nil 781 } 782 } 783 } 784 785 func (b *Int32Builder) Append(v int32) { 786 b.Reserve(1) 787 b.UnsafeAppend(v) 788 } 789 790 func (b *Int32Builder) AppendNull() { 791 b.Reserve(1) 792 b.UnsafeAppendBoolToBitmap(false) 793 } 794 795 func (b *Int32Builder) AppendNulls(n int) { 796 for i := 0; i < n; i++ { 797 b.AppendNull() 798 } 799 } 800 801 func (b *Int32Builder) AppendEmptyValue() { 802 b.Append(0) 803 } 804 805 func (b *Int32Builder) AppendEmptyValues(n int) { 806 for i := 0; i < n; i++ { 807 b.AppendEmptyValue() 808 } 809 } 810 811 func (b *Int32Builder) UnsafeAppend(v int32) { 812 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 813 b.rawData[b.length] = v 814 b.length++ 815 } 816 817 func (b *Int32Builder) UnsafeAppendBoolToBitmap(isValid bool) { 818 if isValid { 819 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 820 } else { 821 b.nulls++ 822 } 823 b.length++ 824 } 825 826 // AppendValues will append the values in the v slice. The valid slice determines which values 827 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 828 // all values in v are appended and considered valid. 829 func (b *Int32Builder) AppendValues(v []int32, valid []bool) { 830 if len(v) != len(valid) && len(valid) != 0 { 831 panic("len(v) != len(valid) && len(valid) != 0") 832 } 833 834 if len(v) == 0 { 835 return 836 } 837 838 b.Reserve(len(v)) 839 arrow.Int32Traits.Copy(b.rawData[b.length:], v) 840 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 841 } 842 843 func (b *Int32Builder) init(capacity int) { 844 b.builder.init(capacity) 845 846 b.data = memory.NewResizableBuffer(b.mem) 847 bytesN := arrow.Int32Traits.BytesRequired(capacity) 848 b.data.Resize(bytesN) 849 b.rawData = arrow.Int32Traits.CastFromBytes(b.data.Bytes()) 850 } 851 852 // Reserve ensures there is enough space for appending n elements 853 // by checking the capacity and calling Resize if necessary. 854 func (b *Int32Builder) Reserve(n int) { 855 b.builder.reserve(n, b.Resize) 856 } 857 858 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 859 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 860 func (b *Int32Builder) Resize(n int) { 861 nBuilder := n 862 if n < minBuilderCapacity { 863 n = minBuilderCapacity 864 } 865 866 if b.capacity == 0 { 867 b.init(n) 868 } else { 869 b.builder.resize(nBuilder, b.init) 870 b.data.Resize(arrow.Int32Traits.BytesRequired(n)) 871 b.rawData = arrow.Int32Traits.CastFromBytes(b.data.Bytes()) 872 } 873 } 874 875 func (b *Int32Builder) Value(i int) int32 { 876 return b.rawData[i] 877 } 878 879 // NewArray creates a Int32 array from the memory buffers used by the builder and resets the Int32Builder 880 // so it can be used to build a new array. 881 func (b *Int32Builder) NewArray() arrow.Array { 882 return b.NewInt32Array() 883 } 884 885 // NewInt32Array creates a Int32 array from the memory buffers used by the builder and resets the Int32Builder 886 // so it can be used to build a new array. 887 func (b *Int32Builder) NewInt32Array() (a *Int32) { 888 data := b.newData() 889 a = NewInt32Data(data) 890 data.Release() 891 return 892 } 893 894 func (b *Int32Builder) newData() (data *Data) { 895 bytesRequired := arrow.Int32Traits.BytesRequired(b.length) 896 if bytesRequired > 0 && bytesRequired < b.data.Len() { 897 // trim buffers 898 b.data.Resize(bytesRequired) 899 } 900 data = NewData(arrow.PrimitiveTypes.Int32, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 901 b.reset() 902 903 if b.data != nil { 904 b.data.Release() 905 b.data = nil 906 b.rawData = nil 907 } 908 909 return 910 } 911 912 func (b *Int32Builder) AppendValueFromString(s string) error { 913 if s == NullValueStr { 914 b.AppendNull() 915 return nil 916 } 917 v, err := strconv.ParseInt(s, 10, 4*8) 918 if err != nil { 919 b.AppendNull() 920 return err 921 } 922 b.Append(int32(v)) 923 return nil 924 } 925 926 func (b *Int32Builder) UnmarshalOne(dec *json.Decoder) error { 927 t, err := dec.Token() 928 if err != nil { 929 return err 930 } 931 932 switch v := t.(type) { 933 case nil: 934 b.AppendNull() 935 936 case string: 937 f, err := strconv.ParseInt(v, 10, 4*8) 938 if err != nil { 939 return &json.UnmarshalTypeError{ 940 Value: v, 941 Type: reflect.TypeOf(int32(0)), 942 Offset: dec.InputOffset(), 943 } 944 } 945 b.Append(int32(f)) 946 case float64: 947 b.Append(int32(v)) 948 case json.Number: 949 f, err := strconv.ParseInt(v.String(), 10, 4*8) 950 if err != nil { 951 return &json.UnmarshalTypeError{ 952 Value: v.String(), 953 Type: reflect.TypeOf(int32(0)), 954 Offset: dec.InputOffset(), 955 } 956 } 957 b.Append(int32(f)) 958 959 default: 960 return &json.UnmarshalTypeError{ 961 Value: fmt.Sprint(t), 962 Type: reflect.TypeOf(int32(0)), 963 Offset: dec.InputOffset(), 964 } 965 } 966 967 return nil 968 } 969 970 func (b *Int32Builder) Unmarshal(dec *json.Decoder) error { 971 for dec.More() { 972 if err := b.UnmarshalOne(dec); err != nil { 973 return err 974 } 975 } 976 return nil 977 } 978 979 func (b *Int32Builder) UnmarshalJSON(data []byte) error { 980 dec := json.NewDecoder(bytes.NewReader(data)) 981 t, err := dec.Token() 982 if err != nil { 983 return err 984 } 985 986 if delim, ok := t.(json.Delim); !ok || delim != '[' { 987 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 988 } 989 990 return b.Unmarshal(dec) 991 } 992 993 type Uint32Builder struct { 994 builder 995 996 data *memory.Buffer 997 rawData []uint32 998 } 999 1000 func NewUint32Builder(mem memory.Allocator) *Uint32Builder { 1001 return &Uint32Builder{builder: builder{refCount: 1, mem: mem}} 1002 } 1003 1004 func (b *Uint32Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Uint32 } 1005 1006 // Release decreases the reference count by 1. 1007 // When the reference count goes to zero, the memory is freed. 1008 func (b *Uint32Builder) Release() { 1009 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 1010 1011 if atomic.AddInt64(&b.refCount, -1) == 0 { 1012 if b.nullBitmap != nil { 1013 b.nullBitmap.Release() 1014 b.nullBitmap = nil 1015 } 1016 if b.data != nil { 1017 b.data.Release() 1018 b.data = nil 1019 b.rawData = nil 1020 } 1021 } 1022 } 1023 1024 func (b *Uint32Builder) Append(v uint32) { 1025 b.Reserve(1) 1026 b.UnsafeAppend(v) 1027 } 1028 1029 func (b *Uint32Builder) AppendNull() { 1030 b.Reserve(1) 1031 b.UnsafeAppendBoolToBitmap(false) 1032 } 1033 1034 func (b *Uint32Builder) AppendNulls(n int) { 1035 for i := 0; i < n; i++ { 1036 b.AppendNull() 1037 } 1038 } 1039 1040 func (b *Uint32Builder) AppendEmptyValue() { 1041 b.Append(0) 1042 } 1043 1044 func (b *Uint32Builder) AppendEmptyValues(n int) { 1045 for i := 0; i < n; i++ { 1046 b.AppendEmptyValue() 1047 } 1048 } 1049 1050 func (b *Uint32Builder) UnsafeAppend(v uint32) { 1051 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1052 b.rawData[b.length] = v 1053 b.length++ 1054 } 1055 1056 func (b *Uint32Builder) UnsafeAppendBoolToBitmap(isValid bool) { 1057 if isValid { 1058 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1059 } else { 1060 b.nulls++ 1061 } 1062 b.length++ 1063 } 1064 1065 // AppendValues will append the values in the v slice. The valid slice determines which values 1066 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 1067 // all values in v are appended and considered valid. 1068 func (b *Uint32Builder) AppendValues(v []uint32, valid []bool) { 1069 if len(v) != len(valid) && len(valid) != 0 { 1070 panic("len(v) != len(valid) && len(valid) != 0") 1071 } 1072 1073 if len(v) == 0 { 1074 return 1075 } 1076 1077 b.Reserve(len(v)) 1078 arrow.Uint32Traits.Copy(b.rawData[b.length:], v) 1079 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 1080 } 1081 1082 func (b *Uint32Builder) init(capacity int) { 1083 b.builder.init(capacity) 1084 1085 b.data = memory.NewResizableBuffer(b.mem) 1086 bytesN := arrow.Uint32Traits.BytesRequired(capacity) 1087 b.data.Resize(bytesN) 1088 b.rawData = arrow.Uint32Traits.CastFromBytes(b.data.Bytes()) 1089 } 1090 1091 // Reserve ensures there is enough space for appending n elements 1092 // by checking the capacity and calling Resize if necessary. 1093 func (b *Uint32Builder) Reserve(n int) { 1094 b.builder.reserve(n, b.Resize) 1095 } 1096 1097 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 1098 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 1099 func (b *Uint32Builder) Resize(n int) { 1100 nBuilder := n 1101 if n < minBuilderCapacity { 1102 n = minBuilderCapacity 1103 } 1104 1105 if b.capacity == 0 { 1106 b.init(n) 1107 } else { 1108 b.builder.resize(nBuilder, b.init) 1109 b.data.Resize(arrow.Uint32Traits.BytesRequired(n)) 1110 b.rawData = arrow.Uint32Traits.CastFromBytes(b.data.Bytes()) 1111 } 1112 } 1113 1114 func (b *Uint32Builder) Value(i int) uint32 { 1115 return b.rawData[i] 1116 } 1117 1118 // NewArray creates a Uint32 array from the memory buffers used by the builder and resets the Uint32Builder 1119 // so it can be used to build a new array. 1120 func (b *Uint32Builder) NewArray() arrow.Array { 1121 return b.NewUint32Array() 1122 } 1123 1124 // NewUint32Array creates a Uint32 array from the memory buffers used by the builder and resets the Uint32Builder 1125 // so it can be used to build a new array. 1126 func (b *Uint32Builder) NewUint32Array() (a *Uint32) { 1127 data := b.newData() 1128 a = NewUint32Data(data) 1129 data.Release() 1130 return 1131 } 1132 1133 func (b *Uint32Builder) newData() (data *Data) { 1134 bytesRequired := arrow.Uint32Traits.BytesRequired(b.length) 1135 if bytesRequired > 0 && bytesRequired < b.data.Len() { 1136 // trim buffers 1137 b.data.Resize(bytesRequired) 1138 } 1139 data = NewData(arrow.PrimitiveTypes.Uint32, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 1140 b.reset() 1141 1142 if b.data != nil { 1143 b.data.Release() 1144 b.data = nil 1145 b.rawData = nil 1146 } 1147 1148 return 1149 } 1150 1151 func (b *Uint32Builder) AppendValueFromString(s string) error { 1152 if s == NullValueStr { 1153 b.AppendNull() 1154 return nil 1155 } 1156 v, err := strconv.ParseUint(s, 10, 4*8) 1157 if err != nil { 1158 b.AppendNull() 1159 return err 1160 } 1161 b.Append(uint32(v)) 1162 return nil 1163 } 1164 1165 func (b *Uint32Builder) UnmarshalOne(dec *json.Decoder) error { 1166 t, err := dec.Token() 1167 if err != nil { 1168 return err 1169 } 1170 1171 switch v := t.(type) { 1172 case nil: 1173 b.AppendNull() 1174 1175 case string: 1176 f, err := strconv.ParseUint(v, 10, 4*8) 1177 if err != nil { 1178 return &json.UnmarshalTypeError{ 1179 Value: v, 1180 Type: reflect.TypeOf(uint32(0)), 1181 Offset: dec.InputOffset(), 1182 } 1183 } 1184 b.Append(uint32(f)) 1185 case float64: 1186 b.Append(uint32(v)) 1187 case json.Number: 1188 f, err := strconv.ParseUint(v.String(), 10, 4*8) 1189 if err != nil { 1190 return &json.UnmarshalTypeError{ 1191 Value: v.String(), 1192 Type: reflect.TypeOf(uint32(0)), 1193 Offset: dec.InputOffset(), 1194 } 1195 } 1196 b.Append(uint32(f)) 1197 1198 default: 1199 return &json.UnmarshalTypeError{ 1200 Value: fmt.Sprint(t), 1201 Type: reflect.TypeOf(uint32(0)), 1202 Offset: dec.InputOffset(), 1203 } 1204 } 1205 1206 return nil 1207 } 1208 1209 func (b *Uint32Builder) Unmarshal(dec *json.Decoder) error { 1210 for dec.More() { 1211 if err := b.UnmarshalOne(dec); err != nil { 1212 return err 1213 } 1214 } 1215 return nil 1216 } 1217 1218 func (b *Uint32Builder) UnmarshalJSON(data []byte) error { 1219 dec := json.NewDecoder(bytes.NewReader(data)) 1220 t, err := dec.Token() 1221 if err != nil { 1222 return err 1223 } 1224 1225 if delim, ok := t.(json.Delim); !ok || delim != '[' { 1226 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 1227 } 1228 1229 return b.Unmarshal(dec) 1230 } 1231 1232 type Float32Builder struct { 1233 builder 1234 1235 data *memory.Buffer 1236 rawData []float32 1237 } 1238 1239 func NewFloat32Builder(mem memory.Allocator) *Float32Builder { 1240 return &Float32Builder{builder: builder{refCount: 1, mem: mem}} 1241 } 1242 1243 func (b *Float32Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Float32 } 1244 1245 // Release decreases the reference count by 1. 1246 // When the reference count goes to zero, the memory is freed. 1247 func (b *Float32Builder) Release() { 1248 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 1249 1250 if atomic.AddInt64(&b.refCount, -1) == 0 { 1251 if b.nullBitmap != nil { 1252 b.nullBitmap.Release() 1253 b.nullBitmap = nil 1254 } 1255 if b.data != nil { 1256 b.data.Release() 1257 b.data = nil 1258 b.rawData = nil 1259 } 1260 } 1261 } 1262 1263 func (b *Float32Builder) Append(v float32) { 1264 b.Reserve(1) 1265 b.UnsafeAppend(v) 1266 } 1267 1268 func (b *Float32Builder) AppendNull() { 1269 b.Reserve(1) 1270 b.UnsafeAppendBoolToBitmap(false) 1271 } 1272 1273 func (b *Float32Builder) AppendNulls(n int) { 1274 for i := 0; i < n; i++ { 1275 b.AppendNull() 1276 } 1277 } 1278 1279 func (b *Float32Builder) AppendEmptyValue() { 1280 b.Append(0) 1281 } 1282 1283 func (b *Float32Builder) AppendEmptyValues(n int) { 1284 for i := 0; i < n; i++ { 1285 b.AppendEmptyValue() 1286 } 1287 } 1288 1289 func (b *Float32Builder) UnsafeAppend(v float32) { 1290 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1291 b.rawData[b.length] = v 1292 b.length++ 1293 } 1294 1295 func (b *Float32Builder) UnsafeAppendBoolToBitmap(isValid bool) { 1296 if isValid { 1297 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1298 } else { 1299 b.nulls++ 1300 } 1301 b.length++ 1302 } 1303 1304 // AppendValues will append the values in the v slice. The valid slice determines which values 1305 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 1306 // all values in v are appended and considered valid. 1307 func (b *Float32Builder) AppendValues(v []float32, valid []bool) { 1308 if len(v) != len(valid) && len(valid) != 0 { 1309 panic("len(v) != len(valid) && len(valid) != 0") 1310 } 1311 1312 if len(v) == 0 { 1313 return 1314 } 1315 1316 b.Reserve(len(v)) 1317 arrow.Float32Traits.Copy(b.rawData[b.length:], v) 1318 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 1319 } 1320 1321 func (b *Float32Builder) init(capacity int) { 1322 b.builder.init(capacity) 1323 1324 b.data = memory.NewResizableBuffer(b.mem) 1325 bytesN := arrow.Float32Traits.BytesRequired(capacity) 1326 b.data.Resize(bytesN) 1327 b.rawData = arrow.Float32Traits.CastFromBytes(b.data.Bytes()) 1328 } 1329 1330 // Reserve ensures there is enough space for appending n elements 1331 // by checking the capacity and calling Resize if necessary. 1332 func (b *Float32Builder) Reserve(n int) { 1333 b.builder.reserve(n, b.Resize) 1334 } 1335 1336 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 1337 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 1338 func (b *Float32Builder) Resize(n int) { 1339 nBuilder := n 1340 if n < minBuilderCapacity { 1341 n = minBuilderCapacity 1342 } 1343 1344 if b.capacity == 0 { 1345 b.init(n) 1346 } else { 1347 b.builder.resize(nBuilder, b.init) 1348 b.data.Resize(arrow.Float32Traits.BytesRequired(n)) 1349 b.rawData = arrow.Float32Traits.CastFromBytes(b.data.Bytes()) 1350 } 1351 } 1352 1353 func (b *Float32Builder) Value(i int) float32 { 1354 return b.rawData[i] 1355 } 1356 1357 // NewArray creates a Float32 array from the memory buffers used by the builder and resets the Float32Builder 1358 // so it can be used to build a new array. 1359 func (b *Float32Builder) NewArray() arrow.Array { 1360 return b.NewFloat32Array() 1361 } 1362 1363 // NewFloat32Array creates a Float32 array from the memory buffers used by the builder and resets the Float32Builder 1364 // so it can be used to build a new array. 1365 func (b *Float32Builder) NewFloat32Array() (a *Float32) { 1366 data := b.newData() 1367 a = NewFloat32Data(data) 1368 data.Release() 1369 return 1370 } 1371 1372 func (b *Float32Builder) newData() (data *Data) { 1373 bytesRequired := arrow.Float32Traits.BytesRequired(b.length) 1374 if bytesRequired > 0 && bytesRequired < b.data.Len() { 1375 // trim buffers 1376 b.data.Resize(bytesRequired) 1377 } 1378 data = NewData(arrow.PrimitiveTypes.Float32, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 1379 b.reset() 1380 1381 if b.data != nil { 1382 b.data.Release() 1383 b.data = nil 1384 b.rawData = nil 1385 } 1386 1387 return 1388 } 1389 1390 func (b *Float32Builder) AppendValueFromString(s string) error { 1391 if s == NullValueStr { 1392 b.AppendNull() 1393 return nil 1394 } 1395 v, err := strconv.ParseFloat(s, 4*8) 1396 if err != nil { 1397 b.AppendNull() 1398 return err 1399 } 1400 b.Append(float32(v)) 1401 return nil 1402 } 1403 1404 func (b *Float32Builder) UnmarshalOne(dec *json.Decoder) error { 1405 t, err := dec.Token() 1406 if err != nil { 1407 return err 1408 } 1409 1410 switch v := t.(type) { 1411 case nil: 1412 b.AppendNull() 1413 1414 case string: 1415 f, err := strconv.ParseFloat(v, 4*8) 1416 if err != nil { 1417 return &json.UnmarshalTypeError{ 1418 Value: v, 1419 Type: reflect.TypeOf(float32(0)), 1420 Offset: dec.InputOffset(), 1421 } 1422 } 1423 b.Append(float32(f)) 1424 case float64: 1425 b.Append(float32(v)) 1426 case json.Number: 1427 f, err := strconv.ParseFloat(v.String(), 4*8) 1428 if err != nil { 1429 return &json.UnmarshalTypeError{ 1430 Value: v.String(), 1431 Type: reflect.TypeOf(float32(0)), 1432 Offset: dec.InputOffset(), 1433 } 1434 } 1435 b.Append(float32(f)) 1436 1437 default: 1438 return &json.UnmarshalTypeError{ 1439 Value: fmt.Sprint(t), 1440 Type: reflect.TypeOf(float32(0)), 1441 Offset: dec.InputOffset(), 1442 } 1443 } 1444 1445 return nil 1446 } 1447 1448 func (b *Float32Builder) Unmarshal(dec *json.Decoder) error { 1449 for dec.More() { 1450 if err := b.UnmarshalOne(dec); err != nil { 1451 return err 1452 } 1453 } 1454 return nil 1455 } 1456 1457 func (b *Float32Builder) UnmarshalJSON(data []byte) error { 1458 dec := json.NewDecoder(bytes.NewReader(data)) 1459 t, err := dec.Token() 1460 if err != nil { 1461 return err 1462 } 1463 1464 if delim, ok := t.(json.Delim); !ok || delim != '[' { 1465 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 1466 } 1467 1468 return b.Unmarshal(dec) 1469 } 1470 1471 type Int16Builder struct { 1472 builder 1473 1474 data *memory.Buffer 1475 rawData []int16 1476 } 1477 1478 func NewInt16Builder(mem memory.Allocator) *Int16Builder { 1479 return &Int16Builder{builder: builder{refCount: 1, mem: mem}} 1480 } 1481 1482 func (b *Int16Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Int16 } 1483 1484 // Release decreases the reference count by 1. 1485 // When the reference count goes to zero, the memory is freed. 1486 func (b *Int16Builder) Release() { 1487 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 1488 1489 if atomic.AddInt64(&b.refCount, -1) == 0 { 1490 if b.nullBitmap != nil { 1491 b.nullBitmap.Release() 1492 b.nullBitmap = nil 1493 } 1494 if b.data != nil { 1495 b.data.Release() 1496 b.data = nil 1497 b.rawData = nil 1498 } 1499 } 1500 } 1501 1502 func (b *Int16Builder) Append(v int16) { 1503 b.Reserve(1) 1504 b.UnsafeAppend(v) 1505 } 1506 1507 func (b *Int16Builder) AppendNull() { 1508 b.Reserve(1) 1509 b.UnsafeAppendBoolToBitmap(false) 1510 } 1511 1512 func (b *Int16Builder) AppendNulls(n int) { 1513 for i := 0; i < n; i++ { 1514 b.AppendNull() 1515 } 1516 } 1517 1518 func (b *Int16Builder) AppendEmptyValue() { 1519 b.Append(0) 1520 } 1521 1522 func (b *Int16Builder) AppendEmptyValues(n int) { 1523 for i := 0; i < n; i++ { 1524 b.AppendEmptyValue() 1525 } 1526 } 1527 1528 func (b *Int16Builder) UnsafeAppend(v int16) { 1529 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1530 b.rawData[b.length] = v 1531 b.length++ 1532 } 1533 1534 func (b *Int16Builder) UnsafeAppendBoolToBitmap(isValid bool) { 1535 if isValid { 1536 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1537 } else { 1538 b.nulls++ 1539 } 1540 b.length++ 1541 } 1542 1543 // AppendValues will append the values in the v slice. The valid slice determines which values 1544 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 1545 // all values in v are appended and considered valid. 1546 func (b *Int16Builder) AppendValues(v []int16, valid []bool) { 1547 if len(v) != len(valid) && len(valid) != 0 { 1548 panic("len(v) != len(valid) && len(valid) != 0") 1549 } 1550 1551 if len(v) == 0 { 1552 return 1553 } 1554 1555 b.Reserve(len(v)) 1556 arrow.Int16Traits.Copy(b.rawData[b.length:], v) 1557 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 1558 } 1559 1560 func (b *Int16Builder) init(capacity int) { 1561 b.builder.init(capacity) 1562 1563 b.data = memory.NewResizableBuffer(b.mem) 1564 bytesN := arrow.Int16Traits.BytesRequired(capacity) 1565 b.data.Resize(bytesN) 1566 b.rawData = arrow.Int16Traits.CastFromBytes(b.data.Bytes()) 1567 } 1568 1569 // Reserve ensures there is enough space for appending n elements 1570 // by checking the capacity and calling Resize if necessary. 1571 func (b *Int16Builder) Reserve(n int) { 1572 b.builder.reserve(n, b.Resize) 1573 } 1574 1575 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 1576 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 1577 func (b *Int16Builder) Resize(n int) { 1578 nBuilder := n 1579 if n < minBuilderCapacity { 1580 n = minBuilderCapacity 1581 } 1582 1583 if b.capacity == 0 { 1584 b.init(n) 1585 } else { 1586 b.builder.resize(nBuilder, b.init) 1587 b.data.Resize(arrow.Int16Traits.BytesRequired(n)) 1588 b.rawData = arrow.Int16Traits.CastFromBytes(b.data.Bytes()) 1589 } 1590 } 1591 1592 func (b *Int16Builder) Value(i int) int16 { 1593 return b.rawData[i] 1594 } 1595 1596 // NewArray creates a Int16 array from the memory buffers used by the builder and resets the Int16Builder 1597 // so it can be used to build a new array. 1598 func (b *Int16Builder) NewArray() arrow.Array { 1599 return b.NewInt16Array() 1600 } 1601 1602 // NewInt16Array creates a Int16 array from the memory buffers used by the builder and resets the Int16Builder 1603 // so it can be used to build a new array. 1604 func (b *Int16Builder) NewInt16Array() (a *Int16) { 1605 data := b.newData() 1606 a = NewInt16Data(data) 1607 data.Release() 1608 return 1609 } 1610 1611 func (b *Int16Builder) newData() (data *Data) { 1612 bytesRequired := arrow.Int16Traits.BytesRequired(b.length) 1613 if bytesRequired > 0 && bytesRequired < b.data.Len() { 1614 // trim buffers 1615 b.data.Resize(bytesRequired) 1616 } 1617 data = NewData(arrow.PrimitiveTypes.Int16, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 1618 b.reset() 1619 1620 if b.data != nil { 1621 b.data.Release() 1622 b.data = nil 1623 b.rawData = nil 1624 } 1625 1626 return 1627 } 1628 1629 func (b *Int16Builder) AppendValueFromString(s string) error { 1630 if s == NullValueStr { 1631 b.AppendNull() 1632 return nil 1633 } 1634 v, err := strconv.ParseInt(s, 10, 2*8) 1635 if err != nil { 1636 b.AppendNull() 1637 return err 1638 } 1639 b.Append(int16(v)) 1640 return nil 1641 } 1642 1643 func (b *Int16Builder) UnmarshalOne(dec *json.Decoder) error { 1644 t, err := dec.Token() 1645 if err != nil { 1646 return err 1647 } 1648 1649 switch v := t.(type) { 1650 case nil: 1651 b.AppendNull() 1652 1653 case string: 1654 f, err := strconv.ParseInt(v, 10, 2*8) 1655 if err != nil { 1656 return &json.UnmarshalTypeError{ 1657 Value: v, 1658 Type: reflect.TypeOf(int16(0)), 1659 Offset: dec.InputOffset(), 1660 } 1661 } 1662 b.Append(int16(f)) 1663 case float64: 1664 b.Append(int16(v)) 1665 case json.Number: 1666 f, err := strconv.ParseInt(v.String(), 10, 2*8) 1667 if err != nil { 1668 return &json.UnmarshalTypeError{ 1669 Value: v.String(), 1670 Type: reflect.TypeOf(int16(0)), 1671 Offset: dec.InputOffset(), 1672 } 1673 } 1674 b.Append(int16(f)) 1675 1676 default: 1677 return &json.UnmarshalTypeError{ 1678 Value: fmt.Sprint(t), 1679 Type: reflect.TypeOf(int16(0)), 1680 Offset: dec.InputOffset(), 1681 } 1682 } 1683 1684 return nil 1685 } 1686 1687 func (b *Int16Builder) Unmarshal(dec *json.Decoder) error { 1688 for dec.More() { 1689 if err := b.UnmarshalOne(dec); err != nil { 1690 return err 1691 } 1692 } 1693 return nil 1694 } 1695 1696 func (b *Int16Builder) UnmarshalJSON(data []byte) error { 1697 dec := json.NewDecoder(bytes.NewReader(data)) 1698 t, err := dec.Token() 1699 if err != nil { 1700 return err 1701 } 1702 1703 if delim, ok := t.(json.Delim); !ok || delim != '[' { 1704 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 1705 } 1706 1707 return b.Unmarshal(dec) 1708 } 1709 1710 type Uint16Builder struct { 1711 builder 1712 1713 data *memory.Buffer 1714 rawData []uint16 1715 } 1716 1717 func NewUint16Builder(mem memory.Allocator) *Uint16Builder { 1718 return &Uint16Builder{builder: builder{refCount: 1, mem: mem}} 1719 } 1720 1721 func (b *Uint16Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Uint16 } 1722 1723 // Release decreases the reference count by 1. 1724 // When the reference count goes to zero, the memory is freed. 1725 func (b *Uint16Builder) Release() { 1726 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 1727 1728 if atomic.AddInt64(&b.refCount, -1) == 0 { 1729 if b.nullBitmap != nil { 1730 b.nullBitmap.Release() 1731 b.nullBitmap = nil 1732 } 1733 if b.data != nil { 1734 b.data.Release() 1735 b.data = nil 1736 b.rawData = nil 1737 } 1738 } 1739 } 1740 1741 func (b *Uint16Builder) Append(v uint16) { 1742 b.Reserve(1) 1743 b.UnsafeAppend(v) 1744 } 1745 1746 func (b *Uint16Builder) AppendNull() { 1747 b.Reserve(1) 1748 b.UnsafeAppendBoolToBitmap(false) 1749 } 1750 1751 func (b *Uint16Builder) AppendNulls(n int) { 1752 for i := 0; i < n; i++ { 1753 b.AppendNull() 1754 } 1755 } 1756 1757 func (b *Uint16Builder) AppendEmptyValue() { 1758 b.Append(0) 1759 } 1760 1761 func (b *Uint16Builder) AppendEmptyValues(n int) { 1762 for i := 0; i < n; i++ { 1763 b.AppendEmptyValue() 1764 } 1765 } 1766 1767 func (b *Uint16Builder) UnsafeAppend(v uint16) { 1768 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1769 b.rawData[b.length] = v 1770 b.length++ 1771 } 1772 1773 func (b *Uint16Builder) UnsafeAppendBoolToBitmap(isValid bool) { 1774 if isValid { 1775 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 1776 } else { 1777 b.nulls++ 1778 } 1779 b.length++ 1780 } 1781 1782 // AppendValues will append the values in the v slice. The valid slice determines which values 1783 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 1784 // all values in v are appended and considered valid. 1785 func (b *Uint16Builder) AppendValues(v []uint16, valid []bool) { 1786 if len(v) != len(valid) && len(valid) != 0 { 1787 panic("len(v) != len(valid) && len(valid) != 0") 1788 } 1789 1790 if len(v) == 0 { 1791 return 1792 } 1793 1794 b.Reserve(len(v)) 1795 arrow.Uint16Traits.Copy(b.rawData[b.length:], v) 1796 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 1797 } 1798 1799 func (b *Uint16Builder) init(capacity int) { 1800 b.builder.init(capacity) 1801 1802 b.data = memory.NewResizableBuffer(b.mem) 1803 bytesN := arrow.Uint16Traits.BytesRequired(capacity) 1804 b.data.Resize(bytesN) 1805 b.rawData = arrow.Uint16Traits.CastFromBytes(b.data.Bytes()) 1806 } 1807 1808 // Reserve ensures there is enough space for appending n elements 1809 // by checking the capacity and calling Resize if necessary. 1810 func (b *Uint16Builder) Reserve(n int) { 1811 b.builder.reserve(n, b.Resize) 1812 } 1813 1814 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 1815 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 1816 func (b *Uint16Builder) Resize(n int) { 1817 nBuilder := n 1818 if n < minBuilderCapacity { 1819 n = minBuilderCapacity 1820 } 1821 1822 if b.capacity == 0 { 1823 b.init(n) 1824 } else { 1825 b.builder.resize(nBuilder, b.init) 1826 b.data.Resize(arrow.Uint16Traits.BytesRequired(n)) 1827 b.rawData = arrow.Uint16Traits.CastFromBytes(b.data.Bytes()) 1828 } 1829 } 1830 1831 func (b *Uint16Builder) Value(i int) uint16 { 1832 return b.rawData[i] 1833 } 1834 1835 // NewArray creates a Uint16 array from the memory buffers used by the builder and resets the Uint16Builder 1836 // so it can be used to build a new array. 1837 func (b *Uint16Builder) NewArray() arrow.Array { 1838 return b.NewUint16Array() 1839 } 1840 1841 // NewUint16Array creates a Uint16 array from the memory buffers used by the builder and resets the Uint16Builder 1842 // so it can be used to build a new array. 1843 func (b *Uint16Builder) NewUint16Array() (a *Uint16) { 1844 data := b.newData() 1845 a = NewUint16Data(data) 1846 data.Release() 1847 return 1848 } 1849 1850 func (b *Uint16Builder) newData() (data *Data) { 1851 bytesRequired := arrow.Uint16Traits.BytesRequired(b.length) 1852 if bytesRequired > 0 && bytesRequired < b.data.Len() { 1853 // trim buffers 1854 b.data.Resize(bytesRequired) 1855 } 1856 data = NewData(arrow.PrimitiveTypes.Uint16, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 1857 b.reset() 1858 1859 if b.data != nil { 1860 b.data.Release() 1861 b.data = nil 1862 b.rawData = nil 1863 } 1864 1865 return 1866 } 1867 1868 func (b *Uint16Builder) AppendValueFromString(s string) error { 1869 if s == NullValueStr { 1870 b.AppendNull() 1871 return nil 1872 } 1873 v, err := strconv.ParseUint(s, 10, 2*8) 1874 if err != nil { 1875 b.AppendNull() 1876 return err 1877 } 1878 b.Append(uint16(v)) 1879 return nil 1880 } 1881 1882 func (b *Uint16Builder) UnmarshalOne(dec *json.Decoder) error { 1883 t, err := dec.Token() 1884 if err != nil { 1885 return err 1886 } 1887 1888 switch v := t.(type) { 1889 case nil: 1890 b.AppendNull() 1891 1892 case string: 1893 f, err := strconv.ParseUint(v, 10, 2*8) 1894 if err != nil { 1895 return &json.UnmarshalTypeError{ 1896 Value: v, 1897 Type: reflect.TypeOf(uint16(0)), 1898 Offset: dec.InputOffset(), 1899 } 1900 } 1901 b.Append(uint16(f)) 1902 case float64: 1903 b.Append(uint16(v)) 1904 case json.Number: 1905 f, err := strconv.ParseUint(v.String(), 10, 2*8) 1906 if err != nil { 1907 return &json.UnmarshalTypeError{ 1908 Value: v.String(), 1909 Type: reflect.TypeOf(uint16(0)), 1910 Offset: dec.InputOffset(), 1911 } 1912 } 1913 b.Append(uint16(f)) 1914 1915 default: 1916 return &json.UnmarshalTypeError{ 1917 Value: fmt.Sprint(t), 1918 Type: reflect.TypeOf(uint16(0)), 1919 Offset: dec.InputOffset(), 1920 } 1921 } 1922 1923 return nil 1924 } 1925 1926 func (b *Uint16Builder) Unmarshal(dec *json.Decoder) error { 1927 for dec.More() { 1928 if err := b.UnmarshalOne(dec); err != nil { 1929 return err 1930 } 1931 } 1932 return nil 1933 } 1934 1935 func (b *Uint16Builder) UnmarshalJSON(data []byte) error { 1936 dec := json.NewDecoder(bytes.NewReader(data)) 1937 t, err := dec.Token() 1938 if err != nil { 1939 return err 1940 } 1941 1942 if delim, ok := t.(json.Delim); !ok || delim != '[' { 1943 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 1944 } 1945 1946 return b.Unmarshal(dec) 1947 } 1948 1949 type Int8Builder struct { 1950 builder 1951 1952 data *memory.Buffer 1953 rawData []int8 1954 } 1955 1956 func NewInt8Builder(mem memory.Allocator) *Int8Builder { 1957 return &Int8Builder{builder: builder{refCount: 1, mem: mem}} 1958 } 1959 1960 func (b *Int8Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Int8 } 1961 1962 // Release decreases the reference count by 1. 1963 // When the reference count goes to zero, the memory is freed. 1964 func (b *Int8Builder) Release() { 1965 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 1966 1967 if atomic.AddInt64(&b.refCount, -1) == 0 { 1968 if b.nullBitmap != nil { 1969 b.nullBitmap.Release() 1970 b.nullBitmap = nil 1971 } 1972 if b.data != nil { 1973 b.data.Release() 1974 b.data = nil 1975 b.rawData = nil 1976 } 1977 } 1978 } 1979 1980 func (b *Int8Builder) Append(v int8) { 1981 b.Reserve(1) 1982 b.UnsafeAppend(v) 1983 } 1984 1985 func (b *Int8Builder) AppendNull() { 1986 b.Reserve(1) 1987 b.UnsafeAppendBoolToBitmap(false) 1988 } 1989 1990 func (b *Int8Builder) AppendNulls(n int) { 1991 for i := 0; i < n; i++ { 1992 b.AppendNull() 1993 } 1994 } 1995 1996 func (b *Int8Builder) AppendEmptyValue() { 1997 b.Append(0) 1998 } 1999 2000 func (b *Int8Builder) AppendEmptyValues(n int) { 2001 for i := 0; i < n; i++ { 2002 b.AppendEmptyValue() 2003 } 2004 } 2005 2006 func (b *Int8Builder) UnsafeAppend(v int8) { 2007 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2008 b.rawData[b.length] = v 2009 b.length++ 2010 } 2011 2012 func (b *Int8Builder) UnsafeAppendBoolToBitmap(isValid bool) { 2013 if isValid { 2014 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2015 } else { 2016 b.nulls++ 2017 } 2018 b.length++ 2019 } 2020 2021 // AppendValues will append the values in the v slice. The valid slice determines which values 2022 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 2023 // all values in v are appended and considered valid. 2024 func (b *Int8Builder) AppendValues(v []int8, valid []bool) { 2025 if len(v) != len(valid) && len(valid) != 0 { 2026 panic("len(v) != len(valid) && len(valid) != 0") 2027 } 2028 2029 if len(v) == 0 { 2030 return 2031 } 2032 2033 b.Reserve(len(v)) 2034 arrow.Int8Traits.Copy(b.rawData[b.length:], v) 2035 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 2036 } 2037 2038 func (b *Int8Builder) init(capacity int) { 2039 b.builder.init(capacity) 2040 2041 b.data = memory.NewResizableBuffer(b.mem) 2042 bytesN := arrow.Int8Traits.BytesRequired(capacity) 2043 b.data.Resize(bytesN) 2044 b.rawData = arrow.Int8Traits.CastFromBytes(b.data.Bytes()) 2045 } 2046 2047 // Reserve ensures there is enough space for appending n elements 2048 // by checking the capacity and calling Resize if necessary. 2049 func (b *Int8Builder) Reserve(n int) { 2050 b.builder.reserve(n, b.Resize) 2051 } 2052 2053 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 2054 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 2055 func (b *Int8Builder) Resize(n int) { 2056 nBuilder := n 2057 if n < minBuilderCapacity { 2058 n = minBuilderCapacity 2059 } 2060 2061 if b.capacity == 0 { 2062 b.init(n) 2063 } else { 2064 b.builder.resize(nBuilder, b.init) 2065 b.data.Resize(arrow.Int8Traits.BytesRequired(n)) 2066 b.rawData = arrow.Int8Traits.CastFromBytes(b.data.Bytes()) 2067 } 2068 } 2069 2070 func (b *Int8Builder) Value(i int) int8 { 2071 return b.rawData[i] 2072 } 2073 2074 // NewArray creates a Int8 array from the memory buffers used by the builder and resets the Int8Builder 2075 // so it can be used to build a new array. 2076 func (b *Int8Builder) NewArray() arrow.Array { 2077 return b.NewInt8Array() 2078 } 2079 2080 // NewInt8Array creates a Int8 array from the memory buffers used by the builder and resets the Int8Builder 2081 // so it can be used to build a new array. 2082 func (b *Int8Builder) NewInt8Array() (a *Int8) { 2083 data := b.newData() 2084 a = NewInt8Data(data) 2085 data.Release() 2086 return 2087 } 2088 2089 func (b *Int8Builder) newData() (data *Data) { 2090 bytesRequired := arrow.Int8Traits.BytesRequired(b.length) 2091 if bytesRequired > 0 && bytesRequired < b.data.Len() { 2092 // trim buffers 2093 b.data.Resize(bytesRequired) 2094 } 2095 data = NewData(arrow.PrimitiveTypes.Int8, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 2096 b.reset() 2097 2098 if b.data != nil { 2099 b.data.Release() 2100 b.data = nil 2101 b.rawData = nil 2102 } 2103 2104 return 2105 } 2106 2107 func (b *Int8Builder) AppendValueFromString(s string) error { 2108 if s == NullValueStr { 2109 b.AppendNull() 2110 return nil 2111 } 2112 v, err := strconv.ParseInt(s, 10, 1*8) 2113 if err != nil { 2114 b.AppendNull() 2115 return err 2116 } 2117 b.Append(int8(v)) 2118 return nil 2119 } 2120 2121 func (b *Int8Builder) UnmarshalOne(dec *json.Decoder) error { 2122 t, err := dec.Token() 2123 if err != nil { 2124 return err 2125 } 2126 2127 switch v := t.(type) { 2128 case nil: 2129 b.AppendNull() 2130 2131 case string: 2132 f, err := strconv.ParseInt(v, 10, 1*8) 2133 if err != nil { 2134 return &json.UnmarshalTypeError{ 2135 Value: v, 2136 Type: reflect.TypeOf(int8(0)), 2137 Offset: dec.InputOffset(), 2138 } 2139 } 2140 b.Append(int8(f)) 2141 case float64: 2142 b.Append(int8(v)) 2143 case json.Number: 2144 f, err := strconv.ParseInt(v.String(), 10, 1*8) 2145 if err != nil { 2146 return &json.UnmarshalTypeError{ 2147 Value: v.String(), 2148 Type: reflect.TypeOf(int8(0)), 2149 Offset: dec.InputOffset(), 2150 } 2151 } 2152 b.Append(int8(f)) 2153 2154 default: 2155 return &json.UnmarshalTypeError{ 2156 Value: fmt.Sprint(t), 2157 Type: reflect.TypeOf(int8(0)), 2158 Offset: dec.InputOffset(), 2159 } 2160 } 2161 2162 return nil 2163 } 2164 2165 func (b *Int8Builder) Unmarshal(dec *json.Decoder) error { 2166 for dec.More() { 2167 if err := b.UnmarshalOne(dec); err != nil { 2168 return err 2169 } 2170 } 2171 return nil 2172 } 2173 2174 func (b *Int8Builder) UnmarshalJSON(data []byte) error { 2175 dec := json.NewDecoder(bytes.NewReader(data)) 2176 t, err := dec.Token() 2177 if err != nil { 2178 return err 2179 } 2180 2181 if delim, ok := t.(json.Delim); !ok || delim != '[' { 2182 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 2183 } 2184 2185 return b.Unmarshal(dec) 2186 } 2187 2188 type Uint8Builder struct { 2189 builder 2190 2191 data *memory.Buffer 2192 rawData []uint8 2193 } 2194 2195 func NewUint8Builder(mem memory.Allocator) *Uint8Builder { 2196 return &Uint8Builder{builder: builder{refCount: 1, mem: mem}} 2197 } 2198 2199 func (b *Uint8Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Uint8 } 2200 2201 // Release decreases the reference count by 1. 2202 // When the reference count goes to zero, the memory is freed. 2203 func (b *Uint8Builder) Release() { 2204 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 2205 2206 if atomic.AddInt64(&b.refCount, -1) == 0 { 2207 if b.nullBitmap != nil { 2208 b.nullBitmap.Release() 2209 b.nullBitmap = nil 2210 } 2211 if b.data != nil { 2212 b.data.Release() 2213 b.data = nil 2214 b.rawData = nil 2215 } 2216 } 2217 } 2218 2219 func (b *Uint8Builder) Append(v uint8) { 2220 b.Reserve(1) 2221 b.UnsafeAppend(v) 2222 } 2223 2224 func (b *Uint8Builder) AppendNull() { 2225 b.Reserve(1) 2226 b.UnsafeAppendBoolToBitmap(false) 2227 } 2228 2229 func (b *Uint8Builder) AppendNulls(n int) { 2230 for i := 0; i < n; i++ { 2231 b.AppendNull() 2232 } 2233 } 2234 2235 func (b *Uint8Builder) AppendEmptyValue() { 2236 b.Append(0) 2237 } 2238 2239 func (b *Uint8Builder) AppendEmptyValues(n int) { 2240 for i := 0; i < n; i++ { 2241 b.AppendEmptyValue() 2242 } 2243 } 2244 2245 func (b *Uint8Builder) UnsafeAppend(v uint8) { 2246 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2247 b.rawData[b.length] = v 2248 b.length++ 2249 } 2250 2251 func (b *Uint8Builder) UnsafeAppendBoolToBitmap(isValid bool) { 2252 if isValid { 2253 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2254 } else { 2255 b.nulls++ 2256 } 2257 b.length++ 2258 } 2259 2260 // AppendValues will append the values in the v slice. The valid slice determines which values 2261 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 2262 // all values in v are appended and considered valid. 2263 func (b *Uint8Builder) AppendValues(v []uint8, valid []bool) { 2264 if len(v) != len(valid) && len(valid) != 0 { 2265 panic("len(v) != len(valid) && len(valid) != 0") 2266 } 2267 2268 if len(v) == 0 { 2269 return 2270 } 2271 2272 b.Reserve(len(v)) 2273 arrow.Uint8Traits.Copy(b.rawData[b.length:], v) 2274 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 2275 } 2276 2277 func (b *Uint8Builder) init(capacity int) { 2278 b.builder.init(capacity) 2279 2280 b.data = memory.NewResizableBuffer(b.mem) 2281 bytesN := arrow.Uint8Traits.BytesRequired(capacity) 2282 b.data.Resize(bytesN) 2283 b.rawData = arrow.Uint8Traits.CastFromBytes(b.data.Bytes()) 2284 } 2285 2286 // Reserve ensures there is enough space for appending n elements 2287 // by checking the capacity and calling Resize if necessary. 2288 func (b *Uint8Builder) Reserve(n int) { 2289 b.builder.reserve(n, b.Resize) 2290 } 2291 2292 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 2293 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 2294 func (b *Uint8Builder) Resize(n int) { 2295 nBuilder := n 2296 if n < minBuilderCapacity { 2297 n = minBuilderCapacity 2298 } 2299 2300 if b.capacity == 0 { 2301 b.init(n) 2302 } else { 2303 b.builder.resize(nBuilder, b.init) 2304 b.data.Resize(arrow.Uint8Traits.BytesRequired(n)) 2305 b.rawData = arrow.Uint8Traits.CastFromBytes(b.data.Bytes()) 2306 } 2307 } 2308 2309 func (b *Uint8Builder) Value(i int) uint8 { 2310 return b.rawData[i] 2311 } 2312 2313 // NewArray creates a Uint8 array from the memory buffers used by the builder and resets the Uint8Builder 2314 // so it can be used to build a new array. 2315 func (b *Uint8Builder) NewArray() arrow.Array { 2316 return b.NewUint8Array() 2317 } 2318 2319 // NewUint8Array creates a Uint8 array from the memory buffers used by the builder and resets the Uint8Builder 2320 // so it can be used to build a new array. 2321 func (b *Uint8Builder) NewUint8Array() (a *Uint8) { 2322 data := b.newData() 2323 a = NewUint8Data(data) 2324 data.Release() 2325 return 2326 } 2327 2328 func (b *Uint8Builder) newData() (data *Data) { 2329 bytesRequired := arrow.Uint8Traits.BytesRequired(b.length) 2330 if bytesRequired > 0 && bytesRequired < b.data.Len() { 2331 // trim buffers 2332 b.data.Resize(bytesRequired) 2333 } 2334 data = NewData(arrow.PrimitiveTypes.Uint8, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 2335 b.reset() 2336 2337 if b.data != nil { 2338 b.data.Release() 2339 b.data = nil 2340 b.rawData = nil 2341 } 2342 2343 return 2344 } 2345 2346 func (b *Uint8Builder) AppendValueFromString(s string) error { 2347 if s == NullValueStr { 2348 b.AppendNull() 2349 return nil 2350 } 2351 v, err := strconv.ParseUint(s, 10, 1*8) 2352 if err != nil { 2353 b.AppendNull() 2354 return err 2355 } 2356 b.Append(uint8(v)) 2357 return nil 2358 } 2359 2360 func (b *Uint8Builder) UnmarshalOne(dec *json.Decoder) error { 2361 t, err := dec.Token() 2362 if err != nil { 2363 return err 2364 } 2365 2366 switch v := t.(type) { 2367 case nil: 2368 b.AppendNull() 2369 2370 case string: 2371 f, err := strconv.ParseUint(v, 10, 1*8) 2372 if err != nil { 2373 return &json.UnmarshalTypeError{ 2374 Value: v, 2375 Type: reflect.TypeOf(uint8(0)), 2376 Offset: dec.InputOffset(), 2377 } 2378 } 2379 b.Append(uint8(f)) 2380 case float64: 2381 b.Append(uint8(v)) 2382 case json.Number: 2383 f, err := strconv.ParseUint(v.String(), 10, 1*8) 2384 if err != nil { 2385 return &json.UnmarshalTypeError{ 2386 Value: v.String(), 2387 Type: reflect.TypeOf(uint8(0)), 2388 Offset: dec.InputOffset(), 2389 } 2390 } 2391 b.Append(uint8(f)) 2392 2393 default: 2394 return &json.UnmarshalTypeError{ 2395 Value: fmt.Sprint(t), 2396 Type: reflect.TypeOf(uint8(0)), 2397 Offset: dec.InputOffset(), 2398 } 2399 } 2400 2401 return nil 2402 } 2403 2404 func (b *Uint8Builder) Unmarshal(dec *json.Decoder) error { 2405 for dec.More() { 2406 if err := b.UnmarshalOne(dec); err != nil { 2407 return err 2408 } 2409 } 2410 return nil 2411 } 2412 2413 func (b *Uint8Builder) UnmarshalJSON(data []byte) error { 2414 dec := json.NewDecoder(bytes.NewReader(data)) 2415 t, err := dec.Token() 2416 if err != nil { 2417 return err 2418 } 2419 2420 if delim, ok := t.(json.Delim); !ok || delim != '[' { 2421 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 2422 } 2423 2424 return b.Unmarshal(dec) 2425 } 2426 2427 type Time32Builder struct { 2428 builder 2429 2430 dtype *arrow.Time32Type 2431 data *memory.Buffer 2432 rawData []arrow.Time32 2433 } 2434 2435 func NewTime32Builder(mem memory.Allocator, dtype *arrow.Time32Type) *Time32Builder { 2436 return &Time32Builder{builder: builder{refCount: 1, mem: mem}, dtype: dtype} 2437 } 2438 2439 func (b *Time32Builder) Type() arrow.DataType { return b.dtype } 2440 2441 // Release decreases the reference count by 1. 2442 // When the reference count goes to zero, the memory is freed. 2443 func (b *Time32Builder) Release() { 2444 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 2445 2446 if atomic.AddInt64(&b.refCount, -1) == 0 { 2447 if b.nullBitmap != nil { 2448 b.nullBitmap.Release() 2449 b.nullBitmap = nil 2450 } 2451 if b.data != nil { 2452 b.data.Release() 2453 b.data = nil 2454 b.rawData = nil 2455 } 2456 } 2457 } 2458 2459 func (b *Time32Builder) Append(v arrow.Time32) { 2460 b.Reserve(1) 2461 b.UnsafeAppend(v) 2462 } 2463 2464 func (b *Time32Builder) AppendNull() { 2465 b.Reserve(1) 2466 b.UnsafeAppendBoolToBitmap(false) 2467 } 2468 2469 func (b *Time32Builder) AppendNulls(n int) { 2470 for i := 0; i < n; i++ { 2471 b.AppendNull() 2472 } 2473 } 2474 2475 func (b *Time32Builder) AppendEmptyValue() { 2476 b.Append(0) 2477 } 2478 2479 func (b *Time32Builder) AppendEmptyValues(n int) { 2480 for i := 0; i < n; i++ { 2481 b.AppendEmptyValue() 2482 } 2483 } 2484 2485 func (b *Time32Builder) UnsafeAppend(v arrow.Time32) { 2486 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2487 b.rawData[b.length] = v 2488 b.length++ 2489 } 2490 2491 func (b *Time32Builder) UnsafeAppendBoolToBitmap(isValid bool) { 2492 if isValid { 2493 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2494 } else { 2495 b.nulls++ 2496 } 2497 b.length++ 2498 } 2499 2500 // AppendValues will append the values in the v slice. The valid slice determines which values 2501 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 2502 // all values in v are appended and considered valid. 2503 func (b *Time32Builder) AppendValues(v []arrow.Time32, valid []bool) { 2504 if len(v) != len(valid) && len(valid) != 0 { 2505 panic("len(v) != len(valid) && len(valid) != 0") 2506 } 2507 2508 if len(v) == 0 { 2509 return 2510 } 2511 2512 b.Reserve(len(v)) 2513 arrow.Time32Traits.Copy(b.rawData[b.length:], v) 2514 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 2515 } 2516 2517 func (b *Time32Builder) init(capacity int) { 2518 b.builder.init(capacity) 2519 2520 b.data = memory.NewResizableBuffer(b.mem) 2521 bytesN := arrow.Time32Traits.BytesRequired(capacity) 2522 b.data.Resize(bytesN) 2523 b.rawData = arrow.Time32Traits.CastFromBytes(b.data.Bytes()) 2524 } 2525 2526 // Reserve ensures there is enough space for appending n elements 2527 // by checking the capacity and calling Resize if necessary. 2528 func (b *Time32Builder) Reserve(n int) { 2529 b.builder.reserve(n, b.Resize) 2530 } 2531 2532 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 2533 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 2534 func (b *Time32Builder) Resize(n int) { 2535 nBuilder := n 2536 if n < minBuilderCapacity { 2537 n = minBuilderCapacity 2538 } 2539 2540 if b.capacity == 0 { 2541 b.init(n) 2542 } else { 2543 b.builder.resize(nBuilder, b.init) 2544 b.data.Resize(arrow.Time32Traits.BytesRequired(n)) 2545 b.rawData = arrow.Time32Traits.CastFromBytes(b.data.Bytes()) 2546 } 2547 } 2548 2549 func (b *Time32Builder) Value(i int) arrow.Time32 { 2550 return b.rawData[i] 2551 } 2552 2553 // NewArray creates a Time32 array from the memory buffers used by the builder and resets the Time32Builder 2554 // so it can be used to build a new array. 2555 func (b *Time32Builder) NewArray() arrow.Array { 2556 return b.NewTime32Array() 2557 } 2558 2559 // NewTime32Array creates a Time32 array from the memory buffers used by the builder and resets the Time32Builder 2560 // so it can be used to build a new array. 2561 func (b *Time32Builder) NewTime32Array() (a *Time32) { 2562 data := b.newData() 2563 a = NewTime32Data(data) 2564 data.Release() 2565 return 2566 } 2567 2568 func (b *Time32Builder) newData() (data *Data) { 2569 bytesRequired := arrow.Time32Traits.BytesRequired(b.length) 2570 if bytesRequired > 0 && bytesRequired < b.data.Len() { 2571 // trim buffers 2572 b.data.Resize(bytesRequired) 2573 } 2574 data = NewData(b.dtype, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 2575 b.reset() 2576 2577 if b.data != nil { 2578 b.data.Release() 2579 b.data = nil 2580 b.rawData = nil 2581 } 2582 2583 return 2584 } 2585 2586 func (b *Time32Builder) AppendValueFromString(s string) error { 2587 if s == NullValueStr { 2588 b.AppendNull() 2589 return nil 2590 } 2591 val, err := arrow.Time32FromString(s, b.dtype.Unit) 2592 if err != nil { 2593 b.AppendNull() 2594 return err 2595 } 2596 b.Append(val) 2597 return nil 2598 } 2599 2600 func (b *Time32Builder) UnmarshalOne(dec *json.Decoder) error { 2601 t, err := dec.Token() 2602 if err != nil { 2603 return err 2604 } 2605 2606 switch v := t.(type) { 2607 case nil: 2608 b.AppendNull() 2609 case string: 2610 tm, err := arrow.Time32FromString(v, b.dtype.Unit) 2611 if err != nil { 2612 return &json.UnmarshalTypeError{ 2613 Value: v, 2614 Type: reflect.TypeOf(arrow.Time32(0)), 2615 Offset: dec.InputOffset(), 2616 } 2617 } 2618 2619 b.Append(tm) 2620 case json.Number: 2621 n, err := v.Int64() 2622 if err != nil { 2623 return &json.UnmarshalTypeError{ 2624 Value: v.String(), 2625 Type: reflect.TypeOf(arrow.Time32(0)), 2626 Offset: dec.InputOffset(), 2627 } 2628 } 2629 b.Append(arrow.Time32(n)) 2630 case float64: 2631 b.Append(arrow.Time32(v)) 2632 2633 default: 2634 return &json.UnmarshalTypeError{ 2635 Value: fmt.Sprint(t), 2636 Type: reflect.TypeOf(arrow.Time32(0)), 2637 Offset: dec.InputOffset(), 2638 } 2639 } 2640 2641 return nil 2642 } 2643 2644 func (b *Time32Builder) Unmarshal(dec *json.Decoder) error { 2645 for dec.More() { 2646 if err := b.UnmarshalOne(dec); err != nil { 2647 return err 2648 } 2649 } 2650 return nil 2651 } 2652 2653 func (b *Time32Builder) UnmarshalJSON(data []byte) error { 2654 dec := json.NewDecoder(bytes.NewReader(data)) 2655 t, err := dec.Token() 2656 if err != nil { 2657 return err 2658 } 2659 2660 if delim, ok := t.(json.Delim); !ok || delim != '[' { 2661 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 2662 } 2663 2664 return b.Unmarshal(dec) 2665 } 2666 2667 type Time64Builder struct { 2668 builder 2669 2670 dtype *arrow.Time64Type 2671 data *memory.Buffer 2672 rawData []arrow.Time64 2673 } 2674 2675 func NewTime64Builder(mem memory.Allocator, dtype *arrow.Time64Type) *Time64Builder { 2676 return &Time64Builder{builder: builder{refCount: 1, mem: mem}, dtype: dtype} 2677 } 2678 2679 func (b *Time64Builder) Type() arrow.DataType { return b.dtype } 2680 2681 // Release decreases the reference count by 1. 2682 // When the reference count goes to zero, the memory is freed. 2683 func (b *Time64Builder) Release() { 2684 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 2685 2686 if atomic.AddInt64(&b.refCount, -1) == 0 { 2687 if b.nullBitmap != nil { 2688 b.nullBitmap.Release() 2689 b.nullBitmap = nil 2690 } 2691 if b.data != nil { 2692 b.data.Release() 2693 b.data = nil 2694 b.rawData = nil 2695 } 2696 } 2697 } 2698 2699 func (b *Time64Builder) Append(v arrow.Time64) { 2700 b.Reserve(1) 2701 b.UnsafeAppend(v) 2702 } 2703 2704 func (b *Time64Builder) AppendNull() { 2705 b.Reserve(1) 2706 b.UnsafeAppendBoolToBitmap(false) 2707 } 2708 2709 func (b *Time64Builder) AppendNulls(n int) { 2710 for i := 0; i < n; i++ { 2711 b.AppendNull() 2712 } 2713 } 2714 2715 func (b *Time64Builder) AppendEmptyValue() { 2716 b.Append(0) 2717 } 2718 2719 func (b *Time64Builder) AppendEmptyValues(n int) { 2720 for i := 0; i < n; i++ { 2721 b.AppendEmptyValue() 2722 } 2723 } 2724 2725 func (b *Time64Builder) UnsafeAppend(v arrow.Time64) { 2726 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2727 b.rawData[b.length] = v 2728 b.length++ 2729 } 2730 2731 func (b *Time64Builder) UnsafeAppendBoolToBitmap(isValid bool) { 2732 if isValid { 2733 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2734 } else { 2735 b.nulls++ 2736 } 2737 b.length++ 2738 } 2739 2740 // AppendValues will append the values in the v slice. The valid slice determines which values 2741 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 2742 // all values in v are appended and considered valid. 2743 func (b *Time64Builder) AppendValues(v []arrow.Time64, valid []bool) { 2744 if len(v) != len(valid) && len(valid) != 0 { 2745 panic("len(v) != len(valid) && len(valid) != 0") 2746 } 2747 2748 if len(v) == 0 { 2749 return 2750 } 2751 2752 b.Reserve(len(v)) 2753 arrow.Time64Traits.Copy(b.rawData[b.length:], v) 2754 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 2755 } 2756 2757 func (b *Time64Builder) init(capacity int) { 2758 b.builder.init(capacity) 2759 2760 b.data = memory.NewResizableBuffer(b.mem) 2761 bytesN := arrow.Time64Traits.BytesRequired(capacity) 2762 b.data.Resize(bytesN) 2763 b.rawData = arrow.Time64Traits.CastFromBytes(b.data.Bytes()) 2764 } 2765 2766 // Reserve ensures there is enough space for appending n elements 2767 // by checking the capacity and calling Resize if necessary. 2768 func (b *Time64Builder) Reserve(n int) { 2769 b.builder.reserve(n, b.Resize) 2770 } 2771 2772 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 2773 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 2774 func (b *Time64Builder) Resize(n int) { 2775 nBuilder := n 2776 if n < minBuilderCapacity { 2777 n = minBuilderCapacity 2778 } 2779 2780 if b.capacity == 0 { 2781 b.init(n) 2782 } else { 2783 b.builder.resize(nBuilder, b.init) 2784 b.data.Resize(arrow.Time64Traits.BytesRequired(n)) 2785 b.rawData = arrow.Time64Traits.CastFromBytes(b.data.Bytes()) 2786 } 2787 } 2788 2789 func (b *Time64Builder) Value(i int) arrow.Time64 { 2790 return b.rawData[i] 2791 } 2792 2793 // NewArray creates a Time64 array from the memory buffers used by the builder and resets the Time64Builder 2794 // so it can be used to build a new array. 2795 func (b *Time64Builder) NewArray() arrow.Array { 2796 return b.NewTime64Array() 2797 } 2798 2799 // NewTime64Array creates a Time64 array from the memory buffers used by the builder and resets the Time64Builder 2800 // so it can be used to build a new array. 2801 func (b *Time64Builder) NewTime64Array() (a *Time64) { 2802 data := b.newData() 2803 a = NewTime64Data(data) 2804 data.Release() 2805 return 2806 } 2807 2808 func (b *Time64Builder) newData() (data *Data) { 2809 bytesRequired := arrow.Time64Traits.BytesRequired(b.length) 2810 if bytesRequired > 0 && bytesRequired < b.data.Len() { 2811 // trim buffers 2812 b.data.Resize(bytesRequired) 2813 } 2814 data = NewData(b.dtype, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 2815 b.reset() 2816 2817 if b.data != nil { 2818 b.data.Release() 2819 b.data = nil 2820 b.rawData = nil 2821 } 2822 2823 return 2824 } 2825 2826 func (b *Time64Builder) AppendValueFromString(s string) error { 2827 if s == NullValueStr { 2828 b.AppendNull() 2829 return nil 2830 } 2831 val, err := arrow.Time64FromString(s, b.dtype.Unit) 2832 if err != nil { 2833 b.AppendNull() 2834 return err 2835 } 2836 b.Append(val) 2837 return nil 2838 } 2839 2840 func (b *Time64Builder) UnmarshalOne(dec *json.Decoder) error { 2841 t, err := dec.Token() 2842 if err != nil { 2843 return err 2844 } 2845 2846 switch v := t.(type) { 2847 case nil: 2848 b.AppendNull() 2849 case string: 2850 tm, err := arrow.Time64FromString(v, b.dtype.Unit) 2851 if err != nil { 2852 return &json.UnmarshalTypeError{ 2853 Value: v, 2854 Type: reflect.TypeOf(arrow.Time64(0)), 2855 Offset: dec.InputOffset(), 2856 } 2857 } 2858 2859 b.Append(tm) 2860 case json.Number: 2861 n, err := v.Int64() 2862 if err != nil { 2863 return &json.UnmarshalTypeError{ 2864 Value: v.String(), 2865 Type: reflect.TypeOf(arrow.Time64(0)), 2866 Offset: dec.InputOffset(), 2867 } 2868 } 2869 b.Append(arrow.Time64(n)) 2870 case float64: 2871 b.Append(arrow.Time64(v)) 2872 2873 default: 2874 return &json.UnmarshalTypeError{ 2875 Value: fmt.Sprint(t), 2876 Type: reflect.TypeOf(arrow.Time64(0)), 2877 Offset: dec.InputOffset(), 2878 } 2879 } 2880 2881 return nil 2882 } 2883 2884 func (b *Time64Builder) Unmarshal(dec *json.Decoder) error { 2885 for dec.More() { 2886 if err := b.UnmarshalOne(dec); err != nil { 2887 return err 2888 } 2889 } 2890 return nil 2891 } 2892 2893 func (b *Time64Builder) UnmarshalJSON(data []byte) error { 2894 dec := json.NewDecoder(bytes.NewReader(data)) 2895 t, err := dec.Token() 2896 if err != nil { 2897 return err 2898 } 2899 2900 if delim, ok := t.(json.Delim); !ok || delim != '[' { 2901 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 2902 } 2903 2904 return b.Unmarshal(dec) 2905 } 2906 2907 type Date32Builder struct { 2908 builder 2909 2910 data *memory.Buffer 2911 rawData []arrow.Date32 2912 } 2913 2914 func NewDate32Builder(mem memory.Allocator) *Date32Builder { 2915 return &Date32Builder{builder: builder{refCount: 1, mem: mem}} 2916 } 2917 2918 func (b *Date32Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Date32 } 2919 2920 // Release decreases the reference count by 1. 2921 // When the reference count goes to zero, the memory is freed. 2922 func (b *Date32Builder) Release() { 2923 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 2924 2925 if atomic.AddInt64(&b.refCount, -1) == 0 { 2926 if b.nullBitmap != nil { 2927 b.nullBitmap.Release() 2928 b.nullBitmap = nil 2929 } 2930 if b.data != nil { 2931 b.data.Release() 2932 b.data = nil 2933 b.rawData = nil 2934 } 2935 } 2936 } 2937 2938 func (b *Date32Builder) Append(v arrow.Date32) { 2939 b.Reserve(1) 2940 b.UnsafeAppend(v) 2941 } 2942 2943 func (b *Date32Builder) AppendNull() { 2944 b.Reserve(1) 2945 b.UnsafeAppendBoolToBitmap(false) 2946 } 2947 2948 func (b *Date32Builder) AppendNulls(n int) { 2949 for i := 0; i < n; i++ { 2950 b.AppendNull() 2951 } 2952 } 2953 2954 func (b *Date32Builder) AppendEmptyValue() { 2955 b.Append(0) 2956 } 2957 2958 func (b *Date32Builder) AppendEmptyValues(n int) { 2959 for i := 0; i < n; i++ { 2960 b.AppendEmptyValue() 2961 } 2962 } 2963 2964 func (b *Date32Builder) UnsafeAppend(v arrow.Date32) { 2965 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2966 b.rawData[b.length] = v 2967 b.length++ 2968 } 2969 2970 func (b *Date32Builder) UnsafeAppendBoolToBitmap(isValid bool) { 2971 if isValid { 2972 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 2973 } else { 2974 b.nulls++ 2975 } 2976 b.length++ 2977 } 2978 2979 // AppendValues will append the values in the v slice. The valid slice determines which values 2980 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 2981 // all values in v are appended and considered valid. 2982 func (b *Date32Builder) AppendValues(v []arrow.Date32, valid []bool) { 2983 if len(v) != len(valid) && len(valid) != 0 { 2984 panic("len(v) != len(valid) && len(valid) != 0") 2985 } 2986 2987 if len(v) == 0 { 2988 return 2989 } 2990 2991 b.Reserve(len(v)) 2992 arrow.Date32Traits.Copy(b.rawData[b.length:], v) 2993 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 2994 } 2995 2996 func (b *Date32Builder) init(capacity int) { 2997 b.builder.init(capacity) 2998 2999 b.data = memory.NewResizableBuffer(b.mem) 3000 bytesN := arrow.Date32Traits.BytesRequired(capacity) 3001 b.data.Resize(bytesN) 3002 b.rawData = arrow.Date32Traits.CastFromBytes(b.data.Bytes()) 3003 } 3004 3005 // Reserve ensures there is enough space for appending n elements 3006 // by checking the capacity and calling Resize if necessary. 3007 func (b *Date32Builder) Reserve(n int) { 3008 b.builder.reserve(n, b.Resize) 3009 } 3010 3011 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 3012 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 3013 func (b *Date32Builder) Resize(n int) { 3014 nBuilder := n 3015 if n < minBuilderCapacity { 3016 n = minBuilderCapacity 3017 } 3018 3019 if b.capacity == 0 { 3020 b.init(n) 3021 } else { 3022 b.builder.resize(nBuilder, b.init) 3023 b.data.Resize(arrow.Date32Traits.BytesRequired(n)) 3024 b.rawData = arrow.Date32Traits.CastFromBytes(b.data.Bytes()) 3025 } 3026 } 3027 3028 func (b *Date32Builder) Value(i int) arrow.Date32 { 3029 return b.rawData[i] 3030 } 3031 3032 // NewArray creates a Date32 array from the memory buffers used by the builder and resets the Date32Builder 3033 // so it can be used to build a new array. 3034 func (b *Date32Builder) NewArray() arrow.Array { 3035 return b.NewDate32Array() 3036 } 3037 3038 // NewDate32Array creates a Date32 array from the memory buffers used by the builder and resets the Date32Builder 3039 // so it can be used to build a new array. 3040 func (b *Date32Builder) NewDate32Array() (a *Date32) { 3041 data := b.newData() 3042 a = NewDate32Data(data) 3043 data.Release() 3044 return 3045 } 3046 3047 func (b *Date32Builder) newData() (data *Data) { 3048 bytesRequired := arrow.Date32Traits.BytesRequired(b.length) 3049 if bytesRequired > 0 && bytesRequired < b.data.Len() { 3050 // trim buffers 3051 b.data.Resize(bytesRequired) 3052 } 3053 data = NewData(arrow.PrimitiveTypes.Date32, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 3054 b.reset() 3055 3056 if b.data != nil { 3057 b.data.Release() 3058 b.data = nil 3059 b.rawData = nil 3060 } 3061 3062 return 3063 } 3064 3065 func (b *Date32Builder) AppendValueFromString(s string) error { 3066 if s == NullValueStr { 3067 b.AppendNull() 3068 return nil 3069 } 3070 tm, err := time.Parse("2006-01-02", s) 3071 if err != nil { 3072 b.AppendNull() 3073 return err 3074 } 3075 b.Append(arrow.Date32FromTime(tm)) 3076 return nil 3077 } 3078 3079 func (b *Date32Builder) UnmarshalOne(dec *json.Decoder) error { 3080 t, err := dec.Token() 3081 if err != nil { 3082 return err 3083 } 3084 3085 switch v := t.(type) { 3086 case nil: 3087 b.AppendNull() 3088 case string: 3089 tm, err := time.Parse("2006-01-02", v) 3090 if err != nil { 3091 return &json.UnmarshalTypeError{ 3092 Value: v, 3093 Type: reflect.TypeOf(arrow.Date32(0)), 3094 Offset: dec.InputOffset(), 3095 } 3096 } 3097 3098 b.Append(arrow.Date32FromTime(tm)) 3099 case json.Number: 3100 n, err := v.Int64() 3101 if err != nil { 3102 return &json.UnmarshalTypeError{ 3103 Value: v.String(), 3104 Type: reflect.TypeOf(arrow.Date32(0)), 3105 Offset: dec.InputOffset(), 3106 } 3107 } 3108 b.Append(arrow.Date32(n)) 3109 case float64: 3110 b.Append(arrow.Date32(v)) 3111 3112 default: 3113 return &json.UnmarshalTypeError{ 3114 Value: fmt.Sprint(t), 3115 Type: reflect.TypeOf(arrow.Date32(0)), 3116 Offset: dec.InputOffset(), 3117 } 3118 } 3119 3120 return nil 3121 } 3122 3123 func (b *Date32Builder) Unmarshal(dec *json.Decoder) error { 3124 for dec.More() { 3125 if err := b.UnmarshalOne(dec); err != nil { 3126 return err 3127 } 3128 } 3129 return nil 3130 } 3131 3132 func (b *Date32Builder) UnmarshalJSON(data []byte) error { 3133 dec := json.NewDecoder(bytes.NewReader(data)) 3134 t, err := dec.Token() 3135 if err != nil { 3136 return err 3137 } 3138 3139 if delim, ok := t.(json.Delim); !ok || delim != '[' { 3140 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 3141 } 3142 3143 return b.Unmarshal(dec) 3144 } 3145 3146 type Date64Builder struct { 3147 builder 3148 3149 data *memory.Buffer 3150 rawData []arrow.Date64 3151 } 3152 3153 func NewDate64Builder(mem memory.Allocator) *Date64Builder { 3154 return &Date64Builder{builder: builder{refCount: 1, mem: mem}} 3155 } 3156 3157 func (b *Date64Builder) Type() arrow.DataType { return arrow.PrimitiveTypes.Date64 } 3158 3159 // Release decreases the reference count by 1. 3160 // When the reference count goes to zero, the memory is freed. 3161 func (b *Date64Builder) Release() { 3162 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 3163 3164 if atomic.AddInt64(&b.refCount, -1) == 0 { 3165 if b.nullBitmap != nil { 3166 b.nullBitmap.Release() 3167 b.nullBitmap = nil 3168 } 3169 if b.data != nil { 3170 b.data.Release() 3171 b.data = nil 3172 b.rawData = nil 3173 } 3174 } 3175 } 3176 3177 func (b *Date64Builder) Append(v arrow.Date64) { 3178 b.Reserve(1) 3179 b.UnsafeAppend(v) 3180 } 3181 3182 func (b *Date64Builder) AppendNull() { 3183 b.Reserve(1) 3184 b.UnsafeAppendBoolToBitmap(false) 3185 } 3186 3187 func (b *Date64Builder) AppendNulls(n int) { 3188 for i := 0; i < n; i++ { 3189 b.AppendNull() 3190 } 3191 } 3192 3193 func (b *Date64Builder) AppendEmptyValue() { 3194 b.Append(0) 3195 } 3196 3197 func (b *Date64Builder) AppendEmptyValues(n int) { 3198 for i := 0; i < n; i++ { 3199 b.AppendEmptyValue() 3200 } 3201 } 3202 3203 func (b *Date64Builder) UnsafeAppend(v arrow.Date64) { 3204 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 3205 b.rawData[b.length] = v 3206 b.length++ 3207 } 3208 3209 func (b *Date64Builder) UnsafeAppendBoolToBitmap(isValid bool) { 3210 if isValid { 3211 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 3212 } else { 3213 b.nulls++ 3214 } 3215 b.length++ 3216 } 3217 3218 // AppendValues will append the values in the v slice. The valid slice determines which values 3219 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 3220 // all values in v are appended and considered valid. 3221 func (b *Date64Builder) AppendValues(v []arrow.Date64, valid []bool) { 3222 if len(v) != len(valid) && len(valid) != 0 { 3223 panic("len(v) != len(valid) && len(valid) != 0") 3224 } 3225 3226 if len(v) == 0 { 3227 return 3228 } 3229 3230 b.Reserve(len(v)) 3231 arrow.Date64Traits.Copy(b.rawData[b.length:], v) 3232 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 3233 } 3234 3235 func (b *Date64Builder) init(capacity int) { 3236 b.builder.init(capacity) 3237 3238 b.data = memory.NewResizableBuffer(b.mem) 3239 bytesN := arrow.Date64Traits.BytesRequired(capacity) 3240 b.data.Resize(bytesN) 3241 b.rawData = arrow.Date64Traits.CastFromBytes(b.data.Bytes()) 3242 } 3243 3244 // Reserve ensures there is enough space for appending n elements 3245 // by checking the capacity and calling Resize if necessary. 3246 func (b *Date64Builder) Reserve(n int) { 3247 b.builder.reserve(n, b.Resize) 3248 } 3249 3250 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 3251 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 3252 func (b *Date64Builder) Resize(n int) { 3253 nBuilder := n 3254 if n < minBuilderCapacity { 3255 n = minBuilderCapacity 3256 } 3257 3258 if b.capacity == 0 { 3259 b.init(n) 3260 } else { 3261 b.builder.resize(nBuilder, b.init) 3262 b.data.Resize(arrow.Date64Traits.BytesRequired(n)) 3263 b.rawData = arrow.Date64Traits.CastFromBytes(b.data.Bytes()) 3264 } 3265 } 3266 3267 func (b *Date64Builder) Value(i int) arrow.Date64 { 3268 return b.rawData[i] 3269 } 3270 3271 // NewArray creates a Date64 array from the memory buffers used by the builder and resets the Date64Builder 3272 // so it can be used to build a new array. 3273 func (b *Date64Builder) NewArray() arrow.Array { 3274 return b.NewDate64Array() 3275 } 3276 3277 // NewDate64Array creates a Date64 array from the memory buffers used by the builder and resets the Date64Builder 3278 // so it can be used to build a new array. 3279 func (b *Date64Builder) NewDate64Array() (a *Date64) { 3280 data := b.newData() 3281 a = NewDate64Data(data) 3282 data.Release() 3283 return 3284 } 3285 3286 func (b *Date64Builder) newData() (data *Data) { 3287 bytesRequired := arrow.Date64Traits.BytesRequired(b.length) 3288 if bytesRequired > 0 && bytesRequired < b.data.Len() { 3289 // trim buffers 3290 b.data.Resize(bytesRequired) 3291 } 3292 data = NewData(arrow.PrimitiveTypes.Date64, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 3293 b.reset() 3294 3295 if b.data != nil { 3296 b.data.Release() 3297 b.data = nil 3298 b.rawData = nil 3299 } 3300 3301 return 3302 } 3303 3304 func (b *Date64Builder) AppendValueFromString(s string) error { 3305 if s == NullValueStr { 3306 b.AppendNull() 3307 return nil 3308 } 3309 tm, err := time.Parse("2006-01-02", s) 3310 if err != nil { 3311 b.AppendNull() 3312 return err 3313 } 3314 b.Append(arrow.Date64FromTime(tm)) 3315 return nil 3316 } 3317 3318 func (b *Date64Builder) UnmarshalOne(dec *json.Decoder) error { 3319 t, err := dec.Token() 3320 if err != nil { 3321 return err 3322 } 3323 3324 switch v := t.(type) { 3325 case nil: 3326 b.AppendNull() 3327 case string: 3328 tm, err := time.Parse("2006-01-02", v) 3329 if err != nil { 3330 return &json.UnmarshalTypeError{ 3331 Value: v, 3332 Type: reflect.TypeOf(arrow.Date64(0)), 3333 Offset: dec.InputOffset(), 3334 } 3335 } 3336 3337 b.Append(arrow.Date64FromTime(tm)) 3338 case json.Number: 3339 n, err := v.Int64() 3340 if err != nil { 3341 return &json.UnmarshalTypeError{ 3342 Value: v.String(), 3343 Type: reflect.TypeOf(arrow.Date64(0)), 3344 Offset: dec.InputOffset(), 3345 } 3346 } 3347 b.Append(arrow.Date64(n)) 3348 case float64: 3349 b.Append(arrow.Date64(v)) 3350 3351 default: 3352 return &json.UnmarshalTypeError{ 3353 Value: fmt.Sprint(t), 3354 Type: reflect.TypeOf(arrow.Date64(0)), 3355 Offset: dec.InputOffset(), 3356 } 3357 } 3358 3359 return nil 3360 } 3361 3362 func (b *Date64Builder) Unmarshal(dec *json.Decoder) error { 3363 for dec.More() { 3364 if err := b.UnmarshalOne(dec); err != nil { 3365 return err 3366 } 3367 } 3368 return nil 3369 } 3370 3371 func (b *Date64Builder) UnmarshalJSON(data []byte) error { 3372 dec := json.NewDecoder(bytes.NewReader(data)) 3373 t, err := dec.Token() 3374 if err != nil { 3375 return err 3376 } 3377 3378 if delim, ok := t.(json.Delim); !ok || delim != '[' { 3379 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 3380 } 3381 3382 return b.Unmarshal(dec) 3383 } 3384 3385 type DurationBuilder struct { 3386 builder 3387 3388 dtype *arrow.DurationType 3389 data *memory.Buffer 3390 rawData []arrow.Duration 3391 } 3392 3393 func NewDurationBuilder(mem memory.Allocator, dtype *arrow.DurationType) *DurationBuilder { 3394 return &DurationBuilder{builder: builder{refCount: 1, mem: mem}, dtype: dtype} 3395 } 3396 3397 func (b *DurationBuilder) Type() arrow.DataType { return b.dtype } 3398 3399 // Release decreases the reference count by 1. 3400 // When the reference count goes to zero, the memory is freed. 3401 func (b *DurationBuilder) Release() { 3402 debug.Assert(atomic.LoadInt64(&b.refCount) > 0, "too many releases") 3403 3404 if atomic.AddInt64(&b.refCount, -1) == 0 { 3405 if b.nullBitmap != nil { 3406 b.nullBitmap.Release() 3407 b.nullBitmap = nil 3408 } 3409 if b.data != nil { 3410 b.data.Release() 3411 b.data = nil 3412 b.rawData = nil 3413 } 3414 } 3415 } 3416 3417 func (b *DurationBuilder) Append(v arrow.Duration) { 3418 b.Reserve(1) 3419 b.UnsafeAppend(v) 3420 } 3421 3422 func (b *DurationBuilder) AppendNull() { 3423 b.Reserve(1) 3424 b.UnsafeAppendBoolToBitmap(false) 3425 } 3426 3427 func (b *DurationBuilder) AppendNulls(n int) { 3428 for i := 0; i < n; i++ { 3429 b.AppendNull() 3430 } 3431 } 3432 3433 func (b *DurationBuilder) AppendEmptyValue() { 3434 b.Append(0) 3435 } 3436 3437 func (b *DurationBuilder) AppendEmptyValues(n int) { 3438 for i := 0; i < n; i++ { 3439 b.AppendEmptyValue() 3440 } 3441 } 3442 3443 func (b *DurationBuilder) UnsafeAppend(v arrow.Duration) { 3444 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 3445 b.rawData[b.length] = v 3446 b.length++ 3447 } 3448 3449 func (b *DurationBuilder) UnsafeAppendBoolToBitmap(isValid bool) { 3450 if isValid { 3451 bitutil.SetBit(b.nullBitmap.Bytes(), b.length) 3452 } else { 3453 b.nulls++ 3454 } 3455 b.length++ 3456 } 3457 3458 // AppendValues will append the values in the v slice. The valid slice determines which values 3459 // in v are valid (not null). The valid slice must either be empty or be equal in length to v. If empty, 3460 // all values in v are appended and considered valid. 3461 func (b *DurationBuilder) AppendValues(v []arrow.Duration, valid []bool) { 3462 if len(v) != len(valid) && len(valid) != 0 { 3463 panic("len(v) != len(valid) && len(valid) != 0") 3464 } 3465 3466 if len(v) == 0 { 3467 return 3468 } 3469 3470 b.Reserve(len(v)) 3471 arrow.DurationTraits.Copy(b.rawData[b.length:], v) 3472 b.builder.unsafeAppendBoolsToBitmap(valid, len(v)) 3473 } 3474 3475 func (b *DurationBuilder) init(capacity int) { 3476 b.builder.init(capacity) 3477 3478 b.data = memory.NewResizableBuffer(b.mem) 3479 bytesN := arrow.DurationTraits.BytesRequired(capacity) 3480 b.data.Resize(bytesN) 3481 b.rawData = arrow.DurationTraits.CastFromBytes(b.data.Bytes()) 3482 } 3483 3484 // Reserve ensures there is enough space for appending n elements 3485 // by checking the capacity and calling Resize if necessary. 3486 func (b *DurationBuilder) Reserve(n int) { 3487 b.builder.reserve(n, b.Resize) 3488 } 3489 3490 // Resize adjusts the space allocated by b to n elements. If n is greater than b.Cap(), 3491 // additional memory will be allocated. If n is smaller, the allocated memory may reduced. 3492 func (b *DurationBuilder) Resize(n int) { 3493 nBuilder := n 3494 if n < minBuilderCapacity { 3495 n = minBuilderCapacity 3496 } 3497 3498 if b.capacity == 0 { 3499 b.init(n) 3500 } else { 3501 b.builder.resize(nBuilder, b.init) 3502 b.data.Resize(arrow.DurationTraits.BytesRequired(n)) 3503 b.rawData = arrow.DurationTraits.CastFromBytes(b.data.Bytes()) 3504 } 3505 } 3506 3507 func (b *DurationBuilder) Value(i int) arrow.Duration { 3508 return b.rawData[i] 3509 } 3510 3511 // NewArray creates a Duration array from the memory buffers used by the builder and resets the DurationBuilder 3512 // so it can be used to build a new array. 3513 func (b *DurationBuilder) NewArray() arrow.Array { 3514 return b.NewDurationArray() 3515 } 3516 3517 // NewDurationArray creates a Duration array from the memory buffers used by the builder and resets the DurationBuilder 3518 // so it can be used to build a new array. 3519 func (b *DurationBuilder) NewDurationArray() (a *Duration) { 3520 data := b.newData() 3521 a = NewDurationData(data) 3522 data.Release() 3523 return 3524 } 3525 3526 func (b *DurationBuilder) newData() (data *Data) { 3527 bytesRequired := arrow.DurationTraits.BytesRequired(b.length) 3528 if bytesRequired > 0 && bytesRequired < b.data.Len() { 3529 // trim buffers 3530 b.data.Resize(bytesRequired) 3531 } 3532 data = NewData(b.dtype, b.length, []*memory.Buffer{b.nullBitmap, b.data}, nil, b.nulls, 0) 3533 b.reset() 3534 3535 if b.data != nil { 3536 b.data.Release() 3537 b.data = nil 3538 b.rawData = nil 3539 } 3540 3541 return 3542 } 3543 3544 func (b *DurationBuilder) AppendValueFromString(s string) error { 3545 if s == NullValueStr { 3546 b.AppendNull() 3547 return nil 3548 } 3549 dur, err := time.ParseDuration(s) 3550 if err != nil { 3551 return err 3552 } 3553 3554 b.Append(arrow.Duration(dur / b.dtype.Unit.Multiplier())) 3555 return nil 3556 } 3557 3558 func (b *DurationBuilder) UnmarshalOne(dec *json.Decoder) error { 3559 t, err := dec.Token() 3560 if err != nil { 3561 return err 3562 } 3563 3564 switch v := t.(type) { 3565 case nil: 3566 b.AppendNull() 3567 case json.Number: 3568 n, err := v.Int64() 3569 if err != nil { 3570 return &json.UnmarshalTypeError{ 3571 Value: v.String(), 3572 Type: reflect.TypeOf(arrow.Duration(0)), 3573 Offset: dec.InputOffset(), 3574 } 3575 } 3576 b.Append(arrow.Duration(n)) 3577 case float64: 3578 b.Append(arrow.Duration(v)) 3579 case string: 3580 // be flexible for specifying durations by accepting forms like 3581 // 3h2m0.5s regardless of the unit and converting it to the proper 3582 // precision. 3583 val, err := time.ParseDuration(v) 3584 if err != nil { 3585 // if we got an error, maybe it was because the attempt to create 3586 // a time.Duration (int64) in nanoseconds would overflow. check if 3587 // the string is just a large number followed by the unit suffix 3588 if strings.HasSuffix(v, b.dtype.Unit.String()) { 3589 value, err := strconv.ParseInt(v[:len(v)-len(b.dtype.Unit.String())], 10, 64) 3590 if err == nil { 3591 b.Append(arrow.Duration(value)) 3592 break 3593 } 3594 } 3595 3596 return &json.UnmarshalTypeError{ 3597 Value: v, 3598 Type: reflect.TypeOf(arrow.Duration(0)), 3599 Offset: dec.InputOffset(), 3600 } 3601 } 3602 3603 switch b.dtype.Unit { 3604 case arrow.Nanosecond: 3605 b.Append(arrow.Duration(val.Nanoseconds())) 3606 case arrow.Microsecond: 3607 b.Append(arrow.Duration(val.Microseconds())) 3608 case arrow.Millisecond: 3609 b.Append(arrow.Duration(val.Milliseconds())) 3610 case arrow.Second: 3611 b.Append(arrow.Duration(val.Seconds())) 3612 } 3613 3614 default: 3615 return &json.UnmarshalTypeError{ 3616 Value: fmt.Sprint(t), 3617 Type: reflect.TypeOf(arrow.Duration(0)), 3618 Offset: dec.InputOffset(), 3619 } 3620 } 3621 3622 return nil 3623 } 3624 3625 func (b *DurationBuilder) Unmarshal(dec *json.Decoder) error { 3626 for dec.More() { 3627 if err := b.UnmarshalOne(dec); err != nil { 3628 return err 3629 } 3630 } 3631 return nil 3632 } 3633 3634 func (b *DurationBuilder) UnmarshalJSON(data []byte) error { 3635 dec := json.NewDecoder(bytes.NewReader(data)) 3636 t, err := dec.Token() 3637 if err != nil { 3638 return err 3639 } 3640 3641 if delim, ok := t.(json.Delim); !ok || delim != '[' { 3642 return fmt.Errorf("binary builder must unpack from json array, found %s", delim) 3643 } 3644 3645 return b.Unmarshal(dec) 3646 } 3647 3648 var ( 3649 _ Builder = (*Int64Builder)(nil) 3650 _ Builder = (*Uint64Builder)(nil) 3651 _ Builder = (*Float64Builder)(nil) 3652 _ Builder = (*Int32Builder)(nil) 3653 _ Builder = (*Uint32Builder)(nil) 3654 _ Builder = (*Float32Builder)(nil) 3655 _ Builder = (*Int16Builder)(nil) 3656 _ Builder = (*Uint16Builder)(nil) 3657 _ Builder = (*Int8Builder)(nil) 3658 _ Builder = (*Uint8Builder)(nil) 3659 _ Builder = (*Time32Builder)(nil) 3660 _ Builder = (*Time64Builder)(nil) 3661 _ Builder = (*Date32Builder)(nil) 3662 _ Builder = (*Date64Builder)(nil) 3663 _ Builder = (*DurationBuilder)(nil) 3664 )