Search in sources :

Example 6 with SqlResult

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

the class SqlMetadataInJobConfigTest method test_selectMetadata_clientJobSummary.

@Test
public void test_selectMetadata_clientJobSummary() {
    String sql = "SELECT * FROM table(generate_stream(1))";
    try (SqlResult ignored = client().getSql().execute(new SqlStatement(sql).setCursorBufferSize(1))) {
        List<JobSummary> jobSummaries = ((JetClientInstanceImpl) client().getJet()).getJobSummaryList().stream().filter(jobSummary -> jobSummary.getStatus() == RUNNING).collect(Collectors.toList());
        assertEquals(1, jobSummaries.size());
        JobSummary jobSummary = jobSummaries.get(0);
    // TODO uncomment this when doing https://github.com/hazelcast/hazelcast/issues/20372
    // assertNotNull(jobSummary.getSqlSummary());
    // assertEquals(sql, jobSummary.getSqlSummary().getQuery());
    // assertEquals(Boolean.TRUE, jobSummary.getSqlSummary().isUnbounded());
    }
}
Also used : JobSummary(com.hazelcast.jet.impl.JobSummary) JobSummary(com.hazelcast.jet.impl.JobSummary) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) BeforeClass(org.junit.BeforeClass) QuickTest(com.hazelcast.test.annotation.QuickTest) JobConfig(com.hazelcast.jet.config.JobConfig) SqlStatement(com.hazelcast.sql.SqlStatement) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) TestBatchSqlConnector(com.hazelcast.jet.sql.impl.connector.test.TestBatchSqlConnector) Collectors(java.util.stream.Collectors) List(java.util.List) Future(java.util.concurrent.Future) Ignore(org.junit.Ignore) KEY_SQL_UNBOUNDED(com.hazelcast.jet.config.JobConfigArguments.KEY_SQL_UNBOUNDED) SqlResult(com.hazelcast.sql.SqlResult) RUNNING(com.hazelcast.jet.core.JobStatus.RUNNING) JetClientInstanceImpl(com.hazelcast.jet.impl.JetClientInstanceImpl) JobStatus(com.hazelcast.jet.core.JobStatus) COMPLETED(com.hazelcast.jet.core.JobStatus.COMPLETED) KEY_SQL_QUERY_TEXT(com.hazelcast.jet.config.JobConfigArguments.KEY_SQL_QUERY_TEXT) Assert.assertEquals(org.junit.Assert.assertEquals) Job(com.hazelcast.jet.Job) 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 7 with SqlResult

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

the class SqlParameterTest method testParameters.

@Test
public void testParameters() {
    boolean valBoolean = true;
    byte valByte = 1;
    short valShort = 2;
    int valInt = 3;
    long valLong = 4;
    float valFloat = 5;
    double valDouble = 6;
    BigDecimal valDecimal = BigDecimal.valueOf(7);
    String valString = "str";
    LocalDate valLocalDate = LocalDate.now();
    LocalTime valLocalTime = LocalTime.now();
    LocalDateTime valLocalDateTime = LocalDateTime.now();
    OffsetDateTime valOffsetDateTime = OffsetDateTime.now();
    CustomObject valObject = new CustomObject(1);
    SqlStatement statement = new SqlStatement("SELECT " + "CAST(? as BOOLEAN), " + "CAST(? as TINYINT), " + "CAST(? as SMALLINT), " + "CAST(? as INTEGER), " + "CAST(? as BIGINT), " + "CAST(? as REAL), " + "CAST(? as DOUBLE), " + "CAST(? as DECIMAL), " + "CAST(? as VARCHAR), " + "CAST(? as DATE), " + "CAST(? as TIME), " + "CAST(? as TIMESTAMP), " + "CAST(? as TIMESTAMP WITH TIME ZONE), " + "CAST(? as OBJECT), " + "CAST(? as OBJECT) " + "FROM " + MAP_NAME);
    statement.addParameter(valBoolean);
    statement.addParameter(valByte);
    statement.addParameter(valShort);
    statement.addParameter(valInt);
    statement.addParameter(valLong);
    statement.addParameter(valFloat);
    statement.addParameter(valDouble);
    statement.addParameter(valDecimal);
    statement.addParameter(valString);
    statement.addParameter(valLocalDate);
    statement.addParameter(valLocalTime);
    statement.addParameter(valLocalDateTime);
    statement.addParameter(valOffsetDateTime);
    statement.addParameter(valObject);
    statement.addParameter(null);
    try (SqlResult res = targetInstance().getSql().execute(statement)) {
        for (SqlRow row : res) {
            assertEquals(valBoolean, row.getObject(0));
            assertEquals(valByte, (byte) row.getObject(1));
            assertEquals(valShort, (short) row.getObject(2));
            assertEquals(valInt, (int) row.getObject(3));
            assertEquals(valLong, (long) row.getObject(4));
            assertEquals(valFloat, row.getObject(5), 0f);
            assertEquals(valDouble, row.getObject(6), 0d);
            assertEquals(valDecimal, row.getObject(7));
            assertEquals(valString, row.getObject(8));
            assertEquals(valLocalDate, row.getObject(9));
            assertEquals(valLocalTime, row.getObject(10));
            assertEquals(valLocalDateTime, row.getObject(11));
            assertEquals(valOffsetDateTime, row.getObject(12));
            assertEquals(valObject, row.getObject(13));
            assertNull(row.getObject(14));
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) LocalTime(java.time.LocalTime) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) SqlStatement(com.hazelcast.sql.SqlStatement) OffsetDateTime(java.time.OffsetDateTime) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with SqlResult

use of com.hazelcast.sql.SqlResult 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 9 with SqlResult

use of com.hazelcast.sql.SqlResult 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 10 with SqlResult

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

the class SqlBasicTest method testSelect.

@SuppressWarnings("ConstantConditions")
@Test
public void testSelect() {
    if (isPortable()) {
        createMapping(mapName(), PORTABLE_FACTORY_ID, PORTABLE_KEY_CLASS_ID, 0, PORTABLE_FACTORY_ID, PORTABLE_VALUE_CLASS_ID, 0);
    } else {
        createMapping(mapName(), keyClass(), valueClass());
    }
    // Get proper map
    IMap<Object, AbstractPojo> map = getTarget().getMap(mapName());
    // Populate map with values
    Map<Object, AbstractPojo> data = new HashMap<>();
    for (long i = 0; i < dataSetSize; i++) {
        data.put(key(i), value(i));
    }
    map.putAll(data);
    assertEquals(dataSetSize, map.size());
    // Execute query
    boolean portable = serializationMode == SerializationMode.PORTABLE;
    boolean multiPageClient;
    try (SqlResult res = query()) {
        multiPageClient = memberClientCursors() > 0;
        SqlRowMetadata rowMetadata = res.getRowMetadata();
        checkRowMetadata(rowMetadata);
        Set<Long> uniqueKeys = new HashSet<>();
        Iterator<SqlRow> rowIterator = res.iterator();
        while (rowIterator.hasNext()) {
            SqlRow row = rowIterator.next();
            assertEquals(rowMetadata, res.getRowMetadata());
            Long key0 = row.getObject(rowMetadata.findColumn(adjustFieldName("key")));
            assertNotNull(key0);
            AbstractPojoKey key = key(key0);
            AbstractPojo val = map.get(key);
            checkRowValue(SqlColumnType.BIGINT, key.getKey(), row, "key");
            checkRowValue(SqlColumnType.BOOLEAN, val.isBooleanVal(), row, "booleanVal");
            checkRowValue(SqlColumnType.TINYINT, val.getTinyIntVal(), row, "tinyIntVal");
            checkRowValue(SqlColumnType.SMALLINT, val.getSmallIntVal(), row, "smallIntVal");
            checkRowValue(SqlColumnType.INTEGER, val.getIntVal(), row, "intVal");
            checkRowValue(SqlColumnType.BIGINT, val.getBigIntVal(), row, "bigIntVal");
            checkRowValue(SqlColumnType.REAL, val.getRealVal(), row, "realVal");
            checkRowValue(SqlColumnType.DOUBLE, val.getDoubleVal(), row, "doubleVal");
            if (!portable) {
                checkRowValue(SqlColumnType.DECIMAL, new BigDecimal(val.getDecimalBigIntegerVal()), row, "decimalBigIntegerVal");
            }
            checkRowValue(SqlColumnType.DECIMAL, val.getDecimalVal(), row, "decimalVal");
            checkRowValue(SqlColumnType.VARCHAR, Character.toString(val.getCharVal()), row, "charVal");
            checkRowValue(SqlColumnType.VARCHAR, val.getVarcharVal(), row, "varcharVal");
            checkRowValue(SqlColumnType.DATE, val.getDateVal(), row, "dateVal");
            checkRowValue(SqlColumnType.TIME, val.getTimeVal(), row, "timeVal");
            checkRowValue(SqlColumnType.TIMESTAMP, val.getTimestampVal(), row, "timestampVal");
            if (portable) {
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzOffsetDateTimeVal(), row, "tsTzOffsetDateTimeVal");
                checkRowValue(SqlColumnType.OBJECT, ((PortablePojo) val).getPortableVal(), row, "portableVal");
                checkRowValue(SqlColumnType.VARCHAR, null, row, "nullVal");
            } else {
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, OffsetDateTime.ofInstant(val.getTsTzDateVal().toInstant(), ZoneId.systemDefault()), row, "tsTzDateVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzCalendarVal().toZonedDateTime().toOffsetDateTime(), row, "tsTzCalendarVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, OffsetDateTime.ofInstant(val.getTsTzInstantVal(), ZoneId.systemDefault()), row, "tsTzInstantVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzOffsetDateTimeVal(), row, "tsTzOffsetDateTimeVal");
                checkRowValue(SqlColumnType.TIMESTAMP_WITH_TIME_ZONE, val.getTsTzZonedDateTimeVal().toOffsetDateTime(), row, "tsTzZonedDateTimeVal");
                checkRowValue(SqlColumnType.OBJECT, val.getObjectVal(), row, "objectVal");
                checkRowValue(SqlColumnType.OBJECT, null, row, "nullVal");
            }
            uniqueKeys.add(key0);
            assertThrows(IndexOutOfBoundsException.class, () -> row.getObject(-1));
            assertThrows(IndexOutOfBoundsException.class, () -> row.getObject(row.getMetadata().getColumnCount()));
            assertThrows(NullPointerException.class, () -> row.getObject(null));
            assertThrows(IllegalArgumentException.class, () -> row.getObject("unknown_field"));
        }
        assertThrows(NoSuchElementException.class, rowIterator::next);
        assertThrows(IllegalStateException.class, res::iterator);
        assertEquals(dataSetSize, uniqueKeys.size());
    }
    if (multiPageClient) {
        // If this request spawns multiple pages, then:
        // 1) Ensure that results are cleared when the whole result set is fetched
        // 2) Ensure that results are cleared when the result set is closed in the middle.
        assertEquals(0, memberClientCursors());
        try (SqlResult res = query()) {
            assertEquals(1, memberClientCursors());
            res.close();
            assertEquals(0, memberClientCursors());
        }
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) HashSet(java.util.HashSet) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SqlResult (com.hazelcast.sql.SqlResult)60 Test (org.junit.Test)38 SqlRow (com.hazelcast.sql.SqlRow)31 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)23 QuickTest (com.hazelcast.test.annotation.QuickTest)23 SqlStatement (com.hazelcast.sql.SqlStatement)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 ArrayList (java.util.ArrayList)9 Job (com.hazelcast.jet.Job)8 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)8 SqlService (com.hazelcast.sql.SqlService)8 JobConfig (com.hazelcast.jet.config.JobConfig)7 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)6 HashMap (java.util.HashMap)5 SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)4 List (java.util.List)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 TimeoutException (java.util.concurrent.TimeoutException)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 IndexConfig (com.hazelcast.config.IndexConfig)3