Search in sources :

Example 56 with SqlRow

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

the class SqlTestSupport method assertTipOfStream.

/**
 * Execute the given `sql` and assert that the first rows it returns are those in
 * `expectedRows`. Ignores the rest of rows.
 * <p>
 * This is useful for asserting initial output of streaming queries where
 * the output arrives in a stable order.
 */
public static void assertTipOfStream(String sql, Collection<Row> expectedRows) {
    assert !expectedRows.isEmpty() : "no point in asserting a zero-length tip of a stream";
    SqlService sqlService = instance().getSql();
    CompletableFuture<Void> future = new CompletableFuture<>();
    Deque<Row> rows = new ArrayDeque<>();
    Thread thread = new Thread(() -> {
        SqlStatement statement = new SqlStatement(sql);
        try (SqlResult result = sqlService.execute(statement)) {
            Iterator<SqlRow> iterator = result.iterator();
            for (int i = 0; i < expectedRows.size() && iterator.hasNext(); i++) {
                rows.add(new Row(iterator.next()));
            }
            future.complete(null);
        } catch (Throwable e) {
            e.printStackTrace();
            future.completeExceptionally(e);
        }
    });
    thread.start();
    try {
        try {
            future.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            thread.interrupt();
            thread.join();
        }
    } catch (Exception e) {
        throw sneakyThrow(e);
    }
    List<Row> actualRows = new ArrayList<>(rows);
    assertThat(actualRows).containsExactlyElementsOf(expectedRows);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) SqlResult(com.hazelcast.sql.SqlResult) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) TimeoutException(java.util.concurrent.TimeoutException) HazelcastException(com.hazelcast.core.HazelcastException) SqlStatement(com.hazelcast.sql.SqlStatement) CompletableFuture(java.util.concurrent.CompletableFuture) SqlService(com.hazelcast.sql.SqlService) SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) TimeoutException(java.util.concurrent.TimeoutException)

Example 57 with SqlRow

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

the class SelectWithoutFromStatementTest method when_multipleColumnsSelected_returnCorrectResult.

@Test
public void when_multipleColumnsSelected_returnCorrectResult() {
    final SqlRow row = execute("SELECT 1 as A, 'B' as B");
    assertEquals(SqlColumnType.TINYINT, row.getMetadata().getColumn(0).getType());
    assertEquals(SqlColumnType.VARCHAR, row.getMetadata().getColumn(1).getType());
    assertEquals((byte) 1, (byte) row.getObject("A"));
    assertEquals("B", row.getObject("B"));
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) Test(org.junit.Test)

Example 58 with SqlRow

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

the class SelectWithoutFromStatementTest method checkWithType.

private void checkWithType(String sql, SqlColumnType type, Object expected) {
    final SqlRow row = execute(sql);
    assertEquals(1, row.getMetadata().getColumnCount());
    assertEquals(type, row.getMetadata().getColumn(0).getType());
    assertEquals(expected, row.getObject(0));
}
Also used : SqlRow(com.hazelcast.sql.SqlRow)

Example 59 with SqlRow

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

the class SqlQueryResultTest method check.

private void check(boolean client) {
    HazelcastInstance member = instance();
    HazelcastInstance target = client ? client() : instance();
    createMapping(MAP_NAME, int.class, int.class);
    member.getMap(MAP_NAME).put(1, 1);
    // Check rows
    List<SqlRow> expectedRows = execute(SQL_READ);
    assertEquals(1, expectedRows.size());
    checkSuccess(target, SQL_READ, SqlExpectedResultType.ANY, expectedRows, -1);
    checkSuccess(target, SQL_READ, SqlExpectedResultType.ROWS, expectedRows, -1);
    checkFailure(target, SQL_READ, SqlExpectedResultType.UPDATE_COUNT);
    // Check update count
    // TODO: implement updateCount for DML and single key plans.
    checkSuccess(target, SQL_DELETE, SqlExpectedResultType.ANY, emptyList(), 0);
    checkFailure(target, SQL_DELETE, SqlExpectedResultType.ROWS);
    checkSuccess(target, SQL_DELETE, SqlExpectedResultType.UPDATE_COUNT, emptyList(), 0);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) HazelcastInstance(com.hazelcast.core.HazelcastInstance)

Example 60 with SqlRow

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

the class SqlQueryResultTest method checkSuccess.

private void checkSuccess(HazelcastInstance target, String sql, SqlExpectedResultType type, List<SqlRow> expectedRows, int expectedUpdateCount) {
    SqlResult result = target.getSql().execute(new SqlStatement(sql).setExpectedResultType(type));
    assertEquals(expectedUpdateCount, result.updateCount());
    if (expectedUpdateCount >= 0) {
        return;
    }
    List<SqlRow> rows = new ArrayList<>();
    for (SqlRow row : result) {
        rows.add(row);
    }
    assertEquals(expectedRows.size(), rows.size());
    for (int i = 0; i < expectedRows.size(); i++) {
        SqlRow expectedRow = expectedRows.get(i);
        SqlRow row = rows.get(i);
        assertEquals(expectedRow.getMetadata(), row.getMetadata());
        for (int j = 0; j < expectedRow.getMetadata().getColumnCount(); j++) {
            Object expectedValue = expectedRow.getObject(j);
            Object value = row.getObject(j);
            assertEquals(expectedValue, value);
        }
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) ArrayList(java.util.ArrayList)

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