Skip to content

Commit b2dd070

Browse files
authored
Merge pull request #797 from ydb-platform/read-table-options
Read table options
2 parents 4733bd0 + f949dea commit b2dd070

File tree

3 files changed

+95
-46
lines changed

3 files changed

+95
-46
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
ydb-version: [22.5, 23.1, 23.2, 23.2-slim]
4848
services:
4949
ydb:
50-
image: cr.yandex/crpsjg1coh47p81vh2lc/yandex-docker-local-ydb:${{ matrix.ydb-version }}
50+
image: cr.yandex/yc/yandex-docker-local-ydb:${{ matrix.ydb-version }}
5151
ports:
5252
- 2135:2135
5353
- 2136:2136

internal/table/session.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ func (s *session) StreamReadTable(
926926

927927
for _, opt := range opts {
928928
if opt != nil {
929-
opt((*options.ReadTableDesc)(&request), a)
929+
opt.ApplyReadTableOption((*options.ReadTableDesc)(&request), a)
930930
}
931931
}
932932

@@ -1080,7 +1080,11 @@ func (s *session) BulkUpsert(ctx context.Context, table string, rows types.Value
10801080
},
10811081
callOptions...,
10821082
)
1083-
return xerrors.WithStackTrace(err)
1083+
if err != nil {
1084+
return xerrors.WithStackTrace(err)
1085+
}
1086+
1087+
return nil
10841088
}
10851089

10861090
// BeginTransaction begins new transaction within given session with given

table/options/options.go

Lines changed: 88 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -976,79 +976,124 @@ func WithExecuteScanQueryStats(stats ExecuteScanQueryStatsType) ExecuteScanQuery
976976
})
977977
}
978978

979-
// Read table options
979+
var (
980+
_ ReadTableOption = readOrderedOption{}
981+
_ ReadTableOption = readKeyRangeOption{}
982+
_ ReadTableOption = readGreaterOrEqualOption{}
983+
_ ReadTableOption = readLessOrEqualOption{}
984+
_ ReadTableOption = readLessOption{}
985+
_ ReadTableOption = readGreaterOption{}
986+
_ ReadTableOption = readRowLimitOption(0)
987+
)
988+
980989
type (
981990
ReadTableDesc Ydb_Table.ReadTableRequest
982-
ReadTableOption func(*ReadTableDesc, *allocator.Allocator)
991+
ReadTableOption interface {
992+
ApplyReadTableOption(*ReadTableDesc, *allocator.Allocator)
993+
}
994+
995+
readColumnsOption []string
996+
readOrderedOption struct{}
997+
readKeyRangeOption KeyRange
998+
readGreaterOrEqualOption struct{ types.Value }
999+
readLessOrEqualOption struct{ types.Value }
1000+
readLessOption struct{ types.Value }
1001+
readGreaterOption struct{ types.Value }
1002+
readRowLimitOption uint64
9831003
)
9841004

985-
func ReadColumn(name string) ReadTableOption {
986-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
987-
desc.Columns = append(desc.Columns, name)
1005+
func (n readRowLimitOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1006+
desc.RowLimit = uint64(n)
1007+
}
1008+
1009+
func (x readGreaterOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1010+
desc.initKeyRange()
1011+
desc.KeyRange.FromBound = &Ydb_Table.KeyRange_Greater{
1012+
Greater: value.ToYDB(x, a),
1013+
}
1014+
}
1015+
1016+
func (x readLessOrEqualOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1017+
desc.initKeyRange()
1018+
desc.KeyRange.ToBound = &Ydb_Table.KeyRange_LessOrEqual{
1019+
LessOrEqual: value.ToYDB(x, a),
1020+
}
1021+
}
1022+
1023+
func (x readLessOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1024+
desc.initKeyRange()
1025+
desc.KeyRange.ToBound = &Ydb_Table.KeyRange_Less{
1026+
Less: value.ToYDB(x, a),
1027+
}
1028+
}
1029+
1030+
func (columns readColumnsOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1031+
desc.Columns = append(desc.Columns, columns...)
1032+
}
1033+
1034+
func (r readOrderedOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1035+
desc.Ordered = true
1036+
}
1037+
1038+
func (x readKeyRangeOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1039+
desc.initKeyRange()
1040+
if x.From != nil {
1041+
desc.KeyRange.FromBound = &Ydb_Table.KeyRange_GreaterOrEqual{
1042+
GreaterOrEqual: value.ToYDB(x.From, a),
1043+
}
1044+
}
1045+
if x.To != nil {
1046+
desc.KeyRange.ToBound = &Ydb_Table.KeyRange_Less{
1047+
Less: value.ToYDB(x.To, a),
1048+
}
9881049
}
9891050
}
9901051

991-
func ReadOrdered() ReadTableOption {
992-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
993-
desc.Ordered = true
1052+
func (x readGreaterOrEqualOption) ApplyReadTableOption(desc *ReadTableDesc, a *allocator.Allocator) {
1053+
desc.initKeyRange()
1054+
desc.KeyRange.FromBound = &Ydb_Table.KeyRange_GreaterOrEqual{
1055+
GreaterOrEqual: value.ToYDB(x, a),
9941056
}
9951057
}
9961058

1059+
func ReadColumn(name string) readColumnsOption {
1060+
return []string{name}
1061+
}
1062+
1063+
func ReadColumns(names ...string) readColumnsOption {
1064+
return names
1065+
}
1066+
1067+
func ReadOrdered() readOrderedOption {
1068+
return readOrderedOption{}
1069+
}
1070+
9971071
// ReadKeyRange returns ReadTableOption which makes ReadTable read values
9981072
// in range [x.From, x.To).
9991073
//
10001074
// Both x.From and x.To may be nil.
10011075
func ReadKeyRange(x KeyRange) ReadTableOption {
1002-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1003-
if x.From != nil {
1004-
ReadGreaterOrEqual(x.From)(desc, a)
1005-
}
1006-
if x.To != nil {
1007-
ReadLess(x.To)(desc, a)
1008-
}
1009-
}
1076+
return readKeyRangeOption(x)
10101077
}
10111078

10121079
func ReadGreater(x types.Value) ReadTableOption {
1013-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1014-
desc.initKeyRange()
1015-
desc.KeyRange.FromBound = &Ydb_Table.KeyRange_Greater{
1016-
Greater: value.ToYDB(x, a),
1017-
}
1018-
}
1080+
return readGreaterOption{x}
10191081
}
10201082

10211083
func ReadGreaterOrEqual(x types.Value) ReadTableOption {
1022-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1023-
desc.initKeyRange()
1024-
desc.KeyRange.FromBound = &Ydb_Table.KeyRange_GreaterOrEqual{
1025-
GreaterOrEqual: value.ToYDB(x, a),
1026-
}
1027-
}
1084+
return readGreaterOrEqualOption{x}
10281085
}
10291086

10301087
func ReadLess(x types.Value) ReadTableOption {
1031-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1032-
desc.initKeyRange()
1033-
desc.KeyRange.ToBound = &Ydb_Table.KeyRange_Less{
1034-
Less: value.ToYDB(x, a),
1035-
}
1036-
}
1088+
return readLessOption{x}
10371089
}
10381090

10391091
func ReadLessOrEqual(x types.Value) ReadTableOption {
1040-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1041-
desc.initKeyRange()
1042-
desc.KeyRange.ToBound = &Ydb_Table.KeyRange_LessOrEqual{
1043-
LessOrEqual: value.ToYDB(x, a),
1044-
}
1045-
}
1092+
return readLessOrEqualOption{x}
10461093
}
10471094

10481095
func ReadRowLimit(n uint64) ReadTableOption {
1049-
return func(desc *ReadTableDesc, a *allocator.Allocator) {
1050-
desc.RowLimit = n
1051-
}
1096+
return readRowLimitOption(n)
10521097
}
10531098

10541099
func (d *ReadTableDesc) initKeyRange() {

0 commit comments

Comments
 (0)