use of com.hazelcast.sql.SqlStatement in project hazelcast by hazelcast.
the class SqlExecuteMessageTask method call.
@Override
protected Object call() {
SqlSecurityContext sqlSecurityContext = prepareSecurityContext();
SqlStatement query = new SqlStatement(parameters.sql);
for (Data param : parameters.parameters) {
query.addParameter(serializationService.toObject(param));
}
query.setSchema(parameters.schema);
query.setTimeoutMillis(parameters.timeoutMillis);
query.setCursorBufferSize(parameters.cursorBufferSize);
query.setExpectedResultType(SqlExpectedResultType.fromId(parameters.expectedResultType));
SqlServiceImpl sqlService = nodeEngine.getSqlService();
boolean skipUpdateStatistics = parameters.isSkipUpdateStatisticsExists && parameters.skipUpdateStatistics;
return sqlService.execute(query, sqlSecurityContext, parameters.queryId, skipUpdateStatistics);
}
use of com.hazelcast.sql.SqlStatement 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);
}
use of com.hazelcast.sql.SqlStatement 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);
}
use of com.hazelcast.sql.SqlStatement 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);
}
use of com.hazelcast.sql.SqlStatement 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");
}
}
}
Aggregations