jasonlin45 commented on code in PR #3325:
URL: https://github.com/apache/arrow-adbc/pull/3325#discussion_r2302260044


##########
go/adbc/driver/databricks/ipc_reader_adapter.go:
##########
@@ -0,0 +1,183 @@
+// 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 databricks
+
+import (
+       "context"
+       "fmt"
+       "io"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/ipc"
+       dbsqlrows "github.com/databricks/databricks-sql-go/rows"
+)
+
+// Check if the rows interface supports IPC streams
+type rowsWithIPCStream interface {
+       GetArrowIPCStreams(context.Context) (dbsqlrows.ArrowIPCStreamIterator, 
error)
+}
+
+// ipcReaderAdapter uses the new IPC stream interface for zero-copy Arrow 
access
+type ipcReaderAdapter struct {
+       ipcIterator   dbsqlrows.ArrowIPCStreamIterator
+       currentReader *ipc.Reader
+       currentRecord arrow.Record
+       schema        *arrow.Schema
+       closed        bool
+       refCount      int64
+}
+
+// newIPCReaderAdapter creates a RecordReader using direct IPC stream access
+func newIPCReaderAdapter(ctx context.Context, rows dbsqlrows.Rows) 
(array.RecordReader, error) {
+       // Check if rows supports IPC streams
+       ipcRows, ok := rows.(rowsWithIPCStream)
+       if !ok {
+               return nil, fmt.Errorf("databricks rows do not support IPC 
stream access")
+       }
+
+       // Get IPC stream iterator
+       ipcIterator, err := ipcRows.GetArrowIPCStreams(ctx)
+       if err != nil {
+               return nil, fmt.Errorf("failed to get IPC streams: %w", err)
+       }
+
+       adapter := &ipcReaderAdapter{
+               refCount:    1,
+               ipcIterator: ipcIterator,
+       }
+
+       // Initialize the first reader
+       err = adapter.loadNextReader()
+       if err != nil && err != io.EOF {
+               return nil, fmt.Errorf("failed to initialize IPC reader: %w", 
err)
+       }
+
+       return adapter, nil
+}
+
+// loadNextReader loads the next IPC stream into a reader
+func (r *ipcReaderAdapter) loadNextReader() error {
+       // Release current reader if any
+       if r.currentReader != nil {
+               r.currentReader.Release()
+               r.currentReader = nil
+       }
+
+       // Get next IPC stream
+       if !r.ipcIterator.HasNext() {
+               return io.EOF
+       }
+
+       ipcStream, err := r.ipcIterator.Next()
+       if err != nil {
+               return err
+       }
+
+       // Create IPC reader from stream
+       reader, err := ipc.NewReader(ipcStream)
+       if err != nil {
+               return fmt.Errorf("failed to create IPC reader: %w", err)
+       }
+
+       r.currentReader = reader
+
+       // Cache schema from first reader
+       if r.schema == nil {
+               r.schema = reader.Schema()
+       }
+
+       return nil
+}
+
+// Implement array.RecordReader interface
+func (r *ipcReaderAdapter) Schema() *arrow.Schema {
+       return r.schema
+}
+
+func (r *ipcReaderAdapter) Next() bool {
+       if r.closed {
+               return false
+       }
+
+       // Release previous record
+       if r.currentRecord != nil {
+               r.currentRecord.Release()
+               r.currentRecord = nil
+       }
+
+       // Try to get next record from current reader
+       if r.currentReader != nil && r.currentReader.Next() {
+               r.currentRecord = r.currentReader.Record()
+               r.currentRecord.Retain()
+               return true
+       }
+
+       // Need to load next IPC stream
+       err := r.loadNextReader()
+       if err != nil {
+               return false
+       }
+
+       // Try again with new reader
+       if r.currentReader != nil && r.currentReader.Next() {
+               r.currentRecord = r.currentReader.Record()
+               r.currentRecord.Retain()
+               return true
+       }
+
+       return false
+}
+
+func (r *ipcReaderAdapter) Record() arrow.Record {
+       return r.currentRecord
+}
+
+func (r *ipcReaderAdapter) Release() {
+       r.refCount -= 1
+       if !r.closed && r.refCount <= 0 {
+               r.closed = true

Review Comment:
   Sounds good, I got rid of it and added a panic for bad reference counting



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

Reply via email to