Search in sources :

Example 36 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest method testUpdateAllJsr310Fields.

/**
 * Tests updating of all JSR-310 fields.
 */
public void testUpdateAllJsr310Fields() {
    EntityWithJsr310Fields expEntity = new EntityWithJsr310Fields(entity);
    expEntity.setLocalTime(expEntity.getLocalTime().plusHours(1));
    expEntity.setLocalDate(expEntity.getLocalDate().plusDays(1));
    expEntity.setLocalDateTime(LocalDateTime.of(expEntity.getLocalDate(), expEntity.getLocalTime()));
    SqlFieldsQuery qry = new SqlFieldsQuery("update EntityWithJsr310Fields set locTime = ?, locDate = ?, locDateTime = ? where id = ?").setArgs(expEntity.getLocalTime(), expEntity.getLocalDate(), expEntity.getLocalDateTime(), entity.getId());
    List<List<?>> qryResults = cache.query(qry).getAll();
    assertEquals(1, qryResults.size());
    assertEquals(1L, qryResults.get(0).get(0));
    assertEquals(expEntity, cache.get(expEntity.getId()));
}
Also used : List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 37 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest method testDateDiffForLocalDateTimeFieldAtMidnight.

/**
 * Tests that DATEDIFF SQL function works for {@link LocalDateTime}
 * fields with the time part set to midnight.
 *
 * @throws Exception If failed.
 */
public void testDateDiffForLocalDateTimeFieldAtMidnight() throws Exception {
    SqlFieldsQuery qry = new SqlFieldsQuery("select DATEDIFF('DAY', locDateTime, CURRENT_DATE ()) from EntityWithJsr310Fields");
    List<List<?>> qryResults = cache.query(qry).getAll();
    assertEquals(1, qryResults.size());
    assertTrue((Long) qryResults.get(0).get(0) >= DAYS_BEFORE_NOW);
}
Also used : List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 38 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class CacheQueryEntityWithJsr310Java8DateTimeApiFieldsTest method testSelectLocalTimeFieldReturnsTime.

/**
 * Tests that selection for a {@link LocalTime} field returns {@link Time}.
 *
 * @throws Exception If failed.
 */
public void testSelectLocalTimeFieldReturnsTime() throws Exception {
    SqlFieldsQuery qry = new SqlFieldsQuery("select locTime from EntityWithJsr310Fields");
    List<List<?>> qryResults = cache.query(qry).getAll();
    assertEquals(1, qryResults.size());
    assertEquals(Time.class, qryResults.get(0).get(0).getClass());
}
Also used : List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 39 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class GAGrid method getChromosomesByFittest.

/**
 * helper routine to retrieve Chromosome keys in order of fittest
 *
 * @return List of primary keys for chromosomes.
 */
private List<Long> getChromosomesByFittest() {
    List<Long> orderChromKeysByFittest = new ArrayList();
    String orderDirection = "desc";
    if (config.isHigherFitnessValueFitter() == false) {
        orderDirection = "asc";
    }
    String fittestSQL = "select _key from Chromosome order by fitnessScore " + orderDirection;
    // Execute query to retrieve keys for ALL Chromosomes by fittnessScore
    QueryCursor<List<?>> cursor = populationCache.query(new SqlFieldsQuery(fittestSQL));
    List<List<?>> res = cursor.getAll();
    for (List row : res) {
        Long key = (Long) row.get(0);
        orderChromKeysByFittest.add(key);
    }
    return orderChromKeysByFittest;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 40 with SqlFieldsQuery

use of org.apache.ignite.cache.query.SqlFieldsQuery in project ignite by apache.

the class IgniteSqlSplitterSelfTest method checkQueryWithGroupsAndAggrs.

/**
 * Query with aggregates and groups
 */
private void checkQueryWithGroupsAndAggrs(IgniteCache<Integer, Value> cache) {
    try (QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("SELECT fst, count(snd), sum(snd), avg(snd), avg(CAST(snd AS DOUBLE)), " + "min(snd), max(snd) FROM Value GROUP BY fst ORDER BY fst"))) {
        List<List<?>> result = qry.getAll();
        assertEquals(6, result.size());
        List<?> row = result.get(0);
        assertEquals("fst", 1, ((Number) row.get(0)).intValue());
        assertEquals("count", 3L, ((Number) row.get(1)).longValue());
        assertEquals("sum", 9L, ((Number) row.get(2)).longValue());
        assertEquals("avg", 3, ((Number) row.get(3)).doubleValue(), 0.001);
        assertEquals("avg dbl", 3d, ((Number) row.get(4)).doubleValue(), 0.001);
        assertEquals("min", 3, ((Integer) row.get(5)).intValue());
        assertEquals("max", 3, ((Integer) row.get(6)).intValue());
        row = result.get(1);
        assertEquals("fst", 2, ((Number) row.get(0)).intValue());
        assertEquals("count", 3L, ((Number) row.get(1)).longValue());
        assertEquals("sum", 6L, ((Number) row.get(2)).longValue());
        assertEquals("avg", 2, ((Number) row.get(3)).doubleValue(), 0.001);
        assertEquals("avg dbl", 2d, ((Number) row.get(4)).doubleValue(), 0.001);
        assertEquals("min", 1, ((Integer) row.get(5)).intValue());
        assertEquals("max", 3, ((Integer) row.get(6)).intValue());
        row = result.get(2);
        assertEquals("fst", 3, ((Number) row.get(0)).intValue());
        assertEquals("count", 6L, ((Number) row.get(1)).longValue());
        assertEquals("sum", 9L, ((Number) row.get(2)).longValue());
        assertEquals("avg", 1, ((Integer) row.get(3)).intValue());
        assertEquals("avg dbl", 1.5d, ((Number) row.get(4)).doubleValue(), 0.001);
        assertEquals("min", 1, ((Integer) row.get(5)).intValue());
        assertEquals("max", 2, ((Integer) row.get(6)).intValue());
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Aggregations

SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)272 List (java.util.List)171 ArrayList (java.util.ArrayList)81 Ignite (org.apache.ignite.Ignite)43 IgniteCache (org.apache.ignite.IgniteCache)36 CacheException (javax.cache.CacheException)28 SqlQuery (org.apache.ignite.cache.query.SqlQuery)21 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)19 Cache (javax.cache.Cache)17 IgniteException (org.apache.ignite.IgniteException)16 IgniteEx (org.apache.ignite.internal.IgniteEx)15 Random (java.util.Random)11 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)10 GridRandom (org.apache.ignite.internal.util.GridRandom)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 BinaryObject (org.apache.ignite.binary.BinaryObject)8 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)8