|
24 | 24 | import tech.ydb.core.settings.BaseRequestSettings; |
25 | 25 | import tech.ydb.jdbc.YdbConst; |
26 | 26 | import tech.ydb.jdbc.YdbPrepareMode; |
| 27 | +import tech.ydb.jdbc.YdbTracer; |
27 | 28 | import tech.ydb.jdbc.exception.ExceptionFactory; |
28 | 29 | import tech.ydb.jdbc.query.QueryType; |
29 | 30 | import tech.ydb.jdbc.query.YdbPreparedQuery; |
|
45 | 46 | import tech.ydb.table.TableClient; |
46 | 47 | import tech.ydb.table.description.TableDescription; |
47 | 48 | import tech.ydb.table.impl.PooledTableClient; |
| 49 | +import tech.ydb.table.query.DataQuery; |
48 | 50 | import tech.ydb.table.query.ExplainDataQueryResult; |
49 | 51 | import tech.ydb.table.rpc.grpc.GrpcTableRpc; |
50 | 52 | import tech.ydb.table.settings.DescribeTableSettings; |
@@ -393,11 +395,20 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode) |
393 | 395 | String tablePath = joined(getPrefixPath(), query.getYqlBatcher().getTableName()); |
394 | 396 | TableDescription description = tableDescribeCache.getIfPresent(tablePath); |
395 | 397 | 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 | + } |
396 | 403 | DescribeTableSettings settings = withDefaultTimeout(new DescribeTableSettings()); |
397 | 404 | Result<TableDescription> result = retryCtx.supplyResult( |
398 | 405 | session -> session.describeTable(tablePath, settings) |
399 | 406 | ).join(); |
400 | 407 |
|
| 408 | + if (tracer != null) { |
| 409 | + tracer.trace("<-- " + result.getStatus()); |
| 410 | + } |
| 411 | + |
401 | 412 | if (result.isSuccess()) { |
402 | 413 | description = result.getValue(); |
403 | 414 | tableDescribeCache.put(query.getOriginQuery(), description); |
@@ -426,32 +437,46 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode) |
426 | 437 | } |
427 | 438 |
|
428 | 439 | // 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); |
439 | 447 | } |
440 | 448 |
|
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(); |
447 | 453 |
|
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(); |
450 | 460 | } |
| 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()); |
451 | 478 | } |
452 | | - return new PreparedQuery(query, types); |
453 | | - } catch (UnexpectedResultException ex) { |
454 | | - throw ExceptionFactory.createException("Cannot prepare data query: " + ex.getMessage(), ex); |
455 | 479 | } |
| 480 | + return new PreparedQuery(query, types); |
456 | 481 | } |
457 | 482 | } |
0 commit comments