Search in sources :

Example 1 with TiColumnInfo

use of com.pingcap.tikv.meta.TiColumnInfo in project tispark by pingcap.

the class ColumnRef method resolve.

public void resolve(TiTableInfo table) {
    TiColumnInfo columnInfo = null;
    for (TiColumnInfo col : table.getColumns()) {
        if (col.matchName(name)) {
            this.dataType = col.getType();
            columnInfo = col;
            break;
        }
    }
    if (columnInfo == null) {
        throw new TiExpressionException(String.format("No Matching column %s from table %s", name, table.getName()));
    }
    if (columnInfo.getId() == 0) {
        throw new TiExpressionException("Zero Id is not a referable column id");
    }
}
Also used : TiExpressionException(com.pingcap.tikv.exception.TiExpressionException) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Example 2 with TiColumnInfo

use of com.pingcap.tikv.meta.TiColumnInfo in project tispark by pingcap.

the class TableCodecV1 method decodeRow.

protected static Row decodeRow(byte[] value, Handle handle, TiTableInfo tableInfo) {
    if (handle == null && tableInfo.isPkHandle()) {
        throw new IllegalArgumentException("when pk is handle, handle cannot be null");
    }
    int colSize = tableInfo.getColumns().size();
    HashMap<Long, TiColumnInfo> idToColumn = new HashMap<>(colSize);
    for (TiColumnInfo col : tableInfo.getColumns()) {
        idToColumn.put(col.getId(), col);
    }
    // decode bytes to Map<ColumnID, Data>
    HashMap<Long, Object> decodedDataMap = new HashMap<>(colSize);
    CodecDataInput cdi = new CodecDataInput(value);
    Object[] res = new Object[colSize];
    while (!cdi.eof()) {
        long colID = (long) IntegerType.BIGINT.decode(cdi);
        Object colValue = idToColumn.get(colID).getType().decodeForBatchWrite(cdi);
        decodedDataMap.put(colID, colValue);
    }
    // construct Row with Map<ColumnID, Data> & handle
    for (int i = 0; i < colSize; i++) {
        // skip pk is handle case
        TiColumnInfo col = tableInfo.getColumn(i);
        if (col.isPrimaryKey() && tableInfo.isPkHandle()) {
            res[i] = handle;
        } else {
            res[i] = decodedDataMap.get(col.getId());
        }
    }
    return ObjectRowImpl.create(res);
}
Also used : HashMap(java.util.HashMap) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Example 3 with TiColumnInfo

use of com.pingcap.tikv.meta.TiColumnInfo in project tispark by pingcap.

the class TableCodecV1 method encodeRow.

/**
 * Row layout: colID1, value1, colID2, value2, .....
 */
protected static byte[] encodeRow(List<TiColumnInfo> columnInfos, Object[] values, boolean isPkHandle) {
    CodecDataOutput cdo = new CodecDataOutput();
    for (int i = 0; i < columnInfos.size(); i++) {
        TiColumnInfo col = columnInfos.get(i);
        // skip pk is handle case
        if (col.isPrimaryKey() && isPkHandle) {
            continue;
        }
        IntegerCodec.writeLongFully(cdo, col.getId(), false);
        col.getType().encode(cdo, EncodeType.VALUE, values[i]);
    }
    // We could not set nil value into kv.
    if (cdo.toBytes().length == 0) {
        return new byte[] { Codec.NULL_FLAG };
    }
    return cdo.toBytes();
}
Also used : TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Example 4 with TiColumnInfo

use of com.pingcap.tikv.meta.TiColumnInfo in project tispark by pingcap.

the class TableCodecV2 method decodeRow.

protected static Row decodeRow(byte[] value, Handle handle, TiTableInfo tableInfo) {
    if (handle == null && tableInfo.isPkHandle()) {
        throw new IllegalArgumentException("when pk is handle, handle cannot be null");
    }
    int colSize = tableInfo.getColumns().size();
    // decode bytes to Map<ColumnID, Data>
    HashMap<Long, Object> decodedDataMap = new HashMap<>(colSize);
    RowV2 rowV2 = RowV2.createNew(value);
    TiIndexInfo pk = tableInfo.getPrimaryKey();
    if (pk != null) {
        List<TiColumnInfo> cols = new ArrayList<>();
        for (TiIndexColumn indexColumn : pk.getIndexColumns()) {
            TiColumnInfo col = tableInfo.getColumn(indexColumn.getOffset());
            cols.add(col);
        }
        if (tableInfo.isPkHandle()) {
            assert cols.size() == 1;
            decodedDataMap.put(cols.get(0).getId(), handle.data()[0]);
        }
        if (tableInfo.isCommonHandle()) {
            for (int i = 0; i < cols.size(); i++) {
                decodedDataMap.put(cols.get(i).getId(), handle.data()[i]);
            }
        }
    }
    for (TiColumnInfo col : tableInfo.getColumns()) {
        if (decodedDataMap.containsKey(col.getId())) {
            continue;
        } else if (col.isPrimaryKey() && tableInfo.isPkHandle()) {
            decodedDataMap.put(col.getId(), handle);
            continue;
        }
        RowV2.ColIDSearchResult searchResult = rowV2.findColID(col.getId());
        if (searchResult.isNull) {
            // current col is null, nothing should be added to decodedMap
            continue;
        }
        if (!searchResult.notFound) {
            // corresponding column should be found
            assert (searchResult.idx != -1);
            byte[] colData = rowV2.getData(searchResult.idx);
            Object d = RowDecoderV2.decodeCol(colData, col.getType());
            decodedDataMap.put(col.getId(), d);
        }
    }
    Object[] res = new Object[colSize];
    // construct Row with Map<ColumnID, Data> & handle
    for (int i = 0; i < colSize; i++) {
        // skip pk is handle case
        TiColumnInfo col = tableInfo.getColumn(i);
        res[i] = decodedDataMap.get(col.getId());
    }
    return ObjectRowImpl.create(res);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo)

Example 5 with TiColumnInfo

use of com.pingcap.tikv.meta.TiColumnInfo in project tispark by pingcap.

the class TableCodecV2 method encodeRow.

/**
 * New Row Format: Reference
 * https://github.com/pingcap/tidb/blob/952d1d7541a8e86be0af58f5b7e3d5e982bab34e/docs/design/2018-07-19-row-format.md
 *
 * <p>- version, flag, numOfNotNullCols, numOfNullCols, notNullCols, nullCols, notNullOffsets,
 * notNullValues
 */
protected static byte[] encodeRow(List<TiColumnInfo> columnInfos, Object[] values, boolean isPkHandle) {
    RowEncoderV2 encoder = new RowEncoderV2();
    List<TiColumnInfo> columnInfoList = new ArrayList<>();
    List<Object> valueList = new ArrayList<>();
    for (int i = 0; i < columnInfos.size(); i++) {
        TiColumnInfo col = columnInfos.get(i);
        // skip pk is handle case
        if (col.isPrimaryKey() && isPkHandle) {
            continue;
        }
        columnInfoList.add(col);
        valueList.add(values[i]);
    }
    return encoder.encode(columnInfoList, valueList);
}
Also used : ArrayList(java.util.ArrayList) TiColumnInfo(com.pingcap.tikv.meta.TiColumnInfo)

Aggregations

TiColumnInfo (com.pingcap.tikv.meta.TiColumnInfo)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)3 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)3 TiExpressionException (com.pingcap.tikv.exception.TiExpressionException)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 BoundType (com.google.common.collect.BoundType)1 ImmutableList (com.google.common.collect.ImmutableList)1 Range (com.google.common.collect.Range)1 ByteString (com.google.protobuf.ByteString)1 EncodeType (com.pingcap.tidb.tipb.EncodeType)1 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)1 Expression (com.pingcap.tikv.expression.Expression)1 PartitionPruner (com.pingcap.tikv.expression.PartitionPruner)1 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)1 IndexScanKeyRangeBuilder (com.pingcap.tikv.key.IndexScanKeyRangeBuilder)1 Key (com.pingcap.tikv.key.Key)1