Skip to content

Commit c327fe6

Browse files
committed
Added context scopes YdbTypes
1 parent 3d74021 commit c327fe6

26 files changed

+192
-189
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/ColumnInfo.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ public class ColumnInfo {
1919
private final boolean isNumber;
2020
private final boolean isNull;
2121

22-
public ColumnInfo(String name, Type type) {
22+
public ColumnInfo(String name, TypeDescription type) {
2323
this.name = name;
2424

25-
TypeDescription desc = TypeDescription.of(type);
26-
this.sqlType = desc.sqlType();
27-
this.getters = desc.getters();
28-
this.isOptional = desc.isOptional();
29-
this.ydbType = desc.ydbType();
25+
this.sqlType = type.sqlType();
26+
this.getters = type.getters();
27+
this.isOptional = type.isOptional();
28+
this.ydbType = type.ydbType();
3029

3130
this.isTimestamp = ydbType == PrimitiveType.Timestamp;
3231
this.isNumber = ydbType == PrimitiveType.Int8 || ydbType == PrimitiveType.Uint8
@@ -68,10 +67,11 @@ public MappingGetters.Getters getGetters() {
6867
return this.getters;
6968
}
7069

71-
public static ColumnInfo[] fromResultSetReader(ResultSetReader rsr) {
70+
public static ColumnInfo[] fromResultSetReader(YdbTypes types, ResultSetReader rsr) {
7271
ColumnInfo[] columns = new ColumnInfo[rsr.getColumnCount()];
7372
for (int idx = 0; idx < rsr.getColumnCount(); idx += 1) {
74-
columns[idx] = new ColumnInfo(rsr.getColumnName(idx), rsr.getColumnType(idx));
73+
TypeDescription type = types.find(rsr.getColumnType(idx));
74+
columns[idx] = new ColumnInfo(rsr.getColumnName(idx), type);
7575
}
7676
return columns;
7777
}

jdbc/src/main/java/tech/ydb/jdbc/common/MappingGetters.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.function.Function;
2323

2424
import tech.ydb.jdbc.YdbConst;
25-
import tech.ydb.jdbc.impl.YdbTypes;
2625
import tech.ydb.table.result.PrimitiveReader;
2726
import tech.ydb.table.result.ValueReader;
2827
import tech.ydb.table.values.DecimalValue;
@@ -644,10 +643,7 @@ private static SqlType buildPrimitiveType(int sqlType, PrimitiveType id) {
644643
}
645644
}
646645

647-
static SqlType buildDataType(Type type) {
648-
// All types must be the same as for #valueToObject
649-
int sqlType = YdbTypes.toSqlType(type);
650-
646+
static SqlType buildDataType(int sqlType, Type type) {
651647
switch (type.getKind()) {
652648
case PRIMITIVE:
653649
return buildPrimitiveType(sqlType, (PrimitiveType) type);

jdbc/src/main/java/tech/ydb/jdbc/common/TypeDescription.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
package tech.ydb.jdbc.common;
22

3-
import java.util.Map;
43
import java.util.Objects;
5-
import java.util.concurrent.ConcurrentHashMap;
64

7-
import tech.ydb.table.values.DecimalType;
85
import tech.ydb.table.values.OptionalType;
96
import tech.ydb.table.values.OptionalValue;
107
import tech.ydb.table.values.PrimitiveType;
118
import tech.ydb.table.values.Type;
129

1310
public class TypeDescription {
14-
private static final Map<Type, TypeDescription> TYPES = new ConcurrentHashMap<>();
15-
16-
static {
17-
ofInternal(DecimalType.of(DecimalType.MAX_PRECISION)); // max
18-
ofInternal(DecimalType.getDefault()); // default for database
19-
for (PrimitiveType type : PrimitiveType.values()) {
20-
ofInternal(type); // All primitive values
21-
}
22-
}
23-
2411
private final Type type;
2512

2613
private final boolean isTimestamp;
@@ -91,12 +78,7 @@ public Type ydbType() {
9178
return type;
9279
}
9380

94-
private static void ofInternal(Type type) {
95-
of(type);
96-
of(type.makeOptional()); // Register both normal and optional types
97-
}
98-
99-
private static TypeDescription buildType(Type origType) {
81+
static TypeDescription buildType(YdbTypes types, Type origType) {
10082
Type type;
10183
OptionalValue optionalValue;
10284
if (origType.getKind() == Type.Kind.OPTIONAL) {
@@ -108,15 +90,13 @@ private static TypeDescription buildType(Type origType) {
10890
optionalValue = null;
10991
}
11092

93+
// All types must be the same as for #valueToObject
94+
int sqlType = types.toSqlType(type);
95+
11196
MappingGetters.Getters getters = MappingGetters.buildGetters(type);
11297
MappingSetters.Setters setters = MappingSetters.buildSetters(type);
113-
MappingGetters.SqlType sqlTypes = MappingGetters.buildDataType(type);
98+
MappingGetters.SqlType sqlTypes = MappingGetters.buildDataType(sqlType, type);
11499

115100
return new TypeDescription(type, optionalValue, getters, setters, sqlTypes);
116101
}
117-
118-
public static TypeDescription of(Type type) {
119-
// TODO: check for cache poisoning?
120-
return TYPES.computeIfAbsent(type, TypeDescription::buildType);
121-
}
122102
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import tech.ydb.jdbc.YdbResultSet;
1515
import tech.ydb.jdbc.YdbStatement;
1616
import tech.ydb.jdbc.YdbTracer;
17+
import tech.ydb.jdbc.common.YdbTypes;
1718
import tech.ydb.jdbc.impl.YdbQueryResult;
1819
import tech.ydb.jdbc.impl.YdbStaticResultSet;
1920
import tech.ydb.jdbc.query.QueryType;
@@ -41,6 +42,7 @@ public abstract class BaseYdbExecutor implements YdbExecutor {
4142
private final AtomicReference<YdbQueryResult> currResult;
4243
protected final boolean traceEnabled;
4344
protected final String prefixPragma;
45+
protected final YdbTypes types;
4446

4547
public BaseYdbExecutor(YdbContext ctx) {
4648
this.retryCtx = ctx.getRetryCtx();
@@ -49,6 +51,7 @@ public BaseYdbExecutor(YdbContext ctx) {
4951
this.useStreamResultSet = ctx.getOperationProperties().getUseStreamResultSets();
5052
this.tableClient = ctx.getTableClient();
5153
this.prefixPragma = ctx.getPrefixPragma();
54+
this.types = ctx.getTypes();
5255
this.currResult = new AtomicReference<>();
5356
}
5457

@@ -155,7 +158,7 @@ public YdbQueryResult executeScanQuery(YdbStatement statement, YdbQuery query, S
155158
() -> session.executeScanQuery(yql, params, settings).start(resultSets::add)
156159
);
157160

158-
YdbResultSet rs = new YdbStaticResultSet(statement, ProtoValueReaders.forResultSets(resultSets));
161+
YdbResultSet rs = new YdbStaticResultSet(types, statement, ProtoValueReaders.forResultSets(resultSets));
159162
return updateCurrentResult(new StaticQueryResult(query, Collections.singletonList(rs)));
160163
} finally {
161164
session.close();
@@ -166,7 +169,7 @@ public YdbQueryResult executeScanQuery(YdbStatement statement, YdbQuery query, S
166169
StreamQueryResult lazy = validator.call(msg, null, () -> {
167170
final CompletableFuture<Result<StreamQueryResult>> future = new CompletableFuture<>();
168171
final GrpcReadStream<ResultSetReader> stream = session.executeScanQuery(yql, params, settings);
169-
final StreamQueryResult result = new StreamQueryResult(msg, statement, query, stream::cancel);
172+
final StreamQueryResult result = new StreamQueryResult(msg, types, statement, query, stream::cancel);
170173

171174
stream.start((rsr) -> {
172175
future.complete(Result.success(result));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public YdbQueryResult executeDataQuery(
258258
StreamQueryResult lazy = validator.call(msg, tracer, () -> {
259259
final CompletableFuture<Result<StreamQueryResult>> future = new CompletableFuture<>();
260260
final QueryStream stream = localTx.createQuery(yql, isAutoCommit, params, settings);
261-
final StreamQueryResult result = new StreamQueryResult(msg, statement, query, stream::cancel);
261+
final StreamQueryResult result = new StreamQueryResult(msg, types, statement, query, stream::cancel);
262262

263263
stream.execute(new QueryStream.PartsHandler() {
264264
@Override
@@ -313,7 +313,7 @@ public void onNextPart(QueryResultPart part) {
313313

314314
List<YdbResultSet> readers = new ArrayList<>();
315315
for (ResultSetReader rst: result) {
316-
readers.add(new YdbStaticResultSet(statement, rst));
316+
readers.add(new YdbStaticResultSet(types, statement, rst));
317317
}
318318
return updateCurrentResult(new StaticQueryResult(query, readers));
319319
} finally {
@@ -387,7 +387,7 @@ public YdbQueryResult executeExplainQuery(YdbStatement statement, YdbQuery query
387387
}
388388

389389
return updateCurrentResult(
390-
new StaticQueryResult(statement, res.getStats().getQueryAst(), res.getStats().getQueryPlan())
390+
new StaticQueryResult(types, statement, res.getStats().getQueryAst(), res.getStats().getQueryPlan())
391391
);
392392
} finally {
393393
if (tx.get() == null) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tech.ydb.jdbc.YdbResultSet;
1111
import tech.ydb.jdbc.YdbStatement;
1212
import tech.ydb.jdbc.common.FixedResultSetFactory;
13+
import tech.ydb.jdbc.common.YdbTypes;
1314
import tech.ydb.jdbc.impl.YdbQueryResult;
1415
import tech.ydb.jdbc.impl.YdbStaticResultSet;
1516
import tech.ydb.jdbc.query.QueryStatement;
@@ -75,15 +76,15 @@ public StaticQueryResult(YdbQuery query, List<YdbResultSet> list) {
7576
}
7677
}
7778

78-
public StaticQueryResult(YdbStatement statement, String ast, String plan) {
79+
public StaticQueryResult(YdbTypes types, YdbStatement statement, String ast, String plan) {
7980
ResultSetReader result = EXPLAIN_RS_FACTORY.createResultSet()
8081
.newRow()
8182
.withTextValue(YdbConst.EXPLAIN_COLUMN_AST, ast)
8283
.withTextValue(YdbConst.EXPLAIN_COLUMN_PLAN, plan)
8384
.build()
8485
.build();
8586

86-
YdbResultSet rs = new YdbStaticResultSet(statement, result);
87+
YdbResultSet rs = new YdbStaticResultSet(types, statement, result);
8788
this.results = Collections.singletonList(new ExpressionResult(rs));
8889
this.resultIndex = 0;
8990
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tech.ydb.jdbc.YdbResultSet;
2323
import tech.ydb.jdbc.YdbStatement;
2424
import tech.ydb.jdbc.common.ColumnInfo;
25+
import tech.ydb.jdbc.common.YdbTypes;
2526
import tech.ydb.jdbc.exception.ExceptionFactory;
2627
import tech.ydb.jdbc.impl.BaseYdbResultSet;
2728
import tech.ydb.jdbc.impl.YdbQueryResult;
@@ -41,6 +42,7 @@ public class StreamQueryResult implements YdbQueryResult {
4142
private static final int UPDATE_EXPRESSION = -2;
4243

4344
private final String msg;
45+
private final YdbTypes types;
4446
private final YdbStatement statement;
4547
private final Runnable streamStopper;
4648

@@ -53,10 +55,11 @@ public class StreamQueryResult implements YdbQueryResult {
5355
private int resultIndex = 0;
5456
private volatile boolean resultClosed = false;
5557

56-
public StreamQueryResult(String msg, YdbStatement statement, YdbQuery query, Runnable streamStopper) {
58+
public StreamQueryResult(String msg, YdbTypes types, YdbStatement statement, YdbQuery query, Runnable stopper) {
5759
this.msg = msg;
60+
this.types = types;
5861
this.statement = statement;
59-
this.streamStopper = streamStopper;
62+
this.streamStopper = stopper;
6063

6164
this.resultIndexes = new int[query.getStatements().size()];
6265

@@ -82,7 +85,7 @@ public void onStreamResultSet(int index, ResultSetReader rsr) {
8285
CompletableFuture<Result<LazyResultSet>> future = resultFutures.get(index);
8386

8487
if (!future.isDone()) {
85-
ColumnInfo[] columns = ColumnInfo.fromResultSetReader(rsr);
88+
ColumnInfo[] columns = ColumnInfo.fromResultSetReader(types, rsr);
8689
LazyResultSet rs = new LazyResultSet(statement, columns);
8790
rs.addResultSet(rsr);
8891
if (future.complete(Result.success(rs))) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public YdbQueryResult executeExplainQuery(YdbStatement statement, YdbQuery query
189189
try (Session session = createNewTableSession(validator)) {
190190
String msg = QueryType.EXPLAIN_QUERY + " >>\n" + yql;
191191
ExplainDataQueryResult res = validator.call(msg, tracer, () -> session.explainDataQuery(yql, settings));
192-
return updateCurrentResult(new StaticQueryResult(statement, res.getQueryAst(), res.getQueryPlan()));
192+
return updateCurrentResult(new StaticQueryResult(types, statement, res.getQueryAst(), res.getQueryPlan()));
193193
} finally {
194194
if (!tx.isInsideTransaction()) {
195195
tracer.close();
@@ -224,7 +224,7 @@ public YdbQueryResult executeDataQuery(YdbStatement statement, YdbQuery query, S
224224
throw new SQLException(msg);
225225
}
226226

227-
readers.add(new YdbStaticResultSet(statement, rs));
227+
readers.add(new YdbStaticResultSet(types, statement, rs));
228228
}
229229

230230
return updateCurrentResult(new StaticQueryResult(query, readers));

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import tech.ydb.jdbc.YdbConst;
2626
import tech.ydb.jdbc.YdbPrepareMode;
2727
import tech.ydb.jdbc.YdbTracer;
28+
import tech.ydb.jdbc.common.YdbTypes;
2829
import tech.ydb.jdbc.exception.ExceptionFactory;
2930
import tech.ydb.jdbc.impl.YdbTracerNone;
3031
import tech.ydb.jdbc.query.QueryType;
@@ -71,6 +72,7 @@ public class YdbContext implements AutoCloseable {
7172

7273
private final YdbOperationProperties operationProps;
7374
private final YdbQueryProperties queryOptions;
75+
private final YdbTypes types;
7476

7577
private final GrpcTransport grpcTransport;
7678
private final PooledTableClient tableClient;
@@ -109,6 +111,8 @@ private YdbContext(
109111
this.schemeClient = SchemeClient.newClient(transport).build();
110112
this.retryCtx = SessionRetryContext.create(tableClient).build();
111113

114+
this.types = new YdbTypes();
115+
112116
int cacheSize = config.getPreparedStatementsCachecSize();
113117
if (cacheSize > 0) {
114118
queriesCache = CacheBuilder.newBuilder().maximumSize(cacheSize).build();
@@ -135,6 +139,10 @@ private YdbContext(
135139
}
136140
}
137141

142+
public YdbTypes getTypes() {
143+
return types;
144+
}
145+
138146
/**
139147
* Grpc Transport for other API YDB server clients
140148
*
@@ -334,7 +342,7 @@ public <T extends BaseRequestSettings.BaseBuilder<T>> T withRequestTimeout(T bui
334342
}
335343

336344
public YdbQuery parseYdbQuery(String sql) throws SQLException {
337-
return YdbQuery.parseQuery(sql, queryOptions);
345+
return YdbQuery.parseQuery(sql, queryOptions, types);
338346
}
339347

340348
public YdbQuery findOrParseYdbQuery(String sql) throws SQLException {
@@ -422,11 +430,11 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
422430
}
423431
}
424432
if (type == QueryType.BULK_QUERY) {
425-
return BulkUpsertQuery.build(tablePath, query.getYqlBatcher().getColumns(), description);
433+
return BulkUpsertQuery.build(types, tablePath, query.getYqlBatcher().getColumns(), description);
426434
}
427435

428436
if (description != null) {
429-
BatchedQuery params = BatchedQuery.createAutoBatched(query.getYqlBatcher(), description);
437+
BatchedQuery params = BatchedQuery.createAutoBatched(types, query.getYqlBatcher(), description);
430438
if (params != null) {
431439
return params;
432440
}
@@ -438,8 +446,8 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
438446
}
439447

440448
// try to prepare data query
441-
Map<String, Type> types = queryParamsCache.getIfPresent(query.getOriginQuery());
442-
if (types == null) {
449+
Map<String, Type> queryTypes = queryParamsCache.getIfPresent(query.getOriginQuery());
450+
if (queryTypes == null) {
443451
String yql = prefixPragma + query.getPreparedYql();
444452
YdbTracer tracer = getTracer();
445453
tracer.trace("--> prepare data query");
@@ -457,13 +465,13 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
457465
new UnexpectedResultException("Unexpected status", result.getStatus()));
458466
}
459467

460-
types = result.getValue().types();
461-
queryParamsCache.put(query.getOriginQuery(), types);
468+
queryTypes = result.getValue().types();
469+
queryParamsCache.put(query.getOriginQuery(), queryTypes);
462470
}
463471

464472
boolean requireBatch = mode == YdbPrepareMode.DATA_QUERY_BATCH;
465473
if (requireBatch || (mode == YdbPrepareMode.AUTO && queryOptions.isDetectBatchQueries())) {
466-
BatchedQuery params = BatchedQuery.tryCreateBatched(query, types);
474+
BatchedQuery params = BatchedQuery.tryCreateBatched(types, query, queryTypes);
467475
if (params != null) {
468476
return params;
469477
}
@@ -472,6 +480,6 @@ public YdbPreparedQuery findOrPrepareParams(YdbQuery query, YdbPrepareMode mode)
472480
throw new SQLDataException(YdbConst.STATEMENT_IS_NOT_A_BATCH + query.getOriginQuery());
473481
}
474482
}
475-
return new PreparedQuery(query, types);
483+
return new PreparedQuery(types, query, queryTypes);
476484
}
477485
}

jdbc/src/main/java/tech/ydb/jdbc/impl/BaseYdbStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import tech.ydb.jdbc.settings.FakeTxMode;
2222
import tech.ydb.jdbc.settings.YdbOperationProperties;
2323
import tech.ydb.table.query.Params;
24+
import tech.ydb.table.result.ResultSetReader;
2425
import tech.ydb.table.values.ListValue;
2526

2627
/**
@@ -183,7 +184,8 @@ protected YdbQueryResult executeDataQuery(YdbQuery query, String yql, Params par
183184
YdbContext ctx = connection.getCtx();
184185
if (ctx.queryStatsEnabled()) {
185186
if (QueryStat.isPrint(yql)) {
186-
YdbResultSet rs = new YdbStaticResultSet(this, QueryStat.toResultSetReader(ctx.getQueryStats()));
187+
ResultSetReader rsr = QueryStat.toResultSetReader(ctx.getQueryStats());
188+
YdbResultSet rs = new YdbStaticResultSet(ctx.getTypes(), this, rsr);
187189
return new StaticQueryResult(query, Collections.singletonList(rs));
188190
}
189191
if (QueryStat.isReset(yql)) {

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbConnectionImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class YdbConnectionImpl implements YdbConnection {
4242

4343
public YdbConnectionImpl(YdbContext context) throws SQLException {
4444
this.ctx = context;
45-
4645
this.validator = new YdbValidator();
4746
this.executor = ctx.createExecutor();
4847
this.ctx.register();

0 commit comments

Comments
 (0)