11
11
import org .junit .ClassRule ;
12
12
import org .junit .Test ;
13
13
14
+ import tech .ydb .core .Result ;
14
15
import tech .ydb .core .StatusCode ;
15
16
import tech .ydb .table .SessionRetryContext ;
16
17
import tech .ydb .table .description .TableDescription ;
17
18
import tech .ydb .table .impl .SimpleTableClient ;
19
+ import tech .ydb .table .query .ReadRowsResult ;
18
20
import tech .ydb .table .result .ResultSetReader ;
19
21
import tech .ydb .table .rpc .grpc .GrpcTableRpc ;
20
22
import tech .ydb .table .settings .BulkUpsertSettings ;
26
28
import tech .ydb .test .junit4 .GrpcTransportRule ;
27
29
28
30
29
- public class TableQueryTest {
31
+ public class ReadRowsTest {
30
32
@ ClassRule
31
33
public static final GrpcTransportRule YDB_TRANSPORT = new GrpcTransportRule ();
32
34
private static final SessionRetryContext CTX = SessionRetryContext .create (SimpleTableClient .newClient (
33
35
GrpcTableRpc .useTransport (YDB_TRANSPORT )
34
36
).build ()).build ();
35
- private static final String TABLE_PREFIX = "query_" ;
37
+
38
+ private static final String SERIES = "rrt_series" ;
39
+ private static final String SEASONS = "rrt_seasons" ;
40
+
41
+ @ Nonnull
42
+ private static String tablePath (String tableName ) {
43
+ return YDB_TRANSPORT .getDatabase () + "/" + tableName ;
44
+ }
36
45
37
46
38
47
/**
39
48
* Create tables `series` and `seasons` and fill them with data
40
49
*/
41
50
@ BeforeClass
42
51
public static void prepare () {
43
- final List <StructValue > series = Arrays .asList (
44
- StructValue .of (
45
- "series_id" , PrimitiveValue .newUint64 (1 ),
46
- "title" , PrimitiveValue .newText ("Once I rose above the noise and confusion" ),
47
- "series_info" , PrimitiveValue .newText ("Carry on my wayward son" )
48
- ), StructValue .of ("series_id" , PrimitiveValue .newUint64 (2 ),
49
- "title" , PrimitiveValue .newText ("There'll be peace when you are done" ),
50
- "series_info" , PrimitiveValue .newText ("Lay your weary head to rest" )
51
- ), StructValue .of ("series_id" , PrimitiveValue .newUint64 (3 ),
52
- "title" , PrimitiveValue .newText ("Just to get a glimpse beyond this illusion" ),
53
- "series_info" , PrimitiveValue .newText ("I was soaring ever higher" )
54
- )
55
- );
56
- final List <StructValue > seasons = Arrays .asList (
57
- StructValue .of (
58
- "series_id" , PrimitiveValue .newUint64 (1 ),
59
- "season_id" , PrimitiveValue .newUint64 (1 ),
60
- "title" , PrimitiveValue .newText ("But I flew too high" )
61
- ), StructValue .of (
62
- "series_id" , PrimitiveValue .newUint64 (2 ),
63
- "season_id" , PrimitiveValue .newUint64 (2 ),
64
- "title" , PrimitiveValue .newText ("Though my eyes could see, I still was a blind man" )
65
- ), StructValue .of (
66
- "series_id" , PrimitiveValue .newUint64 (2 ),
67
- "season_id" , PrimitiveValue .newUint64 (3 ),
68
- "title" , PrimitiveValue .newText ("Though my mind could think, I still was a mad man" )
69
- ), StructValue .of (
70
- "series_id" , PrimitiveValue .newUint64 (2 ),
71
- "season_id" , PrimitiveValue .newUint64 (4 ),
72
- "title" , PrimitiveValue .newText ("I hear the voices when I'm dreaming" )
73
- )
74
- );
75
-
76
- CTX .supplyStatus (session -> session .createTable (getPath ("series" ), TableDescription .newBuilder ()
77
- .addNonnullColumn ("series_id" , PrimitiveType .Uint64 )
78
- .addNullableColumn ("title" , PrimitiveType .Text )
79
- .addNullableColumn ("series_info" , PrimitiveType .Text )
80
- .setPrimaryKey ("series_id" )
81
- .build ()))
82
- .join ().expectSuccess ("Can't create table " + getPath ("series" ));
83
-
84
- CTX .supplyStatus (session -> session .createTable (getPath ("seasons" ), TableDescription .newBuilder ()
85
- .addNonnullColumn ("series_id" , PrimitiveType .Uint64 )
86
- .addNonnullColumn ("season_id" , PrimitiveType .Uint64 )
87
- .addNullableColumn ("title" , PrimitiveType .Text )
88
- .setPrimaryKeys ("series_id" , "season_id" )
89
- .build ()))
90
- .join ().expectSuccess ("Can't create table " + getPath ("seasons" ));
91
-
92
- CTX .supplyStatus (session -> session .executeBulkUpsert (getPath ("series" ),
93
- ListType .of (series .get (0 ).getType ()).newValue (series ), new BulkUpsertSettings ()))
94
- .join ().expectSuccess ("bulk upsert problem in table " + getPath ("series" ));
95
-
96
- CTX .supplyStatus (session -> session .executeBulkUpsert (getPath ("seasons" ),
97
- ListType .of (seasons .get (0 ).getType ()).newValue (seasons ), new BulkUpsertSettings ()))
98
- .join ().expectSuccess ("bulk upsert problem in table " + getPath ("seasons" ));
99
- }
100
-
101
- @ Nonnull
102
- private static String getPath (String tablePostfix ) {
103
- return YDB_TRANSPORT .getDatabase () + "/" + getTableName (tablePostfix );
52
+ String seriesPath = tablePath (SERIES );
53
+ String seasonsPath = tablePath (SEASONS );
54
+
55
+ CTX .supplyStatus (session -> session .createTable (seriesPath , TableDescription .newBuilder ()
56
+ .addNonnullColumn ("series_id" , PrimitiveType .Uint64 )
57
+ .addNullableColumn ("title" , PrimitiveType .Text )
58
+ .addNullableColumn ("series_info" , PrimitiveType .Text )
59
+ .setPrimaryKey ("series_id" )
60
+ .build ())
61
+ ).join ().expectSuccess ("Can't create table " + seriesPath );
62
+
63
+ CTX .supplyStatus (session -> session .createTable (seasonsPath , TableDescription .newBuilder ()
64
+ .addNonnullColumn ("series_id" , PrimitiveType .Uint64 )
65
+ .addNonnullColumn ("season_id" , PrimitiveType .Uint64 )
66
+ .addNullableColumn ("title" , PrimitiveType .Text )
67
+ .setPrimaryKeys ("series_id" , "season_id" )
68
+ .build ())
69
+ ).join ().expectSuccess ("Can't create table " + seasonsPath );
70
+
71
+ final List <StructValue > series = Arrays .asList (StructValue .of (
72
+ "series_id" , PrimitiveValue .newUint64 (1 ),
73
+ "title" , PrimitiveValue .newText ("Once I rose above the noise and confusion" ),
74
+ "series_info" , PrimitiveValue .newText ("Carry on my wayward son" )
75
+ ), StructValue .of (
76
+ "series_id" , PrimitiveValue .newUint64 (2 ),
77
+ "title" , PrimitiveValue .newText ("There'll be peace when you are done" ),
78
+ "series_info" , PrimitiveValue .newText ("Lay your weary head to rest" )
79
+ ), StructValue .of (
80
+ "series_id" , PrimitiveValue .newUint64 (3 ),
81
+ "title" , PrimitiveValue .newText ("Just to get a glimpse beyond this illusion" ),
82
+ "series_info" , PrimitiveValue .newText ("I was soaring ever higher" )
83
+ ));
84
+
85
+ final List <StructValue > seasons = Arrays .asList (StructValue .of (
86
+ "series_id" , PrimitiveValue .newUint64 (1 ),
87
+ "season_id" , PrimitiveValue .newUint64 (1 ),
88
+ "title" , PrimitiveValue .newText ("But I flew too high" )
89
+ ), StructValue .of (
90
+ "series_id" , PrimitiveValue .newUint64 (2 ),
91
+ "season_id" , PrimitiveValue .newUint64 (2 ),
92
+ "title" , PrimitiveValue .newText ("Though my eyes could see, I still was a blind man" )
93
+ ), StructValue .of (
94
+ "series_id" , PrimitiveValue .newUint64 (2 ),
95
+ "season_id" , PrimitiveValue .newUint64 (3 ),
96
+ "title" , PrimitiveValue .newText ("Though my mind could think, I still was a mad man" )
97
+ ), StructValue .of (
98
+ "series_id" , PrimitiveValue .newUint64 (2 ),
99
+ "season_id" , PrimitiveValue .newUint64 (4 ),
100
+ "title" , PrimitiveValue .newText ("I hear the voices when I'm dreaming" )
101
+ ));
102
+
103
+
104
+ CTX .supplyStatus (session -> session .executeBulkUpsert (
105
+ seriesPath , ListType .of (series .get (0 ).getType ()).newValue (series ), new BulkUpsertSettings ())
106
+ ).join ().expectSuccess ("bulk upsert problem in table " + seriesPath );
107
+
108
+ CTX .supplyStatus (session -> session .executeBulkUpsert (
109
+ seasonsPath , ListType .of (seasons .get (0 ).getType ()).newValue (seasons ), new BulkUpsertSettings ())
110
+ ).join ().expectSuccess ("bulk upsert problem in table " + seasonsPath );
104
111
}
105
112
106
- @ Nonnull
107
- private static String getTableName (String tablePostfix ) {
108
- return TABLE_PREFIX + tablePostfix ;
109
- }
110
-
111
-
112
113
@ Test
113
114
public void testReadRowsComplexKey () {
114
115
final List <StructValue > seasonKeys = Arrays .asList (
@@ -122,7 +123,7 @@ public void testReadRowsComplexKey() {
122
123
);
123
124
124
125
final ResultSetReader rsr = CTX .supplyResult (session ->
125
- session .readRows (getPath ( "seasons" ),
126
+ session .readRows (tablePath ( SEASONS ),
126
127
ReadRowsSettings .newBuilder ()
127
128
.addColumns (Collections .emptyList ())
128
129
.addKeys (seasonKeys )
@@ -141,13 +142,24 @@ public void testReadRowsComplexKey() {
141
142
Assert .assertFalse (rsr .next ());
142
143
}
143
144
145
+ @ Test
146
+ public void testReadRowsPartialKey () {
147
+ StructValue partKey = StructValue .of ("series_id" , PrimitiveValue .newUint64 (1 ));
148
+
149
+ Result <ReadRowsResult > result = CTX .supplyResult (session ->
150
+ session .readRows (tablePath (SEASONS ), ReadRowsSettings .newBuilder ().addKey (partKey ).build ())
151
+ ).join ();
152
+
153
+ Assert .assertFalse ("ReadRows by partitial keys must be failed" , result .isSuccess ());
154
+ }
155
+
144
156
/**
145
157
* Empty keys parameter make result be an empty ResultSet
146
158
*/
147
159
@ Test
148
160
public void testReadRowsEmptyKeys () {
149
161
final StatusCode responseStatusCode = CTX .supplyResult (session ->
150
- session .readRows (getPath ( "series" ),
162
+ session .readRows (tablePath ( SERIES ),
151
163
ReadRowsSettings .newBuilder ()
152
164
.addColumns ("series_id" , "title" )
153
165
.build ()
@@ -162,7 +174,7 @@ public void testReadRowsEmptyKeys() {
162
174
@ Test
163
175
public void testReadRowsEmptyColumns () {
164
176
final ResultSetReader rsr = CTX .supplyResult (session ->
165
- session .readRows (getPath ( "series" ),
177
+ session .readRows (tablePath ( SERIES ),
166
178
ReadRowsSettings .newBuilder ()
167
179
.addKey (StructValue .of ("series_id" , PrimitiveValue .newUint64 (1 )))
168
180
.addKey (StructValue .of ("series_id" , PrimitiveValue .newUint64 (2 )))
0 commit comments