Search in sources :

Example 86 with SqlFieldsQuery

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

the class IgniteSqlSegmentedIndexSelfTest method checkLocalSizeQueryWithSegmentedIndex.

/**
 * Verifies that local <code>select count(*)</code> query returns a correct result.
 */
public void checkLocalSizeQueryWithSegmentedIndex() {
    for (int i = 0; i < nodesCount(); i++) {
        final Ignite node = ignite(i);
        IgniteCache<PersonKey, Person> c1 = node.cache(PERSON_CAHE_NAME);
        IgniteCache<Integer, Organization> c2 = node.cache(ORG_CACHE_NAME);
        Set<Integer> locOrgIds = new HashSet<>();
        for (Cache.Entry<Integer, Organization> e : c2.localEntries()) locOrgIds.add(e.getKey());
        int expPersons = 0;
        for (Cache.Entry<PersonKey, Person> e : c1.localEntries()) {
            final Integer orgId = e.getKey().orgId;
            if (locOrgIds.contains(orgId))
                expPersons++;
        }
        String select0 = "select count(*) from \"pers\".Person p, \"org\".Organization o where p.orgId = o._key";
        List<List<?>> res = c1.query(new SqlFieldsQuery(select0).setLocal(true)).getAll();
        assertEquals((long) expPersons, res.get(0).get(0));
    }
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) HashSet(java.util.HashSet) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 87 with SqlFieldsQuery

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

the class SqlSchemaSelfTest method testQueryWithoutCacheOnCacheSchema.

/**
 * Test query without caches.
 *
 * @throws Exception If failed.
 */
@Test
public void testQueryWithoutCacheOnCacheSchema() throws Exception {
    node.createCache(new CacheConfiguration<PersonKey, Person>().setName(CACHE_PERSON).setIndexedTypes(PersonKey.class, Person.class));
    GridQueryProcessor qryProc = node.context().query();
    SqlFieldsQuery qry = new SqlFieldsQuery("SELECT 1").setSchema(CACHE_PERSON);
    List<List<?>> res = qryProc.querySqlFields(qry, true).getAll();
    assertEquals(1, res.size());
    assertEquals(1, res.get(0).size());
    assertEquals(1, res.get(0).get(0));
    Iterator<List<?>> iter = qryProc.querySqlFields(qry, true).iterator();
    assertTrue(iter.hasNext());
    List<?> row = iter.next();
    assertEquals(1, row.size());
    assertEquals(1, row.get(0));
    assertFalse(iter.hasNext());
}
Also used : List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 88 with SqlFieldsQuery

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

the class SqlSchemaSelfTest method testSchemaChangeOnCacheWithPublicSchema.

/**
 * Test simple query.
 *
 * @throws Exception If failed.
 */
@Test
public void testSchemaChangeOnCacheWithPublicSchema() throws Exception {
    IgniteCache<PersonKey, Person> cache = node.createCache(new CacheConfiguration<PersonKey, Person>().setName(CACHE_PERSON).setIndexedTypes(PersonKey.class, Person.class).setSqlSchema(QueryUtils.DFLT_SCHEMA));
    node.createCache(new CacheConfiguration<PersonKey, Person>().setName(CACHE_PERSON_2).setIndexedTypes(PersonKey.class, Person.class));
    cache.put(new PersonKey(1), new Person("Vasya", 2));
    // Normal calls.
    assertEquals(1, cache.query(new SqlFieldsQuery("SELECT id, name, orgId FROM Person")).getAll().size());
    assertEquals(1, cache.query(new SqlFieldsQuery("SELECT id, name, orgId FROM Person").setSchema(QueryUtils.DFLT_SCHEMA)).getAll().size());
    // Call from another schema.
    assertEquals(1, cache.query(new SqlFieldsQuery("SELECT id, name, orgId FROM public.Person").setSchema(CACHE_PERSON_2)).getAll().size());
    assertEquals(1, cache.query(new SqlFieldsQuery("SELECT id, name, orgId FROM \"PUBLIC\".Person").setSchema(CACHE_PERSON_2)).getAll().size());
}
Also used : CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 89 with SqlFieldsQuery

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

the class MultipleStatementsSqlQuerySelfTest method sql.

/**
 * @param sql SQL query.
 * @return Results.
 */
private List<FieldsQueryCursor<List<?>>> sql(String sql) {
    GridQueryProcessor qryProc = node.context().query();
    SqlFieldsQuery qry = new SqlFieldsQuery(sql).setSchema("PUBLIC");
    return qryProc.querySqlFields(qry, true, false);
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 90 with SqlFieldsQuery

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

the class SqlQueriesTopologyMappingTest method testLocalCacheQueryMapping.

/**
 */
@Test
public void testLocalCacheQueryMapping() throws Exception {
    IgniteEx ign0 = startGrid(0);
    IgniteCache<Object, Object> cache = ign0.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME).setCacheMode(CacheMode.LOCAL).setSqlSchema("PUBLIC").setIndexedTypes(Integer.class, Integer.class));
    cache.put(1, 2);
    startGrid(1);
    SqlFieldsQuery qry = new SqlFieldsQuery("select * from Integer");
    {
        List<List<?>> res0 = grid(0).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        assertEquals(1, res0.size());
        assertEqualsCollections(Arrays.asList(1, 2), res0.get(0));
    }
    {
        List<List<?>> res1 = grid(1).cache(DEFAULT_CACHE_NAME).query(qry).getAll();
        assertTrue(res1.isEmpty());
    }
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Aggregations

SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)679 Test (org.junit.Test)388 List (java.util.List)373 Ignite (org.apache.ignite.Ignite)170 ArrayList (java.util.ArrayList)136 IgniteCache (org.apache.ignite.IgniteCache)123 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)115 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)94 IgniteEx (org.apache.ignite.internal.IgniteEx)90 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)83 Transaction (org.apache.ignite.transactions.Transaction)80 CacheException (javax.cache.CacheException)67 Random (java.util.Random)55 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)53 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)52 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)45 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)41 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)32 HashMap (java.util.HashMap)31 Cache (javax.cache.Cache)31