Skip to content

Commit 5492d7e

Browse files
committed
Added support of raw ranges to ReadTableSettings
1 parent 872e4fc commit 5492d7e

File tree

6 files changed

+76
-17
lines changed

6 files changed

+76
-17
lines changed

table/src/main/java/tech/ydb/table/impl/BaseSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,23 +1266,23 @@ public GrpcReadStream<ReadTablePart> executeReadTable(String tablePath, ReadTabl
12661266
.setBatchLimitBytes(settings.batchLimitBytes())
12671267
.setBatchLimitRows(settings.batchLimitRows());
12681268

1269-
Value<?> fromKey = settings.getFromKey();
1269+
ValueProtos.TypedValue fromKey = settings.getFromKeyRaw();
12701270
if (fromKey != null) {
12711271
YdbTable.KeyRange.Builder range = request.getKeyRangeBuilder();
12721272
if (settings.isFromInclusive()) {
1273-
range.setGreaterOrEqual(ProtoValue.toTypedValue(fromKey));
1273+
range.setGreaterOrEqual(fromKey);
12741274
} else {
1275-
range.setGreater(ProtoValue.toTypedValue(fromKey));
1275+
range.setGreater(fromKey);
12761276
}
12771277
}
12781278

1279-
Value<?> toKey = settings.getToKey();
1279+
ValueProtos.TypedValue toKey = settings.getToKeyRaw();
12801280
if (toKey != null) {
12811281
YdbTable.KeyRange.Builder range = request.getKeyRangeBuilder();
12821282
if (settings.isToInclusive()) {
1283-
range.setLessOrEqual(ProtoValue.toTypedValue(toKey));
1283+
range.setLessOrEqual(toKey);
12841284
} else {
1285-
range.setLess(ProtoValue.toTypedValue(toKey));
1285+
range.setLess(toKey);
12861286
}
12871287
}
12881288

table/src/main/java/tech/ydb/table/query/ReadTablePart.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,29 @@ public long getTxId() {
2828
}
2929

3030
private final YdbTable.ReadTableResponse part;
31-
private final ResultSetReader resultSetReader;
32-
private final VirtualTimestamp timestamp;
31+
private ResultSetReader resultSetReader = null;
32+
private VirtualTimestamp timestamp = null;
3333

3434
public ReadTablePart(YdbTable.ReadTableResponse part) {
3535
this.part = part;
36-
this.resultSetReader = ProtoValueReaders.forResultSet(part.getResult().getResultSet());
37-
CommonProtos.VirtualTimestamp vt = part.getSnapshot();
38-
this.timestamp = new VirtualTimestamp(vt.getPlanStep(), vt.getTxId());
3936
}
4037

4138
public YdbTable.ReadTableResponse getReadTableResponse() {
4239
return part;
4340
}
4441

4542
public ResultSetReader getResultSetReader() {
43+
if (resultSetReader == null) {
44+
resultSetReader = ProtoValueReaders.forResultSet(part.getResult().getResultSet());
45+
}
4646
return resultSetReader;
4747
}
4848

4949
public VirtualTimestamp getVirtualTimestamp() {
50+
if (timestamp == null) {
51+
CommonProtos.VirtualTimestamp vt = part.getSnapshot();
52+
timestamp = new VirtualTimestamp(vt.getPlanStep(), vt.getTxId());
53+
}
5054
return timestamp;
5155
}
5256
}

table/src/main/java/tech/ydb/table/result/impl/ProtoValueReaders.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ public static ResultSetReader forResultSets(Collection<ResultSetReader> resultSe
4040
return new ProtoResultSetReader(builder.build());
4141
}
4242

43+
@Deprecated
4344
public static ValueReader forType(ValueProtos.Type type) {
4445
return forTypeImpl(type);
4546
}
4647

48+
public static ValueReader forTypedValue(ValueProtos.TypedValue tv) {
49+
AbstractValueReader vr = forTypeImpl(tv.getType());
50+
vr.setProtoValue(tv.getValue());
51+
return vr;
52+
}
53+
4754
static AbstractValueReader forTypeImpl(ValueProtos.Type type) {
4855
switch (type.getTypeCase()) {
4956
case TYPE_ID:

table/src/main/java/tech/ydb/table/settings/ReadTableSettings.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
import tech.ydb.core.grpc.GrpcFlowControl;
1313
import tech.ydb.core.settings.BaseRequestSettings;
14+
import tech.ydb.proto.ValueProtos;
15+
import tech.ydb.table.result.impl.ProtoValueReaders;
1416
import tech.ydb.table.values.PrimitiveValue;
1517
import tech.ydb.table.values.TupleValue;
18+
import tech.ydb.table.values.proto.ProtoValue;
1619

1720
import static com.google.common.base.Preconditions.checkArgument;
1821

@@ -24,9 +27,9 @@
2427
public class ReadTableSettings extends BaseRequestSettings {
2528

2629
private final boolean ordered;
27-
private final TupleValue fromKey;
30+
private final ValueProtos.TypedValue fromKey;
2831
private final boolean fromInclusive;
29-
private final TupleValue toKey;
32+
private final ValueProtos.TypedValue toKey;
3033
private final boolean toInclusive;
3134
private final int rowLimit;
3235
private final ImmutableList<String> columns;
@@ -58,6 +61,11 @@ public boolean isOrdered() {
5861

5962
@Nullable
6063
public TupleValue getFromKey() {
64+
return fromKey == null ? null : ProtoValueReaders.forTypedValue(fromKey).getValue().asTuple();
65+
}
66+
67+
@Nullable
68+
public ValueProtos.TypedValue getFromKeyRaw() {
6169
return fromKey;
6270
}
6371

@@ -67,6 +75,11 @@ public boolean isFromInclusive() {
6775

6876
@Nullable
6977
public TupleValue getToKey() {
78+
return toKey == null ? null : ProtoValueReaders.forTypedValue(toKey).getValue().asTuple();
79+
}
80+
81+
@Nullable
82+
public ValueProtos.TypedValue getToKeyRaw() {
7083
return toKey;
7184
}
7285

@@ -99,9 +112,9 @@ public GrpcFlowControl getGrpcFlowControl() {
99112
*/
100113
public static final class Builder extends BaseBuilder<Builder> {
101114
private boolean ordered = false;
102-
private TupleValue fromKey = null;
115+
private ValueProtos.TypedValue fromKey = null;
103116
private boolean fromInclusive = false;
104-
private TupleValue toKey = null;
117+
private ValueProtos.TypedValue toKey = null;
105118
private boolean toInclusive = false;
106119
private int rowLimit = 0;
107120
private List<String> columns = Collections.emptyList();
@@ -115,12 +128,24 @@ public Builder orderedRead(boolean ordered) {
115128
}
116129

117130
public Builder fromKey(TupleValue value, boolean inclusive) {
131+
this.fromKey = ProtoValue.toTypedValue(value);
132+
this.fromInclusive = inclusive;
133+
return this;
134+
}
135+
136+
public Builder fromKey(ValueProtos.TypedValue value, boolean inclusive) {
118137
this.fromKey = value;
119138
this.fromInclusive = inclusive;
120139
return this;
121140
}
122141

123142
public Builder toKey(TupleValue value, boolean inclusive) {
143+
this.toKey = ProtoValue.toTypedValue(value);
144+
this.toInclusive = inclusive;
145+
return this;
146+
}
147+
148+
public Builder toKey(ValueProtos.TypedValue value, boolean inclusive) {
124149
this.toKey = value;
125150
this.toInclusive = inclusive;
126151
return this;

table/src/main/java/tech/ydb/table/values/Value.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ default StructValue asStruct() {
4444
return (StructValue) this;
4545
}
4646

47+
default TupleValue asTuple() {
48+
return (TupleValue) this;
49+
}
50+
4751
default VariantValue asVariant() {
4852
return (VariantValue) this;
4953
}

table/src/test/java/tech/ydb/table/integration/ReadTableTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import tech.ydb.table.values.PrimitiveValue;
3939
import tech.ydb.table.values.StructType;
4040
import tech.ydb.table.values.StructValue;
41+
import tech.ydb.table.values.TupleValue;
42+
import tech.ydb.table.values.proto.ProtoValue;
4143
import tech.ydb.test.junit4.GrpcTransportRule;
4244

4345
/**
@@ -143,6 +145,13 @@ public void limitedReadTableTest() {
143145
AtomicLong rewsRead = new AtomicLong(0);
144146

145147
ReadTableSettings rts = ReadTableSettings.newBuilder().column("id").batchLimitRows(100).build();
148+
Assert.assertNull(rts.getFromKey());
149+
Assert.assertNull(rts.getToKey());
150+
Assert.assertNull(rts.getFromKeyRaw());
151+
Assert.assertNull(rts.getToKeyRaw());
152+
Assert.assertEquals(0, rts.batchLimitBytes());
153+
Assert.assertEquals(100, rts.batchLimitRows());
154+
146155
retryCtx.supplyStatus(session -> {
147156
rewsRead.set(0);
148157
return session.executeReadTable(tablePath, rts).start(part -> {
@@ -159,10 +168,20 @@ public void partialReadTableTest() {
159168
String tablePath = tablePath(TEST_TABLE);
160169
AtomicLong rowsRead = new AtomicLong(0);
161170

171+
PrimitiveValue from = PrimitiveValue.newInt64(1);
172+
PrimitiveValue to = PrimitiveValue.newInt64(TEST_TABLE_SIZE);
162173
ReadTableSettings rts = ReadTableSettings.newBuilder().column("id")
163-
.fromKeyExclusive(PrimitiveValue.newInt64(1))
164-
.toKeyExclusive(PrimitiveValue.newInt64(TEST_TABLE_SIZE))
174+
.fromKeyExclusive(from) // always coverted to optional type
175+
.toKeyExclusive(to)
165176
.build();
177+
178+
Assert.assertEquals(TupleValue.of(from.makeOptional()), rts.getFromKey());
179+
Assert.assertEquals(TupleValue.of(to.makeOptional()), rts.getToKey());
180+
Assert.assertEquals(ProtoValue.toTypedValue(TupleValue.of(from.makeOptional())), rts.getFromKeyRaw());
181+
Assert.assertEquals(ProtoValue.toTypedValue(TupleValue.of(to.makeOptional())), rts.getToKeyRaw());
182+
Assert.assertEquals(0, rts.batchLimitBytes());
183+
Assert.assertEquals(0, rts.batchLimitRows());
184+
166185
retryCtx.supplyStatus(session -> {
167186
rowsRead.set(0);
168187
return session.executeReadTable(tablePath, rts).start(part -> {

0 commit comments

Comments
 (0)