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