Search in sources :

Example 51 with SqlResult

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

the class SqlTestSupport method assertEmptyResultStream.

/**
 * Runs a streaming query and checks that for a hard-coded time it
 * doesn't return any results.
 */
public static void assertEmptyResultStream(String sql) {
    Future<Boolean> future;
    try (SqlResult result = instance().getSql().execute(sql)) {
        future = spawn(() -> result.iterator().hasNext());
        assertTrueAllTheTime(() -> assertFalse(future.isDone()), 2);
    }
    assertTrueEventually(() -> assertTrue(future.isDone()));
}
Also used : SqlResult(com.hazelcast.sql.SqlResult)

Example 52 with SqlResult

use of com.hazelcast.sql.SqlResult 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 53 with SqlResult

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

the class ExplainStatementTest method test_explainStatementDynamicParams.

@Test
public void test_explainStatementDynamicParams() {
    IMap<Integer, Integer> map = instance().getMap("map");
    map.put(1, 10);
    createMapping("map", Integer.class, Integer.class);
    String sql = "EXPLAIN SELECT * FROM map WHERE __key = ?";
    SqlStatement statement = new SqlStatement(sql).addParameter(1);
    SqlResult result = instance().getSql().execute(statement);
    String expectedScanRes = "SelectByKeyMapPhysicalRel(table=[[hazelcast, public, map[projects=[$0, $1], " + "filter==($0, ?0)]]], keyCondition=[?0], projections=[__key=[$0], this=[$1]])";
    assertEquals(result.iterator().next().getObject(0), expectedScanRes);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 54 with SqlResult

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

the class PlanExecutorTest method test_insertExecution.

@Test
public void test_insertExecution() {
    // given
    QueryId queryId = QueryId.create(UuidUtil.newSecureUUID());
    DmlPlan plan = new DmlPlan(Operation.INSERT, planKey(), QueryParameterMetadata.EMPTY, emptySet(), dag, null, false, planExecutor, Collections.emptyList());
    given(hazelcastInstance.getJet()).willReturn(jet);
    given(jet.newLightJob(eq(dag), isA(JobConfig.class))).willReturn(job);
    // when
    SqlResult result = planExecutor.execute(plan, queryId, emptyList(), 0L);
    // then
    assertThat(result.updateCount()).isEqualTo(0);
    verify(job).join();
}
Also used : DmlPlan(com.hazelcast.jet.sql.impl.SqlPlanImpl.DmlPlan) SqlResult(com.hazelcast.sql.SqlResult) QueryId(com.hazelcast.sql.impl.QueryId) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test)

Example 55 with SqlResult

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

the class SqlQueryResultTest method checkFailure.

private void checkFailure(HazelcastInstance target, String sql, SqlExpectedResultType type) {
    assert type == SqlExpectedResultType.ROWS || type == SqlExpectedResultType.UPDATE_COUNT : type;
    try (SqlResult result = target.getSql().execute(new SqlStatement(sql).setExpectedResultType(type))) {
        result.iterator().forEachRemaining(row -> {
        });
        fail("Must fail");
    } catch (HazelcastSqlException e) {
        String message = e.getMessage();
        if (type == SqlExpectedResultType.ROWS) {
            assertEquals(message, "The statement doesn't produce rows");
        } else {
            assertEquals(message, "The statement doesn't produce update count");
        }
    }
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Aggregations

SqlResult (com.hazelcast.sql.SqlResult)60 Test (org.junit.Test)38 SqlRow (com.hazelcast.sql.SqlRow)31 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)23 QuickTest (com.hazelcast.test.annotation.QuickTest)23 SqlStatement (com.hazelcast.sql.SqlStatement)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 ArrayList (java.util.ArrayList)9 Job (com.hazelcast.jet.Job)8 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)8 SqlService (com.hazelcast.sql.SqlService)8 JobConfig (com.hazelcast.jet.config.JobConfig)7 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)6 HashMap (java.util.HashMap)5 SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 TimeoutException (java.util.concurrent.TimeoutException)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 IndexConfig (com.hazelcast.config.IndexConfig)3