github.com/apache/arrow/go/v7@v7.0.1/parquet/internal/utils/dictionary.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package utils
    18  
    19  import (
    20  	"math"
    21  	"reflect"
    22  )
    23  
    24  // IndexType is the type we're going to use for Dictionary indexes, currently
    25  // an alias to int32
    26  type IndexType = int32
    27  
    28  // Max and Min constants for the IndexType
    29  const (
    30  	MaxIndexType = math.MaxInt32
    31  	MinIndexType = math.MinInt32
    32  )
    33  
    34  // DictionaryConverter is an interface used for dealing with RLE decoding and encoding
    35  // when working with dictionaries to get values from indexes.
    36  type DictionaryConverter interface {
    37  	// Copy takes an interface{} which must be a slice of the appropriate type, and will be populated
    38  	// by the dictionary values at the indexes from the IndexType slice
    39  	Copy(interface{}, []IndexType) error
    40  	// Fill fills interface{} which must be a slice of the appropriate type, with the value
    41  	// specified by the dictionary index passed in.
    42  	Fill(interface{}, IndexType) error
    43  	// FillZero fills interface{}, which must be a slice of the appropriate type, with the zero value
    44  	// for the given type.
    45  	FillZero(interface{})
    46  	// IsValid validates that all of the indexes passed in are valid indexes for the dictionary
    47  	IsValid(...IndexType) bool
    48  }
    49  
    50  // converter for getspaced that handles runs that get returned directly
    51  // as output, rather than using a dictionary
    52  type plainConverter struct{}
    53  
    54  func (plainConverter) IsValid(...IndexType) bool { return true }
    55  func (plainConverter) Fill(values interface{}, val IndexType) error {
    56  	v := reflect.ValueOf(values)
    57  	switch v.Type().Elem().Kind() {
    58  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    59  		v.Index(0).SetInt(int64(val))
    60  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    61  		v.Index(0).SetUint(uint64(val))
    62  	}
    63  
    64  	for i := 1; i < v.Len(); i *= 2 {
    65  		reflect.Copy(v.Slice(i, v.Len()), v.Slice(0, i))
    66  	}
    67  	return nil
    68  }
    69  
    70  func (plainConverter) FillZero(values interface{}) {
    71  	v := reflect.ValueOf(values)
    72  	zeroVal := reflect.New(v.Type().Elem()).Elem()
    73  
    74  	v.Index(0).Set(zeroVal)
    75  	for i := 1; i < v.Len(); i *= 2 {
    76  		reflect.Copy(v.Slice(i, v.Len()), v.Slice(0, i))
    77  	}
    78  }
    79  
    80  func (plainConverter) Copy(out interface{}, values []IndexType) error {
    81  	vout := reflect.ValueOf(out)
    82  	vin := reflect.ValueOf(values)
    83  	for i := 0; i < vin.Len(); i++ {
    84  		vout.Index(i).Set(vin.Index(i).Convert(vout.Type().Elem()))
    85  	}
    86  	return nil
    87  }