1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// 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.
//go:build go1.18
// +build go1.18
package example
import (
"database/sql"
"reflect"
"strconv"
"strings"
"sync/atomic"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/flight/flightsql"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
)
func getArrowTypeFromString(dbtype string) arrow.DataType {
dbtype = strings.ToLower(dbtype)
if dbtype == "" {
// SQLite may not know the type yet.
return &arrow.NullType{}
}
if strings.HasPrefix(dbtype, "varchar") {
return arrow.BinaryTypes.String
}
switch dbtype {
case "tinyint":
return arrow.PrimitiveTypes.Int8
case "mediumint":
return arrow.PrimitiveTypes.Int32
case "int", "integer":
return arrow.PrimitiveTypes.Int64
case "float":
return arrow.PrimitiveTypes.Float32
case "real", "double":
return arrow.PrimitiveTypes.Float64
case "blob":
return arrow.BinaryTypes.Binary
case "text", "date", "char", "clob":
return arrow.BinaryTypes.String
default:
panic("invalid sqlite type: " + dbtype)
}
}
var sqliteDenseUnion = arrow.DenseUnionOf([]arrow.Field{
{Name: "int", Type: arrow.PrimitiveTypes.Int64, Nullable: true},
{Name: "float", Type: arrow.PrimitiveTypes.Float64, Nullable: true},
{Name: "string", Type: arrow.BinaryTypes.String, Nullable: true},
}, []arrow.UnionTypeCode{0, 1, 2})
func getArrowType(c *sql.ColumnType) arrow.DataType {
dbtype := strings.ToLower(c.DatabaseTypeName())
if dbtype == "" {
if c.ScanType() == nil {
return sqliteDenseUnion
}
switch c.ScanType().Kind() {
case reflect.Int8, reflect.Uint8:
return arrow.PrimitiveTypes.Int8
case reflect.Int32, reflect.Uint32:
return arrow.PrimitiveTypes.Int32
case reflect.Int, reflect.Int64, reflect.Uint64:
return arrow.PrimitiveTypes.Int64
case reflect.Float32:
return arrow.PrimitiveTypes.Float32
case reflect.Float64:
return arrow.PrimitiveTypes.Float64
case reflect.String:
return arrow.BinaryTypes.String
}
}
return getArrowTypeFromString(dbtype)
}
const maxBatchSize = 1024
type SqlBatchReader struct {
refCount int64
schema *arrow.Schema
rows *sql.Rows
record arrow.Record
bldr *array.RecordBuilder
err error
rowdest []interface{}
}
func NewSqlBatchReaderWithSchema(mem memory.Allocator, schema *arrow.Schema, rows *sql.Rows) (*SqlBatchReader, error) {
rowdest := make([]interface{}, schema.NumFields())
for i, f := range schema.Fields() {
switch f.Type.ID() {
case arrow.DENSE_UNION, arrow.SPARSE_UNION:
rowdest[i] = new(interface{})
case arrow.UINT8, arrow.INT8:
if f.Nullable {
rowdest[i] = &sql.NullByte{}
} else {
rowdest[i] = new(uint8)
}
case arrow.INT32:
if f.Nullable {
rowdest[i] = &sql.NullInt32{}
} else {
rowdest[i] = new(int32)
}
case arrow.INT64:
if f.Nullable {
rowdest[i] = &sql.NullInt64{}
} else {
rowdest[i] = new(int64)
}
case arrow.FLOAT32, arrow.FLOAT64:
if f.Nullable {
rowdest[i] = &sql.NullFloat64{}
} else {
rowdest[i] = new(float64)
}
case arrow.BINARY:
var b []byte
rowdest[i] = &b
case arrow.STRING:
if f.Nullable {
rowdest[i] = &sql.NullString{}
} else {
rowdest[i] = new(string)
}
}
}
return &SqlBatchReader{
refCount: 1,
bldr: array.NewRecordBuilder(mem, schema),
schema: schema,
rowdest: rowdest,
rows: rows}, nil
}
func NewSqlBatchReader(mem memory.Allocator, rows *sql.Rows) (*SqlBatchReader, error) {
bldr := flightsql.NewColumnMetadataBuilder()
cols, err := rows.ColumnTypes()
if err != nil {
rows.Close()
return nil, err
}
rowdest := make([]interface{}, len(cols))
fields := make([]arrow.Field, len(cols))
for i, c := range cols {
fields[i].Name = c.Name()
if c.Name() == "?" {
fields[i].Name += ":" + strconv.Itoa(i)
}
fields[i].Nullable, _ = c.Nullable()
fields[i].Type = getArrowType(c)
fields[i].Metadata = getColumnMetadata(bldr, getSqlTypeFromTypeName(c.DatabaseTypeName()), "")
switch fields[i].Type.ID() {
case arrow.DENSE_UNION, arrow.SPARSE_UNION:
rowdest[i] = new(interface{})
case arrow.UINT8, arrow.INT8:
if fields[i].Nullable {
rowdest[i] = &sql.NullByte{}
} else {
rowdest[i] = new(uint8)
}
case arrow.INT32:
if fields[i].Nullable {
rowdest[i] = &sql.NullInt32{}
} else {
rowdest[i] = new(int32)
}
case arrow.INT64:
if fields[i].Nullable {
rowdest[i] = &sql.NullInt64{}
} else {
rowdest[i] = new(int64)
}
case arrow.FLOAT64, arrow.FLOAT32:
if fields[i].Nullable {
rowdest[i] = &sql.NullFloat64{}
} else {
rowdest[i] = new(float64)
}
case arrow.BINARY:
var b []byte
rowdest[i] = &b
case arrow.STRING:
if fields[i].Nullable {
rowdest[i] = &sql.NullString{}
} else {
rowdest[i] = new(string)
}
}
}
schema := arrow.NewSchema(fields, nil)
return &SqlBatchReader{
refCount: 1,
bldr: array.NewRecordBuilder(mem, schema),
schema: schema,
rowdest: rowdest,
rows: rows}, nil
}
func (r *SqlBatchReader) Retain() {
atomic.AddInt64(&r.refCount, 1)
}
func (r *SqlBatchReader) Release() {
debug.Assert(atomic.LoadInt64(&r.refCount) > 0, "too many releases")
if atomic.AddInt64(&r.refCount, -1) == 0 {
r.rows.Close()
r.rows, r.schema, r.rowdest = nil, nil, nil
r.bldr.Release()
r.bldr = nil
if r.record != nil {
r.record.Release()
r.record = nil
}
}
}
func (r *SqlBatchReader) Schema() *arrow.Schema { return r.schema }
func (r *SqlBatchReader) Record() arrow.Record { return r.record }
func (r *SqlBatchReader) Err() error { return r.err }
func (r *SqlBatchReader) Next() bool {
if r.record != nil {
r.record.Release()
r.record = nil
}
rows := 0
for rows < maxBatchSize && r.rows.Next() {
if err := r.rows.Scan(r.rowdest...); err != nil {
// Not really useful except for testing Flight SQL clients
detail := wrapperspb.StringValue{Value: r.schema.String()}
if st, sterr := status.New(codes.Unknown, err.Error()).WithDetails(&detail); sterr != nil {
r.err = err
} else {
r.err = st.Err()
}
return false
}
for i, v := range r.rowdest {
fb := r.bldr.Field(i)
switch v := v.(type) {
case *uint8:
fb.(*array.Uint8Builder).Append(*v)
case *sql.NullByte:
if !v.Valid {
fb.AppendNull()
} else {
fb.(*array.Uint8Builder).Append(v.Byte)
}
case *int64:
fb.(*array.Int64Builder).Append(*v)
case *sql.NullInt64:
if !v.Valid {
fb.AppendNull()
} else {
fb.(*array.Int64Builder).Append(v.Int64)
}
case *int32:
fb.(*array.Int32Builder).Append(*v)
case *sql.NullInt32:
if !v.Valid {
fb.AppendNull()
} else {
fb.(*array.Int32Builder).Append(v.Int32)
}
case *float64:
switch b := fb.(type) {
case *array.Float64Builder:
b.Append(*v)
case *array.Float32Builder:
b.Append(float32(*v))
}
case *sql.NullFloat64:
if !v.Valid {
fb.AppendNull()
} else {
switch b := fb.(type) {
case *array.Float64Builder:
b.Append(v.Float64)
case *array.Float32Builder:
b.Append(float32(v.Float64))
}
}
case *[]byte:
if v == nil {
fb.AppendNull()
} else {
fb.(*array.BinaryBuilder).Append(*v)
}
case *string:
fb.(*array.StringBuilder).Append(*v)
case *sql.NullString:
if !v.Valid {
fb.AppendNull()
} else {
fb.(*array.StringBuilder).Append(v.String)
}
}
}
rows++
}
r.record = r.bldr.NewRecord()
return rows > 0
}
|