caldempsey commented on code in PR #152:
URL: https://github.com/apache/spark-connect-go/pull/152#discussion_r2869472340


##########
spark/sql/types/rowiterator.go:
##########
@@ -0,0 +1,88 @@
+package types
+
+import (
+       "context"
+       "errors"
+       "io"
+       "iter"
+       "sync/atomic"
+
+       "github.com/apache/arrow-go/v18/arrow"
+)
+
+type RowPull2 = iter.Seq2[Row, error]
+
+// NewRowSequence flattens record batches to a sequence of rows stream.
+func NewRowSequence(ctx context.Context, recordSeq iter.Seq2[arrow.Record, 
error]) iter.Seq2[Row, error] {
+       return func(yield func(Row, error) bool) {
+               for rec, recErr := range recordSeq {
+                       select {
+                       case <-ctx.Done():
+                               _ = yield(nil, ctx.Err())
+                               return
+                       default:
+                       }
+                       if recErr != nil {
+                               // forward upstream error once, then stop
+                               _ = yield(nil, recErr)
+                               return
+                       }
+                       if rec == nil {
+                               _ = yield(nil, errors.New("expected 
arrow.Record to contain non-nil Rows, got nil"))
+                               return
+                       }
+
+                       rows, err := func() ([]Row, error) {
+                               defer rec.Release()
+                               return ReadArrowRecordToRows(rec)
+                       }()
+                       if err != nil {
+                               _ = yield(nil, err)
+                               return
+                       }
+                       for _, row := range rows {
+                               if !yield(row, nil) {
+                                       return
+                               }
+                       }

Review Comment:
   So i've extracted ReadArrowRecordToRows, and that let us range over it 
directly as you suggested.
   
   There was an error check that needs to be handled since 
`ReadArrowRecordToRows` can still fail, so made that explicit:
   
   ```go
   for row, err := range rowIterFromRecord(rec) {
       if err != nil {
            _ = yield(nil, err)
           return
       }
       if !yield(row, nil) {
           return
       }
   }
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to