Search in sources :

Example 1 with NativeTypeSpec

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

the class UpgradingRowAdapterTest method validateRow.

private void validateRow(List<Object> values, SchemaRegistryImpl schemaRegistry, ByteBufferRow binaryRow) {
    Row row = schemaRegistry.resolve(binaryRow);
    SchemaDescriptor schema = row.schema();
    for (int i = 0; i < values.size(); i++) {
        Column col = schema.column(i);
        NativeTypeSpec type = col.type().spec();
        if (type == NativeTypeSpec.BYTES) {
            assertArrayEquals((byte[]) values.get(i), (byte[]) NativeTypeSpec.BYTES.objectValue(row, col.schemaIndex()), "Failed for column: " + col);
        } else {
            assertEquals(values.get(i), type.objectValue(row, col.schemaIndex()), "Failed for column: " + col);
        }
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Column(org.apache.ignite.internal.schema.Column) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow)

Example 2 with NativeTypeSpec

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

the class BinaryRowComparator method columnComparator.

/**
 * Creates a comparator for comparing table columns.
 */
private static Comparator<Row> columnComparator(Column column) {
    int schemaIndex = column.schemaIndex();
    NativeTypeSpec typeSpec = column.type().spec();
    switch(typeSpec) {
        case INT8:
            return (row1, row2) -> {
                byte value1 = row1.byteValue(schemaIndex);
                byte value2 = row2.byteValue(schemaIndex);
                return Byte.compare(value1, value2);
            };
        case INT16:
            return (row1, row2) -> {
                short value1 = row1.shortValue(schemaIndex);
                short value2 = row2.shortValue(schemaIndex);
                return Short.compare(value1, value2);
            };
        case INT32:
            return comparingInt(row -> row.intValue(schemaIndex));
        case INT64:
            return comparingLong(row -> row.longValue(schemaIndex));
        case FLOAT:
            return (row1, row2) -> {
                float value1 = row1.floatValue(schemaIndex);
                float value2 = row2.floatValue(schemaIndex);
                return Float.compare(value1, value2);
            };
        case DOUBLE:
            return comparingDouble(row -> row.doubleValue(schemaIndex));
        case BYTES:
            return comparing(row -> row.bytesValue(schemaIndex), Arrays::compare);
        case BITMASK:
            return comparing(row -> row.bitmaskValue(schemaIndex).toLongArray(), Arrays::compare);
        // all other types implement Comparable
        case DECIMAL:
        case UUID:
        case STRING:
        case NUMBER:
        case TIMESTAMP:
        case DATE:
        case TIME:
        case DATETIME:
            return comparing(row -> (Comparable) typeSpec.objectValue(row, schemaIndex));
        default:
            throw new IllegalArgumentException(String.format("Unsupported column schema for creating a sorted index. Column name: %s, column type: %s", column.name(), column.type()));
    }
}
Also used : ComparatorUtils.comparingNull(org.apache.ignite.internal.storage.rocksdb.index.ComparatorUtils.comparingNull) Comparator.comparingInt(java.util.Comparator.comparingInt) Arrays(java.util.Arrays) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) SortedIndexDescriptor(org.apache.ignite.internal.storage.index.SortedIndexDescriptor) Comparator.comparingDouble(java.util.Comparator.comparingDouble) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) AbstractComparator(org.rocksdb.AbstractComparator) ByteBuffer(java.nio.ByteBuffer) ByteOrder(java.nio.ByteOrder) ComparatorOptions(org.rocksdb.ComparatorOptions) Comparator.comparingLong(java.util.Comparator.comparingLong) Row(org.apache.ignite.internal.schema.row.Row) Column(org.apache.ignite.internal.schema.Column) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) Arrays(java.util.Arrays)

Example 3 with NativeTypeSpec

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

the class PrefixComparator method compare.

/**
 * Compares a particular column of a {@code row} with the given value.
 */
private static int compare(Column column, Row row, @Nullable Object value) {
    boolean nullRow = row.hasNullValue(column.schemaIndex(), column.type().spec());
    if (nullRow && value == null) {
        return 0;
    } else if (nullRow) {
        return -1;
    } else if (value == null) {
        return 1;
    }
    int schemaIndex = column.schemaIndex();
    NativeTypeSpec typeSpec = column.type().spec();
    switch(typeSpec) {
        case INT8:
            return Byte.compare(row.byteValue(schemaIndex), (Byte) value);
        case INT16:
            return Short.compare(row.shortValue(schemaIndex), (Short) value);
        case INT32:
            return Integer.compare(row.intValue(schemaIndex), (Integer) value);
        case INT64:
            return Long.compare(row.longValue(schemaIndex), (Long) value);
        case FLOAT:
            return Float.compare(row.floatValue(schemaIndex), (Float) value);
        case DOUBLE:
            return Double.compare(row.doubleValue(schemaIndex), (Double) value);
        case BYTES:
            return Arrays.compare(row.bytesValue(schemaIndex), (byte[]) value);
        case BITMASK:
            return Arrays.compare(row.bitmaskValue(schemaIndex).toLongArray(), ((BitSet) value).toLongArray());
        // all other types implement Comparable
        case DECIMAL:
        case UUID:
        case STRING:
        case NUMBER:
        case TIMESTAMP:
        case DATE:
        case TIME:
        case DATETIME:
            return ((Comparable) typeSpec.objectValue(row, schemaIndex)).compareTo(value);
        default:
            // should never reach here, this invariant is checked during the index creation
            throw new IllegalStateException(String.format("Invalid column schema. Column name: %s, column type: %s", column.name(), column.type()));
    }
}
Also used : NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec)

Example 4 with NativeTypeSpec

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

the class SchemaSerializerImpl method fromByteBuffer.

/**
 * Reads native type from byte buffer.
 *
 * @param buf Byte buffer.
 * @return Native type.
 */
private NativeType fromByteBuffer(ByteBuffer buf) {
    String nativeTypeSpecName = readString(buf);
    NativeTypeSpec spec = NativeTypeSpec.valueOf(nativeTypeSpecName);
    switch(spec) {
        case STRING:
            int strLen = buf.getInt();
            return NativeTypes.stringOf(strLen);
        case BYTES:
            int len = buf.getInt();
            return NativeTypes.blobOf(len);
        case BITMASK:
            int bits = buf.getInt();
            return NativeTypes.bitmaskOf(bits);
        case DECIMAL:
            {
                int precision = buf.getInt();
                int scale = buf.getInt();
                return NativeTypes.decimalOf(precision, scale);
            }
        case TIME:
            {
                int precision = buf.getInt();
                return NativeTypes.time(precision);
            }
        case DATETIME:
            {
                int precision = buf.getInt();
                return NativeTypes.datetime(precision);
            }
        case TIMESTAMP:
            {
                int precision = buf.getInt();
                return NativeTypes.timestamp(precision);
            }
        case NUMBER:
            {
                int precision = buf.getInt();
                return NativeTypes.numberOf(precision);
            }
        case INT8:
            return NativeTypes.INT8;
        case INT16:
            return NativeTypes.INT16;
        case INT32:
            return NativeTypes.INT32;
        case INT64:
            return NativeTypes.INT64;
        case FLOAT:
            return NativeTypes.FLOAT;
        case DOUBLE:
            return NativeTypes.DOUBLE;
        case UUID:
            return NativeTypes.UUID;
        case DATE:
            return NativeTypes.DATE;
        default:
            throw new InvalidTypeException("Unexpected type " + spec);
    }
}
Also used : NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) InvalidTypeException(org.apache.ignite.internal.schema.InvalidTypeException)

Example 5 with NativeTypeSpec

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

the class RecordViewOperationsTest method recordView.

/**
 * Creates RecordView.
 */
private RecordViewImpl<TestObjectWithAllTypes> recordView() {
    ClusterService clusterService = Mockito.mock(ClusterService.class, RETURNS_DEEP_STUBS);
    Mockito.when(clusterService.topologyService().localMember().address()).thenReturn(DummyInternalTableImpl.ADDR);
    TxManager txManager = new TxManagerImpl(clusterService, new HeapLockManager());
    MessagingService messagingService = MessagingServiceTestUtils.mockMessagingService(txManager);
    Mockito.when(clusterService.messagingService()).thenReturn(messagingService);
    DummyInternalTableImpl table = new DummyInternalTableImpl(new VersionedRowStore(new ConcurrentHashMapPartitionStorage(), txManager), txManager);
    Mapper<TestObjectWithAllTypes> recMapper = Mapper.of(TestObjectWithAllTypes.class);
    Column[] valCols = { new Column("primitiveByteCol".toUpperCase(), INT8, false), new Column("primitiveShortCol".toUpperCase(), INT16, false), new Column("primitiveIntCol".toUpperCase(), INT32, false), new Column("primitiveFloatCol".toUpperCase(), FLOAT, false), new Column("primitiveDoubleCol".toUpperCase(), DOUBLE, false), new Column("byteCol".toUpperCase(), INT8, true), new Column("shortCol".toUpperCase(), INT16, true), new Column("intCol".toUpperCase(), INT32, true), new Column("longCol".toUpperCase(), INT64, true), new Column("nullLongCol".toUpperCase(), INT64, true), new Column("floatCol".toUpperCase(), FLOAT, true), new Column("doubleCol".toUpperCase(), DOUBLE, true), new Column("dateCol".toUpperCase(), DATE, true), new Column("timeCol".toUpperCase(), time(), true), new Column("dateTimeCol".toUpperCase(), datetime(), true), new Column("timestampCol".toUpperCase(), timestamp(), true), new Column("uuidCol".toUpperCase(), NativeTypes.UUID, true), new Column("bitmaskCol".toUpperCase(), NativeTypes.bitmaskOf(42), true), new Column("stringCol".toUpperCase(), STRING, true), new Column("nullBytesCol".toUpperCase(), BYTES, true), new Column("bytesCol".toUpperCase(), BYTES, true), new Column("numberCol".toUpperCase(), NativeTypes.numberOf(12), true), new Column("decimalCol".toUpperCase(), NativeTypes.decimalOf(19, 3), true) };
    SchemaDescriptor schema = new SchemaDescriptor(1, new Column[] { new Column("primitiveLongCol".toUpperCase(), NativeTypes.INT64, false) }, valCols);
    // Validate all types are tested.
    Set<NativeTypeSpec> testedTypes = Arrays.stream(valCols).map(c -> c.type().spec()).collect(Collectors.toSet());
    Set<NativeTypeSpec> missedTypes = Arrays.stream(NativeTypeSpec.values()).filter(t -> !testedTypes.contains(t)).collect(Collectors.toSet());
    assertEquals(Collections.emptySet(), missedTypes);
    return new RecordViewImpl<>(table, new DummySchemaManagerImpl(schema), recMapper);
}
Also used : VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) DATE(org.apache.ignite.internal.schema.NativeTypes.DATE) ConcurrentHashMapPartitionStorage(org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage) RecordView(org.apache.ignite.table.RecordView) Arrays(java.util.Arrays) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Random(java.util.Random) VersionedRowStore(org.apache.ignite.internal.table.distributed.storage.VersionedRowStore) DOUBLE(org.apache.ignite.internal.schema.NativeTypes.DOUBLE) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) INT64(org.apache.ignite.internal.schema.NativeTypes.INT64) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) INT8(org.apache.ignite.internal.schema.NativeTypes.INT8) Mapper(org.apache.ignite.table.mapper.Mapper) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessagingService(org.apache.ignite.network.MessagingService) FLOAT(org.apache.ignite.internal.schema.NativeTypes.FLOAT) RETURNS_DEEP_STUBS(org.mockito.Answers.RETURNS_DEEP_STUBS) TestObjectWithAllTypes(org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes) NativeTypes.datetime(org.apache.ignite.internal.schema.NativeTypes.datetime) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) NativeTypes.timestamp(org.apache.ignite.internal.schema.NativeTypes.timestamp) Collection(java.util.Collection) Set(java.util.Set) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) Collectors(java.util.stream.Collectors) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) DummyInternalTableImpl(org.apache.ignite.internal.table.impl.DummyInternalTableImpl) TxManager(org.apache.ignite.internal.tx.TxManager) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) BYTES(org.apache.ignite.internal.schema.NativeTypes.BYTES) List(java.util.List) NativeTypes(org.apache.ignite.internal.schema.NativeTypes) STRING(org.apache.ignite.internal.schema.NativeTypes.STRING) Column(org.apache.ignite.internal.schema.Column) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) NativeTypes.time(org.apache.ignite.internal.schema.NativeTypes.time) ClusterService(org.apache.ignite.network.ClusterService) INT16(org.apache.ignite.internal.schema.NativeTypes.INT16) INT32(org.apache.ignite.internal.schema.NativeTypes.INT32) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) ConcurrentHashMapPartitionStorage(org.apache.ignite.internal.storage.basic.ConcurrentHashMapPartitionStorage) TxManager(org.apache.ignite.internal.tx.TxManager) DummySchemaManagerImpl(org.apache.ignite.internal.table.impl.DummySchemaManagerImpl) MessagingService(org.apache.ignite.network.MessagingService) HeapLockManager(org.apache.ignite.internal.tx.impl.HeapLockManager) ClusterService(org.apache.ignite.network.ClusterService) TestObjectWithAllTypes(org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes) Column(org.apache.ignite.internal.schema.Column) TxManagerImpl(org.apache.ignite.internal.tx.impl.TxManagerImpl) DummyInternalTableImpl(org.apache.ignite.internal.table.impl.DummyInternalTableImpl)

Aggregations

NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)7 Column (org.apache.ignite.internal.schema.Column)4 Arrays (java.util.Arrays)3 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)3 Collections (java.util.Collections)2 List (java.util.List)2 Random (java.util.Random)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ByteBufferRow (org.apache.ignite.internal.schema.ByteBufferRow)2 NativeTypes (org.apache.ignite.internal.schema.NativeTypes)2 BYTES (org.apache.ignite.internal.schema.NativeTypes.BYTES)2 DATE (org.apache.ignite.internal.schema.NativeTypes.DATE)2 DOUBLE (org.apache.ignite.internal.schema.NativeTypes.DOUBLE)2 FLOAT (org.apache.ignite.internal.schema.NativeTypes.FLOAT)2 INT16 (org.apache.ignite.internal.schema.NativeTypes.INT16)2 INT32 (org.apache.ignite.internal.schema.NativeTypes.INT32)2 INT64 (org.apache.ignite.internal.schema.NativeTypes.INT64)2 INT8 (org.apache.ignite.internal.schema.NativeTypes.INT8)2 STRING (org.apache.ignite.internal.schema.NativeTypes.STRING)2