Search in sources :

Example 21 with Column

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

the class AbstractSerializerTest method columnOrderSerializeTest.

/**
 * (de)Serialize schema test.
 */
@Test
public void columnOrderSerializeTest() {
    AbstractSchemaSerializer assembler = SchemaSerializerImpl.INSTANCE;
    Column[] keyCols = { new Column(0, "A", NativeTypes.UUID, false), new Column(1, "B", NativeTypes.INT64, false), new Column(2, "C", NativeTypes.INT8, false) };
    Column[] valCols = { new Column(3, "A1", NativeTypes.stringOf(128), false), new Column(4, "B1", NativeTypes.INT64, false) };
    SchemaDescriptor desc = new SchemaDescriptor(100500, keyCols, valCols);
    byte[] serialize = assembler.serialize(desc);
    SchemaDescriptor deserialize = assembler.deserialize(serialize);
    assertEquals(desc.version(), deserialize.version());
    ArrayList<Column> columns = new ArrayList<>();
    Collections.addAll(columns, keyCols);
    Collections.addAll(columns, valCols);
    for (int i = 0; i < columns.size(); i++) {
        Column col = deserialize.column(i);
        assertEquals(columns.get(col.columnOrder()), col);
    }
    assertArrayEquals(columns.stream().map(Column::name).toArray(String[]::new), desc.columnNames().toArray(String[]::new));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) AbstractSchemaSerializer(org.apache.ignite.internal.schema.marshaller.schema.AbstractSchemaSerializer) Column(org.apache.ignite.internal.schema.Column) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 22 with Column

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

the class AbstractSerializerTest method columnMappingSerializeTest.

/**
 * (de)Serialize column mapping test.
 */
@Test
public void columnMappingSerializeTest() {
    final AbstractSchemaSerializer assembler = SchemaSerializerImpl.INSTANCE;
    SchemaDescriptor desc = new SchemaDescriptor(100500, new Column[] { new Column("A", NativeTypes.INT8, false, () -> (byte) 1) }, new Column[] { new Column("A1", NativeTypes.stringOf(128), false, () -> "test"), new Column("B1", NativeTypes.numberOf(255), false, () -> BigInteger.TEN) });
    ColumnMapper mapper = ColumnMapping.createMapper(desc);
    mapper.add(0, 1);
    Column c1 = new Column("C1", NativeTypes.stringOf(128), false, () -> "brandNewColumn").copy(2);
    mapper.add(c1);
    desc.columnMapping(mapper);
    byte[] serialize = assembler.serialize(desc);
    SchemaDescriptor deserialize = assembler.deserialize(serialize);
    ColumnMapper mapper1 = deserialize.columnMapping();
    assertEquals(1, mapper1.map(0));
    assertEquals(c1, mapper1.mappedColumn(2));
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) AbstractSchemaSerializer(org.apache.ignite.internal.schema.marshaller.schema.AbstractSchemaSerializer) Column(org.apache.ignite.internal.schema.Column) ColumnMapper(org.apache.ignite.internal.schema.mapping.ColumnMapper) Test(org.junit.jupiter.api.Test)

Example 23 with Column

use of org.apache.ignite.internal.schema.Column 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 24 with Column

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

the class BinaryRowComparator method rowComparator.

/**
 * Creates a comparator that compares two {@link Row}s by comparing individual columns.
 */
private static Comparator<Row> rowComparator(SortedIndexDescriptor descriptor) {
    return descriptor.indexRowColumns().stream().map(columnDescriptor -> {
        Column column = columnDescriptor.column();
        Comparator<Row> columnComparator = columnComparator(column);
        if (columnDescriptor.nullable()) {
            columnComparator = comparingNull(row -> row.hasNullValue(column.schemaIndex(), column.type().spec()) ? null : row, columnComparator);
        }
        return columnDescriptor.asc() ? columnComparator : columnComparator.reversed();
    }).reduce(Comparator::thenComparing).orElseThrow();
}
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) Column(org.apache.ignite.internal.schema.Column) BinaryRow(org.apache.ignite.internal.schema.BinaryRow) Row(org.apache.ignite.internal.schema.row.Row) ByteBufferRow(org.apache.ignite.internal.schema.ByteBufferRow)

Example 25 with Column

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

the class TupleMarshallerImpl method toInternalTuple.

/**
 * Analyze tuple and wrap into internal tuple.
 *
 * @param schema  Schema.
 * @param tuple   Key or value tuple.
 * @param keyFlag If {@code true} marshal key columns, otherwise marshall value columns.
 * @return Internal tuple.
 * @throws SchemaMismatchException If tuple doesn't match the schema.
 */
@NotNull
private InternalTuple toInternalTuple(SchemaDescriptor schema, Tuple tuple, boolean keyFlag) throws SchemaMismatchException {
    if (tuple == null) {
        return InternalTuple.NO_VALUE;
    }
    Columns columns = keyFlag ? schema.keyColumns() : schema.valueColumns();
    int nonNullVarlen = 0;
    int nonNullVarlenSize = 0;
    int knownColumns = 0;
    Map<String, Object> defaults = new HashMap<>();
    if (tuple instanceof SchemaAware && Objects.equals(((SchemaAware) tuple).schema(), schema)) {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            assert val != POISON_OBJECT;
            if (val == null || columns.firstVarlengthColumn() < i) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    } else {
        for (int i = 0, len = columns.length(); i < len; i++) {
            final Column col = columns.column(i);
            Object val = tuple.valueOrDefault(col.name(), POISON_OBJECT);
            if (val == POISON_OBJECT) {
                if (keyFlag) {
                    throw new SchemaMismatchException("Missed key column: " + col.name());
                }
                val = col.defaultValue();
                defaults.put(col.name(), val);
            } else {
                knownColumns++;
            }
            col.validate(val);
            if (val == null || columns.isFixedSize(i)) {
                continue;
            }
            nonNullVarlenSize += getValueSize(val, col.type());
            nonNullVarlen++;
        }
    }
    return new InternalTuple(tuple, nonNullVarlen, nonNullVarlenSize, defaults, knownColumns);
}
Also used : SchemaAware(org.apache.ignite.internal.schema.SchemaAware) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) HashMap(java.util.HashMap) Column(org.apache.ignite.internal.schema.Column) Columns(org.apache.ignite.internal.schema.Columns) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Column (org.apache.ignite.internal.schema.Column)131 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)78 Test (org.junit.jupiter.api.Test)44 Row (org.apache.ignite.internal.schema.row.Row)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)31 Tuple (org.apache.ignite.table.Tuple)27 MethodSource (org.junit.jupiter.params.provider.MethodSource)27 SchemaException (org.apache.ignite.internal.schema.SchemaException)22 BinaryRow (org.apache.ignite.internal.schema.BinaryRow)20 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)11 DummySchemaManagerImpl (org.apache.ignite.internal.table.impl.DummySchemaManagerImpl)11 TupleMarshaller (org.apache.ignite.internal.schema.marshaller.TupleMarshaller)10 TupleMarshallerImpl (org.apache.ignite.internal.schema.marshaller.TupleMarshallerImpl)10 TestObjectWithAllTypes (org.apache.ignite.internal.schema.testobjects.TestObjectWithAllTypes)10 Arrays (java.util.Arrays)7 NotNull (org.jetbrains.annotations.NotNull)7 List (java.util.List)6 Random (java.util.Random)6 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)6 BigDecimal (java.math.BigDecimal)5