Search in sources :

Example 1 with DecimalType

use of com.pingcap.tikv.types.DecimalType in project tispark by pingcap.

the class TableCodecV2Test method testNewRowTypes.

@Test
public void testNewRowTypes() {
    // TODO: Enable JSON and Set type encoding
    MetaUtils.TableBuilder tableBuilder = MetaUtils.TableBuilder.newBuilder().name("t").addColumn("c1", IntegerType.BIGINT, 1).addColumn("c2", IntegerType.SMALLINT, 22).addColumn("c3", RealType.DOUBLE, 3).addColumn("c4", BytesType.BLOB, 24).addColumn("c5", StringType.CHAR, 25).addColumn("c6", TimestampType.TIMESTAMP, 5).addColumn("c7", TimeType.TIME, 16).addColumn("c8", new DecimalType(6, 4), 8).addColumn("c9", IntegerType.YEAR, 12).addColumn("c10", TEST_ENUM_TYPE, 9).addColumn("c12", UninitializedType.NULL, // null
    11).addColumn("c13", UninitializedType.NULL, // null
    2).addColumn("c14", UninitializedType.NULL, // null
    100).addColumn("c15", RealType.FLOAT, 116).addColumn("c17", TEST_BIT_TYPE, 118).addColumn("c18", StringType.VAR_STRING, 119);
    TiTableInfo tbl = tableBuilder.build();
    Timestamp ts = new Timestamp(1320923471000L);
    ts.setNanos(999999000);
    TestCase testCase = TestCase.createNew(// },
    new int[] { 128, 0, 13, 0, 3, 0, 1, 3, 5, 8, 9, 12, 16, 22, 24, 25, 116, 118, 119, 2, 11, 100, 1, 0, 9, 0, 17, 0, 22, 0, 23, 0, 25, 0, 33, 0, 34, 0, 37, 0, 39, 0, 47, 0, 51, 0, 51, 0, 1, 192, 0, 0, 0, 0, 0, 0, 0, 63, 66, 15, 203, 178, 148, 138, 25, 6, 4, 139, 38, 172, 2, 207, 7, 0, 128, 226, 194, 24, 13, 0, 0, 1, 97, 98, 99, 97, 98, 192, 24, 0, 0, 0, 0, 0, 0, 48, 48, 49, 0 }, tbl, new Object[] { 1L, 1L, 2.0, "abc".getBytes(), "ab", ts, 4 * 3600 * 1000000000L, new BigDecimal("11.9900"), 1999L, "n", // "{\"a\":2}",
    null, null, null, 6.0, // "n1",
    new byte[] { 49, 48, 48 }, "" });
    testCase.test();
}
Also used : MetaUtils(com.pingcap.tikv.meta.MetaUtils) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) DecimalType(com.pingcap.tikv.types.DecimalType) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with DecimalType

use of com.pingcap.tikv.types.DecimalType in project tispark by pingcap.

the class RowEncoderV2 method encodeValue.

private void encodeValue(CodecDataOutput cdo, Object value, DataType tp) {
    switch(tp.getType()) {
        case TypeLonglong:
        case TypeLong:
        case TypeInt24:
        case TypeShort:
        case TypeTiny:
            // TODO: encode consider unsigned
            encodeInt(cdo, (long) value);
            break;
        case TypeFloat:
        case TypeDouble:
            if (value instanceof Double) {
                encodeDouble(cdo, value);
            } else if (value instanceof Float) {
                encodeFloat(cdo, value);
            } else {
                throw new TypeException("type does not match in encoding, should be float/double");
            }
            break;
        case TypeString:
        case TypeVarString:
        case TypeVarchar:
        case TypeBlob:
        case TypeTinyBlob:
        case TypeMediumBlob:
        case TypeLongBlob:
            encodeString(cdo, value);
            break;
        case TypeNewDecimal:
            DecimalType decimalType = (DecimalType) tp;
            encodeDecimal(cdo, value, (int) decimalType.getLength(), decimalType.getDecimal());
            break;
        case TypeBit:
            encodeBit(cdo, value);
            break;
        case TypeTimestamp:
            encodeTimestamp(cdo, value, DateTimeZone.UTC);
            break;
        case TypeDate:
        case TypeDatetime:
            encodeTimestamp(cdo, value, Converter.getLocalTimezone());
            break;
        case TypeDuration:
        case TypeYear:
            encodeInt(cdo, (long) value);
            break;
        case TypeEnum:
            encodeEnum(cdo, value, tp.getElems());
            break;
        case TypeSet:
            encodeSet(cdo, value, tp.getElems());
            break;
        case TypeJSON:
            encodeJson(cdo, value);
            break;
        case TypeNull:
        // ??
        case TypeDecimal:
        case TypeGeometry:
        case TypeNewDate:
            throw new CodecException("type should not appear in encoding");
        default:
            throw new CodecException("invalid data type: " + tp.getType().name());
    }
}
Also used : DecimalType(com.pingcap.tikv.types.DecimalType) TypeException(com.pingcap.tikv.exception.TypeException) CodecException(com.pingcap.tikv.exception.CodecException)

Aggregations

DecimalType (com.pingcap.tikv.types.DecimalType)2 CodecException (com.pingcap.tikv.exception.CodecException)1 TypeException (com.pingcap.tikv.exception.TypeException)1 MetaUtils (com.pingcap.tikv.meta.MetaUtils)1 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)1 BigDecimal (java.math.BigDecimal)1 Timestamp (java.sql.Timestamp)1 Test (org.junit.Test)1