Skip to content

Commit 45f3b82

Browse files
committed
Fixed usePrefixPath for preparing of YQL statements
1 parent d6978bb commit 45f3b82

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

jdbc/src/main/java/tech/ydb/jdbc/YdbConst.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public final class YdbConst {
9191
public static final String CANNOT_LOAD_DATA_FROM_IS = "Unable to load data from input stream: ";
9292
public static final String CANNOT_LOAD_DATA_FROM_READER = "Unable to load data from reader: ";
9393
public static final String STATEMENT_IS_NOT_A_BATCH = "Statement cannot be executed as batch statement: ";
94+
public static final String UNABLE_PREPARE_STATEMENT = "Cannot prepare statement: ";
9495
public static final String MULTI_TYPES_IN_ONE_QUERY = "Query cannot contain expressions with different types: ";
9596
public static final String SCAN_QUERY_INSIDE_TRANSACTION = "Scan query cannot be executed inside active "
9697
+ "transaction. This behavior may be changed by property scanQueryTxMode";

jdbc/src/main/java/tech/ydb/jdbc/context/YdbContext.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tech.ydb.core.settings.BaseRequestSettings;
2525
import tech.ydb.jdbc.YdbConst;
2626
import tech.ydb.jdbc.YdbPrepareMode;
27+
import tech.ydb.jdbc.YdbTracer;
2728
import tech.ydb.jdbc.exception.ExceptionFactory;
2829
import tech.ydb.jdbc.query.QueryType;
2930
import tech.ydb.jdbc.query.YdbPreparedQuery;
@@ -45,6 +46,7 @@
4546
import tech.ydb.table.TableClient;
4647
import tech.ydb.table.description.TableDescription;
4748
import tech.ydb.table.impl.PooledTableClient;
49+
import tech.ydb.table.query.DataQuery;
4850
import tech.ydb.table.query.ExplainDataQueryResult;
4951
import tech.ydb.table.rpc.grpc.GrpcTableRpc;
5052
import tech.ydb.table.settings.DescribeTableSettings;
@@ -393,11 +395,20 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
393395
String tablePath = joined(getPrefixPath(), query.getYqlBatcher().getTableName());
394396
TableDescription description = tableDescribeCache.getIfPresent(tablePath);
395397
if (description == null) {
398+
YdbTracer tracer = config.isTxTracedEnabled() ? YdbTracer.current() : null;
399+
if (tracer != null) {
400+
tracer.trace("--> describe table");
401+
tracer.traceRequest(tablePath);
402+
}
396403
DescribeTableSettings settings = withDefaultTimeout(new DescribeTableSettings());
397404
Result<TableDescription> result = retryCtx.supplyResult(
398405
session -> session.describeTable(tablePath, settings)
399406
).join();
400407

408+
if (tracer != null) {
409+
tracer.trace("<-- " + result.getStatus());
410+
}
411+
401412
if (result.isSuccess()) {
402413
description = result.getValue();
403414
tableDescribeCache.put(query.getOriginQuery(), description);
@@ -426,32 +437,46 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
426437
}
427438

428439
// try to prepare data query
429-
try {
430-
Map<String, Type> types = queryParamsCache.getIfPresent(query.getOriginQuery());
431-
if (types == null) {
432-
String yql = query.getPreparedYql();
433-
PrepareDataQuerySettings settings = withDefaultTimeout(new PrepareDataQuerySettings());
434-
types = retryCtx.supplyResult(session -> session.prepareDataQuery(yql, settings))
435-
.join()
436-
.getValue()
437-
.types();
438-
queryParamsCache.put(query.getOriginQuery(), types);
440+
Map<String, Type> types = queryParamsCache.getIfPresent(query.getOriginQuery());
441+
if (types == null) {
442+
String yql = prefixPragma + query.getPreparedYql();
443+
YdbTracer tracer = config.isTxTracedEnabled() ? YdbTracer.current() : null;
444+
if (tracer != null) {
445+
tracer.trace("--> prepare data query");
446+
tracer.traceRequest(yql);
439447
}
440448

441-
boolean requireBatch = mode == YdbPrepareMode.DATA_QUERY_BATCH;
442-
if (requireBatch || (mode == YdbPrepareMode.AUTO && queryOptions.isDetectBatchQueries())) {
443-
BatchedQuery params = BatchedQuery.tryCreateBatched(query, types);
444-
if (params != null) {
445-
return params;
446-
}
449+
PrepareDataQuerySettings settings = withDefaultTimeout(new PrepareDataQuerySettings());
450+
Result<DataQuery> result = retryCtx.supplyResult(
451+
session -> session.prepareDataQuery(yql, settings)
452+
).join();
447453

448-
if (requireBatch) {
449-
throw new SQLDataException(YdbConst.STATEMENT_IS_NOT_A_BATCH + query.getOriginQuery());
454+
if (tracer != null) {
455+
tracer.trace("<-- " + result.getStatus());
456+
}
457+
if (!result.isSuccess()) {
458+
if (tracer != null) {
459+
tracer.close();
450460
}
461+
throw ExceptionFactory.createException("Cannot prepare data query: " + result.getStatus(),
462+
new UnexpectedResultException("Unexpected status", result.getStatus()));
463+
}
464+
465+
types = result.getValue().types();
466+
queryParamsCache.put(query.getOriginQuery(), types);
467+
}
468+
469+
boolean requireBatch = mode == YdbPrepareMode.DATA_QUERY_BATCH;
470+
if (requireBatch || (mode == YdbPrepareMode.AUTO && queryOptions.isDetectBatchQueries())) {
471+
BatchedQuery params = BatchedQuery.tryCreateBatched(query, types);
472+
if (params != null) {
473+
return params;
474+
}
475+
476+
if (requireBatch) {
477+
throw new SQLDataException(YdbConst.STATEMENT_IS_NOT_A_BATCH + query.getOriginQuery());
451478
}
452-
return new PreparedQuery(query, types);
453-
} catch (UnexpectedResultException ex) {
454-
throw ExceptionFactory.createException("Cannot prepare data query: " + ex.getMessage(), ex);
455479
}
480+
return new PreparedQuery(query, types);
456481
}
457482
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ public void defaultModeTest() throws SQLException {
102102
}
103103

104104
// read all
105-
try (Statement st = connection.createStatement()) {
105+
try (PreparedStatement ps = connection.prepareStatement(SELECT_ALL)) {
106106
int readed = 0;
107-
try (ResultSet rs = st.executeQuery(SELECT_ALL)) {
107+
try (ResultSet rs = ps.executeQuery()) {
108108
while (rs.next()) {
109109
readed++;
110110
Assertions.assertEquals(readed, rs.getInt("id"));

0 commit comments

Comments
 (0)