use of com.pingcap.tikv.meta.TiIndexColumn in project tispark by pingcap.
the class IndexKey method encodeIndexDataValues.
public static EncodeIndexDataResult encodeIndexDataValues(Row row, List<TiIndexColumn> indexColumns, Handle handle, boolean appendHandleIfContainsNull, TiTableInfo tableInfo) {
// when appendHandleIfContainsNull is true, append handle column if any of the index column is
// NULL
boolean appendHandle = false;
if (handle.isInt()) {
if (appendHandleIfContainsNull) {
for (TiIndexColumn col : indexColumns) {
DataType colTp = tableInfo.getColumn(col.getOffset()).getType();
if (row.get(col.getOffset(), colTp) == null) {
appendHandle = true;
break;
}
}
}
}
Key[] keys = new Key[indexColumns.size() + (appendHandle ? 1 : 0)];
for (int i = 0; i < indexColumns.size(); i++) {
TiIndexColumn col = indexColumns.get(i);
DataType colTp = tableInfo.getColumn(col.getOffset()).getType();
// truncate index's if necessary
Key key = TypedKey.toTypedKey(row.get(col.getOffset(), colTp), colTp, (int) col.getLength());
keys[i] = key;
}
if (appendHandle) {
Key key = TypedKey.toTypedKey(handle, IntegerType.BIGINT);
keys[keys.length - 1] = key;
}
return new EncodeIndexDataResult(keys, appendHandle);
}
use of com.pingcap.tikv.meta.TiIndexColumn 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.TiIndexColumn in project tispark by pingcap.
the class TiKVScanAnalyzer method isCoveringIndex.
// If all the columns requested in the select list of query, are available in the index, then the
// query engine doesn't have to lookup the table again compared with double read.
boolean isCoveringIndex(List<TiColumnInfo> columns, TiIndexInfo indexColumns, boolean pkIsHandle) {
if (columns.isEmpty()) {
return false;
}
Map<String, TiIndexColumn> colInIndex = indexColumns.getIndexColumns().stream().collect(Collectors.toMap(TiIndexColumn::getName, col -> col));
for (TiColumnInfo colInfo : columns) {
if (pkIsHandle && colInfo.isPrimaryKey()) {
continue;
}
if (colInfo.getId() == -1) {
continue;
}
boolean colNotInIndex = false;
if (colInIndex.containsKey(colInfo.getName())) {
TiIndexColumn indexCol = colInIndex.get(colInfo.getName());
boolean isFullLength = indexCol.isLengthUnspecified() || indexCol.getLength() == colInfo.getType().getLength();
if (!colInfo.getName().equalsIgnoreCase(indexCol.getName()) || !isFullLength) {
colNotInIndex = true;
}
} else {
colNotInIndex = true;
}
if (colNotInIndex) {
return false;
}
}
return true;
}
use of com.pingcap.tikv.meta.TiIndexColumn in project tispark by pingcap.
the class IndexMatcherTest method matchOnlyEq.
@Test
public void matchOnlyEq() {
TiTableInfo table = createTable();
TiIndexInfo index = table.getIndices().get(0);
TiIndexColumn col = index.getIndexColumns().get(0);
IndexMatcher matcher = IndexMatcher.equalOnlyMatcher(col);
Constant c0 = Constant.create(0, IntegerType.INT);
Constant c1 = Constant.create(1, IntegerType.INT);
Constant c2 = Constant.create(2, IntegerType.INT);
ColumnRef col1 = ColumnRef.create("c1", table);
ColumnRef col2 = ColumnRef.create("c2", table);
// index col = c1, long
Expression cond = equal(col1, c1);
assertTrue(matcher.match(cond));
cond = equal(c1, col1);
assertTrue(matcher.match(cond));
cond = equal(col2, col1);
assertFalse(matcher.match(cond));
cond = equal(c1, c1);
assertFalse(matcher.match(cond));
cond = and(equal(c1, col1), equal(col1, c2));
assertFalse(matcher.match(cond));
cond = or(equal(c1, col1), equal(col1, c2));
assertTrue(matcher.match(cond));
cond = lessEqual(c0, col1);
assertFalse(matcher.match(cond));
}
use of com.pingcap.tikv.meta.TiIndexColumn in project tispark by pingcap.
the class IndexMatcherTest method matchAll.
@Test
public void matchAll() {
TiTableInfo table = createTable();
TiIndexInfo index = table.getIndices().get(0);
TiIndexColumn col = index.getIndexColumns().get(0);
IndexMatcher matcher = IndexMatcher.matcher(col);
Constant c1 = Constant.create(1, IntegerType.INT);
Constant c2 = Constant.create(2, IntegerType.INT);
ColumnRef col1 = ColumnRef.create("c1", table);
// index col = c1, long
Expression cond = lessEqual(col1, c1);
assertTrue(matcher.match(cond));
cond = greaterEqual(c1, col1);
assertTrue(matcher.match(cond));
cond = lessThan(ColumnRef.create("c2", table), ColumnRef.create("c1", table));
assertFalse(matcher.match(cond));
cond = lessThan(c1, c1);
assertFalse(matcher.match(cond));
cond = and(lessThan(c1, col1), lessThan(col1, c2));
assertTrue(matcher.match(cond));
cond = or(lessThan(c1, col1), lessThan(col1, c2));
assertTrue(matcher.match(cond));
}
Aggregations