zeroshade commented on code in PR #14255: URL: https://github.com/apache/arrow/pull/14255#discussion_r982873283
########## go/arrow/compute/arithmetic.go: ########## @@ -0,0 +1,155 @@ +// 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 + +import ( + "context" + "fmt" + "strings" + + "github.com/apache/arrow/go/v10/arrow" + "github.com/apache/arrow/go/v10/arrow/compute/internal/exec" + "github.com/apache/arrow/go/v10/arrow/compute/internal/kernels" +) + +type arithmeticFunction struct { + ScalarFunction +} + +func (fn *arithmeticFunction) checkDecimals(vals ...arrow.DataType) error { + if !hasDecimal(vals...) { + return nil + } + + if len(vals) != 2 { + return nil + } + + op := fn.name[:strings.Index(fn.name, "_")] + switch op { + case "add", "subtract": + return castBinaryDecimalArgs(decPromoteAdd, vals...) + case "multiply": + return castBinaryDecimalArgs(decPromoteMultiply, vals...) + case "divide": + return castBinaryDecimalArgs(decPromoteDivide, vals...) + default: + return fmt.Errorf("%w: invalid decimal function: %s", arrow.ErrInvalid, fn.name) + } +} + +func (fn *arithmeticFunction) DispatchBest(vals ...arrow.DataType) (exec.Kernel, error) { + if err := fn.checkArity(len(vals)); err != nil { + return nil, err + } + + if err := fn.checkDecimals(vals...); err != nil { + return nil, err + } + + if kn, err := fn.DispatchExact(vals...); err == nil { + return kn, nil + } + + ensureDictionaryDecoded(vals...) + + // only promote types for binary funcs + if len(vals) == 2 { + replaceNullWithOtherType(vals...) + if unit, istime := commonTemporalResolution(vals...); istime { + replaceTemporalTypes(unit, vals...) + } else { + if dt := commonNumeric(vals...); dt != nil { + replaceTypes(dt, vals...) + } + } + } + + return fn.DispatchExact(vals...) +} + +var ( + addDoc FunctionDoc +) + +func RegisterScalarArithmetic(reg FunctionRegistry) { + addFn := &arithmeticFunction{*NewScalarFunction("add", Binary(), addDoc)} + for _, k := range kernels.GetArithmeticKernels(kernels.OpAdd) { + if err := addFn.AddKernel(k); err != nil { + panic(err) + } + } + + reg.AddFunction(addFn, false) + + addCheckedFn := &arithmeticFunction{*NewScalarFunction("add_checked", Binary(), addDoc)} Review Comment: updated to make `add` the default and use the options to specify `add_unchecked` ########## go/arrow/compute/arithmetic_test.go: ########## @@ -0,0 +1,229 @@ +// 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/compute/internal/exec" + "github.com/apache/arrow/go/v10/arrow/memory" + "github.com/apache/arrow/go/v10/arrow/scalar" + "github.com/stretchr/testify/suite" +) + +type binaryFunc = func(context.Context, compute.ArithmeticOptions, compute.Datum, compute.Datum) (compute.Datum, error) + +type BinaryArithmeticSuite[T exec.NumericTypes] struct { + suite.Suite + + mem *memory.CheckedAllocator + opts compute.ArithmeticOptions + ctx context.Context +} + +func (BinaryArithmeticSuite[T]) DataType() arrow.DataType { + return exec.GetDataType[T]() +} + +func (b *BinaryArithmeticSuite[T]) SetupTest() { + b.mem = memory.NewCheckedAllocator(memory.DefaultAllocator) + b.opts.CheckOverflow = false + b.ctx = compute.WithAllocator(context.TODO(), b.mem) +} + +func (b *BinaryArithmeticSuite[T]) TearDownTest() { + b.mem.AssertSize(b.T(), 0) +} + +func (b *BinaryArithmeticSuite[T]) makeNullScalar() scalar.Scalar { + return scalar.MakeNullScalar(b.DataType()) +} + +func (b *BinaryArithmeticSuite[T]) makeScalar(val T) scalar.Scalar { + return scalar.MakeScalar(val) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopScalars(fn binaryFunc, lhs, rhs T, expected T) { + left, right := b.makeScalar(lhs), b.makeScalar(rhs) + exp := b.makeScalar(expected) + + actual, err := fn(b.ctx, b.opts, &compute.ScalarDatum{Value: left}, &compute.ScalarDatum{Value: right}) + b.NoError(err) + sc := actual.(*compute.ScalarDatum).Value + + b.Truef(scalar.Equals(exp, sc), "expected: %s\ngot: %s", exp, sc) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopScArr(fn binaryFunc, lhs T, rhs, expected string) { Review Comment: renamed -- 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]
