Skip to content

Commit 10d41a8

Browse files
authored
Merge pull request #794 from ydb-platform/read-rows
* Added `table.Session.ReadRows`
2 parents d3f9e4f + dcd89dc commit 10d41a8

File tree

9 files changed

+276
-11
lines changed

9 files changed

+276
-11
lines changed

.github/workflows/publish.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
env:
2525
VERSION_CHANGE: ${{ github.event.inputs.version-change }}
2626
RELEASE_CANDIDATE: ${{ github.event.inputs.release-candidate }}
27-
VERSION_FILE: internal/meta/version.go
27+
VERSION_FILE: internal/version/version.go
2828
CHANGELOG_FILE: CHANGELOG.md
2929
GITHUB_TOKEN: ${{ secrets.YDB_PLATFORM_BOT_TOKEN_REPO }}
3030
steps:
@@ -39,9 +39,9 @@ jobs:
3939
echo "CHANGELOG empty"
4040
exit 1;
4141
fi;
42-
MAJOR=$(cat $VERSION_FILE | grep VersionMajor | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
43-
MINOR=$(cat $VERSION_FILE | grep VersionMinor | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
44-
PATCH=$(cat $VERSION_FILE | grep VersionPatch | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
42+
MAJOR=$(cat $VERSION_FILE | grep Major | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
43+
MINOR=$(cat $VERSION_FILE | grep Minor | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
44+
PATCH=$(cat $VERSION_FILE | grep Patch | sed -e 's/^.*\ \(=\ \)*\(\"\)*\([0-9]*\)\(\"\)*.*/\3/g');
4545
LAST_TAG="v$MAJOR.$MINOR.$PATCH";
4646
if [ "$VERSION_CHANGE" = "MINOR" ]
4747
then
@@ -57,8 +57,8 @@ jobs:
5757
RC=$(git tag | grep "v$MAJOR.$MINOR.$PATCH-rc" | wc -l);
5858
TAG="v$MAJOR.$MINOR.$PATCH-rc$RC";
5959
else
60-
sed -e 's/VersionMinor = "\([0-9]*\)"/VersionMinor = "'$MINOR'"/g' -i $VERSION_FILE;
61-
sed -e 's/VersionPatch = "\([0-9]*\)"/VersionPatch = "'$PATCH'"/g' -i $VERSION_FILE;
60+
sed -e 's/Minor = "\([0-9]*\)"/Minor = "'$MINOR'"/g' -i $VERSION_FILE;
61+
sed -e 's/Patch = "\([0-9]*\)"/Patch = "'$PATCH'"/g' -i $VERSION_FILE;
6262
git add $VERSION_FILE;
6363
echo "## v$MAJOR.$MINOR.$PATCH" >> $CHANGELOG_FILE.tmp
6464
cat $CHANGELOG_FILE >> $CHANGELOG_FILE.tmp

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added `table.Session.ReadRows` method for getting rows by keys
12
* Added `table/options.ChangefeedFormatDynamoDBStreamsJSON` format of `DynamoDB` change feeds
23

34
## v3.48.8

examples/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ github.com/ydb-platform/xorm v0.0.3 h1:MXk42lANB6r/MMLg/XdJfyXJycGUDlCeLiMlLGDKV
933933
github.com/ydb-platform/xorm v0.0.3/go.mod h1:hFsU7EUF0o3S+l5c0eyP2yPVjJ0d4gsFdqCsyazzwBc=
934934
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230528143953-42c825ace222 h1:8ddsk8HKBkVPH8w3k81si6SeCVJIAtw8dnw+s3h0ciE=
935935
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230528143953-42c825ace222/go.mod h1:Er+FePu1dNUieD+XTMDduGpQuCPssK5Q4BjF+IIXJ3I=
936+
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230710134411-552fdf84e4f1 h1:+nKxgNmwZXpWqFDfQfFhXlnd2AfABNE8Z2QEBdmrxRg=
937+
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230710134411-552fdf84e4f1/go.mod h1:Er+FePu1dNUieD+XTMDduGpQuCPssK5Q4BjF+IIXJ3I=
936938
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230801151335-81e01be38941 h1:QXgmY0vkYtOoGEXnTjWkyxhOkIzCosMrnoDyYjrv71Q=
937939
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230801151335-81e01be38941/go.mod h1:Er+FePu1dNUieD+XTMDduGpQuCPssK5Q4BjF+IIXJ3I=
938940
github.com/ydb-platform/ydb-go-sdk-auth-environ v0.1.2 h1:EYSI1kulnHb0H0zt3yOw4cRj4ABMSMGwNe43D+fX7e4=

internal/table/session.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,51 @@ func (s *session) StreamReadTable(
970970
), nil
971971
}
972972

973+
func (s *session) ReadRows(
974+
ctx context.Context,
975+
path string,
976+
keys types.Value,
977+
opts ...options.ReadRowsOption,
978+
) (_ result.Result, err error) {
979+
var (
980+
a = allocator.New()
981+
request = Ydb_Table.ReadRowsRequest{
982+
SessionId: s.id,
983+
Path: path,
984+
Keys: value.ToYDB(keys, a),
985+
}
986+
response *Ydb_Table.ReadRowsResponse
987+
)
988+
defer func() {
989+
a.Free()
990+
}()
991+
992+
for _, opt := range opts {
993+
if opt != nil {
994+
opt.ApplyReadRowsOption((*options.ReadRowsDesc)(&request), a)
995+
}
996+
}
997+
998+
response, err = s.tableService.ReadRows(ctx, &request)
999+
if err != nil {
1000+
return nil, xerrors.WithStackTrace(err)
1001+
}
1002+
1003+
if response.GetStatus() != Ydb.StatusIds_SUCCESS {
1004+
return nil, xerrors.WithStackTrace(
1005+
xerrors.Operation(
1006+
xerrors.FromOperation(response),
1007+
),
1008+
)
1009+
}
1010+
1011+
return scanner.NewUnary(
1012+
[]*Ydb.ResultSet{response.GetResultSet()},
1013+
nil,
1014+
scanner.WithIgnoreTruncated(s.config.IgnoreTruncated()),
1015+
), nil
1016+
}
1017+
9731018
// StreamExecuteScanQuery scan-reads table at given path with given options.
9741019
//
9751020
// Note that given ctx controls the lifetime of the whole read, not only this

table/options/options.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,7 @@ func WithExecuteScanQueryStats(stats ExecuteScanQueryStatsType) ExecuteScanQuery
977977
}
978978

979979
var (
980+
_ ReadRowsOption = readColumnsOption{}
980981
_ ReadTableOption = readOrderedOption{}
981982
_ ReadTableOption = readKeyRangeOption{}
982983
_ ReadTableOption = readGreaterOrEqualOption{}
@@ -987,6 +988,11 @@ var (
987988
)
988989

989990
type (
991+
ReadRowsDesc Ydb_Table.ReadRowsRequest
992+
ReadRowsOption interface {
993+
ApplyReadRowsOption(*ReadRowsDesc, *allocator.Allocator)
994+
}
995+
990996
ReadTableDesc Ydb_Table.ReadTableRequest
991997
ReadTableOption interface {
992998
ApplyReadTableOption(*ReadTableDesc, *allocator.Allocator)
@@ -1027,6 +1033,10 @@ func (x readLessOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.A
10271033
}
10281034
}
10291035

1036+
func (columns readColumnsOption) ApplyReadRowsOption(desc *ReadRowsDesc, a *allocator.Allocator) {
1037+
desc.Columns = append(desc.Columns, columns...)
1038+
}
1039+
10301040
func (columns readColumnsOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
10311041
desc.Columns = append(desc.Columns, columns...)
10321042
}

table/table.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ type Session interface {
171171
opts ...options.BulkUpsertOption,
172172
) (err error)
173173

174+
ReadRows(
175+
ctx context.Context,
176+
path string,
177+
keys types.Value,
178+
opts ...options.ReadRowsOption,
179+
) (_ result.Result, err error)
180+
174181
BeginTransaction(
175182
ctx context.Context,
176183
tx *TransactionSettings,

tests/integration/kv_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//go:build integration
2+
// +build integration
3+
4+
package integration
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"os"
10+
"testing"
11+
12+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/version"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
14+
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
16+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
17+
)
18+
19+
func TestKeyValue(t *testing.T) {
20+
if version.Lt(os.Getenv("YDB_VERSION"), "23.3") {
21+
t.Skip("read rows not allowed in YDB version '" + os.Getenv("YDB_VERSION") + "'")
22+
}
23+
24+
var (
25+
scope = newScope(t)
26+
driver = scope.Driver()
27+
tablePath = scope.TablePath()
28+
id = int64(100500)
29+
value = "test value"
30+
)
31+
32+
// set
33+
err := driver.Table().Do(scope.Ctx, func(ctx context.Context, s table.Session) error {
34+
return s.BulkUpsert(ctx, tablePath, types.ListValue(types.StructValue(
35+
types.StructFieldValue("id", types.Int64Value(id)),
36+
types.StructFieldValue("val", types.TextValue(value)),
37+
)))
38+
})
39+
scope.Require.NoError(err)
40+
41+
// get
42+
err = driver.Table().Do(scope.Ctx, func(ctx context.Context, s table.Session) error {
43+
rows, err := s.ReadRows(ctx, tablePath, types.ListValue(types.Int64Value(id)),
44+
options.ReadColumn("val"),
45+
)
46+
if err != nil {
47+
return err
48+
}
49+
defer func() {
50+
_ = rows.Close()
51+
}()
52+
if !rows.HasNextResultSet() {
53+
return fmt.Errorf("no result sets")
54+
}
55+
if !rows.HasNextRow() {
56+
return fmt.Errorf("no rows")
57+
}
58+
if rows.CurrentResultSet().RowCount() != 1 {
59+
return fmt.Errorf("wrong rows count (%d)", rows.CurrentResultSet().RowCount())
60+
}
61+
if rows.CurrentResultSet().ColumnCount() != 1 {
62+
return fmt.Errorf("wrong column count (%d)", rows.CurrentResultSet().ColumnCount())
63+
}
64+
var actualValue int64
65+
if err := rows.ScanNamed(named.Optional("val", &actualValue)); err != nil {
66+
return err
67+
}
68+
return rows.Err()
69+
})
70+
scope.Require.NoError(err)
71+
}

tests/slo/go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.20
44

55
require (
66
github.com/prometheus/client_golang v1.14.0
7-
github.com/ydb-platform/gorm-driver v0.0.6-0.20230529184330-9104dd08ecae
8-
github.com/ydb-platform/ydb-go-sdk/v3 v3.47.3
7+
github.com/ydb-platform/gorm-driver v0.1.1
8+
github.com/ydb-platform/ydb-go-sdk/v3 v3.48.7
99
golang.org/x/sync v0.1.0
1010
golang.org/x/time v0.3.0
1111
gorm.io/gorm v1.25.1
@@ -33,8 +33,8 @@ require (
3333
github.com/prometheus/procfs v0.8.0 // indirect
3434
github.com/syndtr/goleveldb v1.0.0 // indirect
3535
github.com/yandex-cloud/go-genproto v0.0.0-20230403093326-123923969dc6 // indirect
36-
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230528143953-42c825ace222 // indirect
37-
github.com/ydb-platform/ydb-go-sdk-auth-environ v0.1.3 // indirect
36+
github.com/ydb-platform/ydb-go-genproto v0.0.0-20230801151335-81e01be38941 // indirect
37+
github.com/ydb-platform/ydb-go-sdk-auth-environ v0.2.0 // indirect
3838
github.com/ydb-platform/ydb-go-yc v0.10.2 // indirect
3939
github.com/ydb-platform/ydb-go-yc-metadata v0.5.3 // indirect
4040
golang.org/x/net v0.9.0 // indirect
@@ -48,4 +48,4 @@ require (
4848

4949
replace github.com/ydb-platform/ydb-go-sdk/v3 => ../../.
5050

51-
replace xorm.io/xorm => github.com/ydb-platform/xorm v0.0.3
51+
replace xorm.io/xorm => github.com/ydb-platform/xorm v0.0.6

0 commit comments

Comments
 (0)