use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.
the class IgniteH2Indexing method queryLocalSql.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException {
String type = qry.getType();
String sqlQry = qry.getSql();
String alias = qry.getAlias();
Object[] params = qry.getArgs();
GridQueryCancel cancel = new GridQueryCancel();
final GridCloseableIterator<IgniteBiTuple<K, V>> i = queryLocalSql(schemaName, cacheName, sqlQry, alias, F.asList(params), type, filter, cancel);
return new QueryCursorImpl<>(new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return new ClIter<Cache.Entry<K, V>>() {
@Override
public void close() throws Exception {
i.close();
}
@Override
public boolean hasNext() {
return i.hasNext();
}
@Override
public Cache.Entry<K, V> next() {
IgniteBiTuple<K, V> t = i.next();
K key = (K) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get1(), keepBinary, false);
V val = (V) CacheObjectUtils.unwrapBinaryIfNeeded(objectContext(), t.get2(), keepBinary, false);
return new CacheEntryImpl<>(key, val);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}, cancel);
}
use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.
the class IgniteH2Indexing method queryDistributedSql.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <K, V> QueryCursor<Cache.Entry<K, V>> queryDistributedSql(String schemaName, String cacheName, SqlQuery qry, boolean keepBinary) {
String type = qry.getType();
H2TableDescriptor tblDesc = tableDescriptor(schemaName, cacheName, type);
if (tblDesc == null)
throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND);
String sql;
try {
sql = generateQuery(qry.getSql(), qry.getAlias(), tblDesc);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
SqlFieldsQuery fqry = new SqlFieldsQuery(sql);
fqry.setArgs(qry.getArgs());
fqry.setPageSize(qry.getPageSize());
fqry.setDistributedJoins(qry.isDistributedJoins());
fqry.setPartitions(qry.getPartitions());
fqry.setLocal(qry.isLocal());
if (qry.getTimeout() > 0)
fqry.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS);
final QueryCursor<List<?>> res = querySqlFields(schemaName, fqry, null, keepBinary, true, null).get(0);
final Iterable<Cache.Entry<K, V>> converted = new Iterable<Cache.Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
final Iterator<List<?>> iter0 = res.iterator();
return new Iterator<Cache.Entry<K, V>>() {
@Override
public boolean hasNext() {
return iter0.hasNext();
}
@Override
public Cache.Entry<K, V> next() {
List<?> l = iter0.next();
return new CacheEntryImpl<>((K) l.get(0), (V) l.get(1));
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
// No metadata for SQL queries.
return new QueryCursorImpl<Cache.Entry<K, V>>(converted) {
@Override
public void close() {
res.close();
}
};
}
use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.
the class IgniteH2Indexing method doRunDistributedQuery.
/**
* Run distributed query on detected set of partitions.
* @param schemaName Schema name.
* @param qry Original query.
* @param twoStepQry Two-step query.
* @param meta Metadata to set to cursor.
* @param keepBinary Keep binary flag.
* @param cancel Cancel handler.
* @return Cursor representing distributed query result.
*/
private FieldsQueryCursor<List<?>> doRunDistributedQuery(String schemaName, SqlFieldsQuery qry, GridCacheTwoStepQuery twoStepQry, List<GridQueryFieldMetadata> meta, boolean keepBinary, GridQueryCancel cancel) {
if (log.isDebugEnabled())
log.debug("Parsed query: `" + qry.getSql() + "` into two step query: " + twoStepQry);
twoStepQry.pageSize(qry.getPageSize());
if (cancel == null)
cancel = new GridQueryCancel();
int[] partitions = qry.getPartitions();
if (partitions == null && twoStepQry.derivedPartitions() != null) {
try {
partitions = calculateQueryPartitions(twoStepQry.derivedPartitions(), qry.getArgs());
} catch (IgniteCheckedException e) {
throw new CacheException("Failed to calculate derived partitions: [qry=" + qry.getSql() + ", params=" + Arrays.deepToString(qry.getArgs()) + "]", e);
}
}
QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(runQueryTwoStep(schemaName, twoStepQry, keepBinary, qry.isEnforceJoinOrder(), qry.getTimeout(), cancel, qry.getArgs(), partitions, qry.isLazy()), cancel);
cursor.fieldsMeta(meta);
return cursor;
}
use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.
the class IgniteH2Indexing method queryLocalSqlFields.
/**
* {@inheritDoc}
*/
@Override
public FieldsQueryCursor<List<?>> queryLocalSqlFields(String schemaName, SqlFieldsQuery qry, final boolean keepBinary, IndexingQueryFilter filter, GridQueryCancel cancel) throws IgniteCheckedException {
String sql = qry.getSql();
Object[] args = qry.getArgs();
final GridQueryFieldsResult res = queryLocalSqlFields(schemaName, sql, F.asList(args), filter, qry.isEnforceJoinOrder(), qry.getTimeout(), cancel);
QueryCursorImpl<List<?>> cursor = new QueryCursorImpl<>(new Iterable<List<?>>() {
@Override
public Iterator<List<?>> iterator() {
try {
return new GridQueryCacheObjectsIterator(res.iterator(), objectContext(), keepBinary);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
}, cancel);
cursor.fieldsMeta(res.metaData());
return cursor;
}
use of org.apache.ignite.internal.processors.cache.QueryCursorImpl in project ignite by apache.
the class DmlStatementsProcessor method streamUpdateQuery.
/**
* Perform given statement against given data streamer. Only rows based INSERT is supported.
*
* @param schemaName Schema name.
* @param streamer Streamer to feed data to.
* @param stmt Statement.
* @param args Statement arguments.
* @return Number of rows in given INSERT statement.
* @throws IgniteCheckedException if failed.
*/
@SuppressWarnings({ "unchecked", "ConstantConditions" })
long streamUpdateQuery(String schemaName, IgniteDataStreamer streamer, PreparedStatement stmt, final Object[] args) throws IgniteCheckedException {
idx.checkStatementStreamable(stmt);
Prepared p = GridSqlQueryParser.prepared(stmt);
assert p != null;
final UpdatePlan plan = getPlanForStatement(schemaName, null, p, null, true, null);
assert plan.isLocalSubquery();
final GridCacheContext cctx = plan.cacheContext();
QueryCursorImpl<List<?>> cur;
final ArrayList<List<?>> data = new ArrayList<>(plan.rowCount());
QueryCursorImpl<List<?>> stepCur = new QueryCursorImpl<>(new Iterable<List<?>>() {
@Override
public Iterator<List<?>> iterator() {
try {
Iterator<List<?>> it;
if (!F.isEmpty(plan.selectQuery())) {
GridQueryFieldsResult res = idx.queryLocalSqlFields(idx.schema(cctx.name()), plan.selectQuery(), F.asList(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)), null, false, 0, null);
it = res.iterator();
} else
it = plan.createRows(U.firstNotNull(args, X.EMPTY_OBJECT_ARRAY)).iterator();
return new GridQueryCacheObjectsIterator(it, idx.objectContext(), cctx.keepBinary());
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
}, null);
data.addAll(stepCur.getAll());
cur = new QueryCursorImpl<>(new Iterable<List<?>>() {
@Override
public Iterator<List<?>> iterator() {
return data.iterator();
}
}, null);
if (plan.rowCount() == 1) {
IgniteBiTuple t = plan.processRow(cur.iterator().next());
streamer.addData(t.getKey(), t.getValue());
return 1;
}
Map<Object, Object> rows = new LinkedHashMap<>(plan.rowCount());
for (List<?> row : cur) {
final IgniteBiTuple t = plan.processRow(row);
rows.put(t.getKey(), t.getValue());
}
streamer.addData(rows);
return rows.size();
}
Aggregations