Search in sources :

Example 6 with Columns

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

the class TupleMarshallerImpl method marshalKey.

/**
 * {@inheritDoc}
 */
@Override
public Row marshalKey(@NotNull Tuple keyTuple) throws TupleMarshallerException {
    try {
        final SchemaDescriptor schema = schemaReg.schema();
        InternalTuple keyTuple0 = toInternalTuple(schema, keyTuple, true);
        if (keyTuple0.knownColumns() < keyTuple.columnCount()) {
            throw new SchemaMismatchException("Key tuple contains extra columns: " + extraColumnNames(keyTuple, true, schema));
        }
        final RowAssembler rowBuilder = createAssembler(schema, keyTuple0, InternalTuple.NO_VALUE);
        Columns cols = schema.keyColumns();
        for (int i = 0, len = cols.length(); i < len; i++) {
            final Column col = cols.column(i);
            writeColumn(rowBuilder, col, keyTuple0);
        }
        return new Row(schema, rowBuilder.build());
    } catch (Exception ex) {
        throw new TupleMarshallerException("Failed to marshal tuple.", ex);
    }
}
Also used : SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) Column(org.apache.ignite.internal.schema.Column) RowAssembler(org.apache.ignite.internal.schema.row.RowAssembler) Columns(org.apache.ignite.internal.schema.Columns) Row(org.apache.ignite.internal.schema.row.Row) SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException)

Example 7 with Columns

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

the class SchemaDescriptorConverterTest method getBuilder.

/**
 * Get TableSchemaBuilder with default table.
 *
 * @param nullable If all columns should be nullable.
 * @param withPk   If builder should contains primary key index.
 * @return TableSchemaBuilder.
 */
private TableDefinitionBuilder getBuilder(boolean nullable, boolean withPk) {
    Function<ColumnDefinitionBuilder, ColumnDefinition> postProcess = builder -> builder.asNullable(nullable).build();
    TableDefinitionBuilder res = SchemaBuilders.tableBuilder("SCHEMA", "TABLE").columns(SchemaBuilders.column("ID", ColumnType.UUID).build(), postProcess.apply(SchemaBuilders.column("INT8", ColumnType.INT8)), postProcess.apply(SchemaBuilders.column("INT16", ColumnType.INT16)), postProcess.apply(SchemaBuilders.column("INT32", ColumnType.INT32)), postProcess.apply(SchemaBuilders.column("INT64", ColumnType.INT64)), postProcess.apply(SchemaBuilders.column("FLOAT", ColumnType.FLOAT)), postProcess.apply(SchemaBuilders.column("DOUBLE", ColumnType.DOUBLE)), postProcess.apply(SchemaBuilders.column("UUID", ColumnType.UUID)), postProcess.apply(SchemaBuilders.column("STRING", ColumnType.string())), postProcess.apply(SchemaBuilders.column("STRING_FS10", ColumnType.stringOf(10))), postProcess.apply(SchemaBuilders.column("BLOB", ColumnType.blobOf())), postProcess.apply(SchemaBuilders.column("BLOB_FS10", ColumnType.blobOf(10))), postProcess.apply(SchemaBuilders.column("DECIMAL", ColumnType.decimalOf(1, 1))), postProcess.apply(SchemaBuilders.column("NUMBER", ColumnType.numberOf(12))), postProcess.apply(SchemaBuilders.column("BITMASK_FS10", ColumnType.bitmaskOf(10))));
    if (withPk) {
        res.withPrimaryKey("ID");
    }
    return res;
}
Also used : Arrays(java.util.Arrays) SchemaDescriptor(org.apache.ignite.internal.schema.SchemaDescriptor) Columns(org.apache.ignite.internal.schema.Columns) TableDefinition(org.apache.ignite.schema.definition.TableDefinition) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition) ColumnType(org.apache.ignite.schema.definition.ColumnType) Function(java.util.function.Function) NativeTypeSpec(org.apache.ignite.internal.schema.NativeTypeSpec) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) Test(org.junit.jupiter.api.Test) TableDefinitionBuilder(org.apache.ignite.schema.definition.builder.TableDefinitionBuilder) Column(org.apache.ignite.internal.schema.Column) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SchemaBuilders(org.apache.ignite.schema.SchemaBuilders) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ColumnDefinitionBuilder(org.apache.ignite.schema.definition.builder.ColumnDefinitionBuilder) TableDefinitionBuilder(org.apache.ignite.schema.definition.builder.TableDefinitionBuilder) ColumnDefinition(org.apache.ignite.schema.definition.ColumnDefinition)

Example 8 with Columns

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

the class Row method findColumn.

/**
 * Gets the column offset and length encoded into a single 8-byte value (4 least significant bytes encoding the offset from the
 * beginning of the row and 4 most significant bytes encoding the field length for varlength columns). The offset and length should be
 * extracted using {@link #offset(long)} and {@link #length(long)} methods. Will also validate that the actual column type matches the
 * requested column type, throwing {@link InvalidTypeException} if the types do not match.
 *
 * @param colIdx Column index.
 * @param type   Expected column type.
 * @return {@code -1} if value is {@code null} for a column, otherwise encoded offset + length of the column.
 * @see #offset(long)
 * @see #length(long)
 * @see InvalidTypeException If actual column type does not match the requested column type.
 */
protected long findColumn(int colIdx, NativeTypeSpec type) throws InvalidTypeException {
    // Get base offset (key start or value start) for the given column.
    boolean isKeyCol = schema.isKeyColumn(colIdx);
    Columns cols;
    int chunkBaseOff;
    int flags;
    if (isKeyCol) {
        cols = schema.keyColumns();
        chunkBaseOff = KEY_CHUNK_OFFSET;
        flags = keyFlags();
    } else {
        // Adjust the column index according to the number of key columns.
        if (!hasValue()) {
            throw new IllegalStateException("Row has no value.");
        }
        colIdx -= schema.keyColumns().length();
        cols = schema.valueColumns();
        chunkBaseOff = KEY_CHUNK_OFFSET + readInteger(KEY_CHUNK_OFFSET);
        flags = valueFlags();
    }
    if (cols.column(colIdx).type().spec() != type) {
        throw new InvalidTypeException("Invalid column type requested [requested=" + type + ", column=" + cols.column(colIdx) + ']');
    }
    int nullMapLen = cols.nullMapSize();
    VarTableFormat format = VarTableFormat.fromFlags(flags);
    if (nullMapLen > 0 && isNull(chunkBaseOff, colIdx)) {
        return -1;
    }
    int dataOffset = varTableOffset(chunkBaseOff, nullMapLen);
    dataOffset += format.vartableLength(format.readVartableSize(row, dataOffset));
    return type.fixedLength() ? fixedSizeColumnOffset(chunkBaseOff, dataOffset, cols, colIdx, nullMapLen > 0) : varlenColumnOffsetAndLength(chunkBaseOff, dataOffset, cols, colIdx, nullMapLen, format);
}
Also used : Columns(org.apache.ignite.internal.schema.Columns) InvalidTypeException(org.apache.ignite.internal.schema.InvalidTypeException)

Aggregations

Columns (org.apache.ignite.internal.schema.Columns)8 Column (org.apache.ignite.internal.schema.Column)4 Row (org.apache.ignite.internal.schema.row.Row)4 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)3 RowAssembler (org.apache.ignite.internal.schema.row.RowAssembler)3 SchemaMismatchException (org.apache.ignite.internal.schema.SchemaMismatchException)2 Tuple (org.apache.ignite.table.Tuple)2 NotNull (org.jetbrains.annotations.NotNull)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)1 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 Scope (com.facebook.presto.bytecode.Scope)1 Variable (com.facebook.presto.bytecode.Variable)1 IfStatement (com.facebook.presto.bytecode.control.IfStatement)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 InvalidTypeException (org.apache.ignite.internal.schema.InvalidTypeException)1 NativeTypeSpec (org.apache.ignite.internal.schema.NativeTypeSpec)1 SchemaAware (org.apache.ignite.internal.schema.SchemaAware)1