Search in sources :

Example 26 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class WarningOnBigQueryResultsTest method testThinClient.

/**
 */
@Test
public void testThinClient() throws Exception {
    try (IgniteClient cli = Ignition.startClient(new ClientConfiguration().setAddresses(THIN_CLI_ADDR))) {
        assertEquals(KEYS_PER_NODE * 2, cli.query(new SqlFieldsQueryEx("SELECT * FROM TEST0", true).setSchema("TEST0")).getAll().size());
        checkStateAfterQuery0("TEST0");
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Test(org.junit.Test)

Example 27 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class JdbcBatchUpdateTask method doSingleUpdate.

/**
 * Performs update.
 *
 * @param cache Cache.
 * @param sqlText SQL text.
 * @param args Parameters.
 * @return Update counter.
 * @throws SQLException If failed.
 */
private Integer doSingleUpdate(IgniteCache<?, ?> cache, String sqlText, List<Object> args) throws SQLException {
    SqlFieldsQuery qry = new SqlFieldsQueryEx(sqlText, false);
    qry.setPageSize(fetchSize);
    qry.setLocal(locQry);
    qry.setCollocated(collocatedQry);
    qry.setDistributedJoins(distributedJoins);
    qry.setSchema(schemaName);
    qry.setArgs(args == null ? null : args.toArray());
    QueryCursorImpl<List<?>> qryCursor = (QueryCursorImpl<List<?>>) cache.withKeepBinary().query(qry);
    if (qryCursor.isQuery()) {
        throw createJdbcSqlException(getError("Query produced result set", qry), IgniteQueryErrorCode.STMT_TYPE_MISMATCH);
    }
    List<List<?>> rows = qryCursor.getAll();
    if (F.isEmpty(rows))
        return SUCCESS_NO_INFO;
    if (rows.size() != 1)
        throw new SQLException(getError("Expected single row for update operation result", qry));
    List<?> row = rows.get(0);
    if (F.isEmpty(row) || row.size() != 1)
        throw new SQLException(getError("Expected row size of 1 for update operation", qry));
    Object objRes = row.get(0);
    if (!(objRes instanceof Long))
        throw new SQLException(getError("Unexpected update result type", qry));
    Long longRes = (Long) objRes;
    if (longRes > Integer.MAX_VALUE) {
        IgniteLogger log = ignite.log();
        if (log != null)
            log.warning(getError("Query updated row counter (" + longRes + ") exceeds integer range", qry));
        return Integer.MAX_VALUE;
    }
    return longRes.intValue();
}
Also used : SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) List(java.util.List) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) IgniteLogger(org.apache.ignite.IgniteLogger) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 28 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class JdbcQueryMultipleStatementsTask method call.

/**
 * {@inheritDoc}
 */
@Override
public List<JdbcStatementResultInfo> call() throws Exception {
    SqlFieldsQuery qry = (isQry != null ? new SqlFieldsQueryEx(sql, isQry) : new SqlFieldsQuery(sql)).setArgs(args);
    qry.setPageSize(fetchSize);
    qry.setLocal(locQry);
    qry.setCollocated(collocatedQry);
    qry.setDistributedJoins(distributedJoins);
    qry.setEnforceJoinOrder(enforceJoinOrder);
    qry.setLazy(lazy);
    qry.setSchema(schemaName);
    if (!F.isEmpty(queryInitiatorId()))
        qry.setQueryInitiatorId(queryInitiatorId());
    GridKernalContext ctx = ((IgniteKernal) ignite).context();
    List<FieldsQueryCursor<List<?>>> curs = ctx.query().querySqlFields(qry, true, !allowMultipleStatements());
    List<JdbcStatementResultInfo> resultsInfo = new ArrayList<>(curs.size());
    for (FieldsQueryCursor<List<?>> cur0 : curs) {
        if (cur0 instanceof BulkLoadContextCursor) {
            curs.forEach(QueryCursor::close);
            throw new SQLException("COPY command is currently supported only in thin JDBC driver.");
        }
        QueryCursorImpl<List<?>> cur = (QueryCursorImpl<List<?>>) cur0;
        long updCnt = -1;
        UUID qryId = null;
        if (!cur.isQuery()) {
            List<List<?>> items = cur.getAll();
            assert items != null && items.size() == 1 && items.get(0).size() == 1 && items.get(0).get(0) instanceof Long : "Invalid result set for not-SELECT query. [qry=" + sql + ", res=" + S.toString(List.class, items) + ']';
            updCnt = (Long) items.get(0).get(0);
            cur.close();
        } else {
            qryId = UUID.randomUUID();
            JdbcQueryTask.Cursor jdbcCur = new JdbcQueryTask.Cursor(cur, cur.iterator());
            JdbcQueryTask.addCursor(qryId, jdbcCur);
            if (!loc)
                JdbcQueryTask.scheduleRemoval(qryId);
        }
        JdbcStatementResultInfo resInfo = new JdbcStatementResultInfo(cur.isQuery(), qryId, updCnt);
        resultsInfo.add(resInfo);
    }
    return resultsInfo;
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) BulkLoadContextCursor(org.apache.ignite.cache.query.BulkLoadContextCursor) SQLException(java.sql.SQLException) GridKernalContext(org.apache.ignite.internal.GridKernalContext) ArrayList(java.util.ArrayList) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BulkLoadContextCursor(org.apache.ignite.cache.query.BulkLoadContextCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor)

Example 29 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class JdbcQueryTask method call.

/**
 * {@inheritDoc}
 */
@Override
public JdbcQueryTaskResult call() throws Exception {
    Cursor cursor = CURSORS.get(uuid);
    List<String> tbls = null;
    List<String> cols = null;
    List<String> types = null;
    boolean first;
    if (first = (cursor == null)) {
        IgniteCache<?, ?> cache = ignite.cache(cacheName);
        // Don't create caches on server nodes in order to avoid of data rebalancing.
        boolean start = ignite.configuration().isClientMode();
        if (cache == null && cacheName == null)
            cache = ((IgniteKernal) ignite).context().cache().getOrStartPublicCache(start, !loc && locQry);
        if (cache == null) {
            if (cacheName == null)
                throw new SQLException("Failed to execute query. No suitable caches found.");
            else
                throw new SQLException("Cache not found [cacheName=" + cacheName + ']');
        }
        SqlFieldsQuery qry = (isQry != null ? new SqlFieldsQueryEx(sql, isQry) : new SqlFieldsQuery(sql)).setArgs(args);
        qry.setPageSize(fetchSize);
        qry.setLocal(locQry);
        qry.setCollocated(collocatedQry);
        qry.setDistributedJoins(distributedJoins);
        qry.setEnforceJoinOrder(enforceJoinOrder());
        qry.setLazy(lazy());
        qry.setSchema(schemaName);
        FieldsQueryCursor<List<?>> fldQryCursor = cache.withKeepBinary().query(qry);
        if (fldQryCursor instanceof BulkLoadContextCursor) {
            fldQryCursor.close();
            throw new SQLException("COPY command is currently supported only in thin JDBC driver.");
        }
        QueryCursorImpl<List<?>> qryCursor = (QueryCursorImpl<List<?>>) fldQryCursor;
        if (isQry == null)
            isQry = qryCursor.isQuery();
        CURSORS.put(uuid, cursor = new Cursor(qryCursor, qryCursor.iterator()));
    }
    if (first || updateMetadata()) {
        Collection<GridQueryFieldMetadata> meta = cursor.queryCursor().fieldsMeta();
        tbls = new ArrayList<>(meta.size());
        cols = new ArrayList<>(meta.size());
        types = new ArrayList<>(meta.size());
        for (GridQueryFieldMetadata desc : meta) {
            tbls.add(desc.typeName());
            cols.add(desc.fieldName().toUpperCase());
            types.add(desc.fieldTypeName());
        }
    }
    List<List<?>> rows = new ArrayList<>();
    for (List<?> row : cursor) {
        List<Object> row0 = new ArrayList<>(row.size());
        for (Object val : row) row0.add(val == null || JdbcUtils.isSqlType(val.getClass()) ? val : val.toString());
        rows.add(row0);
        if (// If fetchSize is 0 then unlimited
        rows.size() == fetchSize)
            break;
    }
    boolean finished = !cursor.hasNext();
    if (finished)
        remove(uuid, cursor);
    else if (first) {
        if (!loc)
            scheduleRemoval(uuid);
    } else if (!loc && !CURSORS.replace(uuid, cursor, new Cursor(cursor.cursor, cursor.iter)))
        assert !CURSORS.containsKey(uuid) : "Concurrent cursor modification.";
    assert isQry != null : "Query flag must be set prior to returning result";
    return new JdbcQueryTaskResult(uuid, finished, isQry, rows, cols, tbls, types);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) BulkLoadContextCursor(org.apache.ignite.cache.query.BulkLoadContextCursor) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) BulkLoadContextCursor(org.apache.ignite.cache.query.BulkLoadContextCursor) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with SqlFieldsQueryEx

use of org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx in project ignite by apache.

the class OptimizedMarshallerIndexNameTest method testOptimizedMarshallerIndex.

/**
 * Verifies that BPlusTree are not erroneously shared between tables in the same cache
 * due to IGNITE-6915 bug.
 */
@Test
public void testOptimizedMarshallerIndex() {
    // Put objects of different types into the same cache
    CacheConfiguration<Object, Object> ccfg = cacheConfiguration("PersonEn");
    IgniteCache<Object, Object> cache = grid().getOrCreateCache(ccfg);
    cache.put(UUID.randomUUID(), new Person(TEST_NAME1, 42));
    cache.put(UUID.randomUUID(), new FalsePerson(32, TEST_NAME2));
    // Run query against one particular type
    SqlFieldsQueryEx qry = new SqlFieldsQueryEx("select * from " + QueryUtils.typeName(FalsePerson.class), true);
    // If fix for IGNITE-6915 doesn't work you should see exception like the one below in the log:
    // 
    // org.h2.jdbc.JdbcSQLException: General error: "class org.apache.ignite.IgniteCheckedException:
    // Failed to invoke getter method [type=int, property=name,
    // obj=org.apache.ignite.internal.processors.cache.index.OptimizedMarshallerIndexNameTest$Person@...:
    // org.apache.ignite.internal.processors.cache.index.OptimizedMarshallerIndexNameTest$Person@...,
    // getter=public int org.apache.ignite.internal.processors.cache.index.OptimizedMarshallerIndexNameTest$FalsePerson.getName()]"
    List<List<?>> res = cache.query(qry).getAll();
    assertEquals(1, res.size());
}
Also used : SqlFieldsQueryEx(org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx) List(java.util.List) Test(org.junit.Test)

Aggregations

SqlFieldsQueryEx (org.apache.ignite.internal.processors.cache.query.SqlFieldsQueryEx)42 Test (org.junit.Test)23 AbstractIndexingCommonTest (org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest)17 List (java.util.List)11 ArrayList (java.util.ArrayList)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)8 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)8 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 BatchUpdateException (java.sql.BatchUpdateException)6 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)6 FieldsQueryCursor (org.apache.ignite.cache.query.FieldsQueryCursor)5 SQLException (java.sql.SQLException)4 IgniteException (org.apache.ignite.IgniteException)4 QueryCursorImpl (org.apache.ignite.internal.processors.cache.QueryCursorImpl)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 BulkLoadContextCursor (org.apache.ignite.cache.query.BulkLoadContextCursor)3 QueryCancelledException (org.apache.ignite.cache.query.QueryCancelledException)3 IgniteLogger (org.apache.ignite.IgniteLogger)2 QueryCursor (org.apache.ignite.cache.query.QueryCursor)2 GridKernalContext (org.apache.ignite.internal.GridKernalContext)2