Skip to content

Commit 5700cf7

Browse files
committed
Remove BulkUpsertData implementations from public
1 parent 5e4f7d6 commit 5700cf7

File tree

2 files changed

+97
-57
lines changed

2 files changed

+97
-57
lines changed

table/table.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,23 @@ type BulkUpsertData interface {
596596
ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error
597597
}
598598

599-
type BulkUpsertRows struct {
599+
type bulkUpsertRows struct {
600600
Rows value.Value
601601
}
602602

603-
func (data BulkUpsertRows) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
603+
func (data bulkUpsertRows) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
604604
req.Rows = value.ToYDB(data.Rows, a)
605605

606606
return nil
607607
}
608608

609-
type BulkUpsertCsv struct {
609+
func NewBulkUpsertRows(rows value.Value) bulkUpsertRows {
610+
return bulkUpsertRows{
611+
Rows: rows,
612+
}
613+
}
614+
615+
type bulkUpsertCsv struct {
610616
Data []byte
611617
Options []CsvFormatOption
612618
}
@@ -615,7 +621,7 @@ type CsvFormatOption interface {
615621
ApplyCsvFormatOption(req *BulkUpsertRequest) (err error)
616622
}
617623

618-
func (data BulkUpsertCsv) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
624+
func (data bulkUpsertCsv) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
619625
req.Data = data.Data
620626

621627
var err error
@@ -631,8 +637,8 @@ func (data BulkUpsertCsv) ApplyBulkUpsertRequest(a *allocator.Allocator, req *Bu
631637
return err
632638
}
633639

634-
func NewBulkUpsertCsv(data []byte, opts ...CsvFormatOption) BulkUpsertCsv {
635-
return BulkUpsertCsv{
640+
func NewBulkUpsertCsv(data []byte, opts ...CsvFormatOption) bulkUpsertCsv {
641+
return bulkUpsertCsv{
636642
Data: data,
637643
Options: opts,
638644
}
@@ -717,7 +723,7 @@ func WithCsvSkipRows(count uint32) CsvFormatOption {
717723
return &csvSkipRowsOption{count}
718724
}
719725

720-
type BulkUpsertArrow struct {
726+
type bulkUpsertArrow struct {
721727
Data []byte
722728
Options []ArrowFormatOption
723729
}
@@ -726,7 +732,7 @@ type ArrowFormatOption interface {
726732
ApplyArrowFormatOption(req *BulkUpsertRequest) (err error)
727733
}
728734

729-
func (data BulkUpsertArrow) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
735+
func (data bulkUpsertArrow) ApplyBulkUpsertRequest(a *allocator.Allocator, req *BulkUpsertRequest) error {
730736
req.Data = data.Data
731737

732738
var err error
@@ -742,8 +748,8 @@ func (data BulkUpsertArrow) ApplyBulkUpsertRequest(a *allocator.Allocator, req *
742748
return err
743749
}
744750

745-
func NewBulkUpsertArrow(data []byte, opts ...ArrowFormatOption) BulkUpsertArrow {
746-
return BulkUpsertArrow{
751+
func NewBulkUpsertArrow(data []byte, opts ...ArrowFormatOption) bulkUpsertArrow {
752+
return bulkUpsertArrow{
747753
Data: data,
748754
Options: opts,
749755
}

tests/integration/table_bulk_upsert_test.go

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1818
)
1919

20-
func TestTableBulkUpsert(t *testing.T) {
20+
func TestTableBulkUpsertSession(t *testing.T) {
2121
var (
2222
scope = newScope(t)
2323
driver = scope.Driver()
@@ -39,58 +39,40 @@ func TestTableBulkUpsert(t *testing.T) {
3939
return s.BulkUpsert(ctx, tablePath, types.ListValue(rows...))
4040
})
4141
scope.Require.NoError(err)
42-
}
4342

44-
func assertIdValueImpl(ctx context.Context, t *testing.T, tableName string, id int64, val *string) {
45-
ctx, cancel := context.WithCancel(context.Background())
46-
defer cancel()
43+
for i := int64(0); i < 10; i++ {
44+
val := fmt.Sprintf("value for %v", i)
45+
assertIdValue(scope.Ctx, t, tablePath, i, val)
46+
}
47+
}
4748

48-
db, err := ydb.Open(ctx,
49-
os.Getenv("YDB_CONNECTION_STRING"),
50-
// ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
49+
func TestTableBulkUpsert(t *testing.T) {
50+
var (
51+
scope = newScope(t)
52+
driver = scope.Driver()
53+
tablePath = scope.TablePath()
5154
)
52-
require.NoError(t, err)
53-
err = db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) (err error) {
54-
res, err := tx.Execute(ctx, fmt.Sprintf("SELECT val FROM `%s` WHERE id = %d", tableName, id), nil)
55-
if err != nil {
56-
return err
57-
}
58-
err = res.NextResultSetErr(ctx)
59-
if err != nil {
60-
return err
61-
}
62-
require.EqualValues(t, 1, res.ResultSetCount())
63-
if !res.NextRow() {
64-
if err = res.Err(); err != nil {
65-
return err
66-
}
67-
return fmt.Errorf("unexpected empty result set")
68-
}
69-
var resultVal *string
70-
err = res.ScanNamed(
71-
named.Optional("val", &resultVal),
72-
)
73-
if err != nil {
74-
return err
75-
}
76-
if val != nil {
77-
require.NotEmpty(t, resultVal)
78-
require.EqualValues(t, *val, *resultVal)
79-
} else {
80-
require.Nil(t, resultVal)
81-
}
8255

83-
return res.Err()
84-
}, table.WithTxSettings(table.TxSettings(table.WithSnapshotReadOnly())), table.WithIdempotent())
85-
require.NoError(t, err)
86-
}
56+
// upsert
57+
var rows []types.Value
8758

88-
func assertIdValue(ctx context.Context, t *testing.T, tableName string, id int64, val string) {
89-
assertIdValueImpl(ctx, t, tableName, id, &val)
90-
}
59+
for i := int64(0); i < 10; i++ {
60+
val := fmt.Sprintf("value for %v", i)
61+
rows = append(rows, types.StructValue(
62+
types.StructFieldValue("id", types.Int64Value(i)),
63+
types.StructFieldValue("val", types.TextValue(val)),
64+
))
65+
}
9166

92-
func assertIdValueNil(ctx context.Context, t *testing.T, tableName string, id int64) {
93-
assertIdValueImpl(ctx, t, tableName, id, nil)
67+
err := driver.Table().BulkUpsert(scope.Ctx, tablePath, table.NewBulkUpsertRows(
68+
types.ListValue(rows...),
69+
))
70+
scope.Require.NoError(err)
71+
72+
for i := int64(0); i < 10; i++ {
73+
val := fmt.Sprintf("value for %v", i)
74+
assertIdValue(scope.Ctx, t, tablePath, i, val)
75+
}
9476
}
9577

9678
func TestTableCsvBulkUpsert(t *testing.T) {
@@ -209,3 +191,55 @@ func TestTableArrowBulkUpsert(t *testing.T) {
209191
assertIdValue(scope.Ctx, t, tablePath, 123, "data1")
210192
assertIdValue(scope.Ctx, t, tablePath, 234, "data2")
211193
}
194+
195+
func assertIdValueImpl(ctx context.Context, t *testing.T, tableName string, id int64, val *string) {
196+
ctx, cancel := context.WithCancel(context.Background())
197+
defer cancel()
198+
199+
db, err := ydb.Open(ctx,
200+
os.Getenv("YDB_CONNECTION_STRING"),
201+
// ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")),
202+
)
203+
require.NoError(t, err)
204+
err = db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) (err error) {
205+
res, err := tx.Execute(ctx, fmt.Sprintf("SELECT val FROM `%s` WHERE id = %d", tableName, id), nil)
206+
if err != nil {
207+
return err
208+
}
209+
err = res.NextResultSetErr(ctx)
210+
if err != nil {
211+
return err
212+
}
213+
require.EqualValues(t, 1, res.ResultSetCount())
214+
if !res.NextRow() {
215+
if err = res.Err(); err != nil {
216+
return err
217+
}
218+
return fmt.Errorf("unexpected empty result set")
219+
}
220+
var resultVal *string
221+
err = res.ScanNamed(
222+
named.Optional("val", &resultVal),
223+
)
224+
if err != nil {
225+
return err
226+
}
227+
if val != nil {
228+
require.NotEmpty(t, resultVal)
229+
require.EqualValues(t, *val, *resultVal)
230+
} else {
231+
require.Nil(t, resultVal)
232+
}
233+
234+
return res.Err()
235+
}, table.WithTxSettings(table.TxSettings(table.WithSnapshotReadOnly())), table.WithIdempotent())
236+
require.NoError(t, err)
237+
}
238+
239+
func assertIdValue(ctx context.Context, t *testing.T, tableName string, id int64, val string) {
240+
assertIdValueImpl(ctx, t, tableName, id, &val)
241+
}
242+
243+
func assertIdValueNil(ctx context.Context, t *testing.T, tableName string, id int64) {
244+
assertIdValueImpl(ctx, t, tableName, id, nil)
245+
}

0 commit comments

Comments
 (0)