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