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");
}
}
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);
}
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();
}
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);
}
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);
}
Aggregations