Skip to content

Commit d995bd3

Browse files
authored
Use QueryService by default (#80)
2 parents 8c569f3 + 8a3608f commit d995bd3

File tree

9 files changed

+183
-113
lines changed

9 files changed

+183
-113
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ Specify the YDB JDBC driver in the dependencies:
3737
</dependency>
3838
</dependencies>
3939
```
40-
### Using QueryService mode
41-
By default JDBC driver executes all queries via TableService, so it brings corresponding [limitations](https://ydb.tech/docs/en/concepts/limits-ydb#query).
42-
To eliminate these limitations you can try a new experimentail [QueryService](https://ydb.tech/docs/en/conceptrs/query_service) mode by passing property `useQueryService=true` to the JDBC URL
43-
4440
### Authentication modes
4541

4642
YDB JDBC Driver supports the following [authentication modes](https://ydb.tech/en/docs/reference/ydb-sdk/auth):
@@ -62,6 +58,10 @@ Driver supports the following configuration properties, which can be specified i
6258
* `secureConnection` - boolean value, true if TLS should be enforced (normally configured via `grpc://` or `grpcs://` scheme in the JDBC URL);
6359
* `secureConnectionCertificate` - custom CA certificate for TLS connections, can be passed either as literal value or as a file reference.
6460

61+
### Using TableService mode
62+
By default JDBC driver executes all queries via QueryService, which uses grpc streams for the results recieving.
63+
If your database instance doesn't support this service, you can use old TableService mode by passing property `useQueryService=false` to the JDBC URL.
64+
6565
### Building
6666
By default all tests are run using a local YDB instance in Docker (if host has Docker or Docker Machine installed)
6767
To disable these tests run `mvn test -DYDB_DISABLE_INTEGRATION_TESTS=true`

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class YdbConfig {
3838
+ "{@code 0} disables the cache.", 256
3939
);
4040
static final YdbProperty<Boolean> USE_QUERY_SERVICE = YdbProperty.bool("useQueryService",
41-
"Use QueryService instead of TableService", false
41+
"Use QueryService instead of TableService", true
4242
);
4343

4444
static final YdbProperty<String> USE_PREFIX_PATH = YdbProperty.string("usePrefixPath",

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverTablesTest.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void defaultModeTest() throws SQLException {
112112
Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date"));
113113
}
114114
}
115-
Assertions.assertEquals(1000, readed);
115+
Assertions.assertEquals(2002, readed);
116116
}
117117

118118
// single update
@@ -319,7 +319,6 @@ public void forceScanAndBulkTest() throws SQLException {
319319
@Test
320320
public void streamResultsTest() throws SQLException {
321321
try (Connection conn = DriverManager.getConnection(jdbcURL
322-
.withArg("useQueryService", "true")
323322
.withArg("useStreamResultSets", "true")
324323
.build()
325324
)) {
@@ -443,4 +442,86 @@ public void streamResultsTest() throws SQLException {
443442
}
444443
}
445444
}
445+
446+
@Test
447+
public void tableServiceModeTest() throws SQLException {
448+
try (Connection connection = DriverManager.getConnection(jdbcURL.withArg("useQueryService", "false").build())) {
449+
try {
450+
connection.createStatement().execute(DROP_TABLE);
451+
} catch (SQLException e) {
452+
// ignore
453+
}
454+
455+
connection.createStatement().execute(CREATE_TABLE);
456+
457+
LocalDate ld = LocalDate.of(2017, 12, 3);
458+
String prefix = "text-value-";
459+
int idx = 0;
460+
461+
// single upsert
462+
try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) {
463+
ps.setInt(1, ++idx);
464+
ps.setString(2, prefix + idx);
465+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
466+
ps.executeUpdate();
467+
}
468+
469+
// single insert
470+
try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) {
471+
ps.setInt(1, ++idx);
472+
ps.setString(2, prefix + idx);
473+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
474+
ps.executeUpdate();
475+
}
476+
477+
// batch upsert
478+
try (PreparedStatement ps = connection.prepareStatement(UPSERT_ROW)) {
479+
for (int j = 0; j < 1000; j++) {
480+
ps.setInt(1, ++idx);
481+
ps.setString(2, prefix + idx);
482+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
483+
ps.addBatch();
484+
}
485+
ps.executeBatch();
486+
}
487+
488+
// batch insert
489+
try (PreparedStatement ps = connection.prepareStatement(INSERT_ROW)) {
490+
for (int j = 0; j < 1000; j++) {
491+
ps.setInt(1, ++idx);
492+
ps.setString(2, prefix + idx);
493+
ps.setDate(3, Date.valueOf(ld.plusDays(idx)));
494+
ps.addBatch();
495+
}
496+
ps.executeBatch();
497+
}
498+
499+
// read all
500+
try (Statement st = connection.createStatement()) {
501+
int readed = 0;
502+
try (ResultSet rs = st.executeQuery(SELECT_ALL)) {
503+
while (rs.next()) {
504+
readed++;
505+
Assertions.assertEquals(readed, rs.getInt("id"));
506+
Assertions.assertEquals(prefix + readed, rs.getString("value"));
507+
Assertions.assertEquals(Date.valueOf(ld.plusDays(readed)), rs.getDate("date"));
508+
}
509+
}
510+
Assertions.assertEquals(1000, readed);
511+
}
512+
513+
// single update
514+
try (PreparedStatement ps = connection.prepareStatement(UPDATE_ROW)) {
515+
ps.setString(1, "updated-value");
516+
ps.setInt(2, 1);
517+
ps.executeUpdate();
518+
}
519+
520+
// single delete
521+
try (PreparedStatement ps = connection.prepareStatement(DELETE_ROW)) {
522+
ps.setInt(1, 2);
523+
ps.executeUpdate();
524+
}
525+
}
526+
}
446527
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbConnectionImplTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -876,15 +876,15 @@ private String createPayload(Random rnd, int length) {
876876
@Timeout(value = 30, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SAME_THREAD)
877877
public void testBigBulkAndScan() throws SQLException {
878878
String bulkUpsert = QUERIES.upsertOne(SqlQueries.JdbcQuery.BULK, "c_Text", "Text?");
879-
String scanSelectAll = QUERIES.scanSelectSQL();
879+
String selectAll = QUERIES.selectSQL();
880880
String selectOne = QUERIES.selectAllByKey("?");
881881

882882
Random rnd = new Random(0x234567);
883883
int payloadLength = 1000;
884884

885-
try {
885+
try (Connection conn = jdbc.createCustomConnection("useStreamResultSets", "true")) {
886886
// BULK UPSERT
887-
try (PreparedStatement ps = jdbc.connection().prepareStatement(bulkUpsert)) {
887+
try (PreparedStatement ps = conn.prepareStatement(bulkUpsert)) {
888888
for (int idx = 1; idx <= 10000; idx++) {
889889
ps.setInt(1, idx);
890890
String payload = createPayload(rnd, payloadLength);
@@ -898,7 +898,7 @@ public void testBigBulkAndScan() throws SQLException {
898898
}
899899

900900
// SCAN all table
901-
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
901+
try (PreparedStatement ps = conn.prepareStatement(selectAll)) {
902902
int readed = 0;
903903
Assertions.assertTrue(ps.execute());
904904
try (ResultSet rs = ps.getResultSet()) {
@@ -912,7 +912,7 @@ public void testBigBulkAndScan() throws SQLException {
912912
}
913913

914914
// Canceled scan
915-
try (PreparedStatement ps = jdbc.connection().prepareStatement(scanSelectAll)) {
915+
try (PreparedStatement ps = conn.prepareStatement(selectAll)) {
916916
Assertions.assertTrue(ps.execute());
917917
ps.getResultSet().next();
918918
ps.getResultSet().close();
@@ -930,14 +930,14 @@ public void testBigBulkAndScan() throws SQLException {
930930
}
931931

932932
// Scan was cancelled, but connection still work
933-
try (PreparedStatement ps = jdbc.connection().prepareStatement(selectOne)) {
933+
try (PreparedStatement ps = conn.prepareStatement(selectOne)) {
934934
ps.setInt(1, 1234);
935935

936936
Assertions.assertTrue(ps.execute());
937937
try (ResultSet rs = ps.getResultSet()) {
938938
Assertions.assertTrue(rs.next());
939939
Assertions.assertEquals(1234, rs.getInt("key"));
940-
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
940+
Assertions.assertEquals(payloadLength, rs.getString("c_Text").length());
941941
Assertions.assertFalse(rs.next());
942942
}
943943
}
@@ -1176,8 +1176,8 @@ public void fullScanAnalyzerPreparedStatementTest() throws SQLException {
11761176
try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) {
11771177
try (PreparedStatement ps = connection.prepareStatement("print_JDBC_stats();")) {
11781178
sa.check(ps.executeQuery())
1179-
.assertMetaColumns()
1180-
.assertNoRows();
1179+
.assertMetaColumns()
1180+
.assertNoRows();
11811181
}
11821182

11831183
try (PreparedStatement ps = connection.prepareStatement(preparedSelectByKey)) {
@@ -1238,8 +1238,8 @@ public void fullScanAnalyzerPreparedStatementTest() throws SQLException {
12381238

12391239
try (PreparedStatement ps = connection.prepareStatement("print_JDBC_stats();")) {
12401240
sa.check(ps.executeQuery())
1241-
.assertMetaColumns()
1242-
.assertNoRows();
1241+
.assertMetaColumns()
1242+
.assertNoRows();
12431243
}
12441244
}
12451245
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementImplTest.java

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ public class YdbPreparedStatementImplTest {
5151
private static final String SELECT_BY_KEY_SQL = ""
5252
+ "declare $key as Optional<Int32>;\n"
5353
+ "select key, #column from #tableName where key=$key";
54-
private static final String SCAN_SELECT_BY_KEY_SQL = ""
55-
+ "declare $key as Optional<Int32>;\n"
56-
+ "scan select key, #column from #tableName where key=$key";
5754

5855
@BeforeAll
5956
public static void initTable() throws SQLException {
@@ -97,20 +94,6 @@ private YdbPreparedStatement prepareSelectByKey(String column) throws SQLExcepti
9794
return jdbc.connection().prepareStatement(sql).unwrap(YdbPreparedStatement.class);
9895
}
9996

100-
private PreparedStatement prepareScanSelect(String column) throws SQLException {
101-
String sql = SIMPLE_SELECT_SQL
102-
.replaceAll("#column", column)
103-
.replaceAll("#tableName", TEST_TABLE_NAME);
104-
return jdbc.connection().prepareStatement("SCAN " + sql);
105-
}
106-
107-
private String scanSelectByKey(String column) throws SQLException {
108-
String sql = SCAN_SELECT_BY_KEY_SQL
109-
.replaceAll("#column", column)
110-
.replaceAll("#tableName", TEST_TABLE_NAME);
111-
return sql;
112-
}
113-
11497
private YdbPreparedStatement prepareSelectAll() throws SQLException {
11598
return jdbc.connection().prepareStatement(TEST_TABLE.selectSQL())
11699
.unwrap(YdbPreparedStatement.class);
@@ -260,7 +243,7 @@ public void executeEmptyBatch(SqlQueries.YqlQuery mode) throws SQLException {
260243
}
261244

262245
@Test
263-
public void executeQueryBatchWithScanRead() throws SQLException {
246+
public void executeQueryBatchWithBigRead() throws SQLException {
264247
int valuesCount = 5000;
265248
String[] values = new String[valuesCount];
266249
for (int idx = 1; idx <= valuesCount; idx += 1) {
@@ -283,14 +266,7 @@ public void executeQueryBatchWithScanRead() throws SQLException {
283266
}
284267
}
285268

286-
ExceptionAssert.sqlException("Result #0 was truncated to 1000 rows", () -> {
287-
// Result is truncated (and we catch that)
288-
try (PreparedStatement select = prepareSimpleSelect("c_Text")) {
289-
select.executeQuery();
290-
}
291-
});
292-
293-
try (PreparedStatement select = prepareScanSelect("c_Text")) {
269+
try (PreparedStatement select = prepareSimpleSelect("c_Text")) {
294270
TextSelectAssert check = TextSelectAssert.of(select.executeQuery(), "c_Text", "Text");
295271

296272
for (int idx = 1; idx <= valuesCount; idx += 1) {
@@ -365,42 +341,6 @@ public void executeQueryInTx(SqlQueries.YqlQuery mode) throws SQLException {
365341
}
366342
}
367343

368-
@ParameterizedTest(name = "with {0}")
369-
@EnumSource(SqlQueries.YqlQuery.class)
370-
public void executeScanQueryInTx(SqlQueries.YqlQuery mode) throws SQLException {
371-
String upsertYql = TEST_TABLE.upsertOne(mode, "c_Text", "Text");
372-
String scanSelectYql = scanSelectByKey("c_Text");
373-
374-
jdbc.connection().setAutoCommit(false);
375-
YdbConnection conn = jdbc.connection().unwrap(YdbConnection.class);
376-
try {
377-
try (YdbPreparedStatement statement = conn.prepareStatement(upsertYql)) {
378-
statement.setInt("key", 1);
379-
statement.setString("c_Text", "value-1");
380-
statement.execute();
381-
}
382-
383-
try (YdbPreparedStatement select = conn.prepareStatement(scanSelectYql)) {
384-
select.setInt("key", 1);
385-
386-
ExceptionAssert.sqlException(YdbConst.SCAN_QUERY_INSIDE_TRANSACTION, () -> select.executeQuery());
387-
388-
jdbc.connection().commit();
389-
390-
select.setInt("key", 1);
391-
TextSelectAssert.of(select.executeQuery(), "c_Text", "Text")
392-
.nextRow(1, "value-1")
393-
.noNextRows();
394-
395-
select.setInt("key", 2);
396-
TextSelectAssert.of(select.executeQuery(), "c_Text", "Text")
397-
.noNextRows();
398-
}
399-
} finally {
400-
jdbc.connection().setAutoCommit(true);
401-
}
402-
}
403-
404344
@Test
405345
public void executeScanQueryAsUpdate() throws SQLException {
406346
String sql = "SCAN " + TEST_TABLE.upsertOne(SqlQueries.JdbcQuery.STANDARD, "c_Text", "Optional<Text>");

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementWithDataQueryBatchedImplTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,12 @@ public class YdbPreparedStatementWithDataQueryBatchedImplTest {
2525
private static final YdbHelperExtension ydb = new YdbHelperExtension();
2626

2727
@RegisterExtension
28-
private static final JdbcConnectionExtention jdbc = new JdbcConnectionExtention(ydb);
28+
private static final JdbcConnectionExtention jdbc = new JdbcConnectionExtention(ydb)
29+
.withArg("useQueryService", "false");
2930

3031
private static final String TEST_TABLE_NAME = "ydb_prepared_statement_with_batch_test";
3132
private static final SqlQueries TEST_TABLE = new SqlQueries(TEST_TABLE_NAME);
3233

33-
private static final String UPSERT_SQL = ""
34-
+ "declare $key as Optional<Int32>;\n"
35-
+ "declare $#column as #type;\n"
36-
+ "upsert into #tableName (key, #column) values ($key, $#column)";
37-
3834
private static final String BATCH_UPSERT_SQL = ""
3935
+ "declare $values as List<Struct<key:Int32, #column:#type>>; \n"
4036
+ "upsert into #tableName select * from as_table($values)";
@@ -77,13 +73,6 @@ public void afterEach() throws SQLException {
7773
jdbc.connection().close();
7874
}
7975

80-
private String upsertSql(String column, String type) {
81-
return UPSERT_SQL
82-
.replaceAll("#column", column)
83-
.replaceAll("#type", type)
84-
.replaceAll("#tableName", TEST_TABLE_NAME);
85-
}
86-
8776
private String batchUpsertSql(String column, String type) {
8877
return BATCH_UPSERT_SQL
8978
.replaceAll("#column", column)

0 commit comments

Comments
 (0)