use of com.pingcap.tikv.expression.ColumnRef 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.expression.ColumnRef 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));
}
use of com.pingcap.tikv.expression.ColumnRef in project tispark by pingcap.
the class PredicateUtilsTest method logicalBinaryExpressionToIndexRangesTest.
@Test
public void logicalBinaryExpressionToIndexRangesTest() {
TiTableInfo table = createTable();
TiIndexInfo index = table.getIndices().get(0);
TypedKey zero = TypedKey.toTypedKey(0, IntegerType.INT);
TypedKey one = TypedKey.toTypedKey(1, IntegerType.INT);
TypedKey two = TypedKey.toTypedKey(2, IntegerType.INT);
ColumnRef col1 = ColumnRef.create("c1", table);
Expression eq1 = greaterThan(col1, Constant.create(0));
Expression eq2 = lessThan(col1, Constant.create(2));
Expression eq3 = lessEqual(col1, Constant.create(1));
Expression eq4 = greaterThan(col1, Constant.create(2));
Expression and1 = and(eq1, eq2);
Expression and2 = and(eq1, eq3);
Expression and3 = and(eq2, eq3);
Expression or1 = or(eq1, eq2);
Expression or2 = or(eq1, eq4);
Expression or3 = or(eq3, eq4);
Expression xor1 = xor(eq1, eq2);
Expression xor2 = xor(eq1, eq3);
Expression xor3 = xor(eq2, eq3);
List<Range> ans1 = ImmutableList.of(Range.open(zero, two));
List<Range> ans2 = ImmutableList.of(Range.openClosed(zero, one));
List<Range> ans3 = ImmutableList.of(Range.atMost(one));
List<Range> ans4 = ImmutableList.of(Range.all());
List<Range> ans5 = ImmutableList.of(Range.greaterThan(zero));
List<Range> ans6 = ImmutableList.of(Range.atMost(one), Range.greaterThan(two));
List<Range> ans7 = ImmutableList.of(Range.atMost(zero), Range.atLeast(two));
List<Range> ans8 = ImmutableList.of(Range.atMost(zero), Range.greaterThan(one));
List<Range> ans9 = ImmutableList.of(Range.open(one, two));
Tests<Expression, List<Range>> logicalTests = new Tests<>();
logicalTests.addTestCase(and1, ans1);
logicalTests.addTestCase(and2, ans2);
logicalTests.addTestCase(and3, ans3);
logicalTests.addTestCase(or1, ans4);
logicalTests.addTestCase(or2, ans5);
logicalTests.addTestCase(or3, ans6);
logicalTests.addTestCase(xor1, ans7);
logicalTests.addTestCase(xor2, ans8);
logicalTests.addTestCase(xor3, ans9);
logicalTests.test((k, v) -> {
List<Expression> exprs = ImmutableList.of(k);
ScanSpec result = extractConditions(exprs, table, index);
List<IndexRange> irs = PredicateUtils.expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, index);
assertEquals(irs.size(), v.size());
for (int i = 0; i < irs.size(); i++) {
assertEquals(irs.get(i).getRange(), v.get(i));
}
});
}
use of com.pingcap.tikv.expression.ColumnRef in project tispark by pingcap.
the class PredicateUtilsTest method extractColumnRefFromExpressionTest.
@Test
public void extractColumnRefFromExpressionTest() {
TiTableInfo table = createTable();
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);
ColumnRef col3 = ColumnRef.create("c3", table);
ColumnRef col4 = ColumnRef.create("c4", table);
ColumnRef col5 = ColumnRef.create("c5", table);
Set<ColumnRef> baseline = ImmutableSet.of(col1, col2, col3, col4, col5);
Expression expression = and(c1, and(c2, and(col1, and(divide(col4, and(plus(col1, c1), minus(col2, col5))), col3))));
Set<ColumnRef> columns = PredicateUtils.extractColumnRefFromExpression(expression);
assertEquals(baseline, columns);
}
use of com.pingcap.tikv.expression.ColumnRef in project tispark by pingcap.
the class TiDAGRequestTest method testSerializable.
@Test
public void testSerializable() throws Exception {
TiTableInfo table = createTable();
TiDAGRequest selReq = new TiDAGRequest(TiDAGRequest.PushDownType.NORMAL);
Constant c1 = Constant.create(1L, IntegerType.BIGINT);
Constant c2 = Constant.create(2L, IntegerType.BIGINT);
ColumnRef col1 = ColumnRef.create("c1", table);
ColumnRef col2 = ColumnRef.create("c2", table);
ColumnRef col3 = ColumnRef.create("c3", table);
AggregateFunction sum = AggregateFunction.newCall(FunctionType.Sum, col1, col1.getDataType());
AggregateFunction min = AggregateFunction.newCall(FunctionType.Min, col1, col1.getDataType());
selReq.addRequiredColumn(col1).addRequiredColumn(col2).addRequiredColumn(col3).addAggregate(sum).addAggregate(min).addFilters(ImmutableList.of(plus(c1, c2))).addGroupByItem(ByItem.create(ColumnRef.create("c2", table), true)).addOrderByItem(ByItem.create(ColumnRef.create("c3", table), false)).setTableInfo(table).setStartTs(new TiTimestamp(0, 666)).setTruncateMode(TiDAGRequest.TruncateMode.IgnoreTruncation).setDistinct(true).setIndexInfo(table.getIndices().get(0)).setHaving(lessEqual(col3, c2)).setLimit(100).addRanges(ImmutableMap.of(table.getId(), ImmutableList.of(Coprocessor.KeyRange.newBuilder().setStart(ByteString.copyFromUtf8("startkey")).setEnd(ByteString.copyFromUtf8("endkey")).build())));
ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(byteOutStream);
oos.writeObject(selReq);
ByteArrayInputStream byteInStream = new ByteArrayInputStream(byteOutStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(byteInStream);
TiDAGRequest derselReq = (TiDAGRequest) ois.readObject();
assertTrue(selectRequestEquals(selReq, derselReq));
}
Aggregations