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);
}
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);
}
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);
}
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);
}
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"));
}
Aggregations