Search in sources :

Example 51 with SqlRow

use of com.hazelcast.sql.SqlRow in project hazelcast by hazelcast.

the class SqlErrorAbstractTest method checkUserCancel.

@SuppressWarnings("StatementWithEmptyBody")
protected void checkUserCancel(boolean useClient) {
    instance1 = newHazelcastInstance(true);
    client = newClient();
    HazelcastInstance target = useClient ? client : instance1;
    try (SqlResult res = target.getSql().execute("select * from table(generate_stream(1))")) {
        sleepSeconds(1);
        res.close();
        try {
            for (SqlRow ignore : res) {
            // No-op.
            }
            fail("Exception is not thrown");
        } catch (HazelcastSqlException e) {
            assertErrorCode(SqlErrorCode.CANCELLED_BY_USER, e);
        }
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlResult(com.hazelcast.sql.SqlResult) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Example 52 with SqlRow

use of com.hazelcast.sql.SqlRow in project hazelcast by hazelcast.

the class SqlClientResultTest method when_checkingHasNextWithTimeout_then_timeoutOccurs.

@Test
public void when_checkingHasNextWithTimeout_then_timeoutOccurs() {
    try (SqlResult result = execute("select * from table(generate_stream(1))")) {
        assertTrue(result.isRowSet());
        ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
        int timeoutCount = 0;
        for (int i = 0; i < 2; i++) {
            while (iterator.hasNext(10, TimeUnit.MILLISECONDS) == ResultIterator.HasNextResult.TIMEOUT) {
                timeoutCount++;
            }
            iterator.next();
        }
        assertNotEquals(0, timeoutCount);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 53 with SqlRow

use of com.hazelcast.sql.SqlRow in project hazelcast by hazelcast.

the class SqlClientResultTest method when_executingValidQuery.

@Test
public void when_executingValidQuery() {
    try (SqlResult result = execute("SELECT * FROM " + MAP_NAME)) {
        assertEquals(2, result.getRowMetadata().getColumnCount());
        assertEquals(-1, result.updateCount());
        assertTrue(result.isRowSet());
        Iterator<SqlRow> iterator = result.iterator();
        iterator.next();
        iterator.next();
        assertFalse(iterator.hasNext());
        checkIllegalStateException(result::iterator, "Iterator can be requested only once");
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 54 with SqlRow

use of com.hazelcast.sql.SqlRow in project hazelcast by hazelcast.

the class SqlErrorClientTest method testRowError_deserialization.

@Test
public void testRowError_deserialization() {
    try {
        instance1 = newHazelcastInstance(true);
        client = newClient();
        Map<Integer, BadValue> localMap = new HashMap<>();
        IMap<Integer, BadValue> map = instance1.getMap(MAP_NAME);
        createMapping(instance1, MAP_NAME, int.class, BadValue.class);
        for (int i = 0; i < DEFAULT_CURSOR_BUFFER_SIZE + 1; i++) {
            localMap.put(i, new BadValue());
        }
        map.putAll(localMap);
        try (SqlResult result = client.getSql().execute("SELECT __key, this FROM " + MAP_NAME)) {
            Iterator<SqlRow> iterator = result.iterator();
            SqlRow firstRow = iterator.next();
            firstRow.getObject("__key");
            firstRow.getObject("this");
            BadValue.READ_ERROR.set(true);
            SqlRow secondRow = iterator.next();
            secondRow.getObject("__key");
            assertThatThrownBy(() -> secondRow.getObject("this")).isInstanceOf(HazelcastSerializationException.class).hasMessageContaining("Failed to deserialize query result value");
        }
    } finally {
        BadValue.READ_ERROR.set(false);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) HazelcastSerializationException(com.hazelcast.nio.serialization.HazelcastSerializationException) SqlResult(com.hazelcast.sql.SqlResult) HashMap(java.util.HashMap) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with SqlRow

use of com.hazelcast.sql.SqlRow in project hazelcast by hazelcast.

the class QueryClientStateRegistry method fetchInternal.

private SqlPage fetchInternal(QueryClientState clientCursor, int cursorBufferSize, InternalSerializationService serializationService, boolean respondImmediately) {
    List<SqlColumnMetadata> columns = clientCursor.getSqlResult().getRowMetadata().getColumns();
    List<SqlColumnType> columnTypes = new ArrayList<>(columns.size());
    for (SqlColumnMetadata column : columns) {
        columnTypes.add(column.getType());
    }
    if (respondImmediately) {
        return SqlPage.fromRows(columnTypes, Collections.emptyList(), false, serializationService);
    }
    ResultIterator<SqlRow> iterator = clientCursor.getIterator();
    try {
        List<SqlRow> rows = new ArrayList<>(cursorBufferSize);
        boolean last = fetchPage(iterator, rows, cursorBufferSize);
        return SqlPage.fromRows(columnTypes, rows, last, serializationService);
    } catch (HazelcastSqlException e) {
        // it happens, the cursor is already closed with the error, so we just re-throw.
        throw e;
    } catch (Exception e) {
        // Any other exception indicates that something has happened outside of the internal query state. For example,
        // we may fail to serialize a specific column value to Data. We have to close the cursor in this case.
        AbstractSqlResult result = clientCursor.getSqlResult();
        QueryException error = QueryException.error("Failed to prepare the SQL result for the client: " + e.getMessage(), e);
        result.close(error);
        throw error;
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) ArrayList(java.util.ArrayList) SqlColumnType(com.hazelcast.sql.SqlColumnType) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) QueryException(com.hazelcast.sql.impl.QueryException) QueryException(com.hazelcast.sql.impl.QueryException) AbstractSqlResult(com.hazelcast.sql.impl.AbstractSqlResult) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Aggregations

SqlRow (com.hazelcast.sql.SqlRow)65 Test (org.junit.Test)35 SqlResult (com.hazelcast.sql.SqlResult)29 QuickTest (com.hazelcast.test.annotation.QuickTest)25 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)17 SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)13 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)11 ArrayList (java.util.ArrayList)10 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 SqlStatement (com.hazelcast.sql.SqlStatement)7 SqlService (com.hazelcast.sql.SqlService)5 HashSet (java.util.HashSet)5 SqlColumnType (com.hazelcast.sql.SqlColumnType)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 Config (com.hazelcast.config.Config)2 IndexConfig (com.hazelcast.config.IndexConfig)2 HazelcastException (com.hazelcast.core.HazelcastException)2 ExpressionBiValue (com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue)2 IMap (com.hazelcast.map.IMap)2 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)2