SqlFieldsQuery newFieldsQry = new SqlFieldsQuery(plan.selectQuery(), qry.isCollocated()) .setArgs(qry.getArgs()) .setDistributedJoins(qry.isDistributedJoins()) .setEnforceJoinOrder(qry.isEnforceJoinOrder()) .setLocal(qry.isLocal()) .setPageSize(qry.getPageSize()) .setTimeout(qry.getTimeout(), TimeUnit.MILLISECONDS) .setDataPageScanEnabled(qry.isDataPageScanEnabled()); F.asList(qry.getArgs()), filter, qry.isEnforceJoinOrder(), false, qry.getTimeout(), cancel, new StaticMvccQueryTracker(cctx, mvccSnapshot), null);
/** * @param cache Cache. * @param qry Query. * @return Query plan. */ protected final String queryPlan(IgniteCache<?, ?> cache, SqlFieldsQuery qry) { return (String)cache.query(new SqlFieldsQuery("explain " + qry.getSql()) .setArgs(qry.getArgs()) .setLocal(qry.isLocal()) .setCollocated(qry.isCollocated()) .setPageSize(qry.getPageSize()) .setDistributedJoins(qry.isDistributedJoins()) .setEnforceJoinOrder(qry.isEnforceJoinOrder())) .getAll().get(0).get(0); }
/** * Make a copy of {@link SqlFieldsQuery} with all flags and preserving type. * @param oldQry Query to copy. * @return Query copy. */ private SqlFieldsQuery cloneFieldsQuery(SqlFieldsQuery oldQry) { return oldQry.copy().setLocal(oldQry.isLocal()).setPageSize(oldQry.getPageSize()); }
/** * Run DML on remote nodes. * * @param schemaName Schema name. * @param fieldsQry Initial update query. * @param cacheIds Cache identifiers. * @param isReplicatedOnly Whether query uses only replicated caches. * @param cancel Cancel state. * @return Update result. */ UpdateResult runDistributedUpdate( String schemaName, SqlFieldsQuery fieldsQry, List<Integer> cacheIds, boolean isReplicatedOnly, GridQueryCancel cancel) { return rdcQryExec.update(schemaName, cacheIds, fieldsQry.getSql(), fieldsQry.getArgs(), fieldsQry.isEnforceJoinOrder(), fieldsQry.getPageSize(), fieldsQry.getTimeout(), fieldsQry.getPartitions(), isReplicatedOnly, cancel); }
/** * Reads fields query. * * @param reader Binary reader. * @return Query. */ private Query readFieldsQuery(BinaryRawReaderEx reader) { boolean loc = reader.readBoolean(); String sql = reader.readString(); final int pageSize = reader.readInt(); Object[] args = readQueryArgs(reader); boolean distrJoins = reader.readBoolean(); boolean enforceJoinOrder = reader.readBoolean(); boolean lazy = reader.readBoolean(); int timeout = reader.readInt(); boolean replicated = reader.readBoolean(); boolean collocated = reader.readBoolean(); String schema = reader.readString(); return new SqlFieldsQuery(sql) .setPageSize(pageSize) .setArgs(args) .setLocal(loc) .setDistributedJoins(distrJoins) .setEnforceJoinOrder(enforceJoinOrder) .setLazy(lazy) .setTimeout(timeout, TimeUnit.MILLISECONDS) .setReplicatedOnly(replicated) .setCollocated(collocated) .setSchema(schema); }
/** * Default query. * * @param arg Argument. * @return Query. */ private static SqlFieldsQuery query(long arg) { return new SqlFieldsQuery("SELECT id, name FROM Person WHERE id >= ?").setArgs(arg); }
/** * Run DML query as SqlFieldsQuery and check that DML is not allowed or not inside transaction. Also checked that * using DML will not lead to rollback * * @param dmlQry Dml query which should be executed in transaction. * @param isLocal Is local query. * @param isAllowed true in case DML should work inside transaction, false otherwise. */ private void runDmlSqlFieldsQueryInTransactionTest(String dmlQry, boolean isLocal, boolean isAllowed) { SqlFieldsQuery query = new SqlFieldsQuery(dmlQry).setLocal(isLocal); runDmlInTransactionTest(query, isAllowed); }
/** {@inheritDoc} */ @Override public UpdateSourceIterator<?> prepareDistributedUpdate(GridCacheContext<?, ?> cctx, int[] ids, int[] parts, String schema, String qry, Object[] params, int flags, int pageSize, int timeout, AffinityTopologyVersion topVer, MvccSnapshot mvccSnapshot, GridQueryCancel cancel) throws IgniteCheckedException { SqlFieldsQuery fldsQry = new SqlFieldsQuery(qry); if (params != null) fldsQry.setArgs(params); fldsQry.setEnforceJoinOrder(isFlagSet(flags, GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER)); fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS); fldsQry.setPageSize(pageSize); fldsQry.setLocal(true); fldsQry.setDataPageScanEnabled(isDataPageScanEnabled(flags)); boolean loc = true; final boolean replicated = isFlagSet(flags, GridH2QueryRequest.FLAG_REPLICATED); GridCacheContext<?, ?> cctx0; if (!replicated && !F.isEmpty(ids) && (cctx0 = CU.firstPartitioned(cctx.shared(), ids)) != null && cctx0.config().getQueryParallelism() > 1) { fldsQry.setDistributedJoins(true); loc = false; } Connection conn = connMgr.connectionForThread().connection(schema); H2Utils.setupConnection(conn, false, fldsQry.isEnforceJoinOrder()); PreparedStatement stmt = preparedStatementWithParams(conn, fldsQry.getSql(), F.asList(fldsQry.getArgs()), true); return dmlProc.prepareDistributedUpdate(schema, conn, stmt, fldsQry, backupFilter(topVer, parts), cancel, loc, topVer, mvccSnapshot); }
/** * @param sql SQL. * @param cache Cache. * @param enforceJoinOrder Enforce join order flag. * @param args Arguments. * @throws Exception If failed. */ private void checkQuery(String sql, IgniteCache<Object, Object> cache, boolean enforceJoinOrder, Object... args) throws Exception { String plan = (String)cache.query(new SqlFieldsQuery("explain " + sql) .setArgs(args) .setDistributedJoins(true) .setEnforceJoinOrder(enforceJoinOrder)) .getAll().get(0).get(0); log.info("Plan: " + plan); compareQueryRes0(cache, sql, true, enforceJoinOrder, args, Ordering.RANDOM); }
/** * Test metrics for SQL cross cache queries. * * @throws Exception In case of error. */ @Test public void testSqlFieldsQueryHistoryCrossCacheQueryNotFullyFetched() throws Exception { SqlFieldsQuery qry = new SqlFieldsQuery("select * from \"B\".String"); qry.setPageSize(10); checkQueryNotFullyFetchedMetrics(qry, false); }
/** * @param cache Cache. * @param sql SQL. * @param enforceJoinOrder Enforce join order flag. */ private void checkQueryFails(final IgniteCache<Object, Object> cache, String sql, boolean enforceJoinOrder) { final SqlFieldsQuery qry = new SqlFieldsQuery(sql); qry.setDistributedJoins(true); qry.setEnforceJoinOrder(enforceJoinOrder); GridTestUtils.assertThrows(log, new Callable<Void>() { @Override public Void call() throws Exception { cache.query(qry); return null; } }, CacheException.class, null); }
boolean enforceJoinOrderOnParsing = (!qry.isLocal() || qry.isEnforceJoinOrder()); boolean loc = qry.isLocal(); PreparedStatement stmt = prepareStatementAndCaches(c, qry.getSql()); Object[] argsOrig = qry.getArgs(); loc = false; qry.setDistributedJoins(true); SqlFieldsQuery newQry = cloneFieldsQuery(qry).setSql(prepared.getSQL()).setArgs(args); connMgr.statementCacheForThread().remove(schemaName, qry.getSql()); H2TwoStepCachedQueryKey cachedQryKey = new H2TwoStepCachedQueryKey(schemaName, qry.getSql(), qry.isCollocated(), qry.isDistributedJoins(), qry.isEnforceJoinOrder(), qry.isLocal()); .distributedJoinMode(distributedJoinMode(qry.isLocal(), qry.isDistributedJoins()))); throw new IgniteSQLException("Failed to bind parameters: [qry=" + newQry.getSql() + ", params=" + Arrays.deepToString(newQry.getArgs()) + "]", IgniteQueryErrorCode.PARSING, e);
/** Serialize SQL field query to stream. */ void write(SqlFieldsQuery qry, BinaryOutputStream out) { writeObject(out, qry.getSchema()); out.writeInt(qry.getPageSize()); out.writeInt(-1); // do not limit writeObject(out, qry.getSql()); ClientUtils.collection(qry.getArgs() == null ? null : Arrays.asList(qry.getArgs()), out, this::writeObject); out.writeByte((byte)0); // statement type ANY out.writeBoolean(qry.isDistributedJoins()); out.writeBoolean(qry.isLocal()); out.writeBoolean(qry.isReplicatedOnly()); out.writeBoolean(qry.isEnforceJoinOrder()); out.writeBoolean(qry.isCollocated()); out.writeBoolean(qry.isLazy()); out.writeLong(qry.getTimeout()); out.writeBoolean(true); // include column names }
/** * @param q Query. * @return Row set. */ private Set<List<?>> query(String q) { return new HashSet<>(cache.query(new SqlFieldsQuery(q)).getAll()); }
@Override public Void call() throws Exception { SqlFieldsQuery qry = new SqlFieldsQuery(sql); qry.setDistributedJoins(true); cache.query(qry).getAll(); return null; } }, CacheException.class, null);
/** * Execute SQL command and return resulting dataset. * @param cache Cache to initiate query from. * @param sql Statement. * @return result. */ protected List<List<?>> run(IgniteCache<?, ?> cache, String sql, Object... args) { SqlFieldsQuery qry = new SqlFieldsQuery(sql).setSchema(QueryUtils.DFLT_SCHEMA).setArgs(args) .setDistributedJoins(true); return cache.query(qry).getAll(); }
boolean startTx, MvccQueryTracker mvccTracker, GridQueryCancel cancel, boolean registerAsNewQry) { if (log.isDebugEnabled()) log.debug("Parsed query: `" + qry.getSql() + "` into two step query: " + twoStepQry); twoStepQry.pageSize(qry.getPageSize()); Long qryId = registerRunningQuery(schemaName, cancel, qry.getSql(), qry.isLocal(), registerAsNewQry); int explicitParts[] = qry.getPartitions(); PartitionResult derivedParts = twoStepQry.derivedPartitions(); int parts[] = calculatePartitions(explicitParts, derivedParts, qry.getArgs()); twoStepQry, keepBinary, qry.isEnforceJoinOrder(), startTx, qry.getTimeout(), cancel, qry.getArgs(), parts, qry.isLazy(), mvccTracker, qry.isDataPageScanEnabled() );
/** * Creates SqlFieldsQuery with specified enforce join order flag and specified sql from template. * * @param enforceJoinOrder don't let engine to reorder tables in join. * @param tpl query template in java format. * @param args arguments for the template. */ static SqlFieldsQuery joinQry(boolean enforceJoinOrder, String tpl, Object... args) { return new SqlFieldsQuery(String.format(tpl, args)).setEnforceJoinOrder(enforceJoinOrder); }
/** * Test different query modes. */ @Test public void testQueryModes() throws Exception { Ignite ignite = startGrid(0); startGrid(1); UUID nodeId = ignite.cluster().localNode().id(); IgniteCache cache = ignite.getOrCreateCache(DEFAULT_CACHE_NAME); String sql = "SELECT ID FROM IGNITE.NODES WHERE NODE_ORDER = 1"; SqlFieldsQuery qry; qry = new SqlFieldsQuery(sql).setDistributedJoins(true); assertEquals(nodeId, ((List<?>)cache.query(qry).getAll().get(0)).get(0)); qry = new SqlFieldsQuery(sql).setReplicatedOnly(true); assertEquals(nodeId, ((List<?>)cache.query(qry).getAll().get(0)).get(0)); qry = new SqlFieldsQuery(sql).setLocal(true); assertEquals(nodeId, ((List<?>)cache.query(qry).getAll().get(0)).get(0)); }