github.com/fraugster/parquet-go@v0.12.0/type_float.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 floatPlainDecoder struct {
    13  	r io.Reader
    14  }
    15  
    16  func (f *floatPlainDecoder) init(r io.Reader) error {
    17  	f.r = r
    18  
    19  	return nil
    20  }
    21  
    22  func (f *floatPlainDecoder) decodeValues(dst []interface{}) (int, error) {
    23  	var data uint32
    24  	for i := range dst {
    25  		if err := binary.Read(f.r, binary.LittleEndian, &data); err != nil {
    26  			return i, err
    27  		}
    28  		dst[i] = math.Float32frombits(data)
    29  	}
    30  
    31  	return len(dst), nil
    32  }
    33  
    34  type floatPlainEncoder struct {
    35  	w io.Writer
    36  }
    37  
    38  func (d *floatPlainEncoder) Close() error {
    39  	return nil
    40  }
    41  
    42  func (d *floatPlainEncoder) init(w io.Writer) error {
    43  	d.w = w
    44  
    45  	return nil
    46  }
    47  
    48  func (d *floatPlainEncoder) encodeValues(values []interface{}) error {
    49  	data := make([]uint32, len(values))
    50  	for i := range values {
    51  		data[i] = math.Float32bits(values[i].(float32))
    52  	}
    53  
    54  	return binary.Write(d.w, binary.LittleEndian, data)
    55  }
    56  
    57  type floatStore struct {
    58  	repTyp parquet.FieldRepetitionType
    59  
    60  	stats     *floatStats
    61  	pageStats *floatStats
    62  
    63  	*ColumnParameters
    64  }
    65  
    66  func (f *floatStore) getStats() minMaxValues {
    67  	return f.stats
    68  }
    69  
    70  func (f *floatStore) getPageStats() minMaxValues {
    71  	return f.pageStats
    72  }
    73  
    74  func (f *floatStore) params() *ColumnParameters {
    75  	if f.ColumnParameters == nil {
    76  		panic("ColumnParameters is nil")
    77  	}
    78  	return f.ColumnParameters
    79  }
    80  
    81  func (*floatStore) sizeOf(v interface{}) int {
    82  	if vv, ok := v.([]float32); ok {
    83  		return 4 * len(vv)
    84  	}
    85  	return 4
    86  }
    87  
    88  func (f *floatStore) parquetType() parquet.Type {
    89  	return parquet.Type_FLOAT
    90  }
    91  
    92  func (f *floatStore) repetitionType() parquet.FieldRepetitionType {
    93  	return f.repTyp
    94  }
    95  
    96  func (f *floatStore) reset(rep parquet.FieldRepetitionType) {
    97  	f.repTyp = rep
    98  	f.stats.reset()
    99  	f.pageStats.reset()
   100  }
   101  
   102  func (f *floatStore) setMinMax(j float32) {
   103  	f.stats.setMinMax(j)
   104  	f.pageStats.setMinMax(j)
   105  }
   106  
   107  func (f *floatStore) getValues(v interface{}) ([]interface{}, error) {
   108  	var vals []interface{}
   109  	switch typed := v.(type) {
   110  	case float32:
   111  		f.setMinMax(typed)
   112  		vals = []interface{}{typed}
   113  	case []float32:
   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 float32 column: %T => %+v", v, v)
   124  	}
   125  
   126  	return vals, nil
   127  }
   128  
   129  func (*floatStore) append(arrayIn interface{}, value interface{}) interface{} {
   130  	if arrayIn == nil {
   131  		arrayIn = make([]float32, 0, 1)
   132  	}
   133  	return append(arrayIn.([]float32), value.(float32))
   134  }