-
Notifications
You must be signed in to change notification settings - Fork 500
/
read.go
176 lines (164 loc) · 3.89 KB
/
read.go
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
package client
import (
"context"
"database/sql/driver"
"fmt"
"math/big"
"strings"
"time"
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
"github.com/apache/arrow/go/v16/arrow/decimal128"
"github.com/apache/arrow/go/v16/arrow/memory"
"github.com/cloudquery/plugin-sdk/v4/scalar"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
const (
readSQL = "SELECT %s FROM %s"
)
func (c *Client) Read(ctx context.Context, table *schema.Table, res chan<- arrow.Record) error {
colNames := make([]string, 0, len(table.Columns))
for _, col := range table.Columns {
if c.pgType == pgTypeCrateDB {
colNames = append(colNames, pgx.Identifier{strings.Trim(col.Name, "_")}.Sanitize())
continue
}
colNames = append(colNames, pgx.Identifier{col.Name}.Sanitize())
}
cols := strings.Join(colNames, ",")
tableName := table.Name
sql := fmt.Sprintf(readSQL, cols, pgx.Identifier{tableName}.Sanitize())
rows, err := c.conn.Query(ctx, sql)
if err != nil {
return err
}
for rows.Next() {
values, err := rows.Values()
if err != nil {
return err
}
arrowSchema := table.ToArrowSchema()
rb := array.NewRecordBuilder(memory.DefaultAllocator, arrowSchema)
for i := range values {
val, err := prepareValueForResourceSet(arrowSchema.Field(i).Type, values[i])
if err != nil {
return err
}
s := scalar.NewScalar(arrowSchema.Field(i).Type)
if err := s.Set(val); err != nil {
return err
}
scalar.AppendToBuilder(rb.Field(i), s)
}
res <- rb.NewRecord()
}
rows.Close()
return nil
}
func prepareValueForResourceSet(dataType arrow.DataType, v any) (any, error) {
if v == nil {
return nil, nil
}
switch tp := dataType.(type) {
case *arrow.Decimal128Type:
if vt, ok := v.(pgtype.Numeric); ok {
if vt.Valid {
v = decimal128.FromBigInt(vt.Int)
} else {
v = nil
}
}
case *arrow.ListType:
vl := v.([]any)
for i := range vl {
var err error
vl[i], err = prepareValueForResourceSet(tp.Elem(), vl[i])
if err != nil {
return nil, err
}
}
return vl, nil
case *arrow.LargeListType:
vl := v.([]any)
for i := range vl {
var err error
vl[i], err = prepareValueForResourceSet(tp.Elem(), vl[i])
if err != nil {
return nil, err
}
}
return vl, nil
case *arrow.StringType:
if value, ok := v.(driver.Valuer); ok {
if value == driver.Valuer(nil) {
v = nil
} else {
val, err := value.Value()
if err != nil {
return nil, err
}
if s, ok := val.(string); ok {
v = s
}
}
}
return v, nil
case *arrow.Time32Type:
switch vt := v.(type) {
case pgtype.Time:
t, err := vt.TimeValue()
if err != nil {
return nil, err
}
return stringForTime32(t, tp.Unit), nil
case string:
v = vt
}
case *arrow.Time64Type:
switch vt := v.(type) {
case pgtype.Time:
t, err := vt.TimeValue()
if err != nil {
return nil, err
}
return stringForTime64(t, tp.Unit), nil
case string:
v = vt
}
case *arrow.Uint64Type:
if vt, ok := v.(pgtype.Numeric); ok {
if !vt.Valid {
v = nil
} else {
v = numericToUint64(vt)
}
}
return v, nil
}
return v, nil
}
var (
big10 = big.NewInt(10)
)
func numericToUint64(n pgtype.Numeric) uint64 {
if n.Exp < 0 {
panic("unsupported negative exponent")
}
val := n.Int.Uint64()
if n.Exp == 0 {
return val
}
// exp can only be positive for our use-case
return new(big.Int).Mul(
new(big.Int).SetUint64(val),
new(big.Int).Exp(big10, big.NewInt(int64(n.Exp)), nil),
).Uint64()
}
func stringForTime32(t pgtype.Time, unit arrow.TimeUnit) string {
return arrow.Time32((time.Duration(t.Microseconds) * time.Microsecond) / unit.Multiplier()).FormattedString(unit)
}
func stringForTime64(t pgtype.Time, unit arrow.TimeUnit) string {
return arrow.Time64((time.Duration(t.Microseconds) * time.Microsecond) / unit.Multiplier()).FormattedString(unit)
}