Skip to content

Commit

Permalink
moving row scanning into the data chunk api (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch authored Jun 28, 2024
1 parent 2ae6eb8 commit b637da2
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 291 deletions.
41 changes: 41 additions & 0 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ type DataChunk struct {
data C.duckdb_data_chunk
// columns is a helper slice providing direct access to all columns.
columns []vector
// columnNames holds the column names, if known.
columnNames []string
}

// GetDataChunkCapacity returns the capacity of a data chunk.
func GetDataChunkCapacity() int {
return int(C.duckdb_vector_size())
}

// GetSize returns the internal size of the data chunk.
func (chunk *DataChunk) GetSize() int {
return int(C.duckdb_data_chunk_get_size(chunk.data))
}

// SetSize sets the internal size of the data chunk. Cannot exceed GetCapacity().
func (chunk *DataChunk) SetSize(size int) error {
if size > GetDataChunkCapacity() {
Expand All @@ -32,6 +39,15 @@ func (chunk *DataChunk) SetSize(size int) error {
return nil
}

// GetValue returns a single value of a column.
func (chunk *DataChunk) GetValue(colIdx int, rowIdx int) (any, error) {
if colIdx >= len(chunk.columns) {
return nil, getError(errAPI, columnCountError(colIdx, len(chunk.columns)))
}
column := &chunk.columns[colIdx]
return column.getFn(column, C.idx_t(rowIdx)), nil
}

// SetValue writes a single value to a column in a data chunk. Note that this requires casting the type for each invocation.
// NOTE: Custom ENUM types must be passed as string.
func (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
Expand All @@ -56,6 +72,7 @@ func (chunk *DataChunk) SetValue(colIdx int, rowIdx int, val any) error {
}

func (chunk *DataChunk) initFromTypes(ptr unsafe.Pointer, types []C.duckdb_logical_type) error {
// NOTE: initFromTypes does not initialize the column names.
columnCount := len(types)

// Initialize the callback functions to read and write values.
Expand Down Expand Up @@ -83,6 +100,30 @@ func (chunk *DataChunk) initFromTypes(ptr unsafe.Pointer, types []C.duckdb_logic
return nil
}

func (chunk *DataChunk) initFromDuckDataChunk(data C.duckdb_data_chunk) error {
columnCount := int(C.duckdb_data_chunk_get_column_count(data))
chunk.columns = make([]vector, columnCount)
chunk.data = data

var err error
for i := 0; i < columnCount; i++ {
duckdbVector := C.duckdb_data_chunk_get_vector(data, C.idx_t(i))

// Initialize the callback functions to read and write values.
logicalType := C.duckdb_vector_get_column_type(duckdbVector)
err = chunk.columns[i].init(logicalType, i)
C.duckdb_destroy_logical_type(&logicalType)
if err != nil {
break
}

// Initialize the vectors and their child vectors.
chunk.columns[i].duckdbVector = duckdbVector
chunk.columns[i].getChildVectors(duckdbVector)
}
return err
}

func (chunk *DataChunk) close() {
C.duckdb_destroy_data_chunk(&chunk.data)
}
Loading

0 comments on commit b637da2

Please sign in to comment.