Search in sources :

Example 1 with NativeType

use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.

the class KvMarshallerImpl method collectObjectStats.

/**
 * Reads object fields and gather statistic.
 *
 * @throws MarshallerException If failed to read object content.
 */
private ObjectStatistic collectObjectStats(Columns cols, Marshaller marsh, Object obj) throws MarshallerException {
    if (obj == null || !cols.hasVarlengthColumns()) {
        return ObjectStatistic.ZERO_VARLEN_STATISTICS;
    }
    int cnt = 0;
    int size = 0;
    for (int i = cols.firstVarlengthColumn(); i < cols.length(); i++) {
        final Object val = marsh.value(obj, i);
        final NativeType colType = cols.column(i).type();
        if (val == null || colType.spec().fixedLength()) {
            continue;
        }
        size += getValueSize(val, colType);
        cnt++;
    }
    return new ObjectStatistic(cnt, size);
}
Also used : NativeType(org.apache.ignite.internal.schema.NativeType)

Example 2 with NativeType

use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.

the class InteropOperationsTest method validateTuple.

/**
 * Test specified tuple.
 *
 * @param id Expected tuple id.
 * @param t Tuple to test.
 * @param nulls If {@code true} - nullable fields will be filled.
 */
private void validateTuple(int id, Tuple t, boolean nulls) {
    long actualId = t.longValue("id");
    assertEquals(id, actualId);
    Tuple expected = createTuple(id, nulls);
    expected.set("id", (long) id);
    for (Column col : SCHEMA.valueColumns().columns()) {
        if (!nulls && col.nullable()) {
            continue;
        }
        String colName = col.name();
        NativeType type = col.type();
        if (NativeTypes.INT8.equals(type)) {
            assertEquals(expected.byteValue(colName), t.byteValue(colName));
        } else if (NativeTypes.INT16.equals(type)) {
            assertEquals(expected.shortValue(colName), t.shortValue(colName));
        } else if (NativeTypes.INT32.equals(type)) {
            assertEquals(expected.intValue(colName), t.intValue(colName));
        } else if (NativeTypes.INT64.equals(type)) {
            assertEquals(expected.longValue(colName), t.longValue(colName));
        } else if (NativeTypes.FLOAT.equals(type)) {
            assertEquals(expected.floatValue(colName), t.floatValue(colName));
        } else if (NativeTypes.DOUBLE.equals(type)) {
            assertEquals(expected.doubleValue(colName), t.doubleValue(colName));
        } else if (NativeTypes.BYTES.equals(type)) {
            assertArrayEquals((byte[]) expected.value(colName), (byte[]) t.value(colName));
        } else if (NativeTypes.STRING.equals(type)) {
            assertEquals(expected.stringValue(colName), t.stringValue(colName));
        } else if (NativeTypes.UUID.equals(type)) {
            assertEquals(expected.uuidValue(colName), t.uuidValue(colName));
        } else if (NativeTypes.DATE.equals(type)) {
            assertEquals(expected.dateValue(colName), t.dateValue(colName));
        } else if (NativeTypes.time().equals(type)) {
            assertEquals(expected.timeValue(colName), t.timeValue(colName));
        } else if (NativeTypes.datetime().equals(type)) {
            assertEquals(expected.datetimeValue(colName), t.datetimeValue(colName));
        } else if (NativeTypes.timestamp().equals(type)) {
            assertEquals(expected.timestampValue(colName), expected.timestampValue(colName));
        } else if (NativeTypes.numberOf(2).equals(type)) {
            assertEquals((BigInteger) expected.value(colName), t.value(colName));
        } else if (NativeTypes.decimalOf(5, 2).equals(type)) {
            assertEquals((BigDecimal) expected.value(colName), t.value(colName));
        } else if (NativeTypes.bitmaskOf(8).equals(type)) {
            assertEquals(expected.bitmaskValue(colName), t.bitmaskValue(colName));
        } else {
            fail("Unable to validate value of type " + type);
        }
    }
    assertTrue(!nulls ^ expected.equals(t), "nulls = " + nulls + ", id = " + id);
}
Also used : Column(org.apache.ignite.internal.schema.Column) NativeType(org.apache.ignite.internal.schema.NativeType) BigInteger(java.math.BigInteger) Tuple(org.apache.ignite.table.Tuple)

Example 3 with NativeType

use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.

the class InteropOperationsTest method createTuple.

/**
 * Create tuple with specified id and nulls fields filled.
 *
 * @param id Id.
 * @param nulls If {@code true} - nullable fields will be filled.
 * @return Tuple with all requested fields.
 */
private Tuple createTuple(int id, boolean nulls) {
    Tuple res = Tuple.create();
    for (Column col : SCHEMA.valueColumns().columns()) {
        if (!nulls && col.nullable()) {
            continue;
        }
        String colName = col.name();
        NativeType type = col.type();
        if (NativeTypes.INT8.equals(type)) {
            res.set(colName, (byte) id);
        } else if (NativeTypes.INT16.equals(type)) {
            res.set(colName, (short) id);
        } else if (NativeTypes.INT32.equals(type)) {
            res.set(colName, id);
        } else if (NativeTypes.INT64.equals(type)) {
            res.set(colName, (long) id);
        } else if (NativeTypes.FLOAT.equals(type)) {
            res.set(colName, (float) id);
        } else if (NativeTypes.DOUBLE.equals(type)) {
            res.set(colName, (double) id);
        } else if (NativeTypes.BYTES.equals(type)) {
            res.set(colName, String.valueOf(id).getBytes(StandardCharsets.UTF_8));
        } else if (NativeTypes.STRING.equals(type)) {
            res.set(colName, String.valueOf(id));
        } else if (NativeTypes.UUID.equals(type)) {
            res.set(colName, new UUID(0L, (long) id));
        } else if (NativeTypes.DATE.equals(type)) {
            res.set(colName, LocalDate.ofYearDay(2021, id));
        } else if (NativeTypes.time().equals(type)) {
            res.set(colName, LocalTime.ofSecondOfDay(id));
        } else if (NativeTypes.datetime().equals(type)) {
            res.set(colName, LocalDateTime.ofEpochSecond(id, 0, ZoneOffset.UTC));
        } else if (NativeTypes.timestamp().equals(type)) {
            res.set(colName, Instant.ofEpochSecond(id));
        } else if (NativeTypes.numberOf(2).equals(type)) {
            res.set(colName, BigInteger.valueOf(id));
        } else if (NativeTypes.decimalOf(5, 2).equals(type)) {
            res.set(colName, BigDecimal.valueOf(id * 100).movePointLeft(2));
        } else if (NativeTypes.bitmaskOf(8).equals(type)) {
            BitSet bitSet = new BitSet();
            bitSet.set(id);
            res.set(colName, bitSet);
        } else {
            fail("Unable to fullfill value of type " + type);
        }
    }
    return res;
}
Also used : Column(org.apache.ignite.internal.schema.Column) BitSet(java.util.BitSet) NativeType(org.apache.ignite.internal.schema.NativeType) UUID(java.util.UUID) Tuple(org.apache.ignite.table.Tuple)

Example 4 with NativeType

use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.

the class KeyValueViewOperationsSimpleSchemaTest method putGetAllTypes.

@Test
public void putGetAllTypes() {
    Random rnd = new Random();
    Long key = 42L;
    List<NativeType> allTypes = List.of(NativeTypes.INT8, NativeTypes.INT16, NativeTypes.INT32, NativeTypes.INT64, NativeTypes.FLOAT, NativeTypes.DOUBLE, NativeTypes.DATE, NativeTypes.UUID, NativeTypes.numberOf(20), NativeTypes.decimalOf(25, 5), NativeTypes.bitmaskOf(22), NativeTypes.time(), NativeTypes.datetime(), NativeTypes.timestamp(), NativeTypes.BYTES, NativeTypes.STRING);
    // Validate all types are tested.
    assertEquals(Set.of(NativeTypeSpec.values()), allTypes.stream().map(NativeType::spec).collect(Collectors.toSet()));
    for (NativeType type : allTypes) {
        final Object val = SchemaTestUtils.generateRandomValue(rnd, type);
        assertFalse(type.mismatch(NativeTypes.fromObject(val)));
        KeyValueViewImpl<Long, Object> kvView = kvViewForValueType(NativeTypes.fromObject(val), (Class<Object>) val.getClass());
        kvView.put(null, key, val);
        if (val instanceof byte[]) {
            assertArrayEquals((byte[]) val, (byte[]) kvView.get(null, key));
        } else {
            assertEquals(val, kvView.get(null, key));
        }
    }
}
Also used : Random(java.util.Random) NativeType(org.apache.ignite.internal.schema.NativeType) Test(org.junit.jupiter.api.Test)

Example 5 with NativeType

use of org.apache.ignite.internal.schema.NativeType in project ignite-3 by apache.

the class SchemaSerializerImpl method readColumn.

/**
 * Reads column from byte buffer.
 *
 * @param buf Byte buffer.
 * @return Column.
 */
private Column readColumn(ByteBuffer buf) {
    int schemaIdx = buf.getInt();
    int columnOrder = buf.getInt();
    boolean nullable = buf.get() == 1;
    String name = readString(buf);
    NativeType nativeType = fromByteBuffer(buf);
    Object object = readDefaultValue(buf, nativeType);
    return new Column(columnOrder, name, nativeType, nullable, () -> object).copy(schemaIdx);
}
Also used : Column(org.apache.ignite.internal.schema.Column) VarlenNativeType(org.apache.ignite.internal.schema.VarlenNativeType) TemporalNativeType(org.apache.ignite.internal.schema.TemporalNativeType) DecimalNativeType(org.apache.ignite.internal.schema.DecimalNativeType) NumberNativeType(org.apache.ignite.internal.schema.NumberNativeType) NativeType(org.apache.ignite.internal.schema.NativeType) BitmaskNativeType(org.apache.ignite.internal.schema.BitmaskNativeType)

Aggregations

NativeType (org.apache.ignite.internal.schema.NativeType)7 Column (org.apache.ignite.internal.schema.Column)3 BigInteger (java.math.BigInteger)2 Tuple (org.apache.ignite.table.Tuple)2 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Random (java.util.Random)1 UUID (java.util.UUID)1 BitmaskNativeType (org.apache.ignite.internal.schema.BitmaskNativeType)1 DecimalNativeType (org.apache.ignite.internal.schema.DecimalNativeType)1 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)1 NumberNativeType (org.apache.ignite.internal.schema.NumberNativeType)1 TemporalNativeType (org.apache.ignite.internal.schema.TemporalNativeType)1 VarlenNativeType (org.apache.ignite.internal.schema.VarlenNativeType)1 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)1 Test (org.junit.jupiter.api.Test)1