|
| 1 | +package duckdb |
| 2 | + |
| 3 | +/* |
| 4 | +#include <duckdb.h> |
| 5 | +#include <stdint.h> |
| 6 | +
|
| 7 | +#ifndef ARROW_C_DATA_INTERFACE |
| 8 | +#define ARROW_C_DATA_INTERFACE |
| 9 | +
|
| 10 | +#define ARROW_FLAG_DICTIONARY_ORDERED 1 |
| 11 | +#define ARROW_FLAG_NULLABLE 2 |
| 12 | +#define ARROW_FLAG_MAP_KEYS_SORTED 4 |
| 13 | +
|
| 14 | +struct ArrowSchema { |
| 15 | + // Array type description |
| 16 | + const char* format; |
| 17 | + const char* name; |
| 18 | + const char* metadata; |
| 19 | + int64_t flags; |
| 20 | + int64_t n_children; |
| 21 | + struct ArrowSchema** children; |
| 22 | + struct ArrowSchema* dictionary; |
| 23 | +
|
| 24 | + // Release callback |
| 25 | + void (*release)(struct ArrowSchema*); |
| 26 | + // Opaque producer-specific data |
| 27 | + void* private_data; |
| 28 | +}; |
| 29 | +
|
| 30 | +struct ArrowArray { |
| 31 | + // Array data description |
| 32 | + int64_t length; |
| 33 | + int64_t null_count; |
| 34 | + int64_t offset; |
| 35 | + int64_t n_buffers; |
| 36 | + int64_t n_children; |
| 37 | + const void** buffers; |
| 38 | + struct ArrowArray** children; |
| 39 | + struct ArrowArray* dictionary; |
| 40 | +
|
| 41 | + // Release callback |
| 42 | + void (*release)(struct ArrowArray*); |
| 43 | + // Opaque producer-specific data |
| 44 | + void* private_data; |
| 45 | +}; |
| 46 | +
|
| 47 | +#endif // ARROW_C_DATA_INTERFACE |
| 48 | +*/ |
| 49 | +import "C" |
| 50 | + |
| 51 | +import ( |
| 52 | + "context" |
| 53 | + "database/sql/driver" |
| 54 | + "errors" |
| 55 | + "fmt" |
| 56 | + "unsafe" |
| 57 | + |
| 58 | + "github.com/apache/arrow/go/v14/arrow" |
| 59 | + "github.com/apache/arrow/go/v14/arrow/array" |
| 60 | + "github.com/apache/arrow/go/v14/arrow/cdata" |
| 61 | +) |
| 62 | + |
| 63 | +// Arrow exposes DuckDB Apache Arrow interface. |
| 64 | +// https://duckdb.org/docs/api/c/api#arrow-interface |
| 65 | +type Arrow struct { |
| 66 | + c *conn |
| 67 | +} |
| 68 | + |
| 69 | +// NewArrowFromConn returns a new Arrow from a DuckDB driver connection. |
| 70 | +func NewArrowFromConn(driverConn driver.Conn) (*Arrow, error) { |
| 71 | + dbConn, ok := driverConn.(*conn) |
| 72 | + if !ok { |
| 73 | + return nil, fmt.Errorf("not a duckdb driver connection") |
| 74 | + } |
| 75 | + |
| 76 | + if dbConn.closed { |
| 77 | + panic("database/sql/driver: misuse of duckdb driver: Arrow after Close") |
| 78 | + } |
| 79 | + |
| 80 | + return &Arrow{c: dbConn}, nil |
| 81 | +} |
| 82 | + |
| 83 | +// QueryContext prepares statements, executes them, returns Apache Arrow array.RecordReader as a result of the last |
| 84 | +// executed statement. Arguments are bound to the last statement. |
| 85 | +func (a *Arrow) QueryContext(ctx context.Context, query string, args ...any) (array.RecordReader, error) { |
| 86 | + if a.c.closed { |
| 87 | + panic("database/sql/driver: misuse of duckdb driver: Arrow.Query after Close") |
| 88 | + } |
| 89 | + |
| 90 | + stmts, size, err := a.c.extractStmts(query) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + defer C.duckdb_destroy_extracted(&stmts) |
| 95 | + |
| 96 | + // execute all statements without args, except the last one |
| 97 | + for i := C.idx_t(0); i < size-1; i++ { |
| 98 | + stmt, err := a.c.prepareExtractedStmt(stmts, i) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + // send nil args to execute statement and ignore result (using ExecContext since we're ignoring the result anyway) |
| 103 | + _, err = stmt.ExecContext(ctx, nil) |
| 104 | + stmt.Close() |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // prepare and execute last statement with args and return result |
| 111 | + stmt, err := a.c.prepareExtractedStmt(stmts, size-1) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + defer stmt.Close() |
| 116 | + |
| 117 | + res, err := a.execute(stmt, a.anyArgsToNamedArgs(args)) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + defer C.duckdb_destroy_arrow(res) |
| 122 | + |
| 123 | + sc, err := a.queryArrowSchema(res) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + |
| 128 | + var recs []arrow.Record |
| 129 | + defer func() { |
| 130 | + for _, r := range recs { |
| 131 | + r.Release() |
| 132 | + } |
| 133 | + }() |
| 134 | + |
| 135 | + rowCount := uint64(C.duckdb_arrow_row_count(*res)) |
| 136 | + |
| 137 | + var retrievedRows uint64 |
| 138 | + |
| 139 | + for retrievedRows < rowCount { |
| 140 | + select { |
| 141 | + case <-ctx.Done(): |
| 142 | + return nil, ctx.Err() |
| 143 | + default: |
| 144 | + } |
| 145 | + |
| 146 | + rec, err := a.queryArrowArray(res, sc) |
| 147 | + if err != nil { |
| 148 | + return nil, err |
| 149 | + } |
| 150 | + |
| 151 | + recs = append(recs, rec) |
| 152 | + |
| 153 | + retrievedRows += uint64(rec.NumRows()) |
| 154 | + } |
| 155 | + |
| 156 | + return array.NewRecordReader(sc, recs) |
| 157 | +} |
| 158 | + |
| 159 | +// queryArrowSchema fetches the internal arrow schema from the arrow result. |
| 160 | +func (a *Arrow) queryArrowSchema(res *C.duckdb_arrow) (*arrow.Schema, error) { |
| 161 | + schema := C.calloc(1, C.sizeof_struct_ArrowSchema) |
| 162 | + defer func() { |
| 163 | + cdata.ReleaseCArrowSchema((*cdata.CArrowSchema)(schema)) |
| 164 | + C.free(schema) |
| 165 | + }() |
| 166 | + |
| 167 | + if state := C.duckdb_query_arrow_schema( |
| 168 | + *res, |
| 169 | + (*C.duckdb_arrow_schema)(unsafe.Pointer(&schema)), |
| 170 | + ); state == C.DuckDBError { |
| 171 | + return nil, errors.New("duckdb_query_arrow_schema") |
| 172 | + } |
| 173 | + |
| 174 | + sc, err := cdata.ImportCArrowSchema((*cdata.CArrowSchema)(schema)) |
| 175 | + if err != nil { |
| 176 | + return nil, fmt.Errorf("%w: ImportCArrowSchema", err) |
| 177 | + } |
| 178 | + |
| 179 | + return sc, nil |
| 180 | +} |
| 181 | + |
| 182 | +// queryArrowArray fetches an internal arrow array from the arrow result. |
| 183 | +// |
| 184 | +// This function can be called multiple time to get next chunks, |
| 185 | +// which will free the previous out_array. |
| 186 | +func (a *Arrow) queryArrowArray(res *C.duckdb_arrow, sc *arrow.Schema) (arrow.Record, error) { |
| 187 | + arr := C.calloc(1, C.sizeof_struct_ArrowArray) |
| 188 | + defer func() { |
| 189 | + cdata.ReleaseCArrowArray((*cdata.CArrowArray)(arr)) |
| 190 | + C.free(arr) |
| 191 | + }() |
| 192 | + |
| 193 | + if state := C.duckdb_query_arrow_array( |
| 194 | + *res, |
| 195 | + (*C.duckdb_arrow_array)(unsafe.Pointer(&arr)), |
| 196 | + ); state == C.DuckDBError { |
| 197 | + return nil, errors.New("duckdb_query_arrow_array") |
| 198 | + } |
| 199 | + |
| 200 | + rec, err := cdata.ImportCRecordBatchWithSchema((*cdata.CArrowArray)(arr), sc) |
| 201 | + if err != nil { |
| 202 | + return nil, fmt.Errorf("%w: ImportCRecordBatchWithSchema", err) |
| 203 | + } |
| 204 | + |
| 205 | + return rec, nil |
| 206 | +} |
| 207 | + |
| 208 | +func (a *Arrow) execute(s *stmt, args []driver.NamedValue) (*C.duckdb_arrow, error) { |
| 209 | + if s.closed { |
| 210 | + panic("database/sql/driver: misuse of duckdb driver: executeArrow after Close") |
| 211 | + } |
| 212 | + |
| 213 | + if err := s.start(args); err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + var res C.duckdb_arrow |
| 218 | + if state := C.duckdb_execute_prepared_arrow(*s.stmt, &res); state == C.DuckDBError { |
| 219 | + dbErr := C.GoString(C.duckdb_query_arrow_error(res)) |
| 220 | + C.duckdb_destroy_arrow(&res) |
| 221 | + return nil, fmt.Errorf("duckdb_execute_prepared_arrow: %v", dbErr) |
| 222 | + } |
| 223 | + |
| 224 | + return &res, nil |
| 225 | +} |
| 226 | + |
| 227 | +func (a *Arrow) anyArgsToNamedArgs(args []any) []driver.NamedValue { |
| 228 | + if len(args) == 0 { |
| 229 | + return nil |
| 230 | + } |
| 231 | + |
| 232 | + values := make([]driver.Value, len(args)) |
| 233 | + for i, arg := range args { |
| 234 | + values[i] = arg |
| 235 | + } |
| 236 | + |
| 237 | + return argsToNamedArgs(values) |
| 238 | +} |
0 commit comments