1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package csv
import (
"encoding/base64"
"encoding/csv"
"errors"
"fmt"
"io"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/decimal128"
"github.com/apache/arrow-go/v18/arrow/decimal256"
"github.com/apache/arrow-go/v18/arrow/float16"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
)
// Reader wraps encoding/csv.Reader and creates array.Records from a schema.
type Reader struct {
r *csv.Reader
schema *arrow.Schema
refs int64
bld *array.RecordBuilder
cur arrow.Record
err error
chunk int
done bool
next func() bool
mem memory.Allocator
header bool
once sync.Once
fieldConverter []func(val string)
columnFilter []string
columnTypes map[string]arrow.DataType
conversions []conversionColumn
stringsCanBeNull bool
nulls []string
}
// NewInferringReader creates a CSV reader that attempts to infer the types
// and column names from the data in the first row of the CSV file.
//
// This can be further customized using the WithColumnTypes and
// WithIncludeColumns options.
// For BinaryType the reader will use base64 decoding with padding as per base64.StdDecoding.
func NewInferringReader(r io.Reader, opts ...Option) *Reader {
rr := &Reader{
r: csv.NewReader(r),
refs: 1,
chunk: 1,
stringsCanBeNull: false,
}
rr.r.ReuseRecord = true
for _, opt := range opts {
opt(rr)
}
if rr.mem == nil {
rr.mem = memory.DefaultAllocator
}
switch {
case rr.chunk < 0:
rr.next = rr.nextall
case rr.chunk > 1:
rr.next = rr.nextn
default:
rr.next = rr.next1
}
return rr
}
// NewReader returns a reader that reads from the CSV file and creates
// arrow.Records from the given schema.
//
// NewReader panics if the given schema contains fields that have types that are not
// primitive types.
func NewReader(r io.Reader, schema *arrow.Schema, opts ...Option) *Reader {
validate(schema)
rr := &Reader{
r: csv.NewReader(r),
schema: schema,
refs: 1,
chunk: 1,
stringsCanBeNull: false,
}
rr.r.ReuseRecord = true
for _, opt := range opts {
opt(rr)
}
if rr.mem == nil {
rr.mem = memory.DefaultAllocator
}
rr.bld = array.NewRecordBuilder(rr.mem, rr.schema)
switch {
case rr.chunk < 0:
rr.next = rr.nextall
case rr.chunk > 1:
rr.next = rr.nextn
default:
rr.next = rr.next1
}
return rr
}
func (r *Reader) readHeader() error {
// if we have an explicit schema and we want to skip the header
// then just return and do everything normally
if r.schema != nil && !r.header {
return nil
}
// either we need this first line for the header line
// or we are going to need this line to infer types
records, err := r.r.Read()
if err != nil {
return fmt.Errorf("arrow/csv: could not read header from file: %w", err)
}
// if we have an explicit schema, then r.header must be true otherwise
// we would have skipped this via the first line of this func
if r.schema != nil {
if len(records) != len(r.schema.Fields()) {
return ErrMismatchFields
}
fields := make([]arrow.Field, len(records))
for idx, name := range records {
fields[idx] = r.schema.Field(idx)
fields[idx].Name = name
}
meta := r.schema.Metadata()
r.schema = arrow.NewSchema(fields, &meta)
r.bld = array.NewRecordBuilder(r.mem, r.schema)
return nil
}
// we're going to need to infer some column types
r.conversions = make([]conversionColumn, 0, len(records))
if len(r.columnFilter) == 0 {
for i, rec := range records {
// if we are skipping the header, autogenerate field names
// using "f<n>" e.g. f0, f1, ....
if !r.header {
rec = fmt.Sprintf("f%d", i)
}
var dt arrow.DataType
if len(r.columnTypes) > 0 {
dt = r.columnTypes[rec]
}
r.conversions = append(r.conversions, conversionColumn{name: rec, index: i, typ: dt})
}
} else {
// include columns from columnFilter (in that order)
// compute the indices of columns in the csv file
colIndices := make(map[string]int)
for i, n := range records {
// if we are skipping the header, autogenerate field names
// using "f<n>" e.g. f0, f1, ....
if !r.header {
n = fmt.Sprintf("f%d", i)
}
colIndices[n] = i
}
for _, n := range r.columnFilter {
idx, ok := colIndices[n]
if !ok {
return fmt.Errorf("%w: column '%s' in included columns, but doesn't exist in CSV file",
ErrMismatchFields, n)
}
var dt arrow.DataType
if len(r.columnTypes) > 0 {
dt = r.columnTypes[n]
}
r.conversions = append(r.conversions, conversionColumn{name: n, index: idx, typ: dt})
}
r.columnFilter = nil
}
r.columnTypes = nil
return nil
}
// Err returns the last error encountered during the iteration over the
// underlying CSV file.
func (r *Reader) Err() error { return r.err }
func (r *Reader) Schema() *arrow.Schema { return r.schema }
// Record returns the current record that has been extracted from the
// underlying CSV file.
// It is valid until the next call to Next.
func (r *Reader) Record() arrow.Record { return r.cur }
// Next returns whether a Record could be extracted from the underlying CSV file.
//
// Next panics if the number of records extracted from a CSV row does not match
// the number of fields of the associated schema. If a parse failure occurs, Next
// will return true and the Record will contain nulls where failures occurred.
// Subsequent calls to Next will return false - The user should check Err() after
// each call to Next to check if an error took place.
func (r *Reader) Next() bool {
r.once.Do(func() {
r.err = r.readHeader()
if r.err == nil && r.schema != nil {
// Create a table of functions that will parse columns. This optimization
// allows us to specialize the implementation of each column's decoding
// and hoist type-based branches outside the inner loop.
r.fieldConverter = make([]func(string), len(r.schema.Fields()))
for idx := range r.schema.Fields() {
r.fieldConverter[idx] = r.initFieldConverter(r.bld.Field(idx))
}
}
})
if r.cur != nil {
r.cur.Release()
r.cur = nil
}
if r.err != nil || r.done {
return false
}
return r.next()
}
// next1 reads one row from the CSV file and creates a single Record
// from that row.
func (r *Reader) next1() bool {
var recs []string
recs, r.err = r.r.Read()
if r.err != nil {
r.done = true
if errors.Is(r.err, io.EOF) {
r.err = nil
}
return false
}
r.validate(recs)
r.read(recs)
r.cur = r.bld.NewRecord()
return true
}
// nextall reads the whole CSV file into memory and creates one single
// Record from all the CSV rows.
func (r *Reader) nextall() bool {
defer func() {
r.done = true
}()
var (
recs [][]string
)
recs, r.err = r.r.ReadAll()
if r.err != nil {
return false
}
for _, rec := range recs {
r.validate(rec)
r.read(rec)
}
r.cur = r.bld.NewRecord()
return true
}
// nextn reads n rows from the CSV file, where n is the chunk size, and creates
// a Record from these rows.
func (r *Reader) nextn() bool {
var (
recs []string
n = 0
err error
)
for i := 0; i < r.chunk && !r.done; i++ {
recs, err = r.r.Read()
if err != nil {
if !errors.Is(err, io.EOF) {
r.err = err
}
r.done = true
break
}
r.validate(recs)
r.read(recs)
n++
}
if r.err != nil {
r.done = true
}
r.cur = r.bld.NewRecord()
return n > 0
}
func (r *Reader) validate(recs []string) {
if r.err != nil {
return
}
if r.bld == nil {
// initialize the record builder in the case where we're inferring a schema
r.fieldConverter = make([]func(val string), len(recs))
fieldList := make([]arrow.Field, len(r.conversions))
for idx, cc := range r.conversions {
fieldList[idx].Name = cc.name
fieldList[idx].Nullable = true
fieldList[idx].Type = cc.inferType(recs[cc.index])
}
r.schema = arrow.NewSchema(fieldList, nil)
r.bld = array.NewRecordBuilder(r.mem, r.schema)
for idx, cc := range r.conversions {
r.fieldConverter[cc.index] = r.initFieldConverter(r.bld.Field(idx))
}
for idx, fc := range r.fieldConverter {
if fc == nil {
r.fieldConverter[idx] = func(string) {}
}
}
}
if len(recs) != len(r.fieldConverter) {
r.err = ErrMismatchFields
return
}
}
func (r *Reader) isNull(val string) bool {
for _, v := range r.nulls {
if v == val {
return true
}
}
return false
}
func (r *Reader) read(recs []string) {
for i, str := range recs {
r.fieldConverter[i](str)
}
}
func (r *Reader) initFieldConverter(bldr array.Builder) func(string) {
switch dt := bldr.Type().(type) {
case *arrow.BooleanType:
return func(str string) {
r.parseBool(bldr, str)
}
case *arrow.Int8Type:
return func(str string) {
r.parseInt8(bldr, str)
}
case *arrow.Int16Type:
return func(str string) {
r.parseInt16(bldr, str)
}
case *arrow.Int32Type:
return func(str string) {
r.parseInt32(bldr, str)
}
case *arrow.Int64Type:
return func(str string) {
r.parseInt64(bldr, str)
}
case *arrow.Uint8Type:
return func(str string) {
r.parseUint8(bldr, str)
}
case *arrow.Uint16Type:
return func(str string) {
r.parseUint16(bldr, str)
}
case *arrow.Uint32Type:
return func(str string) {
r.parseUint32(bldr, str)
}
case *arrow.Uint64Type:
return func(str string) {
r.parseUint64(bldr, str)
}
case *arrow.Float16Type:
return func(str string) {
r.parseFloat16(bldr, str)
}
case *arrow.Float32Type:
return func(str string) {
r.parseFloat32(bldr, str)
}
case *arrow.Float64Type:
return func(str string) {
r.parseFloat64(bldr, str)
}
case *arrow.StringType:
// specialize the implementation when we know we cannot have nulls
if r.stringsCanBeNull {
return func(str string) {
if r.isNull(str) {
bldr.AppendNull()
} else {
bldr.(*array.StringBuilder).Append(str)
}
}
} else {
return func(str string) {
bldr.(*array.StringBuilder).Append(str)
}
}
case *arrow.LargeStringType:
// specialize the implementation when we know we cannot have nulls
if r.stringsCanBeNull {
return func(str string) {
if r.isNull(str) {
bldr.AppendNull()
} else {
bldr.(*array.LargeStringBuilder).Append(str)
}
}
} else {
return func(str string) {
bldr.(*array.LargeStringBuilder).Append(str)
}
}
case *arrow.TimestampType:
return func(str string) {
r.parseTimestamp(bldr, str, dt.Unit)
}
case *arrow.Date32Type:
return func(str string) {
r.parseDate32(bldr, str)
}
case *arrow.Date64Type:
return func(str string) {
r.parseDate64(bldr, str)
}
case *arrow.Time32Type:
return func(str string) {
r.parseTime32(bldr, str, dt.Unit)
}
case *arrow.Decimal128Type:
return func(str string) {
r.parseDecimal128(bldr, str, dt.Precision, dt.Scale)
}
case *arrow.Decimal256Type:
return func(str string) {
r.parseDecimal256(bldr, str, dt.Precision, dt.Scale)
}
case *arrow.FixedSizeListType:
return func(s string) {
r.parseFixedSizeList(bldr.(*array.FixedSizeListBuilder), s, int(dt.Len()))
}
case arrow.ListLikeType:
return func(s string) {
r.parseListLike(bldr.(array.ListLikeBuilder), s)
}
case *arrow.BinaryType:
return func(s string) {
r.parseBinaryType(bldr, s)
}
case *arrow.LargeBinaryType:
return func(s string) {
r.parseLargeBinaryType(bldr, s)
}
case *arrow.FixedSizeBinaryType:
return func(s string) {
r.parseFixedSizeBinaryType(bldr, s, dt.Bytes())
}
case arrow.ExtensionType:
return func(s string) {
r.parseExtension(bldr, s)
}
default:
panic(fmt.Errorf("arrow/csv: unhandled field type %T", bldr.Type()))
}
}
func (r *Reader) parseBool(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseBool(str)
if err != nil {
r.err = fmt.Errorf("%w: unrecognized boolean: %s", err, str)
field.AppendNull()
return
}
field.(*array.BooleanBuilder).Append(v)
}
func (r *Reader) parseInt8(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseInt(str, 10, 8)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Int8Builder).Append(int8(v))
}
func (r *Reader) parseInt16(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseInt(str, 10, 16)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Int16Builder).Append(int16(v))
}
func (r *Reader) parseInt32(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseInt(str, 10, 32)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Int32Builder).Append(int32(v))
}
func (r *Reader) parseInt64(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseInt(str, 10, 64)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Int64Builder).Append(v)
}
func (r *Reader) parseUint8(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseUint(str, 10, 8)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Uint8Builder).Append(uint8(v))
}
func (r *Reader) parseUint16(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseUint(str, 10, 16)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Uint16Builder).Append(uint16(v))
}
func (r *Reader) parseUint32(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseUint(str, 10, 32)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Uint32Builder).Append(uint32(v))
}
func (r *Reader) parseUint64(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseUint(str, 10, 64)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Uint64Builder).Append(v)
}
func (r *Reader) parseFloat16(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseFloat(str, 32)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Float16Builder).Append(float16.New(float32(v)))
}
func (r *Reader) parseFloat32(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseFloat(str, 32)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Float32Builder).Append(float32(v))
}
func (r *Reader) parseFloat64(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := strconv.ParseFloat(str, 64)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Float64Builder).Append(v)
}
// parses timestamps using millisecond precision
func (r *Reader) parseTimestamp(field array.Builder, str string, unit arrow.TimeUnit) {
if r.isNull(str) {
field.AppendNull()
return
}
v, err := arrow.TimestampFromString(str, unit)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.TimestampBuilder).Append(v)
}
func (r *Reader) parseDate32(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
tm, err := time.Parse("2006-01-02", str)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Date32Builder).Append(arrow.Date32FromTime(tm))
}
func (r *Reader) parseDate64(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
tm, err := time.Parse("2006-01-02", str)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Date64Builder).Append(arrow.Date64FromTime(tm))
}
func (r *Reader) parseTime32(field array.Builder, str string, unit arrow.TimeUnit) {
if r.isNull(str) {
field.AppendNull()
return
}
val, err := arrow.Time32FromString(str, unit)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Time32Builder).Append(val)
}
func (r *Reader) parseDecimal128(field array.Builder, str string, prec, scale int32) {
if r.isNull(str) {
field.AppendNull()
return
}
val, err := decimal128.FromString(str, prec, scale)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Decimal128Builder).Append(val)
}
func (r *Reader) parseDecimal256(field array.Builder, str string, prec, scale int32) {
if r.isNull(str) {
field.AppendNull()
return
}
val, err := decimal256.FromString(str, prec, scale)
if err != nil && r.err == nil {
r.err = err
field.AppendNull()
return
}
field.(*array.Decimal256Builder).Append(val)
}
func (r *Reader) parseListLike(field array.ListLikeBuilder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
if !(strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}")) {
r.err = errors.New("invalid list format. should start with '{' and end with '}'")
return
}
str = strings.Trim(str, "{}")
field.Append(true)
if len(str) == 0 {
// we don't want to create the csv reader if we already know the
// string is empty
return
}
valueBldr := field.ValueBuilder()
reader := csv.NewReader(strings.NewReader(str))
items, err := reader.Read()
if err != nil {
r.err = err
return
}
for _, str := range items {
r.initFieldConverter(valueBldr)(str)
}
}
func (r *Reader) parseFixedSizeList(field *array.FixedSizeListBuilder, str string, n int) {
if r.isNull(str) {
field.AppendNull()
return
}
if !(strings.HasPrefix(str, "{") && strings.HasSuffix(str, "}")) {
r.err = errors.New("invalid list format. should start with '{' and end with '}'")
return
}
str = strings.Trim(str, "{}")
field.Append(true)
if len(str) == 0 {
// we don't want to create the csv reader if we already know the
// string is empty
return
}
valueBldr := field.ValueBuilder()
reader := csv.NewReader(strings.NewReader(str))
items, err := reader.Read()
if err != nil {
r.err = err
return
}
if len(items) == n {
for _, str := range items {
r.initFieldConverter(valueBldr)(str)
}
} else {
r.err = fmt.Errorf("%w: fixed size list items should match the fixed size list length, expected %d, got %d", arrow.ErrInvalid, n, len(items))
}
}
func (r *Reader) parseBinaryType(field array.Builder, str string) {
// specialize the implementation when we know we cannot have nulls
if r.isNull(str) {
field.AppendNull()
return
}
decodedVal, err := base64.StdEncoding.DecodeString(str)
if err != nil {
r.err = fmt.Errorf("cannot decode base64 string %s", str)
field.AppendNull()
return
}
field.(*array.BinaryBuilder).Append(decodedVal)
}
func (r *Reader) parseLargeBinaryType(field array.Builder, str string) {
// specialize the implementation when we know we cannot have nulls
if r.isNull(str) {
field.AppendNull()
return
}
decodedVal, err := base64.StdEncoding.DecodeString(str)
if err != nil {
r.err = fmt.Errorf("cannot decode base64 string %s", str)
field.AppendNull()
return
}
field.(*array.BinaryBuilder).Append(decodedVal)
}
func (r *Reader) parseFixedSizeBinaryType(field array.Builder, str string, byteWidth int) {
// specialize the implementation when we know we cannot have nulls
if r.isNull(str) {
field.AppendNull()
return
}
decodedVal, err := base64.StdEncoding.DecodeString(str)
if err != nil {
r.err = fmt.Errorf("cannot decode base64 string %s", str)
field.AppendNull()
return
}
if len(decodedVal) == byteWidth {
field.(*array.FixedSizeBinaryBuilder).Append(decodedVal)
} else {
r.err = fmt.Errorf("%w: the length of fixed size binary value should match the fixed size binary byte width, expected %d, got %d", arrow.ErrInvalid, byteWidth, len(decodedVal))
}
}
func (r *Reader) parseExtension(field array.Builder, str string) {
if r.isNull(str) {
field.AppendNull()
return
}
if err := field.AppendValueFromString(str); err != nil {
r.err = err
return
}
}
// Retain increases the reference count by 1.
// Retain may be called simultaneously from multiple goroutines.
func (r *Reader) Retain() {
atomic.AddInt64(&r.refs, 1)
}
// Release decreases the reference count by 1.
// When the reference count goes to zero, the memory is freed.
// Release may be called simultaneously from multiple goroutines.
func (r *Reader) Release() {
debug.Assert(atomic.LoadInt64(&r.refs) > 0, "too many releases")
if atomic.AddInt64(&r.refs, -1) == 0 {
if r.cur != nil {
r.cur.Release()
}
}
}
type conversionColumn struct {
name string
index int
typ arrow.DataType
}
func (c conversionColumn) inferType(v string) arrow.DataType {
if c.typ != nil {
return c.typ
}
var err error
c.typ = arrow.PrimitiveTypes.Int64
for {
// attempt to parse
if err = tryParse(v, c.typ); err == nil {
return c.typ
}
switch dt := c.typ.(type) {
case *arrow.Int64Type:
c.typ = arrow.FixedWidthTypes.Boolean
case *arrow.BooleanType:
c.typ = arrow.FixedWidthTypes.Date32
case *arrow.Date32Type:
c.typ = arrow.FixedWidthTypes.Time32s
case *arrow.Time32Type:
c.typ = &arrow.TimestampType{Unit: arrow.Second}
case *arrow.TimestampType:
if dt.TimeZone == "" {
if dt.Unit == arrow.Second {
c.typ = &arrow.TimestampType{Unit: arrow.Nanosecond}
} else {
c.typ = &arrow.TimestampType{Unit: arrow.Second, TimeZone: "UTC"}
}
} else {
if dt.Unit == arrow.Second {
c.typ = &arrow.TimestampType{Unit: arrow.Nanosecond, TimeZone: "UTC"}
} else {
c.typ = arrow.PrimitiveTypes.Float64
}
}
case *arrow.Float64Type:
c.typ = arrow.BinaryTypes.String
case *arrow.StringType:
// binary is the fallback type
return arrow.BinaryTypes.Binary
}
}
}
func tryParse(val string, dt arrow.DataType) error {
switch dt := dt.(type) {
case *arrow.Int64Type:
_, err := strconv.ParseInt(val, 10, 64)
return err
case *arrow.BooleanType:
_, err := strconv.ParseBool(val)
return err
case *arrow.Date32Type:
_, err := time.Parse("2006-01-02", val)
return err
case *arrow.Time32Type:
_, err := arrow.Time32FromString(val, dt.Unit)
return err
case *arrow.TimestampType:
_, err := arrow.TimestampFromString(val, dt.Unit)
return err
case *arrow.Float64Type:
_, err := strconv.ParseFloat(val, 64)
return err
case *arrow.StringType:
if !utf8.ValidString(val) {
return arrow.ErrInvalid
}
return nil
case *arrow.BinaryType:
_, err := base64.RawStdEncoding.DecodeString(val)
return err
}
panic("shouldn't end up here")
}
var (
_ array.RecordReader = (*Reader)(nil)
)
|