lostluck commented on a change in pull request #15136:
URL: https://github.com/apache/beam/pull/15136#discussion_r667100654



##########
File path: sdks/go/pkg/beam/testing/passert/floats.go
##########
@@ -0,0 +1,63 @@
+// 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 passert
+
+import (
+       "fmt"
+       "reflect"
+       "strings"
+
+       "github.com/apache/beam/sdks/go/pkg/beam"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx"
+       "github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+)
+
+// AllWithinBounds checks that a PCollection of numeric types is within the 
bounds
+// [lo, high]. Checks for case where bounds are flipped and swaps them so the 
bounds
+// passed to the doFn are always lo <= hi.
+func AllWithinBounds(s beam.Scope, col beam.PCollection, lo, hi float64) {
+       t := beam.ValidateNonCompositeType(col)
+       if !reflectx.IsNumber(t.Type()) || reflectx.IsComplex(t.Type()) {
+               panic(fmt.Sprintf("type must be a non-complex number: %v", t))
+       }
+       if lo > hi {
+               lo, hi = hi, lo
+       }
+       s = s.Scope(fmt.Sprintf("passert.AllWithinBounds([%v, %v])", lo, hi))
+       beam.ParDo0(s, &boundsFn{lo: lo, hi: hi}, beam.Impulse(s), 
beam.SideInput{Input: col})
+}
+
+type boundsFn struct {
+       lo, hi float64
+}
+
+func (f *boundsFn) ProcessElement(_ []byte, col func(*beam.T) bool) error {
+       errorStrings := []string{}
+       var input beam.T
+       for col(&input) {
+               val := 
reflect.ValueOf(input.(interface{})).Convert(reflectx.Float64).Interface().(float64)
+               if val < f.lo {
+                       errorStrings = append(errorStrings, fmt.Sprintf("value 
%v too low", input))
+               } else if val > f.hi {
+                       errorStrings = append(errorStrings, fmt.Sprintf("value 
%v too high", input))

Review comment:
       Pre-doing the error strings like this will lead to very verbose and tall 
error messages. Also, since PCollections are in arbitrary orders, no-two runs 
are guaranteed to be the same, which makes it harder to read the messages or 
when debugging a specific issue.
   
   Consider instead keeping two slices `var tooLow, tooHigh []float64` and 
appending qualifying values to them. Then, sort the two lists so the error 
messages are deterministic. And then only include the low/high if the slice is 
non empty. Eg for low: say they were too low, what the low threshold was, then 
print the slice of values. It's OK if they're all float converted at this point.
   
   This is more verbose in the single bad value case, but makes the affected 
bound clear, and is far more readable in the multiple value case.




-- 
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...@beam.apache.org

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


Reply via email to