use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.
the class GridReduceQueryExecutor method createMergeTable.
/**
* @param conn Connection.
* @param qry Query.
* @param explain Explain.
* @return Table.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("unchecked")
private ReduceTable createMergeTable(H2PooledConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
try {
Session ses = H2Utils.session(conn);
CreateTableData data = new CreateTableData();
data.tableName = "T___";
data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
data.create = true;
if (!explain) {
LinkedHashMap<String, ?> colsMap = qry.columns();
assert colsMap != null;
ArrayList<Column> cols = new ArrayList<>(colsMap.size());
for (Map.Entry<String, ?> e : colsMap.entrySet()) {
String alias = e.getKey();
GridSqlType type = (GridSqlType) e.getValue();
assert !F.isEmpty(alias);
Column col0;
if (type == GridSqlType.UNKNOWN) {
// Special case for parameter being set at the top of the query (e.g. SELECT ? FROM ...).
// Re-map it to STRING in the same way it is done in H2, because any argument can be cast
// to string.
col0 = new Column(alias, Value.STRING);
} else {
col0 = new Column(alias, type.type(), type.precision(), type.scale(), type.displaySize());
}
cols.add(col0);
}
data.columns = cols;
} else
data.columns = planColumns();
boolean sortedIndex = !F.isEmpty(qry.sortColumns());
ReduceTable tbl = new ReduceTable(data);
ArrayList<Index> idxs = new ArrayList<>(2);
if (explain) {
idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, sortedIndex ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
} else if (sortedIndex) {
List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
SortedReduceIndexAdapter sortedMergeIdx = new SortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
idxs.add(ReduceTable.createScanIndex(sortedMergeIdx));
idxs.add(sortedMergeIdx);
} else
idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_UNSORTED));
tbl.indexes(idxs);
return tbl;
} catch (Exception e) {
throw new IgniteCheckedException(e);
}
}
use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project gridgain by gridgain.
the class GridQueryParsingTest method parse.
/**
* @param sql Sql.
*/
@SuppressWarnings("unchecked")
private <T extends Prepared> T parse(String sql) throws Exception {
try (H2PooledConnection conn = connection()) {
Session ses = H2Utils.session(conn);
ses.setQueryContext(QueryContext.parseContext(null, true));
return (T) ses.prepare(sql);
}
}
use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.
the class GridQueryParsingTest method parse.
/**
* @param sql Sql.
*/
@SuppressWarnings("unchecked")
private <T extends Prepared> T parse(String sql) throws Exception {
try (H2PooledConnection conn = connection()) {
Session ses = H2Utils.session(conn);
H2Utils.setupConnection(conn, QueryContext.parseContext(null, true), false, false, false);
return (T) ses.prepare(sql);
}
}
use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project ignite by apache.
the class IgniteH2Indexing method executeSqlQuery.
/**
* Executes sql query statement.
*
* @param conn Connection,.
* @param stmt Statement.
* @param timeoutMillis Query timeout.
* @param cancel Query cancel.
* @return Result.
* @throws IgniteCheckedException If failed.
*/
private ResultSet executeSqlQuery(final Connection conn, final PreparedStatement stmt, int timeoutMillis, @Nullable GridQueryCancel cancel) throws IgniteCheckedException {
final MapQueryLazyWorker lazyWorker = MapQueryLazyWorker.currentWorker();
if (cancel != null) {
cancel.set(new Runnable() {
@Override
public void run() {
if (lazyWorker != null) {
lazyWorker.submit(new Runnable() {
@Override
public void run() {
cancelStatement(stmt);
}
});
} else
cancelStatement(stmt);
}
});
}
Session ses = H2Utils.session(conn);
if (timeoutMillis > 0)
ses.setQueryTimeout(timeoutMillis);
if (lazyWorker != null)
ses.setLazyQueryExecution(true);
try {
return stmt.executeQuery();
} catch (SQLException e) {
// Throw special exception.
if (e.getErrorCode() == ErrorCode.STATEMENT_WAS_CANCELED)
throw new QueryCancelledException();
throw new IgniteCheckedException("Failed to execute SQL query. " + e.getMessage(), e);
} finally {
if (timeoutMillis > 0)
ses.setQueryTimeout(0);
if (lazyWorker != null)
ses.setLazyQueryExecution(false);
}
}
use of org.apache.ignite.internal.processors.query.h2.H2Utils.session in project gridgain by gridgain.
the class GridReduceQueryExecutor method createMergeTable.
/**
* @param conn Connection.
* @param qry Query.
* @param explain Explain.
* @return Table.
* @throws IgniteCheckedException If failed.
*/
private ReduceTable createMergeTable(H2PooledConnection conn, GridCacheSqlQuery qry, boolean explain) throws IgniteCheckedException {
try {
Session ses = H2Utils.session(conn);
CreateTableData data = new CreateTableData();
data.tableName = "T___";
data.schema = ses.getDatabase().getSchema(ses.getCurrentSchemaName());
data.create = true;
if (!explain) {
LinkedHashMap<String, ?> colsMap = qry.columns();
assert colsMap != null;
ArrayList<Column> cols = new ArrayList<>(colsMap.size());
for (Map.Entry<String, ?> e : colsMap.entrySet()) {
String alias = e.getKey();
GridSqlType type = (GridSqlType) e.getValue();
assert !F.isEmpty(alias);
Column col0;
if (type == GridSqlType.UNKNOWN) {
// Special case for parameter being set at the top of the query (e.g. SELECT ? FROM ...).
// Re-map it to STRING in the same way it is done in H2, because any argument can be cast
// to string.
col0 = new Column(alias, Value.STRING);
} else
col0 = new Column(alias, type.type());
cols.add(col0);
}
data.columns = cols;
} else
data.columns = planColumns();
boolean sortedIdx = !F.isEmpty(qry.sortColumns());
ReduceTable tbl = new ReduceTable(data);
ArrayList<Index> idxs = new ArrayList<>(2);
if (explain) {
idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, sortedIdx ? MERGE_INDEX_SORTED : MERGE_INDEX_UNSORTED));
} else if (sortedIdx) {
List<GridSqlSortColumn> sortCols = (List<GridSqlSortColumn>) qry.sortColumns();
SortedReduceIndexAdapter sortedMergeIdx = new SortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_SORTED, GridSqlSortColumn.toIndexColumns(tbl, sortCols));
idxs.add(ReduceTable.createScanIndex(sortedMergeIdx, tbl));
idxs.add(sortedMergeIdx);
} else
idxs.add(new UnsortedReduceIndexAdapter(ctx, tbl, MERGE_INDEX_UNSORTED));
tbl.indexes(idxs);
return tbl;
} catch (Exception e) {
throw new IgniteCheckedException(e);
}
}
Aggregations