use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.
the class PlanExecutor method execute.
SqlResult execute(ShowStatementPlan plan) {
Stream<String> rows;
switch(plan.getShowTarget()) {
case MAPPINGS:
rows = catalog.getMappingNames().stream();
break;
case VIEWS:
rows = catalog.getViewNames().stream();
break;
case JOBS:
assert plan.getShowTarget() == ShowStatementTarget.JOBS;
NodeEngine nodeEngine = getNodeEngine(hazelcastInstance);
JetServiceBackend jetServiceBackend = nodeEngine.getService(JetServiceBackend.SERVICE_NAME);
rows = jetServiceBackend.getJobRepository().getJobRecords().stream().map(record -> record.getConfig().getName()).filter(Objects::nonNull);
break;
default:
throw new AssertionError("Unsupported SHOW statement target.");
}
SqlRowMetadata metadata = new SqlRowMetadata(singletonList(new SqlColumnMetadata("name", VARCHAR, false)));
InternalSerializationService serializationService = Util.getSerializationService(hazelcastInstance);
return new SqlResultImpl(QueryId.create(hazelcastInstance.getLocalEndpoint().getUuid()), new StaticQueryResultProducerImpl(rows.sorted().map(name -> new JetSqlRow(serializationService, new Object[] { name })).iterator()), metadata, false);
}
use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.
the class CalciteSqlOptimizer method createRowMetadata.
private SqlRowMetadata createRowMetadata(List<String> columnNames, List<QueryDataType> columnTypes, List<RelDataTypeField> fields) {
assert columnNames.size() == columnTypes.size();
assert columnTypes.size() == fields.size();
List<SqlColumnMetadata> columns = new ArrayList<>(columnNames.size());
for (int i = 0; i < columnNames.size(); i++) {
SqlColumnMetadata column = QueryUtils.getColumnMetadata(columnNames.get(i), columnTypes.get(i), fields.get(i).getType().isNullable());
columns.add(column);
}
return new SqlRowMetadata(columns);
}
use of com.hazelcast.sql.SqlRowMetadata 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());
}
}
}
use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.
the class SqlOrderByTest method assertSqlResultOrdered.
private void assertSqlResultOrdered(String sql, List<String> orderFields, List<Boolean> orderDirections, int expectedCount, Integer low, Integer high) {
try (SqlResult res = query(sql)) {
SqlRowMetadata rowMetadata = res.getRowMetadata();
Iterator<SqlRow> rowIterator = res.iterator();
SqlRow prevRow = null;
SqlRow lowRow = null;
SqlRow highRow = null;
int count = 0;
while (rowIterator.hasNext()) {
SqlRow row = rowIterator.next();
assertOrdered(prevRow, row, orderFields, orderDirections, rowMetadata);
prevRow = row;
count++;
if (count == 1) {
lowRow = row;
}
if (!rowIterator.hasNext()) {
highRow = row;
}
}
assertEquals(expectedCount, count);
if (lowRow != null && low != null) {
String fieldName = orderFields.get(0);
Object fieldValue = lowRow.getObject(rowMetadata.findColumn(fieldName));
assertEquals(low, fieldValue);
}
if (highRow != null && high != null) {
String fieldName = orderFields.get(0);
Object fieldValue = highRow.getObject(rowMetadata.findColumn(fieldName));
assertEquals(high, fieldValue);
}
assertThrows(NoSuchElementException.class, rowIterator::next);
assertThrows(IllegalStateException.class, res::iterator);
}
}
use of com.hazelcast.sql.SqlRowMetadata in project hazelcast by hazelcast.
the class SqlInsertWindowBoundsTest method test_windowBoundsSameTypeAsDescriptor_INT.
@Test
public void test_windowBoundsSameTypeAsDescriptor_INT() {
String name = randomName();
TestStreamSqlConnector.create(sqlService, name, asList("ts", "name"), asList(INTEGER, VARCHAR), row(1, "Alice"), row(10, null));
String interval = "1";
final SqlRow row = instance().getSql().execute(sql(name, interval)).iterator().next();
final SqlRowMetadata metadata = row.getMetadata();
assertThat(metadata.getColumn(0).getType()).isEqualTo(SqlColumnType.INTEGER);
assertThat((Object) row.getObject(0)).isInstanceOf(SqlColumnType.INTEGER.getValueClass());
assertThat(metadata.getColumn(1).getType()).isEqualTo(SqlColumnType.INTEGER);
assertThat((Object) row.getObject(1)).isInstanceOf(SqlColumnType.INTEGER.getValueClass());
assertThat(metadata.getColumn(2).getType()).isEqualTo(SqlColumnType.INTEGER);
assertThat((Object) row.getObject(2)).isInstanceOf(SqlColumnType.INTEGER.getValueClass());
}
Aggregations