Search in sources :

Example 11 with SqlService

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

the class SqlTestSupport method assertRowsAnyOrder.

/**
 * Execute a query and wait until it completes. Assert that the returned
 * rows contain the expected rows, in any order.
 *
 * @param instance     Hazelcast Instance to be used
 * @param sql          The query
 * @param arguments    The query arguments
 * @param expectedRows Expected rows
 */
public static void assertRowsAnyOrder(HazelcastInstance instance, String sql, List<Object> arguments, Collection<Row> expectedRows) {
    SqlStatement statement = new SqlStatement(sql);
    arguments.forEach(statement::addParameter);
    SqlService sqlService = instance.getSql();
    List<Row> actualRows = new ArrayList<>();
    try (SqlResult result = sqlService.execute(statement)) {
        result.iterator().forEachRemaining(row -> actualRows.add(new Row(row)));
    }
    assertThat(actualRows).containsExactlyInAnyOrderElementsOf(expectedRows);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) SqlService(com.hazelcast.sql.SqlService) ArrayList(java.util.ArrayList) SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow)

Example 12 with SqlService

use of com.hazelcast.sql.SqlService 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)

Aggregations

SqlService (com.hazelcast.sql.SqlService)12 SqlResult (com.hazelcast.sql.SqlResult)8 SqlRow (com.hazelcast.sql.SqlRow)8 Test (org.junit.Test)7 HazelcastInstance (com.hazelcast.core.HazelcastInstance)6 Job (com.hazelcast.jet.Job)4 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)4 ArrayList (java.util.ArrayList)4 SqlStatement (com.hazelcast.sql.SqlStatement)3 HazelcastException (com.hazelcast.core.HazelcastException)2 ArrayDeque (java.util.ArrayDeque)2 BitSet (java.util.BitSet)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 Util.idToString (com.hazelcast.jet.Util.idToString)1 FAILED (com.hazelcast.jet.core.JobStatus.FAILED)1 JetServiceBackend (com.hazelcast.jet.impl.JetServiceBackend)1 ExecutionContext (com.hazelcast.jet.impl.execution.ExecutionContext)1 TestBatchSqlConnector (com.hazelcast.jet.sql.impl.connector.test.TestBatchSqlConnector)1 TestFailingSqlConnector (com.hazelcast.jet.sql.impl.connector.test.TestFailingSqlConnector)1