Search in sources :

Example 21 with SqlStatement

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);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlServiceImpl(com.hazelcast.sql.impl.SqlServiceImpl) NoOpSqlSecurityContext(com.hazelcast.sql.impl.security.NoOpSqlSecurityContext) SqlSecurityContext(com.hazelcast.sql.impl.security.SqlSecurityContext) Data(com.hazelcast.internal.serialization.Data)

Example 22 with SqlStatement

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);
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) 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 23 with SqlStatement

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);
}
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 24 with SqlStatement

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);
}
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 25 with SqlStatement

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");
        }
    }
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) SqlResult(com.hazelcast.sql.SqlResult) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

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