Skip to content

Commit 1265554

Browse files
authored
feat!: Upgrade to SDK V3 make Column.Type an arrow.DataType (#854)
This upgrades the SDK to v3 so `schema.Column.Type` now holds `arrow.DataType` and removes all CQTypes. This also removes `source plugins because this will come in a follow-up PR (This will ensure this sdk wont be merged into sources). Instead of #832
1 parent ea90e4e commit 1265554

File tree

105 files changed

+844
-11619
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+844
-11619
lines changed

Makefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ test:
66
lint:
77
golangci-lint run
88

9-
.PHONY: gen-proto
10-
gen-proto:
11-
protoc --proto_path=. --go_out . --go_opt=module="github.com/cloudquery/plugin-sdk/v2" --go-grpc_out=. --go-grpc_opt=module="github.com/cloudquery/plugin-sdk/v2" cloudquery/base/v0/base.proto cloudquery/destination/v0/destination.proto cloudquery/source/v0/source.proto
12-
protoc --proto_path=. --go_out . --go_opt=module="github.com/cloudquery/plugin-sdk/v2" --go-grpc_out=. --go-grpc_opt=module="github.com/cloudquery/plugin-sdk/v2" cloudquery/source/v1/source.proto
13-
protoc --proto_path=. --go_out . --go_opt=module="github.com/cloudquery/plugin-sdk/v2" --go-grpc_out=. --go-grpc_opt=module="github.com/cloudquery/plugin-sdk/v2" cloudquery/discovery/v0/discovery.proto
14-
159
.PHONY: benchmark
1610
benchmark:
1711
go test -bench=Benchmark -run="^$$" ./...

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CloudQuery Plugin SDK
22

3-
[![Go Reference](https://pkg.go.dev/badge/github.com/cloudquery/plugin-sdk/v2#section-readme.svg)](https://pkg.go.dev/github.com/cloudquery/plugin-sdk/v2#section-readme)
4-
[![Go Report Card](https://goreportcard.com/badge/github.com/cloudquery/plugin-sdk/v2)](https://goreportcard.com/report/github.com/cloudquery/plugin-sdk/v2)
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/cloudquery/plugin-sdk/v3#section-readme.svg)](https://pkg.go.dev/github.com/cloudquery/plugin-sdk/v3#section-readme)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/cloudquery/plugin-sdk/v3)](https://goreportcard.com/report/github.com/cloudquery/plugin-sdk/v3)
55
[![Unit tests](https://github.com/cloudquery/plugin-sdk/actions/workflows/unittest.yml/badge.svg)](https://github.com/cloudquery/plugin-sdk/actions/workflows/unittest.yml)
66

77
CloudQuery SDK enables building CloudQuery source and destination plugins.

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/cloudquery/plugin-sdk/v2
1+
module github.com/cloudquery/plugin-sdk/v3
22

33
go 1.19
44

@@ -30,6 +30,7 @@ replace github.com/apache/arrow/go/v13 => github.com/cloudquery/arrow/go/v13 v13
3030
require (
3131
github.com/andybalholm/brotli v1.0.5 // indirect
3232
github.com/apache/thrift v0.16.0 // indirect
33+
github.com/cloudquery/plugin-sdk/v2 v2.7.0 // indirect
3334
github.com/davecgh/go-spew v1.1.1 // indirect
3435
github.com/ghodss/yaml v1.0.0 // indirect
3536
github.com/golang/protobuf v1.5.3 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ github.com/cloudquery/arrow/go/v13 v13.0.0-20230509053643-898a79b1d3c8 h1:CmgLSE
4949
github.com/cloudquery/arrow/go/v13 v13.0.0-20230509053643-898a79b1d3c8/go.mod h1:/XatdE3kDIBqZKhZ7OBUHwP2jaASDFZHqF4puOWM8po=
5050
github.com/cloudquery/plugin-pb-go v1.0.5 h1:Du6pXI2JZRtgWfc0K69/gtNcyHICqEbAmfJXTARAqCc=
5151
github.com/cloudquery/plugin-pb-go v1.0.5/go.mod h1:vAGA27psem7ZZNAY4a3S9TKuA/JDQWstjKcHPJX91Mc=
52+
github.com/cloudquery/plugin-sdk/v2 v2.6.0 h1:fRD9XoHhD5KN5A/c8S8ZRqqGitr1Y2GNofaMV5TaR9Y=
53+
github.com/cloudquery/plugin-sdk/v2 v2.6.0/go.mod h1:q9Q3aX6yE1i9NRDo91Nty+GHJSg+Eb4EFvg7+4ARjAw=
54+
github.com/cloudquery/plugin-sdk/v2 v2.7.0 h1:hRXsdEiaOxJtsn/wZMFQC9/jPfU1MeMK3KF+gPGqm7U=
55+
github.com/cloudquery/plugin-sdk/v2 v2.7.0/go.mod h1:pAX6ojIW99b/Vg4CkhnsGkRIzNaVEceYMR+Bdit73ug=
5256
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
5357
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
5458
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=

internal/memdb/memdb.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import (
1111
"github.com/apache/arrow/go/v13/arrow"
1212
"github.com/apache/arrow/go/v13/arrow/array"
1313
"github.com/cloudquery/plugin-pb-go/specs"
14-
"github.com/cloudquery/plugin-sdk/v2/plugins/destination"
15-
"github.com/cloudquery/plugin-sdk/v2/schema"
14+
"github.com/cloudquery/plugin-sdk/v3/plugins/destination"
15+
"github.com/cloudquery/plugin-sdk/v3/schema"
1616
"github.com/rs/zerolog"
1717
)
1818

1919
// client is mostly used for testing the destination plugin.
2020
type client struct {
2121
spec specs.Destination
2222
memoryDB map[string][]arrow.Record
23-
tables map[string]*arrow.Schema
23+
tables map[string]*schema.Table
2424
memoryDBLock sync.RWMutex
2525
errOnWrite bool
2626
blockingWrite bool
@@ -64,7 +64,7 @@ func getTestLogger(t *testing.T) zerolog.Logger {
6464
func NewClient(_ context.Context, _ zerolog.Logger, spec specs.Destination) (destination.Client, error) {
6565
return &client{
6666
memoryDB: make(map[string][]arrow.Record),
67-
tables: make(map[string]*arrow.Schema),
67+
tables: make(map[string]*schema.Table),
6868
spec: spec,
6969
}, nil
7070
}
@@ -73,9 +73,9 @@ func NewClientErrOnNew(context.Context, zerolog.Logger, specs.Destination) (dest
7373
return nil, fmt.Errorf("newTestDestinationMemDBClientErrOnNew")
7474
}
7575

76-
func (c *client) overwrite(table *arrow.Schema, data arrow.Record) {
77-
pksIndex := schema.PrimaryKeyIndices(table)
78-
tableName := schema.TableName(table)
76+
func (c *client) overwrite(table *schema.Table, data arrow.Record) {
77+
pksIndex := table.PrimaryKeysIndexes()
78+
tableName := table.Name
7979
for i, row := range c.memoryDB[tableName] {
8080
found := true
8181
for _, pkIndex := range pksIndex {
@@ -94,16 +94,17 @@ func (c *client) overwrite(table *arrow.Schema, data arrow.Record) {
9494
c.memoryDB[tableName] = append(c.memoryDB[tableName], data)
9595
}
9696

97-
func (c *client) Migrate(_ context.Context, tables schema.Schemas) error {
97+
func (c *client) Migrate(_ context.Context, tables schema.Tables) error {
9898
for _, table := range tables {
99-
tableName := schema.TableName(table)
99+
tableName := table.Name
100100
memTable := c.memoryDB[tableName]
101101
if memTable == nil {
102102
c.memoryDB[tableName] = make([]arrow.Record, 0)
103103
c.tables[tableName] = table
104104
continue
105105
}
106-
changes := schema.GetSchemaChanges(table, c.tables[tableName])
106+
107+
changes := table.GetChanges(c.tables[tableName])
107108
// memdb doesn't support any auto-migrate
108109
if changes == nil {
109110
continue
@@ -114,16 +115,15 @@ func (c *client) Migrate(_ context.Context, tables schema.Schemas) error {
114115
return nil
115116
}
116117

117-
func (c *client) Read(_ context.Context, table *arrow.Schema, source string, res chan<- arrow.Record) error {
118-
tableName := schema.TableName(table)
118+
func (c *client) Read(_ context.Context, table *schema.Table, source string, res chan<- arrow.Record) error {
119+
tableName := table.Name
119120
if c.memoryDB[tableName] == nil {
120121
return nil
121122
}
122-
indices := table.FieldIndices(schema.CqSourceNameColumn.Name)
123-
if len(indices) == 0 {
123+
sourceColIndex := table.Columns.Index(schema.CqSourceNameColumn.Name)
124+
if sourceColIndex == -1 {
124125
return fmt.Errorf("table %s doesn't have source column", tableName)
125126
}
126-
sourceColIndex := indices[0]
127127
var sortedRes []arrow.Record
128128
c.memoryDBLock.RLock()
129129
for _, row := range c.memoryDB[tableName] {
@@ -140,7 +140,7 @@ func (c *client) Read(_ context.Context, table *arrow.Schema, source string, res
140140
return nil
141141
}
142142

143-
func (c *client) Write(ctx context.Context, _ schema.Schemas, resources <-chan arrow.Record) error {
143+
func (c *client) Write(ctx context.Context, _ schema.Tables, resources <-chan arrow.Record) error {
144144
if c.errOnWrite {
145145
return fmt.Errorf("errOnWrite")
146146
}
@@ -154,21 +154,23 @@ func (c *client) Write(ctx context.Context, _ schema.Schemas, resources <-chan a
154154

155155
for resource := range resources {
156156
c.memoryDBLock.Lock()
157-
tableName, err := schema.TableNameFromSchema(resource.Schema())
158-
if err != nil {
159-
return err
157+
sc := resource.Schema()
158+
tableName, ok := sc.Metadata().GetValue(schema.MetadataTableName)
159+
if !ok {
160+
return fmt.Errorf("table name not found in schema metadata")
160161
}
162+
table := c.tables[tableName]
161163
if c.spec.WriteMode == specs.WriteModeAppend {
162164
c.memoryDB[tableName] = append(c.memoryDB[tableName], resource)
163165
} else {
164-
c.overwrite(resource.Schema(), resource)
166+
c.overwrite(table, resource)
165167
}
166168
c.memoryDBLock.Unlock()
167169
}
168170
return nil
169171
}
170172

171-
func (c *client) WriteTableBatch(ctx context.Context, table *arrow.Schema, resources []arrow.Record) error {
173+
func (c *client) WriteTableBatch(ctx context.Context, table *schema.Table, resources []arrow.Record) error {
172174
if c.errOnWrite {
173175
return fmt.Errorf("errOnWrite")
174176
}
@@ -179,7 +181,7 @@ func (c *client) WriteTableBatch(ctx context.Context, table *arrow.Schema, resou
179181
}
180182
return nil
181183
}
182-
tableName := schema.TableName(table)
184+
tableName := table.Name
183185
for _, resource := range resources {
184186
c.memoryDBLock.Lock()
185187
if c.spec.WriteMode == specs.WriteModeAppend {
@@ -201,17 +203,17 @@ func (c *client) Close(context.Context) error {
201203
return nil
202204
}
203205

204-
func (c *client) DeleteStale(ctx context.Context, tables schema.Schemas, source string, syncTime time.Time) error {
206+
func (c *client) DeleteStale(ctx context.Context, tables schema.Tables, source string, syncTime time.Time) error {
205207
for _, table := range tables {
206208
c.deleteStaleTable(ctx, table, source, syncTime)
207209
}
208210
return nil
209211
}
210212

211-
func (c *client) deleteStaleTable(_ context.Context, table *arrow.Schema, source string, syncTime time.Time) {
212-
sourceColIndex := table.FieldIndices(schema.CqSourceNameColumn.Name)[0]
213-
syncColIndex := table.FieldIndices(schema.CqSyncTimeColumn.Name)[0]
214-
tableName := schema.TableName(table)
213+
func (c *client) deleteStaleTable(_ context.Context, table *schema.Table, source string, syncTime time.Time) {
214+
sourceColIndex := table.Columns.Index(schema.CqSourceNameColumn.Name)
215+
syncColIndex := table.Columns.Index(schema.CqSyncTimeColumn.Name)
216+
tableName := table.Name
215217
var filteredTable []arrow.Record
216218
for i, row := range c.memoryDB[tableName] {
217219
if row.Column(sourceColIndex).(*array.String).Value(0) == source {

internal/memdb/memdb_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
"github.com/apache/arrow/go/v13/arrow"
99
"github.com/cloudquery/plugin-pb-go/specs"
10-
"github.com/cloudquery/plugin-sdk/v2/plugins/destination"
11-
"github.com/cloudquery/plugin-sdk/v2/testdata"
10+
"github.com/cloudquery/plugin-sdk/v3/plugins/destination"
11+
"github.com/cloudquery/plugin-sdk/v3/schema"
1212
"github.com/google/uuid"
1313
"github.com/rs/zerolog"
1414
"github.com/stretchr/testify/require"
@@ -112,23 +112,23 @@ func TestOnWriteError(t *testing.T) {
112112
if err := p.Init(ctx, getTestLogger(t), specs.Destination{}); err != nil {
113113
t.Fatal(err)
114114
}
115-
table := testdata.TestTable("test")
116-
tables := []*arrow.Schema{
117-
table.ToArrowSchema(),
115+
table := schema.TestTable("test")
116+
tables := schema.Tables{
117+
table,
118118
}
119119
sourceName := "TestDestinationOnWriteError"
120120
syncTime := time.Now()
121121
sourceSpec := specs.Source{
122122
Name: sourceName,
123123
}
124124
ch := make(chan arrow.Record, 1)
125-
opts := testdata.GenTestDataOptions{
125+
opts := schema.GenTestDataOptions{
126126
SourceName: "test",
127127
SyncTime: time.Now(),
128128
MaxRows: 1,
129129
StableUUID: uuid.Nil,
130130
}
131-
record := testdata.GenTestData(table.ToArrowSchema(), opts)[0]
131+
record := schema.GenTestData(table, opts)[0]
132132
ch <- record
133133
close(ch)
134134
err := p.Write(ctx, sourceSpec, tables, syncTime, ch)
@@ -147,9 +147,9 @@ func TestOnWriteCtxCancelled(t *testing.T) {
147147
if err := p.Init(ctx, getTestLogger(t), specs.Destination{}); err != nil {
148148
t.Fatal(err)
149149
}
150-
table := testdata.TestTable("test")
151-
tables := []*arrow.Schema{
152-
table.ToArrowSchema(),
150+
table := schema.TestTable("test")
151+
tables := schema.Tables{
152+
table,
153153
}
154154
sourceName := "TestDestinationOnWriteError"
155155
syncTime := time.Now()
@@ -158,13 +158,13 @@ func TestOnWriteCtxCancelled(t *testing.T) {
158158
}
159159
ch := make(chan arrow.Record, 1)
160160
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
161-
opts := testdata.GenTestDataOptions{
161+
opts := schema.GenTestDataOptions{
162162
SourceName: "test",
163163
SyncTime: time.Now(),
164164
MaxRows: 1,
165165
StableUUID: uuid.Nil,
166166
}
167-
record := testdata.GenTestData(table.ToArrowSchema(), opts)[0]
167+
record := schema.GenTestData(table, opts)[0]
168168
ch <- record
169169
defer cancel()
170170
err := p.Write(ctx, sourceSpec, tables, syncTime, ch)

internal/pk/pk.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import (
44
"strings"
55

66
"github.com/apache/arrow/go/v13/arrow"
7-
"github.com/cloudquery/plugin-sdk/v2/schema"
7+
"github.com/cloudquery/plugin-sdk/v3/schema"
88
)
99

1010
func String(resource arrow.Record) string {
1111
sc := resource.Schema()
12-
pkIndices := schema.PrimaryKeyIndices(sc)
12+
table, err := schema.NewTableFromArrowSchema(sc)
13+
if err != nil {
14+
panic(err)
15+
}
16+
pkIndices := table.PrimaryKeysIndexes()
1317
parts := make([]string, 0, len(pkIndices))
1418
for _, i := range pkIndices {
1519
parts = append(parts, resource.Column(i).String())

0 commit comments

Comments
 (0)