zeroshade commented on a change in pull request #12158: URL: https://github.com/apache/arrow/pull/12158#discussion_r800703148
########## File path: go/arrow/array/dictionary.go ########## @@ -0,0 +1,1305 @@ +// 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 array + +import ( + "errors" + "fmt" + "math" + "sync/atomic" + "unsafe" + + "github.com/apache/arrow/go/v7/arrow" + "github.com/apache/arrow/go/v7/arrow/bitutil" + "github.com/apache/arrow/go/v7/arrow/decimal128" + "github.com/apache/arrow/go/v7/arrow/float16" + "github.com/apache/arrow/go/v7/arrow/internal/debug" + "github.com/apache/arrow/go/v7/arrow/memory" + "github.com/apache/arrow/go/v7/internal/hashing" + "github.com/goccy/go-json" +) + +// Dictionary represents the type for dictionary-encoded data with a data +// dependent dictionary. +// +// A dictionary array contains an array of non-negative integers (the "dictionary" +// indices") along with a data type containing a "dictionary" corresponding to +// the distinct values represented in the data. +// +// For example, the array: +// +// ["foo", "bar", "foo", "bar", "foo", "bar"] +// +// with dictionary ["bar", "foo"], would have the representation of: +// +// indices: [1, 0, 1, 0, 1, 0] +// dictionary: ["bar", "foo"] +// +// The indices in principle may be any integer type. +type Dictionary struct { + array + + indices Interface + dict Interface +} + +// NewDictionaryArray constructs a dictionary array with the provided indices +// and dictionary using the given type. +func NewDictionaryArray(typ arrow.DataType, indices, dict Interface) *Dictionary { + a := &Dictionary{} + a.array.refCount = 1 + dictdata := NewData(typ, indices.Len(), indices.Data().Buffers(), indices.Data().Children(), indices.NullN(), indices.Data().Offset()) + dictdata.dictionary = dict.Data().(*Data) + dict.Data().Retain() + + defer dictdata.Release() + a.setData(dictdata) + return a +} + +// checkIndexBounds returns an error if any value in the provided integer +// arraydata is >= the passed upperlimit or < 0. otherwise nil +func checkIndexBounds(indices *Data, upperlimit uint64) error { + if indices.length == 0 { + return nil + } + + var maxval uint64 + switch indices.dtype.ID() { + case arrow.UINT8: + maxval = math.MaxUint8 + case arrow.UINT16: + maxval = math.MaxUint16 + case arrow.UINT32: + maxval = math.MaxUint32 + case arrow.UINT64: + maxval = math.MaxUint64 + } + isSigned := maxval == 0 + if !isSigned && upperlimit > maxval { + return nil + } + + // TODO(mtopol): lift BitSetRunReader from parquet to utils + // and use it here for performance improvement. + var nullbitmap []byte + if indices.buffers[0] != nil { + nullbitmap = indices.buffers[0].Bytes() + } + + var outOfBounds func(i int) error + switch indices.dtype.ID() { + case arrow.INT8: + data := arrow.Int8Traits.CastFromBytes(indices.buffers[1].Bytes()) + outOfBounds = func(i int) error { + if data[i] < 0 || data[i] >= int8(upperlimit) { + return fmt.Errorf("index %d out of bounds", data[i]) + } + return nil + } + case arrow.UINT8: + data := arrow.Uint8Traits.CastFromBytes(indices.buffers[1].Bytes()) + outOfBounds = func(i int) error { + if data[i] >= uint8(upperlimit) { + return fmt.Errorf("index %d out of bounds", data[i]) + } + return nil + } + case arrow.INT16: + data := arrow.Int16Traits.CastFromBytes(indices.buffers[1].Bytes()) + outOfBounds = func(i int) error { + if data[i] < 0 || data[i] >= int16(upperlimit) { Review comment: yea, the function to do that natively with SIMD is in the Parquet implemention's native code currently. That's still a good idea for me to lift that into the shared utilities and use that here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org