Search in sources :

Example 6 with SqlStatement

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

the class SqlSchemaPropagationTest method check.

private void check(HazelcastInstance target) {
    // Set the wrapped optimizer to track optimization requests.
    SqlServiceImpl service = (SqlServiceImpl) instance().getSql();
    // Execute the query from the target without schema.
    SqlStatement statement = new SqlStatement("SELECT __key FROM map");
    List<SqlRow> rows = executeStatement(target, statement);
    assertEquals(1, rows.size());
    assertEquals(1, (int) rows.get(0).getObject(0));
    assertEquals(1, service.getPlanCache().size());
    List<List<String>> originalSearchPaths = Iterables.getOnlyElement(extractSearchPaths());
    // Execute the query from the target with schema.
    statement.setSchema(SCHEMA_NAME);
    rows = executeStatement(target, statement);
    assertEquals(1, rows.size());
    assertEquals(1, (int) rows.get(0).getObject(0));
    assertEquals(2, service.getPlanCache().size());
    List<List<List<String>>> searchPaths = extractSearchPaths();
    List<List<String>> expectedSearchPaths = new ArrayList<>(originalSearchPaths);
    expectedSearchPaths.add(0, asList(CATALOG, SCHEMA_NAME));
    assertThat(searchPaths).containsExactlyInAnyOrder(originalSearchPaths, expectedSearchPaths);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlStatement(com.hazelcast.sql.SqlStatement) SqlServiceImpl(com.hazelcast.sql.impl.SqlServiceImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Arrays.asList(java.util.Arrays.asList)

Example 7 with SqlStatement

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

the class SqlTestSupport method assertRowsEventuallyInAnyOrder.

/**
 * Execute a query and wait for the results to contain all the {@code
 * expectedRows}. Suitable for streaming queries that don't terminate, but
 * return a deterministic set of rows. Rows can arrive in any order.
 * <p>
 * After all expected rows are received, the method further waits a little
 * more if any extra rows are received, and fails, if they are.
 *
 * @param sql          The query
 * @param arguments    The query arguments
 * @param expectedRows Expected rows
 */
public static void assertRowsEventuallyInAnyOrder(String sql, List<Object> arguments, Collection<Row> expectedRows) {
    SqlService sqlService = instance().getSql();
    CompletableFuture<Void> future = new CompletableFuture<>();
    Deque<Row> rows = new ArrayDeque<>();
    Thread thread = new Thread(() -> {
        SqlStatement statement = new SqlStatement(sql);
        arguments.forEach(statement::addParameter);
        try (SqlResult result = sqlService.execute(statement)) {
            ResultIterator<SqlRow> iterator = (ResultIterator<SqlRow>) result.iterator();
            for (int i = 0; i < expectedRows.size() && iterator.hasNext() || iterator.hasNext(50, TimeUnit.MILLISECONDS) == YES; 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).containsExactlyInAnyOrderElementsOf(expectedRows);
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) SqlResult(com.hazelcast.sql.SqlResult) ResultIterator(com.hazelcast.sql.impl.ResultIterator) 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 8 with SqlStatement

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

the class SqlTestSupport method assertMapEventually.

/**
 * Execute a query and assert that it eventually returns the expected entries.
 *
 * @param mapName   The IMap name
 * @param sql       The query
 * @param arguments The query arguments
 * @param expected  Expected IMap contents after executing the query
 */
public static <K, V> void assertMapEventually(String mapName, String sql, List<Object> arguments, Map<K, V> expected) {
    SqlStatement statement = new SqlStatement(sql);
    statement.setParameters(arguments);
    // noinspection EmptyTryBlock
    try (@SuppressWarnings("unused") SqlResult result = instance().getSql().execute(statement)) {
    }
    Map<K, V> map = instance().getMap(mapName);
    assertTrueEventually(() -> assertThat(new HashMap<>(map)).containsExactlyInAnyOrderEntriesOf(expected), 10);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult)

Example 9 with SqlStatement

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

the class SqlErrorClientTest method testMemberDisconnect_execute.

/**
 * Test proper handling of member disconnect while waiting for execute result.
 */
@Test
public void testMemberDisconnect_execute() {
    instance1 = newHazelcastInstance(true);
    client = newClient();
    SqlStatement streamingQuery = new SqlStatement("SELECT * FROM TABLE(GENERATE_STREAM(5000))");
    HazelcastSqlException error = assertSqlExceptionWithShutdown(client, streamingQuery);
    assertErrorCode(SqlErrorCode.CONNECTION_PROBLEM, error);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with SqlStatement

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

the class SqlErrorClientTest method testParameterError_deserialization.

@Test
public void testParameterError_deserialization() {
    instance1 = newHazelcastInstance(true);
    client = newClient();
    SqlStatement query = new SqlStatement("SELECT * FROM map").addParameter(new BadParameter(false, true));
    HazelcastSqlException error = assertSqlException(client, query);
    assertErrorCode(SqlErrorCode.GENERIC, error);
    assertTrue(error.getMessage().contains("Read error"));
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SqlStatement (com.hazelcast.sql.SqlStatement)29 SqlResult (com.hazelcast.sql.SqlResult)14 Test (org.junit.Test)11 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)9 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 SqlRow (com.hazelcast.sql.SqlRow)8 ArrayList (java.util.ArrayList)6 Job (com.hazelcast.jet.Job)3 JobConfig (com.hazelcast.jet.config.JobConfig)3 SqlService (com.hazelcast.sql.SqlService)3 SqlServiceImpl (com.hazelcast.sql.impl.SqlServiceImpl)3 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)3 HazelcastException (com.hazelcast.core.HazelcastException)2 ArrayDeque (java.util.ArrayDeque)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeoutException (java.util.concurrent.TimeoutException)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 Data (com.hazelcast.internal.serialization.Data)1