Search in sources :

Example 6 with SqlService

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

the class SqlTestSupport method createIndex.

static void createIndex(HazelcastInstance instance, String name, String mapName, IndexType type, String... attributes) {
    SqlService sqlService = instance.getSql();
    StringBuilder sb = new StringBuilder("CREATE INDEX IF NOT EXISTS ");
    sb.append(name);
    sb.append(" ON ");
    sb.append(mapName);
    sb.append(" ( ");
    for (int i = 0; i < attributes.length; ++i) {
        if (attributes.length - i - 1 == 0) {
            sb.append(attributes[i]);
        } else {
            sb.append(attributes[i]).append(", ");
        }
    }
    sb.append(" ) ");
    sb.append(" TYPE ");
    sb.append(type.name());
    sqlService.execute(sb.toString());
}
Also used : SqlService(com.hazelcast.sql.SqlService)

Example 7 with SqlService

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

the class SqlTestSupport method assertRowsOrdered.

/**
 * Execute a query and wait until it completes. Assert that the returned
 * rows contain the expected rows, in the given order.
 *
 * @param sql          The query
 * @param expectedRows Expected rows
 */
public static void assertRowsOrdered(String sql, List<Row> expectedRows) {
    SqlService sqlService = instance().getSql();
    List<Row> actualRows = new ArrayList<>();
    try (SqlResult result = sqlService.execute(sql)) {
        result.iterator().forEachRemaining(row -> actualRows.add(new Row(row)));
    }
    assertThat(actualRows).containsExactlyElementsOf(expectedRows);
}
Also used : 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 8 with SqlService

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

the class SqlClientTest method test_jetJobReturnRowsToClientFrom.

@Test
public void test_jetJobReturnRowsToClientFrom() {
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sqlService = client.getSql();
    int itemCount = 10_000;
    TestBatchSqlConnector.create(sqlService, "t", itemCount);
    SqlResult result = sqlService.execute("SELECT v FROM t");
    BitSet seenValues = new BitSet(itemCount);
    for (SqlRow r : result) {
        Integer v = r.getObject(0);
        assertFalse("value already seen: " + v, seenValues.get(v));
        seenValues.set(v);
    }
    assertEquals(itemCount, seenValues.cardinality());
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlResult(com.hazelcast.sql.SqlResult) SqlService(com.hazelcast.sql.SqlService) BitSet(java.util.BitSet) Test(org.junit.Test)

Example 9 with SqlService

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

the class SqlClientTest method when_clientDisconnects_then_jobCancelled.

@Test
public void when_clientDisconnects_then_jobCancelled() {
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sqlService = client.getSql();
    sqlService.execute("SELECT * FROM TABLE(GENERATE_STREAM(100))");
    Job job = awaitSingleRunningJob(instance());
    client.shutdown();
    assertJobStatusEventually(job, FAILED);
    assertThatThrownBy(job::join).hasMessageContaining("QueryException: Client cannot be reached");
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlService(com.hazelcast.sql.SqlService) Job(com.hazelcast.jet.Job) Test(org.junit.Test)

Example 10 with SqlService

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

the class SqlClientTest method when_resultClosed_then_executionContextCleanedUp.

// test for https://github.com/hazelcast/hazelcast/issues/19897
@Test
public void when_resultClosed_then_executionContextCleanedUp() {
    HazelcastInstance client = factory().newHazelcastClient();
    SqlService sql = client.getSql();
    IMap<Integer, Integer> map = instance().getMap("map");
    Map<Integer, Integer> tmpMap = new HashMap<>();
    for (int i = 0; i < 100_000; i++) {
        tmpMap.put(i, i);
        if (i % 10_000 == 0) {
            map.putAll(tmpMap);
            tmpMap.clear();
        }
    }
    createMapping("map", Integer.class, Integer.class);
    for (int i = 0; i < 100; i++) {
        SqlResult result = sql.execute("SELECT * FROM map");
        result.close();
    }
    JetServiceBackend jetService = getJetServiceBackend(instance());
    Collection<ExecutionContext> contexts = jetService.getJobExecutionService().getExecutionContexts();
    // Assert that all ExecutionContexts are eventually cleaned up
    // This assert will fail if a network packet arrives after the JobExecutionService#FAILED_EXECUTION_EXPIRY_NS
    // time. Hopefully Jenkins isn't that slow.
    assertTrueEventually(() -> {
        String remainingContexts = contexts.stream().map(c -> idToString(c.executionId())).collect(Collectors.joining(", "));
        assertEquals("remaining execIds: " + remainingContexts, 0, contexts.size());
    }, 5);
    // assert that failedJobs is also cleaned up
    ConcurrentMap<Long, Long> failedJobs = jetService.getJobExecutionService().getFailedJobs();
    assertTrueEventually(() -> assertEquals(0, failedJobs.size()));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) SqlService(com.hazelcast.sql.SqlService) ExecutionContext(com.hazelcast.jet.impl.execution.ExecutionContext) BeforeClass(org.junit.BeforeClass) Collection(java.util.Collection) Test(org.junit.Test) HashMap(java.util.HashMap) TestBatchSqlConnector(com.hazelcast.jet.sql.impl.connector.test.TestBatchSqlConnector) Collectors(java.util.stream.Collectors) ConcurrentMap(java.util.concurrent.ConcurrentMap) TestFailingSqlConnector(com.hazelcast.jet.sql.impl.connector.test.TestFailingSqlConnector) Util.idToString(com.hazelcast.jet.Util.idToString) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) SqlResult(com.hazelcast.sql.SqlResult) FAILED(com.hazelcast.jet.core.JobStatus.FAILED) BitSet(java.util.BitSet) SqlRow(com.hazelcast.sql.SqlRow) Assert.assertEquals(org.junit.Assert.assertEquals) Job(com.hazelcast.jet.Job) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) IMap(com.hazelcast.map.IMap) SqlResult(com.hazelcast.sql.SqlResult) HashMap(java.util.HashMap) Util.idToString(com.hazelcast.jet.Util.idToString) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ExecutionContext(com.hazelcast.jet.impl.execution.ExecutionContext) SqlService(com.hazelcast.sql.SqlService) JetServiceBackend(com.hazelcast.jet.impl.JetServiceBackend) Test(org.junit.Test)

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