1111import org .junit .ClassRule ;
1212import org .junit .Test ;
1313
14+ import tech .ydb .core .Result ;
1415import tech .ydb .core .StatusCode ;
1516import tech .ydb .table .SessionRetryContext ;
1617import tech .ydb .table .description .TableDescription ;
1718import tech .ydb .table .impl .SimpleTableClient ;
19+ import tech .ydb .table .query .ReadRowsResult ;
1820import tech .ydb .table .result .ResultSetReader ;
1921import tech .ydb .table .rpc .grpc .GrpcTableRpc ;
2022import tech .ydb .table .settings .BulkUpsertSettings ;
2628import tech .ydb .test .junit4 .GrpcTransportRule ;
2729
2830
29- public class TableQueryTest {
31+ public class ReadRowsTest {
3032 @ ClassRule
3133 public static final GrpcTransportRule YDB_TRANSPORT = new GrpcTransportRule ();
3234 private static final SessionRetryContext CTX = SessionRetryContext .create (SimpleTableClient .newClient (
3335 GrpcTableRpc .useTransport (YDB_TRANSPORT )
3436 ).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+ }
3645
3746
3847 /**
3948 * Create tables `series` and `seasons` and fill them with data
4049 */
4150 @ BeforeClass
4251 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 );
104111 }
105112
106- @ Nonnull
107- private static String getTableName (String tablePostfix ) {
108- return TABLE_PREFIX + tablePostfix ;
109- }
110-
111-
112113 @ Test
113114 public void testReadRowsComplexKey () {
114115 final List <StructValue > seasonKeys = Arrays .asList (
@@ -122,7 +123,7 @@ public void testReadRowsComplexKey() {
122123 );
123124
124125 final ResultSetReader rsr = CTX .supplyResult (session ->
125- session .readRows (getPath ( "seasons" ),
126+ session .readRows (tablePath ( SEASONS ),
126127 ReadRowsSettings .newBuilder ()
127128 .addColumns (Collections .emptyList ())
128129 .addKeys (seasonKeys )
@@ -141,13 +142,24 @@ public void testReadRowsComplexKey() {
141142 Assert .assertFalse (rsr .next ());
142143 }
143144
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+
144156 /**
145157 * Empty keys parameter make result be an empty ResultSet
146158 */
147159 @ Test
148160 public void testReadRowsEmptyKeys () {
149161 final StatusCode responseStatusCode = CTX .supplyResult (session ->
150- session .readRows (getPath ( "series" ),
162+ session .readRows (tablePath ( SERIES ),
151163 ReadRowsSettings .newBuilder ()
152164 .addColumns ("series_id" , "title" )
153165 .build ()
@@ -162,7 +174,7 @@ public void testReadRowsEmptyKeys() {
162174 @ Test
163175 public void testReadRowsEmptyColumns () {
164176 final ResultSetReader rsr = CTX .supplyResult (session ->
165- session .readRows (getPath ( "series" ),
177+ session .readRows (tablePath ( SERIES ),
166178 ReadRowsSettings .newBuilder ()
167179 .addKey (StructValue .of ("series_id" , PrimitiveValue .newUint64 (1 )))
168180 .addKey (StructValue .of ("series_id" , PrimitiveValue .newUint64 (2 )))
0 commit comments