Skip to content

Commit c89847b

Browse files
committed
Update execution implementation
1 parent 6556a93 commit c89847b

File tree

7 files changed

+96
-14
lines changed

7 files changed

+96
-14
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public final class YdbConst {
9191
public static final String UNSUPPORTED_QUERY_TYPE_IN_PS = "Query type in prepared statement not supported: ";
9292
public static final String STATEMENT_IS_NOT_A_BATCH = "Statement cannot be executed as batch statement: ";
9393
public static final String MULTI_TYPES_IN_ONE_QUERY = "Query cannot contain expressions with different types: ";
94+
public static final String SCAN_QUERY_INSIDE_TRANSACTION = "Scan query cannot be executed inside active "
95+
+ "transaction. This behavior may be changed by property scanQueryTxMode";
96+
public static final String SCHEME_QUERY_INSIDE_TRANSACTION = "Scheme query cannot be executed inside active "
97+
+ "transaction. This behavior may be changed by property schemeQueryTxMode";
9498

9599
// Cast errors
96100

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
import tech.ydb.jdbc.context.YdbContext;
3737
import tech.ydb.jdbc.context.YdbExecutor;
3838
import tech.ydb.jdbc.context.YdbTxState;
39+
import tech.ydb.jdbc.exception.YdbExecutionException;
3940
import tech.ydb.jdbc.query.QueryType;
4041
import tech.ydb.jdbc.query.YdbQuery;
42+
import tech.ydb.jdbc.settings.FakeTxMode;
43+
import tech.ydb.jdbc.settings.YdbOperationProperties;
4144
import tech.ydb.table.Session;
4245
import tech.ydb.table.query.DataQuery;
4346
import tech.ydb.table.query.DataQueryResult;
@@ -61,6 +64,8 @@ public class YdbConnectionImpl implements YdbConnection {
6164
private final YdbContext ctx;
6265
private final YdbExecutor executor;
6366
private final Supplier<YdbDatabaseMetaData> metaDataSupplier;
67+
private final FakeTxMode scanQueryTxMode;
68+
private final FakeTxMode schemeQueryTxMode;
6469

6570
private volatile YdbTxState state;
6671

@@ -69,10 +74,11 @@ public YdbConnectionImpl(YdbContext context) throws SQLException {
6974
this.metaDataSupplier = Suppliers.memoize(() -> new YdbDatabaseMetaDataImpl(this))::get;
7075
this.executor = new YdbExecutor(LOGGER);
7176

72-
int txLevel = ctx.getOperationProperties().getTransactionLevel();
73-
boolean txAutoCommit = ctx.getOperationProperties().isAutoCommit();
74-
this.state = YdbTxState.create(txLevel, txAutoCommit);
77+
YdbOperationProperties props = ctx.getOperationProperties();
78+
this.scanQueryTxMode = props.getScanQueryTxMode();
79+
this.schemeQueryTxMode = props.getSchemeQueryTxMode();
7580

81+
this.state = YdbTxState.create(props.getTransactionLevel(), props.isAutoCommit());
7682
this.ctx.register();
7783
}
7884

@@ -256,7 +262,17 @@ public void executeSchemeQuery(YdbQuery query, YdbExecutor executor) throws SQLE
256262
ensureOpened();
257263

258264
if (state.isInsideTransaction()) {
259-
commit();
265+
switch (schemeQueryTxMode) {
266+
case FAKE_TX:
267+
break;
268+
case SHADOW_COMMIT:
269+
commit();
270+
break;
271+
case ERROR:
272+
default:
273+
throw new YdbExecutionException(YdbConst.SCHEME_QUERY_INSIDE_TRANSACTION);
274+
275+
}
260276
}
261277

262278
// Scheme query does not affect transactions or result sets
@@ -292,6 +308,20 @@ public DataQueryResult executeDataQuery(YdbQuery query, YdbExecutor executor,
292308
public ResultSetReader executeScanQuery(YdbQuery query, YdbExecutor executor, Params params) throws SQLException {
293309
ensureOpened();
294310

311+
if (state.isInsideTransaction()) {
312+
switch (scanQueryTxMode) {
313+
case FAKE_TX:
314+
break;
315+
case SHADOW_COMMIT:
316+
commit();
317+
break;
318+
case ERROR:
319+
default:
320+
throw new YdbExecutionException(YdbConst.SCAN_QUERY_INSIDE_TRANSACTION);
321+
322+
}
323+
}
324+
295325
String yql = query.getYqlQuery(params);
296326
Collection<ResultSetReader> resultSets = new LinkedBlockingQueue<>();
297327
Duration scanQueryTimeout = ctx.getOperationProperties().getScanQueryTimeout();

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public QueryType type() {
9191
}
9292

9393
public static YdbQuery from(YdbQueryOptions opts, String sql) throws SQLException {
94-
YdbQueryBuilder builder = new YdbQueryBuilder(sql);
94+
YdbQueryBuilder builder = new YdbQueryBuilder(sql, opts.getForcedQueryType());
9595
JdbcQueryLexer.buildQuery(builder, opts);
9696
return new YdbQuery(opts, builder);
9797
}

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQueryBuilder.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ public class YdbQueryBuilder {
1515
private final String origin;
1616
private final StringBuilder query;
1717
private final List<String> args = new ArrayList<>();
18+
private final QueryType forcedType;
1819

1920
private int argsCounter = 0;
2021
private QueryType currentType = null;
2122

22-
public YdbQueryBuilder(String origin) {
23+
public YdbQueryBuilder(String origin, QueryType forcedType) {
2324
this.origin = origin;
2425
this.query = new StringBuilder(origin.length() + 10);
26+
this.forcedType = forcedType;
2527
}
2628

2729
public String createNextArgName() {
@@ -36,6 +38,10 @@ public String createNextArgName() {
3638
}
3739

3840
public void setQueryType(QueryType type) throws YdbNonRetryableException {
41+
if (forcedType != null) {
42+
return;
43+
}
44+
3945
if (currentType != null && currentType != type) {
4046
String msg = YdbConst.MULTI_TYPES_IN_ONE_QUERY + currentType + ", " + type;
4147
throw new YdbNonRetryableException(msg, StatusCode.BAD_REQUEST);
@@ -44,7 +50,15 @@ public void setQueryType(QueryType type) throws YdbNonRetryableException {
4450
}
4551

4652
public QueryType getQueryType() {
47-
return currentType != null ? currentType : QueryType.DATA_QUERY;
53+
if (forcedType != null) {
54+
return forcedType;
55+
}
56+
57+
if (currentType != null) {
58+
return currentType;
59+
}
60+
61+
return QueryType.DATA_QUERY;
4862
}
4963

5064
public String getOriginSQL() {

jdbc/src/main/java/tech/ydb/jdbc/query/YdbQueryOptions.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ public class YdbQueryOptions {
2323
private final boolean isPrepareDataQueries;
2424
private final boolean isDetectBatchQueries;
2525

26+
private final QueryType forcedType;
27+
2628
@VisibleForTesting
2729
YdbQueryOptions(
2830
boolean enforceV1,
2931
boolean detectQueryType,
3032
boolean detectJbdcParams,
3133
boolean declareJdbcParams,
3234
boolean prepareDataQuery,
33-
boolean detectBatchQuery
35+
boolean detectBatchQuery,
36+
QueryType forcedType
3437
) {
3538
this.isEnforceSyntaxV1 = enforceV1;
3639

@@ -40,6 +43,8 @@ public class YdbQueryOptions {
4043

4144
this.isPrepareDataQueries = prepareDataQuery;
4245
this.isDetectBatchQueries = detectBatchQuery;
46+
47+
this.forcedType = forcedType;
4348
}
4449

4550
public boolean isEnforceSyntaxV1() {
@@ -66,6 +71,10 @@ public boolean isDetectBatchQueries() {
6671
return isDetectBatchQueries;
6772
}
6873

74+
public QueryType getForcedQueryType() {
75+
return forcedType;
76+
}
77+
6978
public static YdbQueryOptions createFrom(YdbOperationProperties props) {
7079
int level = props.getJdbcSupportLevel();
7180

@@ -111,13 +120,16 @@ public static YdbQueryOptions createFrom(YdbOperationProperties props) {
111120
declareJdbcParams = declareJdbcParams && detectJbdcParams;
112121
}
113122

123+
QueryType forcedQueryType = props.getForcedQueryType();
124+
114125
return new YdbQueryOptions(
115126
enforceV1,
116127
detectQueryType,
117128
detectJbdcParams,
118129
declareJdbcParams,
119130
prepareDataQuery,
120-
detectBatchQuery
131+
detectBatchQuery,
132+
forcedQueryType
121133
);
122134
}
123135
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.Objects;
66

7+
import tech.ydb.jdbc.query.QueryType;
8+
79
public class YdbOperationProperties {
810
public static final int MAX_ROWS = 1000; // TODO: how to figure out the max rows of current connection?
911

@@ -21,6 +23,10 @@ public class YdbOperationProperties {
2123

2224
private final int jdbcSupportLevel;
2325

26+
private final FakeTxMode scanQueryTxMode;
27+
private final FakeTxMode schemeQueryTxMode;
28+
private final QueryType forcedQueryType;
29+
2430
public YdbOperationProperties(Map<YdbOperationProperty<?>, ParsedProperty> params) {
2531
this.params = Objects.requireNonNull(params);
2632

@@ -36,6 +42,10 @@ public YdbOperationProperties(Map<YdbOperationProperty<?>, ParsedProperty> param
3642
this.cacheConnectionsInDriver = params.get(YdbOperationProperty.CACHE_CONNECTIONS_IN_DRIVER).getParsedValue();
3743

3844
this.jdbcSupportLevel = params.get(YdbOperationProperty.JDBC_SUPPORT_LEVEL).getParsedValue();
45+
46+
this.scanQueryTxMode = params.get(YdbOperationProperty.SCAN_QUERY_TX_MODE).getParsedValue();
47+
this.schemeQueryTxMode = params.get(YdbOperationProperty.SCHEME_QUERY_TX_MODE).getParsedValue();
48+
this.forcedQueryType = params.get(YdbOperationProperty.FORCE_QUERY_MODE).getParsedValue();
3949
}
4050

4151
public Map<YdbOperationProperty<?>, ParsedProperty> getParams() {
@@ -58,6 +68,18 @@ public boolean isFailOnTruncatedResult() {
5868
return failOnTruncatedResult;
5969
}
6070

71+
public FakeTxMode getScanQueryTxMode() {
72+
return scanQueryTxMode;
73+
}
74+
75+
public FakeTxMode getSchemeQueryTxMode() {
76+
return schemeQueryTxMode;
77+
}
78+
79+
public QueryType getForcedQueryType() {
80+
return forcedQueryType;
81+
}
82+
6183
public Duration getSessionTimeout() {
6284
return sessionTimeout;
6385
}

jdbc/src/test/java/tech/ydb/jdbc/query/QueryLexerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ private String parseQuery(YdbQueryOptions opts, String sql) throws SQLException
2323

2424
private void assertMixType(YdbQueryOptions opts, String types, String sql) {
2525
SQLException ex = Assertions.assertThrows(SQLException.class, () -> {
26-
YdbQueryBuilder builder = new YdbQueryBuilder(sql);
26+
YdbQueryBuilder builder = new YdbQueryBuilder(sql, null);
2727
JdbcQueryLexer.buildQuery(builder, opts);
2828
}, "Mix type query must throw SQLException");
2929
Assertions.assertEquals("Query cannot contain expressions with different types: " + types, ex.getMessage());
3030
}
3131

3232
@Test
3333
public void enforceV1Test() throws SQLException {
34-
YdbQueryOptions disabled = new YdbQueryOptions(false, true, true, true, true, true);
35-
YdbQueryOptions enabled = new YdbQueryOptions(true, true, true, true, true, true);
34+
YdbQueryOptions disabled = new YdbQueryOptions(false, true, true, true, true, true, null);
35+
YdbQueryOptions enabled = new YdbQueryOptions(true, true, true, true, true, true, null);
3636

3737
Assertions.assertEquals("CREATE TABLE test_table (id int, value text)",
3838
parseQuery(disabled, "CREATE TABLE test_table (id int, value text)"));
@@ -43,7 +43,7 @@ public void enforceV1Test() throws SQLException {
4343

4444
@Test
4545
public void queryTypesTest() throws SQLException {
46-
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false);
46+
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false, null);
4747

4848
Assertions.assertEquals(QueryType.SCHEME_QUERY, parseQueryType(opts,
4949
"CREATE TABLE test_table (id int, value text)"
@@ -89,7 +89,7 @@ public void queryTypesTest() throws SQLException {
8989

9090
@Test
9191
public void mixQueryExceptionTest() throws SQLException {
92-
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false);
92+
YdbQueryOptions opts = new YdbQueryOptions(false, true, false, false, false, false, null);
9393

9494
assertMixType(opts, "SCHEME_QUERY, DATA_QUERY",
9595
"CREATE TABLE test_table (id int, value text);" +

0 commit comments

Comments
 (0)