Search in sources :

Example 1 with QueryCursorEx

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);
}
Also used : QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) ArrayList(java.util.ArrayList) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 2 with QueryCursorEx

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);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) CacheEntry(org.apache.ignite.cache.CacheEntry) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) IgniteException(org.apache.ignite.IgniteException) QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) EventListener(org.apache.ignite.cache.query.ContinuousQueryWithTransformer.EventListener) UUID(java.util.UUID) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 3 with QueryCursorEx

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));
    }
}
Also used : QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) ArrayList(java.util.ArrayList) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List) BigDecimal(java.math.BigDecimal) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 4 with QueryCursorEx

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());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) IndexQueryCriteriaBuilder.gt(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gt) IndexQueryCriteriaBuilder.eq(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.eq) IntUnaryOperator(java.util.function.IntUnaryOperator) RunWith(org.junit.runner.RunWith) Random(java.util.Random) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) Cache(javax.cache.Cache) PARTITIONED(org.apache.ignite.cache.CacheMode.PARTITIONED) LinkedList(java.util.LinkedList) Parameterized(org.junit.runners.Parameterized) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) IndexQueryCriteriaBuilder.lte(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lte) QuerySqlField(org.apache.ignite.cache.query.annotations.QuerySqlField) IndexQueryCriteriaBuilder.gte(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.gte) Collection(java.util.Collection) IndexQueryCriteriaBuilder.lt(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.lt) Set(java.util.Set) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) IgniteCache(org.apache.ignite.IgniteCache) TRANSACTIONAL(org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL) REPLICATED(org.apache.ignite.cache.CacheMode.REPLICATED) Objects(java.util.Objects) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Stream(java.util.stream.Stream) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IndexQueryCriteriaBuilder.between(org.apache.ignite.cache.query.IndexQueryCriteriaBuilder.between) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) ATOMIC(org.apache.ignite.cache.CacheAtomicityMode.ATOMIC) CacheMode(org.apache.ignite.cache.CacheMode) IntUnaryOperator(java.util.function.IntUnaryOperator) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryCursorEx(org.apache.ignite.internal.processors.cache.query.QueryCursorEx) HashSet(java.util.HashSet) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

QueryCursorEx (org.apache.ignite.internal.processors.cache.query.QueryCursorEx)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 GridQueryFieldMetadata (org.apache.ignite.internal.processors.query.GridQueryFieldMetadata)3 Cache (javax.cache.Cache)2 IgniteCache (org.apache.ignite.IgniteCache)2 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Objects (java.util.Objects)1 Random (java.util.Random)1 Set (java.util.Set)1 UUID (java.util.UUID)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IntUnaryOperator (java.util.function.IntUnaryOperator)1 Stream (java.util.stream.Stream)1 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1