use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.
the class GridIndexingSpiAbstractSelfTest method testSpi.
/**
* @throws Exception If failed.
*/
public void testSpi() throws Exception {
IgniteH2Indexing spi = getIndexing();
IgniteCache<Integer, BinaryObject> cacheA = ignite0.createCache(cacheACfg());
IgniteCache<Integer, BinaryObject> cacheB = ignite0.createCache(cacheBCfg());
assertFalse(spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "select * from A.A", null, Collections.emptySet(), typeAA.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select * from A.B", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select * from B.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select * from B.A, A.B, A.A", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
try {
spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select aa.*, ab.*, ba.* from A.A aa, A.B ab, B.A ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext();
fail("Enumerations of aliases in select block must be prohibited");
} catch (IgniteCheckedException ignored) {
// all fine
}
assertFalse(spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select ab.* from A.B ab", null, Collections.emptySet(), typeAB.name(), null, null).hasNext());
assertFalse(spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "select ba.* from B.A as ba", null, Collections.emptySet(), typeBA.name(), null, null).hasNext());
cacheA.put(1, aa("A", 1, "Vasya", 10).build());
cacheA.put(1, ab(1, "Vasya", 20, "Some text about Vasya goes here.").build());
cacheB.put(1, ba(2, "Petya", 25, true).build());
cacheB.put(1, ba(2, "Kolya", 25, true).build());
cacheA.put(2, aa("A", 2, "Valera", 19).build());
cacheA.put(3, aa("A", 3, "Borya", 18).build());
cacheA.put(4, ab(4, "Vitalya", 20, "Very Good guy").build());
// Query data.
Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "from a order by age", null, Collections.emptySet(), typeAA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAA.cacheName()), typeAA.cacheName(), "select aa.* from a aa order by aa.age", null, Collections.emptySet(), typeAA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(aa("A", 3, "Borya", 18).build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(aa("A", 2, "Valera", 19).build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "from b order by name", null, Collections.emptySet(), typeAB.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "select bb.* from b as bb order by bb.name", null, Collections.emptySet(), typeAB.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ab(1, "Vasya", 20, "Some text about Vasya goes here.").build(), value(res.next()));
assertTrue(res.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(res.next()));
assertFalse(res.hasNext());
res = spi.queryLocalSql(spi.schema(typeBA.cacheName()), typeBA.cacheName(), "from a", null, Collections.emptySet(), typeBA.name(), null, null);
assertTrue(res.hasNext());
assertEquals(ba(2, "Kolya", 25, true).build(), value(res.next()));
assertFalse(res.hasNext());
// Text queries
Iterator<IgniteBiTuple<Integer, BinaryObjectImpl>> txtRes = spi.queryLocalText(spi.schema(typeAB.cacheName()), typeAB.cacheName(), "good", typeAB.name(), null);
assertTrue(txtRes.hasNext());
assertEquals(ab(4, "Vitalya", 20, "Very Good guy").build(), value(txtRes.next()));
assertFalse(txtRes.hasNext());
// Fields query
GridQueryFieldsResult fieldsRes = spi.queryLocalSqlFields(spi.schema("A"), "select a.a.name n1, a.a.age a1, b.a.name n2, " + "b.a.age a2 from a.a, b.a where a.a.id = b.a.id ", Collections.emptySet(), null, false, 0, null);
String[] aliases = { "N1", "A1", "N2", "A2" };
Object[] vals = { "Valera", 19, "Kolya", 25 };
IgniteSpiCloseableIterator<List<?>> it = fieldsRes.iterator();
assertTrue(it.hasNext());
List<?> fields = it.next();
assertEquals(4, fields.size());
int i = 0;
for (Object f : fields) {
assertEquals(aliases[i], fieldsRes.metaData().get(i).fieldName());
assertEquals(vals[i++], f);
}
assertFalse(it.hasNext());
// Remove
cacheA.remove(2);
cacheB.remove(1);
}
use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.
the class IgniteH2Indexing method executeUpdateOnDataNodeTransactional.
/**
* {@inheritDoc}
*/
@Override
public UpdateSourceIterator<?> executeUpdateOnDataNodeTransactional(GridCacheContext<?, ?> cctx, int[] ids, int[] parts, String schema, String qry, Object[] params, int flags, int pageSize, int timeout, AffinityTopologyVersion topVer, MvccSnapshot mvccSnapshot, GridQueryCancel cancel) throws IgniteCheckedException {
SqlFieldsQuery fldsQry = QueryUtils.withQueryTimeout(new SqlFieldsQuery(qry), timeout, TimeUnit.MILLISECONDS);
if (params != null)
fldsQry.setArgs(params);
fldsQry.setEnforceJoinOrder(U.isFlagSet(flags, GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER));
fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS);
fldsQry.setPageSize(pageSize);
fldsQry.setLocal(true);
fldsQry.setLazy(U.isFlagSet(flags, GridH2QueryRequest.FLAG_LAZY));
boolean loc = true;
final boolean replicated = U.isFlagSet(flags, GridH2QueryRequest.FLAG_REPLICATED);
GridCacheContext<?, ?> cctx0;
if (!replicated && !F.isEmpty(ids) && (cctx0 = CU.firstPartitioned(cctx.shared(), ids)) != null && cctx0.config().getQueryParallelism() > 1) {
fldsQry.setDistributedJoins(true);
loc = false;
}
QueryParserResult parseRes = parser.parse(schema, fldsQry, false);
assert parseRes.remainingQuery() == null;
QueryParserResultDml dml = parseRes.dml();
assert dml != null;
IndexingQueryFilter filter = backupFilter(topVer, parts);
UpdatePlan plan = dml.plan();
GridCacheContext planCctx = plan.cacheContext();
// Force keepBinary for operation context to avoid binary deserialization inside entry processor
DmlUtils.setKeepBinaryContext(planCctx);
SqlFieldsQuery selectFieldsQry = QueryUtils.withQueryTimeout(new SqlFieldsQuery(plan.selectQuery(), fldsQry.isCollocated()), fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setArgs(fldsQry.getArgs()).setDistributedJoins(fldsQry.isDistributedJoins()).setEnforceJoinOrder(fldsQry.isEnforceJoinOrder()).setLocal(fldsQry.isLocal()).setPageSize(fldsQry.getPageSize()).setTimeout(fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setLazy(fldsQry.isLazy());
QueryCursorImpl<List<?>> cur;
// sub-query and not some dummy stuff like "select 1, 2, 3;"
if (!loc && !plan.isLocalSubquery()) {
cur = executeSelectForDml(RunningQueryManager.UNDEFINED_QUERY_ID, schema, selectFieldsQry, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, timeout);
} else {
selectFieldsQry.setLocal(true);
QueryParserResult selectParseRes = parser.parse(schema, selectFieldsQry, false);
GridQueryFieldsResult res = executeSelectLocal(RunningQueryManager.UNDEFINED_QUERY_ID, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), filter, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, true, timeout);
cur = new QueryCursorImpl<>(new Iterable<List<?>>() {
@Override
public Iterator<List<?>> iterator() {
try {
return res.iterator();
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
}, cancel, true, selectParseRes.queryParameters().lazy());
}
return plan.iteratorForTransaction(connMgr, cur);
}
use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.
the class IgniteH2Indexing method updateQueryRows.
/**
* Calculates rows for update query.
*
* @param qryId Query id.
* @param schemaName Schema name.
* @param plan Update plan.
* @param args Statement arguments.
* @return Rows for update.
* @throws IgniteCheckedException If failed.
*/
private Iterator<List<?>> updateQueryRows(long qryId, String schemaName, UpdatePlan plan, Object[] args) throws IgniteCheckedException {
Object[] params = args != null ? args : X.EMPTY_OBJECT_ARRAY;
if (!F.isEmpty(plan.selectQuery())) {
SqlFieldsQuery selectQry = new SqlFieldsQuery(plan.selectQuery()).setArgs(params).setLocal(true);
QueryParserResult selectParseRes = parser.parse(schemaName, selectQry, false);
GridQueryFieldsResult res = executeSelectLocal(qryId, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), null, null, null, false, 0);
return res.iterator();
} else
return plan.createRows(params).iterator();
}
use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult in project ignite by apache.
the class IgniteH2Indexing method executeUpdateNonTransactional.
/**
* Execute update in non-transactional mode.
*
* @param qryId Query id.
* @param qryDesc Query descriptor.
* @param qryParams Query parameters.
* @param dml Plan.
* @param loc Local flag.
* @param filters Filters.
* @param cancel Cancel hook.
* @return Update result.
* @throws IgniteCheckedException If failed.
*/
private UpdateResult executeUpdateNonTransactional(long qryId, QueryDescriptor qryDesc, QueryParameters qryParams, QueryParserResultDml dml, boolean loc, IndexingQueryFilter filters, GridQueryCancel cancel) throws IgniteCheckedException {
UpdatePlan plan = dml.plan();
UpdateResult fastUpdateRes = plan.processFast(qryParams.arguments());
if (fastUpdateRes != null)
return fastUpdateRes;
DmlDistributedPlanInfo distributedPlan = loc ? null : plan.distributedPlan();
if (distributedPlan != null) {
if (cancel == null)
cancel = new GridQueryCancel();
UpdateResult result = rdcQryExec.update(qryDesc.schemaName(), distributedPlan.getCacheIds(), qryDesc.sql(), qryParams.arguments(), qryDesc.enforceJoinOrder(), qryParams.pageSize(), qryParams.timeout(), qryParams.partitions(), distributedPlan.isReplicatedOnly(), cancel);
// Null is returned in case not all nodes support distributed DML.
if (result != null)
return result;
}
final GridQueryCancel selectCancel = (cancel != null) ? new GridQueryCancel() : null;
if (cancel != null)
cancel.add(selectCancel::cancel);
SqlFieldsQuery selectFieldsQry = new SqlFieldsQuery(plan.selectQuery(), qryDesc.collocated()).setArgs(qryParams.arguments()).setDistributedJoins(qryDesc.distributedJoins()).setEnforceJoinOrder(qryDesc.enforceJoinOrder()).setLocal(qryDesc.local()).setPageSize(qryParams.pageSize()).setTimeout(qryParams.timeout(), TimeUnit.MILLISECONDS).setLazy(qryParams.lazy() && plan.canSelectBeLazy());
Iterable<List<?>> cur;
// sub-query and not some dummy stuff like "select 1, 2, 3;"
if (!loc && !plan.isLocalSubquery()) {
assert !F.isEmpty(plan.selectQuery());
cur = executeSelectForDml(qryId, qryDesc.schemaName(), selectFieldsQry, null, selectCancel, qryParams.timeout());
} else if (plan.hasRows())
cur = plan.createRows(qryParams.arguments());
else {
selectFieldsQry.setLocal(true);
QueryParserResult selectParseRes = parser.parse(qryDesc.schemaName(), selectFieldsQry, false);
final GridQueryFieldsResult res = executeSelectLocal(qryId, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), filters, null, selectCancel, false, qryParams.timeout());
cur = new QueryCursorImpl<>(new Iterable<List<?>>() {
@Override
public Iterator<List<?>> iterator() {
try {
return new GridQueryCacheObjectsIterator(res.iterator(), objectContext(), true);
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
}
}, cancel, true, qryParams.lazy());
}
int pageSize = qryParams.updateBatchSize();
// TODO: IGNITE-11176 - Need to support cancellation
try {
return DmlUtils.processSelectResult(plan, cur, pageSize);
} finally {
if (cur instanceof AutoCloseable)
U.closeQuiet((AutoCloseable) cur);
}
}
use of org.apache.ignite.internal.processors.query.GridQueryFieldsResult 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;
}
Aggregations