Search in sources :

Example 61 with SqlFieldsQuery

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

the class IgnitePdsMultiNodePutGetRestartTest method checkPutGetSql.

/**
 * @param ig Ig.
 * @param write Write.
 */
private void checkPutGetSql(IgniteEx ig, boolean write) {
    IgniteCache<Integer, DbValue> cache = ig.cache(CACHE_NAME);
    if (write) {
        try (IgniteDataStreamer<Object, Object> streamer = ig.dataStreamer(CACHE_NAME)) {
            for (int i = 0; i < 10_000; i++) streamer.addData(i, new DbValue(i, "value-" + i, i));
        }
    }
    List<List<?>> res = cache.query(new SqlFieldsQuery("select ival from dbvalue where ival < ? order by ival asc").setArgs(10_000)).getAll();
    assertEquals(10_000, res.size());
    for (int i = 0; i < 10_000; i++) {
        assertEquals(1, res.get(i).size());
        assertEquals(i, res.get(i).get(0));
    }
    assertEquals(1, cache.query(new SqlFieldsQuery("select lval from dbvalue where ival = 7899")).getAll().size());
    assertEquals(5000, cache.query(new SqlFieldsQuery("select lval from dbvalue where ival >= 5000 and ival < 10000")).getAll().size());
    for (int i = 0; i < 10_000; i++) assertEquals(new DbValue(i, "value-" + i, i), cache.get(i));
}
Also used : List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 62 with SqlFieldsQuery

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

the class IgniteH2Indexing method executeUpdateOnDataNodeTransactional.

/**
 * {@inheritDoc}
 */
@Override
public UpdateSourceIterator<?> executeUpdateOnDataNodeTransactional(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 = QueryUtils.withQueryTimeout(new SqlFieldsQuery(qry), timeout, TimeUnit.MILLISECONDS);
    if (params != null)
        fldsQry.setArgs(params);
    fldsQry.setEnforceJoinOrder(U.isFlagSet(flags, GridH2QueryRequest.FLAG_ENFORCE_JOIN_ORDER));
    fldsQry.setTimeout(timeout, TimeUnit.MILLISECONDS);
    fldsQry.setPageSize(pageSize);
    fldsQry.setLocal(true);
    fldsQry.setLazy(U.isFlagSet(flags, GridH2QueryRequest.FLAG_LAZY));
    boolean loc = true;
    final boolean replicated = U.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;
    }
    QueryParserResult parseRes = parser.parse(schema, fldsQry, false);
    assert parseRes.remainingQuery() == null;
    QueryParserResultDml dml = parseRes.dml();
    assert dml != null;
    IndexingQueryFilter filter = backupFilter(topVer, parts);
    UpdatePlan plan = dml.plan();
    GridCacheContext planCctx = plan.cacheContext();
    // Force keepBinary for operation context to avoid binary deserialization inside entry processor
    DmlUtils.setKeepBinaryContext(planCctx);
    SqlFieldsQuery selectFieldsQry = QueryUtils.withQueryTimeout(new SqlFieldsQuery(plan.selectQuery(), fldsQry.isCollocated()), fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setArgs(fldsQry.getArgs()).setDistributedJoins(fldsQry.isDistributedJoins()).setEnforceJoinOrder(fldsQry.isEnforceJoinOrder()).setLocal(fldsQry.isLocal()).setPageSize(fldsQry.getPageSize()).setTimeout(fldsQry.getTimeout(), TimeUnit.MILLISECONDS).setLazy(fldsQry.isLazy());
    QueryCursorImpl<List<?>> cur;
    // sub-query and not some dummy stuff like "select 1, 2, 3;"
    if (!loc && !plan.isLocalSubquery()) {
        cur = executeSelectForDml(RunningQueryManager.UNDEFINED_QUERY_ID, schema, selectFieldsQry, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, timeout);
    } else {
        selectFieldsQry.setLocal(true);
        QueryParserResult selectParseRes = parser.parse(schema, selectFieldsQry, false);
        GridQueryFieldsResult res = executeSelectLocal(RunningQueryManager.UNDEFINED_QUERY_ID, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), filter, new StaticMvccQueryTracker(planCctx, mvccSnapshot), cancel, true, timeout);
        cur = new QueryCursorImpl<>(new Iterable<List<?>>() {

            @Override
            public Iterator<List<?>> iterator() {
                try {
                    return res.iterator();
                } catch (IgniteCheckedException e) {
                    throw new IgniteException(e);
                }
            }
        }, cancel, true, selectParseRes.queryParameters().lazy());
    }
    return plan.iteratorForTransaction(connMgr, cur);
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) IndexingQueryFilter(org.apache.ignite.spi.indexing.IndexingQueryFilter) StaticMvccQueryTracker(org.apache.ignite.internal.processors.cache.mvcc.StaticMvccQueryTracker) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) UpdatePlan(org.apache.ignite.internal.processors.query.h2.dml.UpdatePlan)

Example 63 with SqlFieldsQuery

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

the class IgniteH2Indexing method parameterMetaData.

/**
 * {@inheritDoc}
 */
@Override
public List<JdbcParameterMeta> parameterMetaData(String schemaName, SqlFieldsQuery qry) throws IgniteSQLException {
    assert qry != null;
    ArrayList<JdbcParameterMeta> metas = new ArrayList<>();
    SqlFieldsQuery curQry = qry;
    while (curQry != null) {
        QueryParserResult parsed = parser.parse(schemaName, curQry, true);
        metas.addAll(parsed.parametersMeta());
        curQry = parsed.remainingQuery();
    }
    return metas;
}
Also used : JdbcParameterMeta(org.apache.ignite.internal.processors.odbc.jdbc.JdbcParameterMeta) ArrayList(java.util.ArrayList) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 64 with SqlFieldsQuery

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

the class QueryParser method parseNative.

/**
 * Tries to parse sql query text using native parser. Only first (leading) sql command of the multi-statement is
 * actually parsed.
 *
 * @param schemaName Schema name.
 * @param qry which sql text to parse.
 * @param remainingAllowed Whether multiple statements are allowed.
 * @return Command or {@code null} if cannot parse this query.
 */
@SuppressWarnings("IfMayBeConditional")
@Nullable
private QueryParserResult parseNative(String schemaName, SqlFieldsQuery qry, boolean remainingAllowed) {
    String sql = qry.getSql();
    // Heuristic check for fast return.
    if (!INTERNAL_CMD_RE.matcher(sql.trim()).find())
        return null;
    try {
        SqlParser parser = new SqlParser(schemaName, sql);
        SqlCommand nativeCmd = parser.nextCommand();
        assert nativeCmd != null : "Empty query. Parser met end of data";
        if (!(nativeCmd instanceof SqlCreateIndexCommand || nativeCmd instanceof SqlDropIndexCommand || nativeCmd instanceof SqlBeginTransactionCommand || nativeCmd instanceof SqlCommitTransactionCommand || nativeCmd instanceof SqlRollbackTransactionCommand || nativeCmd instanceof SqlBulkLoadCommand || nativeCmd instanceof SqlAlterTableCommand || nativeCmd instanceof SqlSetStreamingCommand || nativeCmd instanceof SqlCreateUserCommand || nativeCmd instanceof SqlAlterUserCommand || nativeCmd instanceof SqlDropUserCommand || nativeCmd instanceof SqlKillQueryCommand || nativeCmd instanceof SqlKillComputeTaskCommand || nativeCmd instanceof SqlKillServiceCommand || nativeCmd instanceof SqlKillTransactionCommand || nativeCmd instanceof SqlKillScanQueryCommand || nativeCmd instanceof SqlKillContinuousQueryCommand || nativeCmd instanceof SqlAnalyzeCommand || nativeCmd instanceof SqlRefreshStatitsicsCommand || nativeCmd instanceof SqlDropStatisticsCommand))
            return null;
        SqlFieldsQuery newQry = cloneFieldsQuery(qry).setSql(parser.lastCommandSql());
        QueryDescriptor newPlanKey = queryDescriptor(schemaName, newQry);
        SqlFieldsQuery remainingQry = null;
        if (!F.isEmpty(parser.remainingSql())) {
            checkRemainingAllowed(remainingAllowed);
            remainingQry = cloneFieldsQuery(qry).setSql(parser.remainingSql()).setArgs(qry.getArgs());
        }
        QueryParserResultCommand cmd = new QueryParserResultCommand(nativeCmd, null, false);
        return new QueryParserResult(newPlanKey, queryParameters(newQry), remainingQry, // Currently none of native statements supports parameters.
        Collections.emptyList(), null, null, cmd);
    } catch (SqlStrictParseException e) {
        throw new IgniteSQLException(e.getMessage(), e.errorCode(), e);
    } catch (Exception e) {
        // Cannot parse, return.
        if (log.isDebugEnabled())
            log.debug("Failed to parse SQL with native parser [qry=" + sql + ", err=" + e + ']');
        if (!IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_SQL_PARSER_DISABLE_H2_FALLBACK))
            return null;
        int code = IgniteQueryErrorCode.PARSING;
        if (e instanceof SqlParseException)
            code = ((SqlParseException) e).code();
        throw new IgniteSQLException("Failed to parse DDL statement: " + sql + ": " + e.getMessage(), code, e);
    }
}
Also used : SqlBulkLoadCommand(org.apache.ignite.internal.sql.command.SqlBulkLoadCommand) SqlStrictParseException(org.apache.ignite.internal.sql.SqlStrictParseException) SqlAlterTableCommand(org.apache.ignite.internal.sql.command.SqlAlterTableCommand) SqlKillContinuousQueryCommand(org.apache.ignite.internal.sql.command.SqlKillContinuousQueryCommand) SqlCommitTransactionCommand(org.apache.ignite.internal.sql.command.SqlCommitTransactionCommand) SqlRollbackTransactionCommand(org.apache.ignite.internal.sql.command.SqlRollbackTransactionCommand) SqlCreateUserCommand(org.apache.ignite.internal.sql.command.SqlCreateUserCommand) SqlKillScanQueryCommand(org.apache.ignite.internal.sql.command.SqlKillScanQueryCommand) SqlAnalyzeCommand(org.apache.ignite.internal.sql.command.SqlAnalyzeCommand) SqlAlterUserCommand(org.apache.ignite.internal.sql.command.SqlAlterUserCommand) SqlCommand(org.apache.ignite.internal.sql.command.SqlCommand) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SqlCreateIndexCommand(org.apache.ignite.internal.sql.command.SqlCreateIndexCommand) SqlDropStatisticsCommand(org.apache.ignite.internal.sql.command.SqlDropStatisticsCommand) SqlParser(org.apache.ignite.internal.sql.SqlParser) SqlKillComputeTaskCommand(org.apache.ignite.internal.sql.command.SqlKillComputeTaskCommand) SqlRefreshStatitsicsCommand(org.apache.ignite.internal.sql.command.SqlRefreshStatitsicsCommand) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlStrictParseException(org.apache.ignite.internal.sql.SqlStrictParseException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlDropIndexCommand(org.apache.ignite.internal.sql.command.SqlDropIndexCommand) SqlKillTransactionCommand(org.apache.ignite.internal.sql.command.SqlKillTransactionCommand) SqlDropUserCommand(org.apache.ignite.internal.sql.command.SqlDropUserCommand) SqlKillServiceCommand(org.apache.ignite.internal.sql.command.SqlKillServiceCommand) SqlKillQueryCommand(org.apache.ignite.internal.sql.command.SqlKillQueryCommand) SqlSetStreamingCommand(org.apache.ignite.internal.sql.command.SqlSetStreamingCommand) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlBeginTransactionCommand(org.apache.ignite.internal.sql.command.SqlBeginTransactionCommand) Nullable(org.jetbrains.annotations.Nullable)

Example 65 with SqlFieldsQuery

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

the class IgniteH2Indexing method updateQueryRows.

/**
 * Calculates rows for update query.
 *
 * @param qryId Query id.
 * @param schemaName Schema name.
 * @param plan Update plan.
 * @param args Statement arguments.
 * @return Rows for update.
 * @throws IgniteCheckedException If failed.
 */
private Iterator<List<?>> updateQueryRows(long qryId, String schemaName, UpdatePlan plan, Object[] args) throws IgniteCheckedException {
    Object[] params = args != null ? args : X.EMPTY_OBJECT_ARRAY;
    if (!F.isEmpty(plan.selectQuery())) {
        SqlFieldsQuery selectQry = new SqlFieldsQuery(plan.selectQuery()).setArgs(params).setLocal(true);
        QueryParserResult selectParseRes = parser.parse(schemaName, selectQry, false);
        GridQueryFieldsResult res = executeSelectLocal(qryId, selectParseRes.queryDescriptor(), selectParseRes.queryParameters(), selectParseRes.select(), null, null, null, false, 0);
        return res.iterator();
    } else
        return plan.createRows(params).iterator();
}
Also used : KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) GridQueryFieldsResult(org.apache.ignite.internal.processors.query.GridQueryFieldsResult)

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