Search in sources :

Example 1 with SqlColumnType

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()));
}
Also used : SqlColumnType(com.hazelcast.sql.SqlColumnType) SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata)

Example 2 with SqlColumnType

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());
}
Also used : LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) SqlColumnType(com.hazelcast.sql.SqlColumnType) LocalDate(java.time.LocalDate) LocalTime(java.time.LocalTime) HazelcastJsonValue(com.hazelcast.core.HazelcastJsonValue) Data(com.hazelcast.internal.serialization.Data) BigDecimal(java.math.BigDecimal) OffsetDateTime(java.time.OffsetDateTime) HazelcastJsonValueCodec(com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec)

Example 3 with SqlColumnType

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);
}
Also used : ArrayList(java.util.ArrayList) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) SqlColumnType(com.hazelcast.sql.SqlColumnType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SqlColumnType

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;
}
Also used : SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) SqlColumnType(com.hazelcast.sql.SqlColumnType)

Example 5 with SqlColumnType

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);
    }
}
Also used : ExpressionValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionValue) SqlColumnType(com.hazelcast.sql.SqlColumnType) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SqlColumnType (com.hazelcast.sql.SqlColumnType)12 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)4 ArrayList (java.util.ArrayList)4 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)3 SqlRow (com.hazelcast.sql.SqlRow)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)2 ExpressionType (com.hazelcast.jet.sql.impl.support.expressions.ExpressionType)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 LocalTime (java.time.LocalTime)2 List (java.util.List)2 HazelcastJsonValueCodec (com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec)1 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)1 Data (com.hazelcast.internal.serialization.Data)1 Tuple2 (com.hazelcast.jet.datamodel.Tuple2)1 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)1 ExpressionTestSupport (com.hazelcast.jet.sql.impl.expression.ExpressionTestSupport)1