Skip to content

Commit 6b53c00

Browse files
authored
Added lazy result sets for SCAN queries (#67)
2 parents a7344aa + 6e7d7fc commit 6b53c00

31 files changed

+3574
-1019
lines changed

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

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,19 @@
22

33
import java.sql.Connection;
44
import java.sql.SQLException;
5-
import java.util.List;
65

76
import javax.annotation.Nullable;
87

98
import tech.ydb.jdbc.context.YdbContext;
10-
import tech.ydb.jdbc.context.YdbValidator;
11-
import tech.ydb.jdbc.query.ExplainedQuery;
12-
import tech.ydb.jdbc.query.YdbQuery;
13-
import tech.ydb.table.query.Params;
14-
import tech.ydb.table.result.ResultSetReader;
15-
import tech.ydb.table.values.ListValue;
9+
import tech.ydb.jdbc.context.YdbExecutor;
1610

1711
public interface YdbConnection extends Connection {
18-
/**
19-
* Return current YDB transaction, if exists
20-
*
21-
* @return YDB transaction ID or null, if no transaction started
22-
*/
2312
@Nullable
24-
String getYdbTxId();
25-
13+
String getYdbTxId() throws SQLException;
2614

2715
YdbContext getCtx();
2816

29-
/**
30-
* Explicitly execute query as a schema query
31-
*
32-
* @param yql query (DDL) to execute
33-
* @param validator handler for logging and warnings
34-
* @throws SQLException if query cannot be executed
35-
*/
36-
void executeSchemeQuery(String yql, YdbValidator validator) throws SQLException;
37-
38-
/**
39-
* Explicitly execute bulk upsert to the table
40-
*
41-
* @param yql description of request
42-
* @param tablePath path to table
43-
* @param validator handler for logging and warnings
44-
* @param rows bulk rows
45-
* @throws SQLException if query cannot be executed
46-
*/
47-
void executeBulkUpsertQuery(String yql, String tablePath, YdbValidator validator, ListValue rows)
48-
throws SQLException;
49-
50-
/**
51-
* Explicitly execute query as a data query
52-
*
53-
* @param query query to execute
54-
* @param yql YQL text to execute
55-
* @param params parameters for query
56-
* @param timeout timeout of operation
57-
* @param keepInCache flag to store query in server-side cache
58-
* @param validator handler for logging and warnings
59-
* @return list of result set
60-
* @throws SQLException if query cannot be executed
61-
*/
62-
List<ResultSetReader> executeDataQuery(YdbQuery query, String yql, YdbValidator validator,
63-
int timeout, boolean keepInCache, Params params) throws SQLException;
64-
65-
/**
66-
* Explicitly execute query as a scan query
67-
*
68-
* @param query query to execute
69-
* @param yql YQL text to execute
70-
* @param params parameters for query
71-
* @param validator handler for logging and warnings
72-
* @return single result set with rows
73-
* @throws SQLException if query cannot be executed
74-
*/
75-
ResultSetReader executeScanQuery(YdbQuery query, String yql, YdbValidator validator, Params params)
76-
throws SQLException;
77-
78-
/**
79-
* Explicitly explain this query
80-
*
81-
* @param yql query to explain
82-
* @param validator handler for logging and warnings
83-
* @return list of result set of two string columns: {@link YdbConst#EXPLAIN_COLUMN_AST}
84-
* and {@link YdbConst#EXPLAIN_COLUMN_PLAN}
85-
* @throws SQLException if query cannot be explained
86-
*/
87-
ExplainedQuery executeExplainQuery(String yql, YdbValidator validator) throws SQLException;
17+
YdbExecutor getExecutor();
8818

8919
@Override
9020
YdbDatabaseMetaData getMetaData() throws SQLException;

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,10 @@
22

33
import java.sql.ResultSet;
44
import java.sql.SQLException;
5-
import java.util.Optional;
65

7-
import tech.ydb.table.result.ResultSetReader;
86
import tech.ydb.table.values.Value;
97

108
public interface YdbResultSet extends ResultSet {
11-
12-
/**
13-
* Return YDB result set reader for direct processing
14-
*
15-
* @return YDB original result set reader
16-
*/
17-
ResultSetReader getYdbResultSetReader();
18-
19-
//
20-
219
/**
2210
* Returns native YDB value, extracted from optional value.
2311
* Please note that this method will create value object for each method call.
@@ -26,7 +14,7 @@ public interface YdbResultSet extends ResultSet {
2614
* @return value if available; return empty if value is optional and no value provided
2715
* @throws SQLException if column cannot be read
2816
*/
29-
Optional<Value<?>> getNativeColumn(int columnIndex) throws SQLException;
17+
Value<?> getNativeColumn(int columnIndex) throws SQLException;
3018

3119
/**
3220
* Return native YDB value.
@@ -36,7 +24,7 @@ public interface YdbResultSet extends ResultSet {
3624
* @return value if available
3725
* @throws SQLException if column cannot be read
3826
*/
39-
Optional<Value<?>> getNativeColumn(String columnLabel) throws SQLException;
27+
Value<?> getNativeColumn(String columnLabel) throws SQLException;
4028

4129
//
4230

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import java.sql.ResultSetMetaData;
44
import java.sql.SQLException;
5-
import java.util.ArrayList;
6-
import java.util.Collection;
7-
import java.util.List;
85

96
import tech.ydb.table.values.Type;
107

@@ -15,31 +12,7 @@ public interface YdbResultSetMetaData extends ResultSetMetaData {
1512
*
1613
* @param column column, 1..N
1714
* @return YDB type
15+
* @throws java.sql.SQLException
1816
*/
1917
Type getYdbType(int column) throws SQLException;
20-
21-
/**
22-
* Returns column index by it's name, could be useful sometimes; basically because in YDB you should always
23-
* work with names (columns or parameters), indexes are just internal details
24-
*
25-
* @param columnName column name to find
26-
* @return column (1..N)
27-
* @throws SQLException if column is unknown
28-
*/
29-
int getColumnIndex(String columnName) throws SQLException;
30-
31-
/**
32-
* Returns all column names
33-
*
34-
* @return column names
35-
* @throws SQLException in case something bad happens
36-
*/
37-
default Collection<String> getColumnNames() throws SQLException {
38-
int count = getColumnCount();
39-
List<String> columns = new ArrayList<>(count);
40-
for (int i = 0; i < count; i++) {
41-
columns.add(getColumnName(i + 1));
42-
}
43-
return columns;
44-
}
4518
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.sql.SQLException;
44
import java.sql.Statement;
55

6+
import tech.ydb.jdbc.context.YdbValidator;
7+
68
public interface YdbStatement extends Statement {
79
/**
810
* Explicitly execute query as a schema query
@@ -31,16 +33,7 @@ public interface YdbStatement extends Statement {
3133
*/
3234
YdbResultSet executeExplainQuery(String sql) throws SQLException;
3335

34-
/**
35-
* Cant return previous results sets after {@link #getMoreResults()} traverse,
36-
* in case previous rows were not removed by providing {@link Statement#CLOSE_ALL_RESULTS}
37-
* or {@link Statement#CLOSE_CURRENT_RESULT}
38-
*
39-
* @param resultSetIndex index
40-
* @return optional result (if traversed before) with {@link #getMoreResults()}
41-
* @throws SQLException when trying to get result set on closed connection
42-
*/
43-
YdbResultSet getResultSetAt(int resultSetIndex) throws SQLException;
36+
YdbValidator getValidator();
4437

4538
@Override
4639
YdbResultSet executeQuery(String sql) throws SQLException;
@@ -51,4 +44,5 @@ public interface YdbStatement extends Statement {
5144
@Override
5245
YdbConnection getConnection() throws SQLException;
5346

47+
void waitReady() throws SQLException;
5448
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package tech.ydb.jdbc.common;
2+
3+
import tech.ydb.table.result.ResultSetReader;
4+
import tech.ydb.table.values.PrimitiveType;
5+
import tech.ydb.table.values.Type;
6+
7+
/**
8+
*
9+
* @author Aleksandr Gorshenin
10+
*/
11+
public class ColumnInfo {
12+
private final String name;
13+
private final Type ydbType;
14+
private final MappingGetters.SqlType sqlType;
15+
private final MappingGetters.Getters getters;
16+
17+
private final boolean isOptional;
18+
private final boolean isTimestamp;
19+
private final boolean isNumber;
20+
private final boolean isNull;
21+
22+
public ColumnInfo(String name, Type type) {
23+
this.name = name;
24+
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();
30+
31+
this.isTimestamp = ydbType == PrimitiveType.Timestamp;
32+
this.isNumber = ydbType == PrimitiveType.Int8 || ydbType == PrimitiveType.Uint8
33+
|| ydbType == PrimitiveType.Int16 || ydbType == PrimitiveType.Uint16
34+
|| ydbType == PrimitiveType.Int32 || ydbType == PrimitiveType.Uint32
35+
|| ydbType == PrimitiveType.Int64 || ydbType == PrimitiveType.Uint64;
36+
this.isNull = ydbType.getKind() == Type.Kind.NULL || ydbType.getKind() == Type.Kind.VOID;
37+
}
38+
39+
public String getName() {
40+
return this.name;
41+
}
42+
43+
public Type getYdbType() {
44+
return this.ydbType;
45+
}
46+
47+
public boolean isNull() {
48+
return isNull;
49+
}
50+
51+
public boolean isTimestamp() {
52+
return isTimestamp;
53+
}
54+
55+
public boolean isNumber() {
56+
return isNumber;
57+
}
58+
59+
public boolean isOptional() {
60+
return isOptional;
61+
}
62+
63+
public MappingGetters.SqlType getSqlType() {
64+
return this.sqlType;
65+
}
66+
67+
public MappingGetters.Getters getGetters() {
68+
return this.getters;
69+
}
70+
71+
public static ColumnInfo[] fromResultSetReader(ResultSetReader rsr) {
72+
ColumnInfo[] columns = new ColumnInfo[rsr.getColumnCount()];
73+
for (int idx = 0; idx < rsr.getColumnCount(); idx += 1) {
74+
columns[idx] = new ColumnInfo(rsr.getColumnName(idx), rsr.getColumnType(idx));
75+
}
76+
return columns;
77+
}
78+
79+
}

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

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)