/** * Fails if result has entries. * * @param qryStr to execute. * @param arg argument that will be passed to query. * @param cache cache on which query will be executed. */ private void assertEmptyEnumQry(String qryStr, Object arg, IgniteCache<Long, EnumObject> cache) { final SqlQuery<Long, EnumObject> qry = new SqlQuery<>(EnumObject.class, qryStr); qry.setArgs(arg); final List<Cache.Entry<Long, EnumObject>> res = cache.query(qry).getAll(); assert res.isEmpty(); }
/** {@inheritDoc} */ @SuppressWarnings("deprecation") @Override public SqlFieldsQuery generateFieldsQuery(String cacheName, SqlQuery qry) { String schemaName = schema(cacheName); String type = qry.getType(); H2TableDescriptor tblDesc = schemaMgr.tableForType(schemaName, cacheName, type); if (tblDesc == null) throw new IgniteSQLException("Failed to find SQL table for type: " + type, IgniteQueryErrorCode.TABLE_NOT_FOUND); String sql; try { sql = H2Utils.generateFieldsQueryString(qry.getSql(), qry.getAlias(), tblDesc); } catch (IgniteCheckedException e) { throw new IgniteException(e); } SqlFieldsQuery res = new SqlFieldsQuery(sql); res.setArgs(qry.getArgs()); res.setDistributedJoins(qry.isDistributedJoins()); res.setLocal(qry.isLocal()); res.setPageSize(qry.getPageSize()); res.setPartitions(qry.getPartitions()); res.setReplicatedOnly(qry.isReplicatedOnly()); res.setSchema(schemaName); res.setSql(sql); res.setDataPageScanEnabled(qry.isDataPageScanEnabled()); if (qry.getTimeout() > 0) res.setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS); return res; }
/** * Ctor. * * @param reader Reader. */ public ClientCacheSqlQueryRequest(BinaryRawReaderEx reader) { super(reader); qry = new SqlQuery(reader.readString(), reader.readString()) .setArgs(PlatformCache.readQueryArgs(reader)) .setDistributedJoins(reader.readBoolean()) .setLocal(reader.readBoolean()) .setReplicatedOnly(reader.readBoolean()) .setPageSize(reader.readInt()) .setTimeout((int) reader.readLong(), TimeUnit.MILLISECONDS); }
/** Handle SQL query. */ private QueryCursor<Cache.Entry<K, V>> sqlQuery(SqlQuery qry) { Consumer<BinaryOutputStream> qryWriter = out -> { writeCacheInfo(out); serDes.writeObject(out, qry.getType()); serDes.writeObject(out, qry.getSql()); ClientUtils.collection(qry.getArgs(), out, serDes::writeObject); out.writeBoolean(qry.isDistributedJoins()); out.writeBoolean(qry.isLocal()); out.writeBoolean(qry.isReplicatedOnly()); out.writeInt(qry.getPageSize()); out.writeLong(qry.getTimeout()); }; return new ClientQueryCursor<>(new ClientQueryPager<>( ch, ClientOperation.QUERY_SQL, ClientOperation.QUERY_SQL_CURSOR_GET_PAGE, qryWriter, keepBinary, marsh )); }
/** */ @Test public void testDisappearedCacheWasNotFoundMessage() { SqlQuery<String, Person> qry = new SqlQuery<String, Person>(Person.class, JoinSqlTestHelper.JOIN_SQL).setArgs("Organization #0"); qry.setDistributedJoins(true); try { personCache.query(qry).getAll(); fail("No CacheException emitted."); } catch (CacheException e) { boolean exp = e.getMessage().contains("Cache not found on local node (was concurrently destroyed?)"); if (!exp) throw e; } }
@Override public Object call() throws Exception { cachePers.query(new SqlQuery(Person.class, "SELECT firstName from PERSON")); return null; } }, CacheException.class, null);
/** * @param ignite Ignite. * @param orgId Organization id. * @return Count of found Person object with specified orgId */ private static int getPersonsCountBySqlLocalQuery(final IgniteEx ignite, int orgId) { List res = ignite.cache(Person.class.getSimpleName()) .query(new SqlQuery<Person.Key, Person>(Person.class, "orgId = ?").setArgs(orgId).setLocal(true)) .getAll(); return res.size(); }
/** {@inheritDoc} */ @Override public List<Cache.Entry<Integer, GridCacheQueryTestValue>> call() throws Exception { IgniteCache<Integer, GridCacheQueryTestValue> c = ignite.cache(CACHE_NAME); String sqlStr = "FROM GridCacheQueryTestValue WHERE fieldname = ?"; SqlQuery<Integer, GridCacheQueryTestValue> sql = new SqlQuery<>(GridCacheQueryTestValue.class, sqlStr); sql.setArgs("C"); return c.query(sql.setSql(sqlStr)).getAll(); } }
private void checkSqlQuery(IgniteCache<Long,TestData> cache, Boolean dataPageScanEnabled) { DirectPageScanIndexing.expectedDataPageScanEnabled = dataPageScanEnabled; assertTrue(cache.query(new SqlQuery<>(TestData.class, "from TestData use index() where check_scan_flag(?,false)") // Force full scan with USE INDEX() .setArgs(DirectPageScanIndexing.expectedDataPageScanEnabled) .setDataPageScanEnabled(DirectPageScanIndexing.expectedDataPageScanEnabled)) .getAll().isEmpty()); checkSqlLastFindDataPageScan(dataPageScanEnabled); }
/** * Test metrics for Scan queries. * * @throws Exception In case of error. */ @Test public void testSqlQueryHistoryNotFullyFetched() throws Exception { SqlQuery<Integer, String> qry = new SqlQuery<>("String", "from String"); qry.setPageSize(10); checkQueryNotFullyFetchedMetrics(qry, true); }
new SqlQuery<Integer, Person>(Person.class, "id >= ?").setArgs(minId).setPageSize(pageSize) );
/** * Test table alias in SqlQuery. * * @throws Exception In case of error. */ @Test public void testTableAliasInSqlQuery() throws Exception { IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class); int key = 898; int val = 2; cache.put(key, val); SqlQuery<Integer, Integer> sqlQry = new SqlQuery<>(Integer.class, "t1._key = ? and t1._val > 1"); QueryCursor<Cache.Entry<Integer, Integer>> qry = cache.query(sqlQry.setAlias("t1").setArgs(key)); Cache.Entry<Integer, Integer> entry = F.first(qry.getAll()); assert entry != null; assertEquals(key, entry.getKey().intValue()); assertEquals(val, entry.getValue().intValue()); sqlQry = new SqlQuery<>(Integer.class, "FROM Integer as t1 WHERE t1._key = ? and t1._val > 1"); qry = cache.query(sqlQry.setAlias("t1").setArgs(key)); entry = F.first(qry.getAll()); assert entry != null; assertEquals(key, entry.getKey().intValue()); assertEquals(val, entry.getValue().intValue()); }
/** * @param orig Originator. */ protected void doTestRegionQuery(Ignite orig) { IgniteCache<ClientKey, Client> cl = orig.cache("cl"); for (int regionId = 1; regionId <= PARTS_PER_REGION.length; regionId++) { SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "regionId=?"); qry1.setArgs(regionId); List<Cache.Entry<ClientKey, Client>> clients1 = cl.query(qry1).getAll(); int expRegionCnt = regionId == 5 ? 0 : PARTS_PER_REGION[regionId - 1] * CLIENTS_PER_PARTITION; assertEquals("Region " + regionId + " count", expRegionCnt, clients1.size()); validateClients(regionId, clients1); // Repeat the same query with partition set condition. List<Integer> range = REGION_TO_PART_MAP.get(regionId); SqlQuery<ClientKey, Client> qry2 = new SqlQuery<>(Client.class, "1=1"); qry2.setPartitions(createRange(range.get(0), range.get(1))); try { List<Cache.Entry<ClientKey, Client>> clients2 = cl.query(qry2).getAll(); assertEquals("Region " + regionId + " count with partition set", expRegionCnt, clients2.size()); // Query must produce only results from single region. validateClients(regionId, clients2); if (regionId == UNMAPPED_REGION) fail(); } catch (CacheException ignored) { if (regionId != UNMAPPED_REGION) fail(); } } }
/** * Tests local sql query. */ @Test public void testLocalSqlQuery() { IgniteCache<Integer, Value> cache = grid(0).cache(DEFAULT_CACHE_NAME); SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class.getSimpleName(), "select * from Value"); qry.setLocal(true); List<Cache.Entry<Integer, Value>> all = cache.query(qry).getAll(); assertFalse(all.isEmpty()); for (Cache.Entry<Integer, Value> entry : all) entry.getValue().str = "after"; check(cache); }
/** * Execute distributed SQL query. * * @param cctx Cache context. * @param qry Query. * @param keepBinary Keep binary flag. * @return Cursor. */ public <K, V> QueryCursor<Cache.Entry<K,V>> querySql(final GridCacheContext<?,?> cctx, final SqlQuery qry, boolean keepBinary) { validateSqlQuery(qry, ctx, cctx); if (qry.isReplicatedOnly() && qry.getPartitions() != null) throw new CacheException("Partitions are not supported in replicated only mode."); if (qry.isDistributedJoins() && qry.getPartitions() != null) throw new CacheException( "Using both partitions and distributed JOINs is not supported for the same query"); if ((qry.isReplicatedOnly() && cctx.isReplicatedAffinityNode()) || cctx.isLocal() || qry.isLocal()) return queryLocalSql(cctx, qry, keepBinary); return queryDistributedSql(cctx, qry, keepBinary); }
@Override public <K, V> QueryCursor<Cache.Entry<K,V>> queryLocalSql(String schemaName, String cacheName, final SqlQuery qry, final IndexingQueryFilter filter, final boolean keepBinary) throws IgniteCheckedException { String type = qry.getType(); String sqlQry = qry.getSql(); String alias = qry.getAlias(); Object[] params = qry.getArgs();
/** {@inheritDoc} */ @Override public ClientResponse process(ClientConnectionContext ctx) { IgniteCache cache = cache(ctx); ctx.incrementCursors(); try { QueryCursor cur = cache.query(qry); ClientCacheEntryQueryCursor cliCur = new ClientCacheEntryQueryCursor( cur, qry.getPageSize(), ctx); long cursorId = ctx.resources().put(cliCur); cliCur.id(cursorId); return new ClientCacheQueryResponse(requestId(), cliCur); } catch (Exception e) { ctx.decrementCursors(); throw e; } } }
/** * Convert query arguments to BinaryObjects if binary marshaller used. * * @param qry Query. */ private void convertToBinary(final Query qry) { GridCacheContext<K, V> ctx = getContextSafe(); if (ctx.binaryMarshaller()) { if (qry instanceof SqlQuery) { final SqlQuery sqlQry = (SqlQuery) qry; convertToBinary(sqlQry.getArgs()); } else if (qry instanceof SpiQuery) { final SpiQuery spiQry = (SpiQuery) qry; convertToBinary(spiQry.getArgs()); } else if (qry instanceof SqlFieldsQuery) { final SqlFieldsQuery fieldsQry = (SqlFieldsQuery) qry; convertToBinary(fieldsQry.getArgs()); } } }
return executeQuery(GridCacheQueryType.SQL, qry.getSql(), cctx, new IgniteOutClosureX<QueryCursor<Cache.Entry<K, V>>>() { @Override public QueryCursor<Cache.Entry<K, V>> applyx() throws IgniteCheckedException {
/** */ @Test public void testDisappearedCacheCauseRetryMessage() { SqlQuery<String, JoinSqlTestHelper.Person> qry = new SqlQuery<String, JoinSqlTestHelper.Person>(JoinSqlTestHelper.Person.class, JoinSqlTestHelper.JOIN_SQL).setArgs("Organization #0"); qry.setDistributedJoins(true); try { personCache.query(qry).getAll(); fail("No CacheException emitted."); } catch (CacheException e) { assertTrue(e.getMessage(), e.getMessage().contains("Failed to reserve partitions for query (cache is not found on local node) [")); } }