Search in sources :

Example 6 with GridQueryProcessor

use of org.apache.ignite.internal.processors.query.GridQueryProcessor in project ignite by apache.

the class CacheSqlQueryValueCopySelfTest method testRunningSqlQuery.

/**
     * Test collecting info about running.
     *
     * @throws Exception If failed.
     */
public void testRunningSqlQuery() throws Exception {
    IgniteInternalFuture<?> fut = runQueryAsync(new SqlQuery<Integer, Value>(Value.class, "id > sleep(100)"));
    Thread.sleep(500);
    GridQueryProcessor qryProc = grid(0).context().query();
    Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
    SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class, "id > sleep(100)");
    qry.setLocal(true);
    fut = runQueryAsync(qry);
    Thread.sleep(500);
    queries = qryProc.runningQueries(0);
    assertEquals(1, queries.size());
    fut.get();
    queries = qryProc.runningQueries(0);
    assertEquals(0, queries.size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) GridRunningQueryInfo(org.apache.ignite.internal.processors.query.GridRunningQueryInfo)

Example 7 with GridQueryProcessor

use of org.apache.ignite.internal.processors.query.GridQueryProcessor in project ignite by apache.

the class GridCacheCrossCacheQuerySelfTest method testTwoStepGroupAndAggregates.

/**
     * @throws Exception If failed.
     */
public void testTwoStepGroupAndAggregates() throws Exception {
    IgniteInternalCache<Integer, FactPurchase> cache = ((IgniteKernal) ignite).getCache("partitioned");
    GridQueryProcessor qryProc = ((IgniteKernal) ignite).context().query();
    Set<Integer> set1 = new HashSet<>();
    X.println("___ simple");
    SqlFieldsQuery qry = new SqlFieldsQuery("select f.productId, p.name, f.price " + "from FactPurchase f, \"replicated-prod\".DimProduct p where p.id = f.productId ");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        set1.add((Integer) o.get(0));
    }
    assertFalse(set1.isEmpty());
    Set<Integer> set0 = new HashSet<>();
    X.println("___ GROUP BY");
    qry = new SqlFieldsQuery("select productId from FactPurchase group by productId");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        assertTrue(set0.add((Integer) o.get(0)));
    }
    assertEquals(set0, set1);
    X.println("___ GROUP BY AVG MIN MAX SUM COUNT(*) COUNT(x) (MAX - MIN) * 2 as");
    Set<String> names = new HashSet<>();
    qry = new SqlFieldsQuery("select p.name, avg(f.price), min(f.price), max(f.price), sum(f.price), count(*), " + "count(nullif(f.price, 5)), (max(f.price) - min(f.price)) * 3 as nn " + ", CAST(max(f.price) + 7 AS VARCHAR) " + "from FactPurchase f, \"replicated-prod\".DimProduct p " + "where p.id = f.productId " + "group by f.productId, p.name");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        assertTrue(names.add((String) o.get(0)));
        assertEquals(i(o, 4), i(o, 2) + i(o, 3));
        assertEquals(i(o, 7), (i(o, 3) - i(o, 2)) * 3);
        assertEquals(o.get(8), Integer.toString(i(o, 3) + 7));
    }
    X.println("___ SUM HAVING");
    qry = new SqlFieldsQuery("select p.name, sum(f.price) s " + "from FactPurchase f, \"replicated-prod\".DimProduct p " + "where p.id = f.productId " + "group by f.productId, p.name " + "having s >= 15");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        assertTrue(i(o, 1) >= 15);
    }
    X.println("___ DISTINCT ORDER BY TOP");
    int top = 6;
    qry = new SqlFieldsQuery("select top 3 distinct productId " + "from FactPurchase f order by productId desc ");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        assertEquals(top--, o.get(0));
    }
    X.println("___ DISTINCT ORDER BY OFFSET LIMIT");
    top = 5;
    qry = new SqlFieldsQuery("select distinct productId " + "from FactPurchase f order by productId desc limit 2 offset 1");
    for (List<?> o : qryProc.querySqlFields(cache.context(), qry, false).getAll()) {
        X.println("___ -> " + o);
        assertEquals(top--, o.get(0));
    }
    assertEquals(3, top);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridQueryProcessor(org.apache.ignite.internal.processors.query.GridQueryProcessor) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) HashSet(java.util.HashSet)

Aggregations

GridQueryProcessor (org.apache.ignite.internal.processors.query.GridQueryProcessor)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 GridRunningQueryInfo (org.apache.ignite.internal.processors.query.GridRunningQueryInfo)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 DecimalFormat (java.text.DecimalFormat)1 HashSet (java.util.HashSet)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)1 IgniteException (org.apache.ignite.IgniteException)1 ClusterMetrics (org.apache.ignite.cluster.ClusterMetrics)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 IgniteEx (org.apache.ignite.internal.IgniteEx)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 GridManager (org.apache.ignite.internal.managers.GridManager)1 GridCheckpointManager (org.apache.ignite.internal.managers.checkpoint.GridCheckpointManager)1 GridCollisionManager (org.apache.ignite.internal.managers.collision.GridCollisionManager)1