zeroshade commented on code in PR #14255: URL: https://github.com/apache/arrow/pull/14255#discussion_r982510945
########## 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) { + left := b.makeScalar(lhs) + b.assertBinopScalarArr(fn, left, rhs, expected) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopScalarArr(fn binaryFunc, lhs scalar.Scalar, rhs, expected string) { + right, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(rhs)) + defer right.Release() + exp, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(expected)) + defer exp.Release() + + actual, err := fn(b.ctx, b.opts, &compute.ScalarDatum{Value: lhs}, &compute.ArrayDatum{Value: right.Data()}) + b.NoError(err) + defer actual.Release() + assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: exp.Data()}, actual) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopArrSc(fn binaryFunc, lhs string, rhs T, expected string) { + right := b.makeScalar(rhs) + b.assertBinopArrScalar(fn, lhs, right, expected) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopArrScalar(fn binaryFunc, lhs string, rhs scalar.Scalar, expected string) { + left, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(lhs)) + defer left.Release() + exp, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(expected)) + defer exp.Release() + + actual, err := fn(b.ctx, b.opts, &compute.ArrayDatum{Value: left.Data()}, &compute.ScalarDatum{Value: rhs}) + b.NoError(err) + defer actual.Release() + assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: exp.Data()}, actual) +} + +func (b *BinaryArithmeticSuite[T]) assertBinopArrays(fn binaryFunc, lhs, rhs, expected string) { + left, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(lhs)) + defer left.Release() + right, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(rhs)) + defer right.Release() + exp, _, _ := array.FromJSON(b.mem, b.DataType(), strings.NewReader(expected)) + defer exp.Release() + + b.assertBinop(fn, left, right, exp) +} + +func (b *BinaryArithmeticSuite[T]) assertBinop(fn binaryFunc, left, right, expected arrow.Array) { + actual, err := fn(b.ctx, b.opts, &compute.ArrayDatum{Value: left.Data()}, &compute.ArrayDatum{Value: right.Data()}) + b.Require().NoError(err) + defer actual.Release() + assertDatumsEqual(b.T(), &compute.ArrayDatum{Value: expected.Data()}, actual) + + // also check (Scalar, Scalar) operations Review Comment: What do you mean? The difficulty with automatically checking `(Array, Scalar)` and `(Scalar, Array)` automatically is that not all operations are commutative so I can't just use the same expected result. -- 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