Search in sources :

Example 6 with SqlRow

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

the class IsNullPredicateIntegrationTest method checkColumn.

private void checkColumn(String function, Set<Integer> expectedKeys) {
    String expression = "field1 " + function;
    String sql = "SELECT key, " + expression + " FROM map WHERE " + expression;
    List<SqlRow> rows = execute(sql);
    assertEquals(expectedKeys.size(), rows.size());
    for (SqlRow row : rows) {
        assertEquals(SqlColumnType.BOOLEAN, row.getMetadata().getColumn(1).getType());
        int key = row.getObject(0);
        boolean value = row.getObject(1);
        assertTrue("Key is not returned: " + key, expectedKeys.contains(key));
        assertTrue(value);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow)

Example 7 with SqlRow

use of com.hazelcast.sql.SqlRow 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 SqlRow

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

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

use of com.hazelcast.sql.SqlRow 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

SqlRow (com.hazelcast.sql.SqlRow)65 Test (org.junit.Test)35 SqlResult (com.hazelcast.sql.SqlResult)29 QuickTest (com.hazelcast.test.annotation.QuickTest)25 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)17 SqlRowMetadata (com.hazelcast.sql.SqlRowMetadata)13 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)11 ArrayList (java.util.ArrayList)10 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 SqlStatement (com.hazelcast.sql.SqlStatement)7 SqlService (com.hazelcast.sql.SqlService)5 HashSet (java.util.HashSet)5 SqlColumnType (com.hazelcast.sql.SqlColumnType)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 Config (com.hazelcast.config.Config)2 IndexConfig (com.hazelcast.config.IndexConfig)2 HazelcastException (com.hazelcast.core.HazelcastException)2 ExpressionBiValue (com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue)2 IMap (com.hazelcast.map.IMap)2 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)2