github.com/fraugster/parquet-go@v0.12.0/type_int32.go (about)

     1  package goparquet
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/fraugster/parquet-go/parquet"
     9  )
    10  
    11  type int32PlainDecoder struct {
    12  	r io.Reader
    13  }
    14  
    15  func (i *int32PlainDecoder) init(r io.Reader) error {
    16  	i.r = r
    17  
    18  	return nil
    19  }
    20  
    21  func (i *int32PlainDecoder) decodeValues(dst []interface{}) (int, error) {
    22  	var d int32
    23  	for idx := range dst {
    24  		if err := binary.Read(i.r, binary.LittleEndian, &d); err != nil {
    25  			return idx, err
    26  		}
    27  		dst[idx] = d
    28  	}
    29  
    30  	return len(dst), nil
    31  }
    32  
    33  type int32PlainEncoder struct {
    34  	w io.Writer
    35  }
    36  
    37  func (i *int32PlainEncoder) Close() error {
    38  	return nil
    39  }
    40  
    41  func (i *int32PlainEncoder) init(w io.Writer) error {
    42  	i.w = w
    43  
    44  	return nil
    45  }
    46  
    47  func (i *int32PlainEncoder) encodeValues(values []interface{}) error {
    48  	d := make([]int32, len(values))
    49  	for j := range values {
    50  		d[j] = values[j].(int32)
    51  	}
    52  	return binary.Write(i.w, binary.LittleEndian, d)
    53  }
    54  
    55  type int32DeltaBPDecoder struct {
    56  	deltaBitPackDecoder32
    57  }
    58  
    59  func (d *int32DeltaBPDecoder) decodeValues(dst []interface{}) (int, error) {
    60  	for i := range dst {
    61  		u, err := d.next()
    62  		if err != nil {
    63  			return i, err
    64  		}
    65  		dst[i] = u
    66  	}
    67  
    68  	return len(dst), nil
    69  }
    70  
    71  type int32DeltaBPEncoder struct {
    72  	deltaBitPackEncoder32
    73  }
    74  
    75  func (d *int32DeltaBPEncoder) encodeValues(values []interface{}) error {
    76  	for i := range values {
    77  		if err := d.addInt32(values[i].(int32)); err != nil {
    78  			return err
    79  		}
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  type int32Store struct {
    86  	repTyp parquet.FieldRepetitionType
    87  
    88  	stats     *int32Stats
    89  	pageStats *int32Stats
    90  
    91  	*ColumnParameters
    92  }
    93  
    94  func (is *int32Store) getStats() minMaxValues {
    95  	return is.stats
    96  }
    97  
    98  func (is *int32Store) getPageStats() minMaxValues {
    99  	return is.pageStats
   100  }
   101  
   102  func (is *int32Store) params() *ColumnParameters {
   103  	if is.ColumnParameters == nil {
   104  		panic("ColumnParameters is nil")
   105  	}
   106  	return is.ColumnParameters
   107  }
   108  
   109  func (*int32Store) sizeOf(v interface{}) int {
   110  	if vv, ok := v.([]int32); ok {
   111  		return 4 * len(vv)
   112  	}
   113  	return 4
   114  }
   115  
   116  func (is *int32Store) parquetType() parquet.Type {
   117  	return parquet.Type_INT32
   118  }
   119  
   120  func (is *int32Store) repetitionType() parquet.FieldRepetitionType {
   121  	return is.repTyp
   122  }
   123  
   124  func (is *int32Store) reset(rep parquet.FieldRepetitionType) {
   125  	is.repTyp = rep
   126  	is.stats.reset()
   127  	is.pageStats.reset()
   128  }
   129  
   130  func (is *int32Store) setMinMax(j int32) {
   131  	is.stats.setMinMax(j)
   132  	is.pageStats.setMinMax(j)
   133  }
   134  
   135  func (is *int32Store) getValues(v interface{}) ([]interface{}, error) {
   136  	var vals []interface{}
   137  	switch typed := v.(type) {
   138  	case int32:
   139  		is.setMinMax(typed)
   140  		vals = []interface{}{typed}
   141  	case []int32:
   142  		if is.repTyp != parquet.FieldRepetitionType_REPEATED {
   143  			return nil, fmt.Errorf("the value is not repeated but it is an array")
   144  		}
   145  		vals = make([]interface{}, len(typed))
   146  		for j := range typed {
   147  			is.setMinMax(typed[j])
   148  			vals[j] = typed[j]
   149  		}
   150  	default:
   151  		return nil, fmt.Errorf("unsupported type for storing in int32 column: %T => %+v", v, v)
   152  	}
   153  
   154  	return vals, nil
   155  }
   156  
   157  func (*int32Store) append(arrayIn interface{}, value interface{}) interface{} {
   158  	if arrayIn == nil {
   159  		arrayIn = make([]int32, 0, 1)
   160  	}
   161  	return append(arrayIn.([]int32), value.(int32))
   162  }