use of org.apache.ignite.internal.processors.cache.query.QueryCursorEx in project ignite by apache.
the class GridSubqueryJoinOptimizerSelfTest method check.
/**
* @param sql Sql.
* @param expSelectClauses Expected select clauses.
*/
private void check(String sql, int expSelectClauses) {
optimizationEnabled(false);
FieldsQueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery(sql));
List<GridQueryFieldMetadata> expMetaList = ((QueryCursorEx<List<?>>) qry).fieldsMeta();
List<List<?>> exp = qry.getAll();
exp.sort(ROW_COMPARATOR);
optimizationEnabled(true);
FieldsQueryCursor<List<?>> optQry = cache.query(new SqlFieldsQuery(sql).setEnforceJoinOrder(true));
List<GridQueryFieldMetadata> actMetaList = ((QueryCursorEx<List<?>>) optQry).fieldsMeta();
List<List<?>> act = optQry.getAll();
act.sort(ROW_COMPARATOR);
Assert.assertEquals("Result set mismatch", exp, act);
List<String> expFieldTypes = new ArrayList<>();
List<String> actualFieldTypes = new ArrayList<>();
for (int i = 0; i < expMetaList.size(); i++) {
GridQueryFieldMetadata expMeta = expMetaList.get(i);
GridQueryFieldMetadata actMeta = actMetaList.get(i);
expFieldTypes.add(expMeta.fieldName() + ":" + expMeta.fieldTypeName());
actualFieldTypes.add(actMeta.fieldName() + ":" + actMeta.fieldTypeName());
}
Assert.assertEquals("Result set field names or field types mismatch", expFieldTypes, actualFieldTypes);
String plan = cache.query(new SqlFieldsQuery("explain " + sql)).getAll().get(0).get(0).toString();
System.out.println(plan);
int actCnt = countEntries(plan, "SELECT");
Assert.assertEquals(String.format("SELECT-clause count mismatch: exp=%d, act=%d, plan=[%s]", expSelectClauses, actCnt, plan), expSelectClauses, actCnt);
}
use of org.apache.ignite.internal.processors.cache.query.QueryCursorEx in project ignite by apache.
the class IgniteCacheProxyImpl method queryContinuous.
/**
* Executes continuous query.
*
* @param qry Query.
* @param loc Local flag.
* @param keepBinary Keep binary flag.
* @return Initial iteration cursor.
*/
@SuppressWarnings("unchecked")
private QueryCursor<Cache.Entry<K, V>> queryContinuous(AbstractContinuousQuery qry, boolean loc, boolean keepBinary) {
GridCacheContext<K, V> ctx = getContextSafe();
assert qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer;
if (qry.getInitialQuery() instanceof ContinuousQuery || qry.getInitialQuery() instanceof ContinuousQueryWithTransformer) {
throw new IgniteException("Initial predicate for continuous query can't be an instance of another " + "continuous query. Use SCAN or SQL query for initial iteration.");
}
CacheEntryUpdatedListener locLsnr = null;
EventListener locTransLsnr = null;
CacheEntryEventSerializableFilter rmtFilter = null;
Factory<? extends IgniteClosure> rmtTransFactory = null;
if (qry instanceof ContinuousQuery) {
ContinuousQuery<K, V> qry0 = (ContinuousQuery<K, V>) qry;
if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null && qry0.getRemoteFilter() == null) {
throw new IgniteException("LocalListener, RemoterFilter " + "or RemoteFilterFactory must be specified for the query: " + qry);
}
if (qry0.getRemoteFilter() != null && qry0.getRemoteFilterFactory() != null)
throw new IgniteException("Should be used either RemoterFilter or RemoteFilterFactory.");
locLsnr = qry0.getLocalListener();
rmtFilter = qry0.getRemoteFilter();
} else {
ContinuousQueryWithTransformer<K, V, ?> qry0 = (ContinuousQueryWithTransformer<K, V, ?>) qry;
if (qry0.getLocalListener() == null && qry0.getRemoteFilterFactory() == null) {
throw new IgniteException("LocalListener " + "or RemoteFilterFactory must be specified for the query: " + qry);
}
if (qry0.getRemoteTransformerFactory() == null)
throw new IgniteException("Mandatory RemoteTransformerFactory is not set for the query: " + qry);
Collection<ClusterNode> nodes = context().grid().cluster().nodes();
for (ClusterNode node : nodes) {
if (node.version().compareTo(CONT_QRY_WITH_TRANSFORMER_SINCE) < 0) {
throw new IgniteException("Can't start ContinuousQueryWithTransformer, " + "because some nodes in cluster doesn't support this feature: " + node);
}
}
locTransLsnr = qry0.getLocalListener();
rmtTransFactory = qry0.getRemoteTransformerFactory();
}
try {
final UUID routineId = ctx.continuousQueries().executeQuery(locLsnr, locTransLsnr, rmtFilter, qry.getRemoteFilterFactory(), rmtTransFactory, qry.getPageSize(), qry.getTimeInterval(), qry.isAutoUnsubscribe(), loc, keepBinary, qry.isIncludeExpired());
try {
final QueryCursor<Cache.Entry<K, V>> cur = qry.getInitialQuery() != null ? query(qry.getInitialQuery()) : null;
return new QueryCursorEx<Entry<K, V>>() {
@Override
public Iterator<Cache.Entry<K, V>> iterator() {
return cur != null ? cur.iterator() : new GridEmptyIterator<Cache.Entry<K, V>>();
}
@Override
public List<Cache.Entry<K, V>> getAll() {
return cur != null ? cur.getAll() : Collections.<Cache.Entry<K, V>>emptyList();
}
@Override
public void close() {
if (cur != null)
cur.close();
try {
ctx.kernalContext().continuous().stopRoutine(routineId).get();
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
@Override
public void getAll(Consumer<Entry<K, V>> c) {
// No-op.
}
@Override
public List<GridQueryFieldMetadata> fieldsMeta() {
// noinspection rawtypes
return cur instanceof QueryCursorEx ? ((QueryCursorEx) cur).fieldsMeta() : null;
}
};
} catch (Throwable t) {
// Initial query failed: stop the routine.
ctx.kernalContext().continuous().stopRoutine(routineId).get();
throw t;
}
} catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
use of org.apache.ignite.internal.processors.cache.query.QueryCursorEx in project ignite by apache.
the class ParameterTypeInferenceTest method check.
/**
* Execute query.
*
* @param qry Query.
* @param loc Local flag.
*/
private void check(String qry, boolean loc) {
List<Object[]> argss = new ArrayList<>();
argss.add(new Object[] { null });
argss.add(new Object[] { "STRING" });
argss.add(new Object[] { 1 });
argss.add(new Object[] { 1L });
argss.add(new Object[] { new BigDecimal("12.12") });
argss.add(new Object[] { UUID.randomUUID() });
clearParserCache();
for (int i = 0; i < argss.size(); i++) {
for (Object[] args : argss) {
for (int j = 0; j < 2; j++) {
SqlFieldsQuery qry0 = new SqlFieldsQuery(qry).setLocal(loc).setArgs(args);
try (QueryCursorEx<List<?>> cur = (QueryCursorEx<List<?>>) grid(0).cache(CACHE_NAME).query(qry0)) {
GridQueryFieldMetadata meta = cur.fieldsMeta().get(0);
cur.getAll();
String errMsg = "Failure on i=" + i + ", j=" + j + ": " + meta.fieldTypeName();
assertEquals(errMsg, Object.class.getName(), meta.fieldTypeName());
}
}
}
argss.add(argss.remove(0));
}
}
use of org.apache.ignite.internal.processors.cache.query.QueryCursorEx in project ignite by apache.
the class IndexQueryRangeTest method check.
/**
* @param left First cache key, inclusive.
* @param right Last cache key, exclusive.
*/
private void check(Query<Cache.Entry<Long, Person>> qry, int left, int right) throws Exception {
QueryCursor<Cache.Entry<Long, Person>> cursor = cache.query(qry);
int expSize = (right - left) * duplicates;
Set<Long> expKeys = new HashSet<>(expSize);
List<Integer> expOrderedValues = new LinkedList<>();
boolean desc = idxName.equals(DESC_IDX);
int from = desc ? right - 1 : left;
int to = desc ? left - 1 : right;
IntUnaryOperator op = (i) -> desc ? i - 1 : i + 1;
for (int i = from; i != to; i = op.applyAsInt(i)) {
for (int j = 0; j < duplicates; j++) {
expOrderedValues.add(i);
expKeys.add((long) CNT * j + i);
}
}
AtomicInteger actSize = new AtomicInteger();
((QueryCursorEx<Cache.Entry<Long, Person>>) cursor).getAll(entry -> {
assertEquals(expOrderedValues.remove(0).intValue(), entry.getValue().id);
assertTrue(expKeys.remove(entry.getKey()));
int persId = entry.getKey().intValue() % CNT;
assertEquals(new Person(persId), entry.getValue());
actSize.incrementAndGet();
});
assertEquals(expSize, actSize.get());
assertTrue(expKeys.isEmpty());
}
Aggregations