zeroshade commented on a change in pull request #9671:
URL: https://github.com/apache/arrow/pull/9671#discussion_r595191969



##########
File path: go/parquet/internal/testutils/random.go
##########
@@ -0,0 +1,383 @@
+// 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 testutils
+
+import (
+       "math"
+       "time"
+       "unsafe"
+
+       "github.com/apache/arrow/go/arrow"
+       "github.com/apache/arrow/go/arrow/array"
+       "github.com/apache/arrow/go/arrow/bitutil"
+       "github.com/apache/arrow/go/arrow/memory"
+       "github.com/apache/arrow/go/parquet"
+
+       // "github.factset.com/mtopol/parquet-go/pqarrow"
+       "golang.org/x/exp/rand"
+       "gonum.org/v1/gonum/stat/distuv"
+)
+
+type RandomArrayGenerator struct {
+       seed     uint64
+       extra    uint64
+       src      rand.Source
+       seedRand *rand.Rand
+}
+
+func NewRandomArrayGenerator(seed uint64) RandomArrayGenerator {
+       src := rand.NewSource(seed)
+       return RandomArrayGenerator{seed, 0, src, rand.New(src)}
+}
+
+func (r *RandomArrayGenerator) GenerateBitmap(buffer []byte, n int64, prob 
float64) int64 {
+       count := int64(0)
+       r.extra++
+
+       dist := distuv.Bernoulli{P: prob, Src: rand.NewSource(r.seed + r.extra)}
+       for i := int(0); int64(i) < n; i++ {
+               if dist.Rand() != float64(0.0) {
+                       bitutil.SetBit(buffer, i)
+               } else {
+                       count++
+               }
+       }
+
+       return count
+}
+
+func (r *RandomArrayGenerator) ByteArray(size int64, minLen, maxLen int32, 
nullProb float64) array.Interface {
+       if nullProb < 0 || nullProb > 1 {
+               panic("null prob must be between 0 and 1")
+       }
+
+       lengths := r.Int32(size, minLen, maxLen, nullProb)
+       defer lengths.Release()
+
+       r.extra++
+       dist := rand.New(rand.NewSource(r.seed + r.extra))
+       bldr := array.NewStringBuilder(memory.DefaultAllocator)
+       defer bldr.Release()
+
+       strbuf := make([]byte, maxLen)
+
+       for i := 0; int64(i) < size; i++ {
+               if lengths.IsValid(i) {
+                       l := lengths.Value(i)
+                       for j := int32(0); j < l; j++ {
+                               strbuf[j] = 
byte(dist.Int31n(int32('z')-int32('A')+1) + int32('A'))
+                       }
+                       val := strbuf[:l]
+                       bldr.Append(*(*string)(unsafe.Pointer(&val)))
+               } else {
+                       bldr.AppendNull()
+               }
+       }
+
+       return bldr.NewArray()
+}
+
+func (r *RandomArrayGenerator) Uint8(size int64, min, max uint8, prob float64) 
array.Interface {
+       buffers := make([]*memory.Buffer, 2)
+       nullCount := int64(0)
+
+       buffers[0] = memory.NewResizableBuffer(memory.DefaultAllocator)
+       buffers[0].Resize(int(bitutil.BytesForBits(size)))
+       nullCount = r.GenerateBitmap(buffers[0].Bytes(), size, prob)
+
+       buffers[1] = memory.NewResizableBuffer(memory.DefaultAllocator)
+       buffers[1].Resize(int(size * int64(arrow.Uint8SizeBytes)))
+
+       r.extra++
+       dist := rand.New(rand.NewSource(r.seed + r.extra))
+       out := arrow.Uint8Traits.CastFromBytes(buffers[1].Bytes())
+       for i := int64(0); i < size; i++ {
+               out[i] = uint8(dist.Intn(int(max-min+1))) + min
+       }
+
+       return array.NewUint8Data(array.NewData(arrow.PrimitiveTypes.Uint8, 
int(size), buffers, nil, int(nullCount), 0))
+}
+
+func (r *RandomArrayGenerator) Int32(size int64, min, max int32, pctNull 
float64) *array.Int32 {
+       buffers := make([]*memory.Buffer, 2)
+       nullCount := int64(0)
+
+       buffers[0] = memory.NewResizableBuffer(memory.DefaultAllocator)
+       buffers[0].Resize(int(bitutil.BytesForBits(size)))
+       nullCount = r.GenerateBitmap(buffers[0].Bytes(), size, 1-pctNull)
+
+       buffers[1] = memory.NewResizableBuffer(memory.DefaultAllocator)
+       buffers[1].Resize(arrow.Int32Traits.BytesRequired(int(size)))
+
+       r.extra++
+       dist := rand.New(rand.NewSource(r.seed + r.extra))
+       out := arrow.Int32Traits.CastFromBytes(buffers[1].Bytes())
+       for i := int64(0); i < size; i++ {
+               out[i] = dist.Int31n(max-min+1) + min
+       }
+       return array.NewInt32Data(array.NewData(arrow.PrimitiveTypes.Int32, 
int(size), buffers, nil, int(nullCount), 0))
+}
+
+func (r *RandomArrayGenerator) Float64(size int64, pctNull float64) 
*array.Float64 {

Review comment:
       so these are in an `internal` package so that they cannot be imported 
outside of the the parquet module as they are only utilized for generating 
values for unit tests they are equivalent to the similar functions in the c++ 
arrow test utils files. 
   
   That said, I'll absolutely add some more docs on the top of the file




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to