You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/08/26 15:23:52 UTC
[GitHub] [arrow] lidavidm commented on a diff in pull request #13974: ARROW-17527: [Go] Implement Cast to Boolean Functions
lidavidm commented on code in PR #13974:
URL: https://github.com/apache/arrow/pull/13974#discussion_r956151661
##########
go/arrow/compute/cast_test.go:
##########
@@ -0,0 +1,329 @@
+// 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 compute_test
+
+import (
+ "context"
+ "fmt"
+ "strings"
+ "testing"
+
+ "github.com/apache/arrow/go/v10/arrow"
+ "github.com/apache/arrow/go/v10/arrow/array"
+ "github.com/apache/arrow/go/v10/arrow/compute"
+ "github.com/apache/arrow/go/v10/arrow/memory"
+ "github.com/apache/arrow/go/v10/arrow/scalar"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "github.com/stretchr/testify/suite"
+)
+
+func getScalars(inputs []compute.Datum, idx int) []scalar.Scalar {
+ out := make([]scalar.Scalar, len(inputs))
+ for i, in := range inputs {
+ if in.Kind() == compute.KindArray {
+ arr := in.(*compute.ArrayDatum).MakeArray()
+ defer arr.Release()
+ out[i], _ = scalar.GetScalar(arr, idx)
+ } else {
+ out[i] = in.(*compute.ScalarDatum).Value
+ }
+ }
+ return out
+}
+
+func getDatums[T any](inputs []T) []compute.Datum {
+ out := make([]compute.Datum, len(inputs))
+ for i, in := range inputs {
+ out[i] = compute.NewDatum(in)
+ }
+ return out
+}
+
+func assertDatumsEqual(t *testing.T, expected, actual compute.Datum) {
+ require.Equal(t, expected.Kind(), actual.Kind())
+
+ switch expected.Kind() {
+ case compute.KindScalar:
+ want := expected.(*compute.ScalarDatum).Value
+ got := actual.(*compute.ScalarDatum).Value
Review Comment:
I guess we'd just eat a panic if the actual value wasn't the right Kind? Is it worth doing an explicit check or is the regular message clear enough?
##########
go/arrow/compute/internal/kernels/cast.go:
##########
@@ -0,0 +1,120 @@
+// 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 kernels
+
+import (
+ "github.com/apache/arrow/go/v10/arrow"
+ "github.com/apache/arrow/go/v10/arrow/array"
+ "github.com/apache/arrow/go/v10/arrow/compute/internal/exec"
+)
+
+type CastOptions struct {
+ ToType arrow.DataType `compute:"to_type"`
+ AllowIntOverflow bool `compute:"allow_int_overflow"`
+ AllowTimeTruncate bool `compute:"allow_time_truncate"`
+ AllowTimeOverflow bool `compute:"allow_time_overflow"`
+ AllowDecimalTruncate bool `compute:"allow_decimal_truncate"`
+ AllowFloatTruncate bool `compute:"allow_float_truncate"`
+ AllowInvalidUtf8 bool `compute:"allow_invalid_utf8"`
+}
+
+func (CastOptions) TypeName() string { return "CastOptions" }
+
+// CastState is the kernel state for Cast functions, it is an alias to
+// the CastOptions object.
+type CastState = CastOptions
+
+// ZeroCopyCastExec is a kernel for performing a cast which can be executed
+// as a zero-copy operation. It simply forwards the buffers to the output.
+//
+// This can be used for casting a type to itself, or for casts between
+// equivalent representations such as Int32 and Date32.
+func ZeroCopyCastExec(_ *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
+ dt := out.Type
+ *out = batch.Values[0].Array
+ out.Type = dt
+ return nil
+}
+
+func recursiveSetSelfAlloc(arr *exec.ArraySpan) {
+ for i := range arr.Buffers {
+ if len(arr.Buffers[i].Buf) > 0 {
+ arr.Buffers[i].SelfAlloc = true
+ if arr.Buffers[i].Owner != nil {
+ arr.Buffers[i].Owner.Retain()
+ }
+ }
+ }
+
+ for i := range arr.Children {
+ recursiveSetSelfAlloc(&arr.Children[i])
+ }
+}
+
+// CastFromNull is a simple kernel for constructing an array of null values
+// for the requested data type, allowing casting of an arrow.Null typed value
+// to any other arbitrary data type.
+func CastFromNull(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
+ arr := array.MakeArrayOfNull(exec.GetAllocator(ctx.Ctx), out.Type, int(batch.Len))
+ defer arr.Release()
+
+ out.SetMembers(arr.Data())
+ recursiveSetSelfAlloc(out)
+ return nil
+}
+
+// OutputAllNull is a simple kernel that initializes the output as an array
+// whose output is all null by setting nulls to the length.
+func OutputAllNull(_ *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
+ out.Nulls = batch.Len
+ return nil
+}
+
+func canCastFromDict(id arrow.Type) bool {
+ return arrow.IsPrimitive(id) || arrow.IsBaseBinary(id) || arrow.IsFixedSizeBinary(id)
+}
+
+// GetZeroCastKernel returns a kernel for performing ZeroCast execution using
+// the ZeroCopyCastExec kernel function.
+func GetZeroCastKernel(inID arrow.Type, inType exec.InputType, out exec.OutputType) exec.ScalarKernel {
+ k := exec.NewScalarKernel([]exec.InputType{inType}, out, ZeroCopyCastExec, nil)
+ k.NullHandling = exec.NullComputedNoPrealloc
+ k.MemAlloc = exec.MemNoPrealloc
+ return k
+}
+
+// GetCommonCastKernels returns the list of kernels common to all types
+// such as casting from null or from Extension types of the appropriate
+// underlying type.
+func GetCommonCastKernels(outID arrow.Type, outType arrow.DataType) (out []exec.ScalarKernel) {
+ out = make([]exec.ScalarKernel, 0, 2)
+
+ kernel := exec.NewScalarKernel([]exec.InputType{exec.NewExactInput(arrow.Null)}, exec.NewOutputType(outType),
+ CastFromNull, nil)
+ kernel.NullHandling = exec.NullComputedNoPrealloc
+ kernel.MemAlloc = exec.MemNoPrealloc
+ out = append(out, kernel)
+
+ // if canCastFromDict(outID) {
+ // dictionary unpacking not implemented for boolean or nested types
+ // TODO dict cast
+ // panic(fmt.Errorf("%w: dictionary casting", arrow.ErrNotImplemented))
+ // }
+
+ // Cast from extension
+ return
Review Comment:
Just remove the commented out code/comments that don't apply?
##########
go/arrow/compute/internal/kernels/helpers.go:
##########
@@ -0,0 +1,119 @@
+// 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 kernels
+
+import (
+ "github.com/apache/arrow/go/v10/arrow/bitutil"
+ "github.com/apache/arrow/go/v10/arrow/compute/internal/exec"
+ "github.com/apache/arrow/go/v10/internal/bitutils"
+)
+
+// ScalarUnary returns a kernel for performing a unary operation on
+// FixedWidth types which is implemented using the passed in function
+// which will receive a slice containing the raw input data along with
+// a slice to populate for the output data.
+//
+// Note that bool is not included in exec.FixedWidthTypes since it is
+// represented as a bitmap, not as a slice of bool.
+func ScalarUnary[OutT, Arg0T exec.FixedWidthTypes](op func(*exec.KernelCtx, []Arg0T, []OutT) error) exec.ArrayKernelExec {
+ return func(ctx *exec.KernelCtx, in *exec.ExecSpan, out *exec.ExecResult) error {
+ arg0 := in.Values[0].Array
+ inData := exec.GetSpanValues[Arg0T](&arg0, 1)
+ outData := exec.GetSpanValues[OutT](out, 1)
+ return op(ctx, inData, outData)
+ }
+}
+
+// ScalarUnaryBoolOutput is like ScalarUnary only it is for cases of boolean
+// output. The function should take in a slice of the input type and a slice
+// of bytes to fill with the output boolean bitmap.
+func ScalarUnaryBoolOutput[Arg0T exec.FixedWidthTypes](op func(*exec.KernelCtx, []Arg0T, []byte) error) exec.ArrayKernelExec {
+ return func(ctx *exec.KernelCtx, in *exec.ExecSpan, out *exec.ExecResult) error {
+ arg0 := in.Values[0].Array
+ inData := exec.GetSpanValues[Arg0T](&arg0, 1)
+ return op(ctx, inData, out.Buffers[1].Buf)
+ }
+}
+
+// ScalarUnaryNotNullBinaryArgBoolOut creates a unary kernel that accepts
+// a binary type input (Binary [offset int32], String [offset int32],
+// LargeBinary [offset int64], LargeString [offset int64]) and returns
+// a boolean output which is never null.
+//
+// It implements the handling to iterate the offsets and values calling
+// the provided function on each byte slice. The provided default value
+// will be used as the output for elements of the input that are null.
+func ScalarUnaryNotNullBinaryArgBoolOut[OffsetT int32 | int64](defVal bool, op func(*exec.KernelCtx, []byte) (bool, error)) exec.ArrayKernelExec {
Review Comment:
just a general comment, but seeing as these are patterned after the C++ names, the C++ names aren't very consistent/don't follow a consistent convention - you may want to consider something more principled. (I think @edponce was trying to refactor them a while back, but it's a lot of work)
--
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-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org