Skip to content

Commit a06a557

Browse files
author
S.Vladykin
committed
ignite-sql-tests - jdbc
1 parent cfcb9a4 commit a06a557

File tree

9 files changed

+150
-136
lines changed

9 files changed

+150
-136
lines changed

modules/clients/src/test/java/org/apache/ignite/jdbc/JdbcEmptyCacheSelfTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class JdbcEmptyCacheSelfTest extends GridCommonAbstractTest {
5454
cache.setCacheMode(PARTITIONED);
5555
cache.setBackups(1);
5656
cache.setWriteSynchronizationMode(FULL_SYNC);
57+
cache.setIndexedTypes(
58+
Byte.class, Byte.class
59+
);
5760

5861
cfg.setCacheConfiguration(cache);
5962

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ private QueryCursor<Entry<K,V>> doLocalQuery(SqlQuery p) {
489489
* @return Cursor.
490490
*/
491491
private QueryCursor<List<?>> doLocalFieldsQuery(SqlFieldsQuery q) {
492-
return new QueryCursorImpl<>(ctx.kernalContext().query().queryLocalFields(
493-
ctx.name(), q.getSql(), q.getArgs()));
492+
return ctx.kernalContext().query().queryLocalFields(
493+
ctx.name(), q.getSql(), q.getArgs());
494494
}
495495

496496
/**

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/QueryCursorImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.apache.ignite.*;
2121
import org.apache.ignite.internal.processors.cache.query.*;
22+
import org.apache.ignite.internal.processors.query.*;
2223

2324
import java.util.*;
2425

@@ -32,6 +33,9 @@ public class QueryCursorImpl<T> implements QueryCursorEx<T> {
3233
/** */
3334
private boolean iterTaken;
3435

36+
/** */
37+
private Collection<GridQueryFieldMetadata> fieldsMeta;
38+
3539
/**
3640
* @param iter Iterator.
3741
*/
@@ -95,4 +99,18 @@ public QueryCursorImpl(Iterator<T> iter) {
9599
}
96100
}
97101
}
102+
103+
/**
104+
* @param fieldsMeta SQL Fields query result metadata.
105+
*/
106+
public void fieldsMeta(Collection<GridQueryFieldMetadata> fieldsMeta) {
107+
this.fieldsMeta = fieldsMeta;
108+
}
109+
110+
/**
111+
* @return SQL Fields query result metadata.
112+
*/
113+
public Collection<GridQueryFieldMetadata> fieldsMeta() {
114+
return fieldsMeta;
115+
}
98116
}

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/jdbc/GridCacheQueryJdbcTask.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.ignite.cache.query.*;
2222
import org.apache.ignite.cluster.*;
2323
import org.apache.ignite.compute.*;
24+
import org.apache.ignite.internal.processors.cache.*;
2425
import org.apache.ignite.internal.processors.query.*;
2526
import org.apache.ignite.internal.util.typedef.*;
2627
import org.apache.ignite.internal.util.typedef.internal.*;
@@ -194,7 +195,9 @@ private static class JdbcDriverJob extends ComputeJobAdapter {
194195

195196
QueryCursor<List<?>> cursor = cache.queryFields(qry);
196197

197-
Collection<GridQueryFieldMetadata> meta = null; // TODO
198+
Collection<GridQueryFieldMetadata> meta = ((QueryCursorImpl<List<?>>)cursor).fieldsMeta();
199+
200+
assert meta != null;
198201

199202
tbls = new ArrayList<>(meta.size());
200203
cols = new ArrayList<>(meta.size());

modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.apache.ignite.internal.util.typedef.internal.*;
3636
import org.apache.ignite.internal.util.worker.*;
3737
import org.apache.ignite.lang.*;
38-
import org.apache.ignite.spi.*;
3938
import org.apache.ignite.spi.indexing.*;
4039
import org.jdk8.backport.*;
4140
import org.jetbrains.annotations.*;
@@ -555,13 +554,12 @@ private static interface ClIter<X> extends AutoCloseable, Iterator<X> {
555554
* @param args Arguments.
556555
* @return Iterator.
557556
*/
558-
public Iterator<List<?>> queryLocalFields(String space, String sql, Object[] args) {
557+
public QueryCursor<List<?>> queryLocalFields(String space, String sql, Object[] args) {
559558
if (!busyLock.enterBusy())
560559
throw new IllegalStateException("Failed to execute query (grid is stopping).");
561560

562561
try {
563-
IgniteSpiCloseableIterator<List<?>> iterator =
564-
idx.queryFields(space, sql, F.asList(args), idx.backupFilter()).iterator();
562+
GridQueryFieldsResult res = idx.queryFields(space, sql, F.asList(args), idx.backupFilter());
565563

566564
if (ctx.event().isRecordable(EVT_CACHE_QUERY_EXECUTED)) {
567565
ctx.event().record(new CacheQueryExecutedEvent<>(
@@ -579,10 +577,14 @@ public Iterator<List<?>> queryLocalFields(String space, String sql, Object[] arg
579577
null));
580578
}
581579

582-
return iterator;
580+
QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(res.iterator());
581+
582+
cursor.fieldsMeta(res.metaData());
583+
584+
return cursor;
583585
}
584586
catch (IgniteCheckedException e) {
585-
throw new IgniteException(e);
587+
throw new CacheException(e);
586588
}
587589
finally {
588590
busyLock.leaveBusy();

modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/IgniteH2Indexing.java

Lines changed: 52 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import java.text.*;
6666
import java.util.*;
6767
import java.util.concurrent.*;
68-
import java.util.concurrent.locks.*;
6968

7069
import static org.apache.ignite.IgniteSystemProperties.*;
7170
import static org.apache.ignite.internal.processors.query.GridQueryIndexType.*;
@@ -496,18 +495,7 @@ private void removeTable(TableDescriptor tbl) throws IgniteCheckedException {
496495

497496
if (rs != null) {
498497
try {
499-
ResultSetMetaData rsMeta = rs.getMetaData();
500-
501-
meta = new ArrayList<>(rsMeta.getColumnCount());
502-
503-
for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
504-
String schemaName = rsMeta.getSchemaName(i);
505-
String typeName = rsMeta.getTableName(i);
506-
String name = rsMeta.getColumnLabel(i);
507-
String type = rsMeta.getColumnClassName(i);
508-
509-
meta.add(new SqlFieldMetadata(schemaName, typeName, name, type));
510-
}
498+
meta = meta(rs.getMetaData());
511499
}
512500
catch (SQLException e) {
513501
throw new IgniteSpiException("Failed to get meta data.", e);
@@ -521,6 +509,26 @@ private void removeTable(TableDescriptor tbl) throws IgniteCheckedException {
521509
}
522510
}
523511

512+
/**
513+
* @param rsMeta Metadata.
514+
* @return List of fields metadata.
515+
* @throws SQLException If failed.
516+
*/
517+
private static List<GridQueryFieldMetadata> meta(ResultSetMetaData rsMeta) throws SQLException {
518+
ArrayList<GridQueryFieldMetadata> meta = new ArrayList<>(rsMeta.getColumnCount());
519+
520+
for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
521+
String schemaName = rsMeta.getSchemaName(i);
522+
String typeName = rsMeta.getTableName(i);
523+
String name = rsMeta.getColumnLabel(i);
524+
String type = rsMeta.getColumnClassName(i);
525+
526+
meta.add(new SqlFieldMetadata(schemaName, typeName, name, type));
527+
}
528+
529+
return meta;
530+
}
531+
524532
/**
525533
* @param stmt Prepared statement.
526534
* @return Command type.
@@ -739,12 +747,38 @@ public void bindParameters(PreparedStatement stmt, @Nullable Collection<Object>
739747
@Override public QueryCursor<List<?>> queryTwoStep(String space, String sqlQry, Object[] params) {
740748
Connection c = connectionForSpace(space);
741749

742-
GridCacheTwoStepQuery twoStepQry = GridSqlQuerySplitter.split(c, sqlQry, params);
750+
PreparedStatement stmt;
751+
752+
try {
753+
stmt = c.prepareStatement(sqlQry);
754+
}
755+
catch (SQLException e) {
756+
throw new CacheException("Failed to parse query: " + sqlQry, e);
757+
}
758+
759+
GridCacheTwoStepQuery twoStepQry;
760+
Collection<GridQueryFieldMetadata> meta;
761+
762+
try {
763+
twoStepQry = GridSqlQuerySplitter.split((JdbcPreparedStatement)stmt, params);
764+
765+
meta = meta(stmt.getMetaData());
766+
}
767+
catch (SQLException e) {
768+
throw new CacheException(e);
769+
}
770+
finally {
771+
U.close(stmt, log);
772+
}
743773

744774
if (log.isDebugEnabled())
745775
log.debug("Parsed query: `" + sqlQry + "` into two step query: " + twoStepQry);
746776

747-
return queryTwoStep(space, twoStepQry);
777+
QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>)queryTwoStep(space, twoStepQry);
778+
779+
cursor.fieldsMeta(meta);
780+
781+
return cursor;
748782
}
749783

750784
/**
@@ -1049,7 +1083,7 @@ public GridReduceQueryExecutor reduceQueryExecutor() {
10491083
if (Utils.serializer != null)
10501084
U.warn(log, "Custom H2 serialization is already configured, will override.");
10511085

1052-
Utils.serializer = h2Serializer(ctx != null && ctx.deploy().enabled());
1086+
Utils.serializer = h2Serializer();
10531087

10541088
String dbName = (ctx != null ? ctx.localNodeId() : UUID.randomUUID()).toString();
10551089

@@ -1097,83 +1131,10 @@ public GridReduceQueryExecutor reduceQueryExecutor() {
10971131
}
10981132

10991133
/**
1100-
* @param p2pEnabled If peer-deployment is enabled.
11011134
* @return Serializer.
11021135
*/
1103-
protected JavaObjectSerializer h2Serializer(boolean p2pEnabled) {
1104-
return p2pEnabled ?
1105-
new JavaObjectSerializer() {
1106-
/** */
1107-
private volatile Map<ClassLoader, Byte> ldr2id = Collections.emptyMap();
1108-
1109-
/** */
1110-
private volatile Map<Byte, ClassLoader> id2ldr = Collections.emptyMap();
1111-
1112-
/** */
1113-
private byte ldrIdGen = Byte.MIN_VALUE;
1114-
1115-
/** */
1116-
private final Lock lock = new ReentrantLock();
1117-
1118-
@Override public byte[] serialize(Object obj) throws Exception {
1119-
ClassLoader ldr = obj.getClass().getClassLoader();
1120-
1121-
Byte ldrId = ldr2id.get(ldr);
1122-
1123-
if (ldrId == null) {
1124-
lock.lock();
1125-
1126-
try {
1127-
ldrId = ldr2id.get(ldr);
1128-
1129-
if (ldrId == null) {
1130-
ldrId = ldrIdGen++;
1131-
1132-
if (id2ldr.containsKey(ldrId)) // Overflow.
1133-
throw new IgniteException("Failed to add new peer-to-peer class loader.");
1134-
1135-
Map<Byte, ClassLoader> id2ldr0 = new HashMap<>(id2ldr);
1136-
Map<ClassLoader, Byte> ldr2id0 = new IdentityHashMap<>(ldr2id);
1137-
1138-
id2ldr0.put(ldrId, ldr);
1139-
ldr2id0.put(ldr, ldrId);
1140-
1141-
ldr2id = ldr2id0;
1142-
id2ldr = id2ldr0;
1143-
}
1144-
}
1145-
finally {
1146-
lock.unlock();
1147-
}
1148-
}
1149-
1150-
byte[] bytes = marshaller.marshal(obj);
1151-
1152-
int len = bytes.length;
1153-
1154-
bytes = Arrays.copyOf(bytes, len + 1); // The last byte is for ldrId.
1155-
1156-
bytes[len] = ldrId;
1157-
1158-
return bytes;
1159-
}
1160-
1161-
@Override public Object deserialize(byte[] bytes) throws Exception {
1162-
int last = bytes.length - 1;
1163-
1164-
byte ldrId = bytes[last];
1165-
1166-
ClassLoader ldr = id2ldr.get(ldrId);
1167-
1168-
if (ldr == null)
1169-
throw new IllegalStateException("Class loader was not found: " + ldrId);
1170-
1171-
bytes = Arrays.copyOf(bytes, last); // Trim the last byte.
1172-
1173-
return marshaller.unmarshal(bytes, ldr);
1174-
}
1175-
} :
1176-
new JavaObjectSerializer() {
1136+
protected JavaObjectSerializer h2Serializer() {
1137+
return new JavaObjectSerializer() {
11771138
@Override public byte[] serialize(Object obj) throws Exception {
11781139
return marshaller.marshal(obj);
11791140
}

modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/sql/GridSqlQueryParser.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.ignite.internal.processors.query.h2.sql;
1919

2020
import org.apache.ignite.*;
21+
import org.h2.command.*;
2122
import org.h2.command.dml.*;
2223
import org.h2.engine.*;
2324
import org.h2.expression.*;
@@ -28,7 +29,6 @@
2829
import org.jetbrains.annotations.*;
2930

3031
import java.lang.reflect.*;
31-
import java.sql.*;
3232
import java.util.*;
3333
import java.util.Set;
3434

@@ -150,18 +150,35 @@ public class GridSqlQueryParser {
150150
/** */
151151
private static final Getter<JavaFunction, FunctionAlias> FUNC_ALIAS = getter(JavaFunction.class, "functionAlias");
152152

153+
/** */
154+
private static final Getter<JdbcPreparedStatement,Command> COMMAND = getter(JdbcPreparedStatement.class, "command");
155+
156+
/** */
157+
private static volatile Getter<Command,Prepared> prepared;
158+
153159
/** */
154160
private final IdentityHashMap<Object, Object> h2ObjToGridObj = new IdentityHashMap<>();
155161

156162
/**
157-
* @param conn Connection.
158-
* @param select Select query.
159-
* @return Parsed select query.
163+
* @param stmt Prepared statement.
164+
* @return Parsed select.
160165
*/
161-
public static GridSqlSelect parse(Connection conn, String select) {
162-
Session ses = (Session)((JdbcConnection)conn).getSession();
166+
public static GridSqlSelect parse(JdbcPreparedStatement stmt) {
167+
Command cmd = COMMAND.get(stmt);
168+
169+
Getter<Command,Prepared> p = prepared;
170+
171+
if (p == null) {
172+
Class<? extends Command> cls = cmd.getClass();
173+
174+
assert cls.getSimpleName().equals("CommandContainer");
175+
176+
prepared = p = getter(cls, "prepared");
177+
}
178+
179+
Prepared select = p.get(cmd);
163180

164-
return new GridSqlQueryParser().parse((Select)ses.prepare(select));
181+
return new GridSqlQueryParser().parse((Select)select);
165182
}
166183

167184
/**
@@ -510,7 +527,7 @@ private static void assert0(boolean cond, Object o) {
510527
* @param cls Class.
511528
* @param fldName Fld name.
512529
*/
513-
private static <T, R> Getter<T, R> getter(Class<T> cls, String fldName) {
530+
private static <T, R> Getter<T, R> getter(Class<? extends T> cls, String fldName) {
514531
Field field;
515532

516533
try {

0 commit comments

Comments
 (0)