cyb70289 commented on code in PR #13946: URL: https://github.com/apache/arrow/pull/13946#discussion_r953326376
########## go/arrow/compute/internal/kernel.go: ########## @@ -0,0 +1,521 @@ +// 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 internal + +import ( + "context" + "fmt" + "hash/maphash" + "strings" + + "github.com/apache/arrow/go/v10/arrow" + "github.com/apache/arrow/go/v10/arrow/bitutil" + "github.com/apache/arrow/go/v10/arrow/memory" + "github.com/apache/arrow/go/v10/parquet/internal/debug" + "golang.org/x/exp/constraints" + "golang.org/x/exp/slices" +) + +type ctxAllocKey struct{} + +// WithAllocator returns a new context with the provided allocator +// embedded into the context. +func WithAllocator(ctx context.Context, mem memory.Allocator) context.Context { + return context.WithValue(ctx, ctxAllocKey{}, mem) +} + +// GetAllocator retrieves the allocator from the context, or returns +// memory.DefaultAllocator if there was no allocator in the provided +// context. +func GetAllocator(ctx context.Context) memory.Allocator { + mem, ok := ctx.Value(ctxAllocKey{}).(memory.Allocator) + if !ok { + return memory.DefaultAllocator + } + return mem +} + +// Kernel defines the minimum interface required for the basic execution +// kernel. It will grow as the implementation requires. +type Kernel interface { + GetInitFn() KernelInitFn + GetSig() *KernelSignature +} + +// KernelCtx is a small struct holding the context for a kernel execution +// consisting of a pointer to the kernel, initialized state (if needed) +// and the context for this execution. +type KernelCtx struct { + Ctx context.Context + Kernel Kernel + State KernelState +} + +func (k *KernelCtx) Allocate(bufsize int) *memory.Buffer { + buf := memory.NewResizableBuffer(GetAllocator(k.Ctx)) + buf.Resize(bufsize) + return buf +} + +func (k *KernelCtx) AllocateBitmap(nbits int64) *memory.Buffer { + nbytes := bitutil.BytesForBits(nbits) + return k.Allocate(int(nbytes)) +} + +// TypeMatcher define an interface for matching Input or Output types +// for execution kernels. There are multiple implementations of this +// interface provided by this package. +type TypeMatcher interface { + fmt.Stringer + Matches(typ arrow.DataType) bool + Equals(other TypeMatcher) bool +} + +type sameTypeIDMatcher struct { + accepted arrow.Type +} + +func (s sameTypeIDMatcher) Matches(typ arrow.DataType) bool { return s.accepted == typ.ID() } +func (s *sameTypeIDMatcher) Equals(other TypeMatcher) bool { Review Comment: Nit: a bit dogmatic but is it possible to make this method also accepts value, not pointer? From https://go.dev/doc/faq#methods_on_values_or_pointers, looks it's recommended to use value or pointer receiver consistently. > Next is consistency. If some of the methods of the type must have pointer receivers, the rest should too, so the method set is consistent regardless of how the type is used. See the section on [method sets](https://go.dev/doc/faq#different_method_sets) for details. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
