use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.
the class SqlBasicTest method checkRowMetadata.
private void checkRowMetadata(SqlRowMetadata rowMetadata) {
List<String> fields = fields();
List<SqlColumnType> fieldTypes = fieldTypes();
assertEquals(fields.size(), rowMetadata.getColumnCount());
for (int i = 0; i < fields.size(); i++) {
String field = fields.get(i);
String adjustedField = adjustFieldName(field);
SqlColumnType fieldType = fieldTypes.get(i);
int fieldIndex = rowMetadata.findColumn(adjustedField);
assertNotEquals(SqlRowMetadata.COLUMN_NOT_FOUND, fieldIndex);
SqlColumnMetadata columnMetadata = rowMetadata.getColumn(fieldIndex);
assertEquals(adjustedField, columnMetadata.getName());
assertEquals(fieldType, columnMetadata.getType());
assertTrue(columnMetadata.isNullable());
}
assertThrows(IndexOutOfBoundsException.class, () -> rowMetadata.getColumn(-1));
assertThrows(IndexOutOfBoundsException.class, () -> rowMetadata.getColumn(fields.size()));
}
use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.
the class SqlPageCodec method encode.
@SuppressWarnings({ "unchecked", "checkstyle:CyclomaticComplexity", "checkstyle:MethodLength" })
public static void encode(ClientMessage clientMessage, SqlPage sqlPage) {
clientMessage.add(BEGIN_FRAME.copy());
// Write the "last" flag.
byte[] content = new byte[] { (byte) (sqlPage.isLast() ? 1 : 0) };
clientMessage.add(new ClientMessage.Frame(content));
// Write column types.
List<SqlColumnType> columnTypes = sqlPage.getColumnTypes();
List<Integer> columnTypeIds = new ArrayList<>(columnTypes.size());
for (SqlColumnType columnType : columnTypes) {
columnTypeIds.add(columnType.getId());
}
ListIntegerCodec.encode(clientMessage, columnTypeIds);
// Write columns.
for (int i = 0; i < sqlPage.getColumnCount(); i++) {
SqlColumnType columnType = columnTypes.get(i);
Iterable<?> column = sqlPage.getColumnValuesForServer(i);
switch(columnType) {
case VARCHAR:
ListMultiFrameCodec.encodeContainsNullable(clientMessage, (Iterable<String>) column, StringCodec::encode);
break;
case BOOLEAN:
ListCNBooleanCodec.encode(clientMessage, (Iterable<Boolean>) column);
break;
case TINYINT:
ListCNByteCodec.encode(clientMessage, (Iterable<Byte>) column);
break;
case SMALLINT:
ListCNShortCodec.encode(clientMessage, (Iterable<Short>) column);
break;
case INTEGER:
ListCNIntegerCodec.encode(clientMessage, (Iterable<Integer>) column);
break;
case BIGINT:
ListCNLongCodec.encode(clientMessage, (Iterable<Long>) column);
break;
case REAL:
ListCNFloatCodec.encode(clientMessage, (Iterable<Float>) column);
break;
case DOUBLE:
ListCNDoubleCodec.encode(clientMessage, (Iterable<Double>) column);
break;
case DATE:
ListCNLocalDateCodec.encode(clientMessage, (Iterable<LocalDate>) column);
break;
case TIME:
ListCNLocalTimeCodec.encode(clientMessage, (Iterable<LocalTime>) column);
break;
case TIMESTAMP:
ListCNLocalDateTimeCodec.encode(clientMessage, (Iterable<LocalDateTime>) column);
break;
case TIMESTAMP_WITH_TIME_ZONE:
ListCNOffsetDateTimeCodec.encode(clientMessage, (Iterable<OffsetDateTime>) column);
break;
case DECIMAL:
ListMultiFrameCodec.encode(clientMessage, (Iterable<BigDecimal>) column, BigDecimalCodec::encodeNullable);
break;
case NULL:
int size = 0;
for (Object ignore : column) {
size++;
}
byte[] sizeBuffer = new byte[FixedSizeTypesCodec.INT_SIZE_IN_BYTES];
FixedSizeTypesCodec.encodeInt(sizeBuffer, 0, size);
clientMessage.add(new ClientMessage.Frame(sizeBuffer));
break;
case OBJECT:
assert SqlPage.convertToData(columnType);
ListMultiFrameCodec.encode(clientMessage, (Iterable<Data>) column, DataCodec::encodeNullable);
break;
case JSON:
ListMultiFrameCodec.encodeContainsNullable(clientMessage, (Iterable<HazelcastJsonValue>) column, HazelcastJsonValueCodec::encode);
break;
default:
throw new IllegalStateException("Unknown type " + columnType);
}
}
clientMessage.add(END_FRAME.copy());
}
use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.
the class SqlPageCodec method decode.
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:MethodLength" })
public static SqlPage decode(ClientMessage.ForwardFrameIterator iterator) {
// begin frame
iterator.next();
// Read the "last" flag.
boolean isLast = iterator.next().content[0] == 1;
// Read column types.
List<Integer> columnTypeIds = ListIntegerCodec.decode(iterator);
List<SqlColumnType> columnTypes = new ArrayList<>(columnTypeIds.size());
// Read columns.
List<List<?>> columns = new ArrayList<>(columnTypeIds.size());
for (int columnTypeId : columnTypeIds) {
SqlColumnType columnType = SqlColumnType.getById(columnTypeId);
assert columnType != null;
columnTypes.add(columnType);
switch(columnType) {
case VARCHAR:
columns.add(ListMultiFrameCodec.decodeContainsNullable(iterator, StringCodec::decode));
break;
case BOOLEAN:
columns.add(ListCNBooleanCodec.decode(iterator));
break;
case TINYINT:
columns.add(ListCNByteCodec.decode(iterator));
break;
case SMALLINT:
columns.add(ListCNShortCodec.decode(iterator));
break;
case INTEGER:
columns.add(ListCNIntegerCodec.decode(iterator));
break;
case BIGINT:
columns.add(ListCNLongCodec.decode(iterator));
break;
case REAL:
columns.add(ListCNFloatCodec.decode(iterator));
break;
case DOUBLE:
columns.add(ListCNDoubleCodec.decode(iterator));
break;
case DATE:
columns.add(ListCNLocalDateCodec.decode(iterator));
break;
case TIME:
columns.add(ListCNLocalTimeCodec.decode(iterator));
break;
case TIMESTAMP:
columns.add(ListCNLocalDateTimeCodec.decode(iterator));
break;
case TIMESTAMP_WITH_TIME_ZONE:
columns.add(ListCNOffsetDateTimeCodec.decode(iterator));
break;
case DECIMAL:
columns.add(ListMultiFrameCodec.decode(iterator, BigDecimalCodec::decodeNullable));
break;
case NULL:
ClientMessage.Frame frame = iterator.next();
int size = FixedSizeTypesCodec.decodeInt(frame.content, 0);
List<Object> column = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
column.add(null);
}
columns.add(column);
break;
case OBJECT:
assert SqlPage.convertToData(columnType);
columns.add(ListMultiFrameCodec.decode(iterator, DataCodec::decodeNullable));
break;
case JSON:
columns.add(ListMultiFrameCodec.decodeContainsNullable(iterator, HazelcastJsonValueCodec::decode));
break;
default:
throw new IllegalStateException("Unknown type " + columnType);
}
}
fastForwardToEndFrame(iterator);
return SqlPage.fromColumns(columnTypes, columns, isLast);
}
use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.
the class SqlConsole method determineColumnWidths.
private static int[] determineColumnWidths(SqlRowMetadata metadata) {
int colCount = metadata.getColumnCount();
int[] colWidths = new int[colCount];
for (int i = 0; i < colCount; i++) {
SqlColumnMetadata colMetadata = metadata.getColumn(i);
SqlColumnType type = colMetadata.getType();
String colName = colMetadata.getName();
switch(type) {
case BOOLEAN:
colWidths[i] = determineColumnWidth(colName, Constants.BOOLEAN_FORMAT_LENGTH);
break;
case DATE:
colWidths[i] = determineColumnWidth(colName, Constants.DATE_FORMAT_LENGTH);
break;
case TIMESTAMP_WITH_TIME_ZONE:
colWidths[i] = determineColumnWidth(colName, Constants.TIMESTAMP_WITH_TIME_ZONE_FORMAT_LENGTH);
break;
case DECIMAL:
colWidths[i] = determineColumnWidth(colName, Constants.DECIMAL_FORMAT_LENGTH);
break;
case REAL:
colWidths[i] = determineColumnWidth(colName, Constants.REAL_FORMAT_LENGTH);
break;
case DOUBLE:
colWidths[i] = determineColumnWidth(colName, Constants.DOUBLE_FORMAT_LENGTH);
break;
case INTEGER:
colWidths[i] = determineColumnWidth(colName, Constants.INTEGER_FORMAT_LENGTH);
break;
case NULL:
colWidths[i] = determineColumnWidth(colName, Constants.NULL_FORMAT_LENGTH);
break;
case TINYINT:
colWidths[i] = determineColumnWidth(colName, Constants.TINYINT_FORMAT_LENGTH);
break;
case SMALLINT:
colWidths[i] = determineColumnWidth(colName, Constants.SMALLINT_FORMAT_LENGTH);
break;
case TIMESTAMP:
colWidths[i] = determineColumnWidth(colName, Constants.TIMESTAMP_FORMAT_LENGTH);
break;
case BIGINT:
colWidths[i] = determineColumnWidth(colName, Constants.BIGINT_FORMAT_LENGTH);
break;
case VARCHAR:
colWidths[i] = determineColumnWidth(colName, Constants.VARCHAR_FORMAT_LENGTH);
break;
case OBJECT:
colWidths[i] = determineColumnWidth(colName, Constants.OBJECT_FORMAT_LENGTH);
break;
case JSON:
colWidths[i] = determineColumnWidth(colName, Constants.JSON_FORMAT_LENGTH);
break;
default:
throw new UnsupportedOperationException(type.toString());
}
}
return colWidths;
}
use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.
the class ColumnIntegrationTest method testColumn_auto.
@Test
public void testColumn_auto() {
for (ExpressionType<?> type : ExpressionTypes.all()) {
Class<? extends ExpressionValue> clazz = ExpressionValue.createClass(type.typeName());
ExpressionValue value = ExpressionValue.create(clazz, type.valueFrom());
ExpressionValue nullValue = ExpressionValue.create(clazz, null);
SqlColumnType expectedType = type.getFieldConverterType().getTypeFamily().getPublicType();
Object expectedResult = type.getFieldConverterType().getConverter().convertToSelf(type.getFieldConverterType().getConverter(), type.valueFrom());
putAndCheckValue(value, sql("field1"), expectedType, expectedResult);
putAndCheckValue(nullValue, sql("field1"), expectedType, null);
}
}
Aggregations