use of com.pingcap.tikv.expression.Constant in project tispark by pingcap.
the class PredicateUtilsTest method mergeCNFExpressionsTest.
@Test
public void mergeCNFExpressionsTest() {
Constant c1 = Constant.create(1, IntegerType.INT);
Constant c2 = Constant.create(2, IntegerType.INT);
Constant c3 = Constant.create(3, IntegerType.INT);
Constant c4 = Constant.create(4, IntegerType.INT);
Constant c5 = Constant.create(5, IntegerType.INT);
List<Expression> exprs = ImmutableList.of(c1, c2, c3, c4, c5);
Expression res = and(c1, and(c2, and(c3, and(c4, c5))));
assertEquals(res, PredicateUtils.mergeCNFExpressions(exprs));
}
use of com.pingcap.tikv.expression.Constant in project tispark by pingcap.
the class PredicateUtilsTest method expressionToIndexRangesTest.
@Test
public void expressionToIndexRangesTest() {
TiTableInfo table = createTable();
ColumnRef col1 = ColumnRef.create("c1", table);
ColumnRef col4 = ColumnRef.create("c4", table);
ColumnRef col5 = ColumnRef.create("c5", table);
Constant c1 = Constant.create(1, IntegerType.INT);
Constant c2 = Constant.create(2, IntegerType.INT);
Constant c3 = Constant.create(3, IntegerType.INT);
Constant c4 = Constant.create(4, IntegerType.INT);
TypedKey key1 = TypedKey.toTypedKey(1, IntegerType.INT);
TypedKey key2 = TypedKey.toTypedKey(2, IntegerType.INT);
TypedKey key3 = TypedKey.toTypedKey(3, IntegerType.INT);
TypedKey key4 = TypedKey.toTypedKey(4, IntegerType.INT);
Expression predicate1 = or(or(equal(c1, col1), equal(col1, c2)), equal(col1, c1));
Expression predicate2 = or(equal(c3, col4), equal(c4, col4));
Expression rangePredicate = notEqual(col5, c1);
List<IndexRange> indexRanges = PredicateUtils.expressionToIndexRanges(ImmutableList.of(predicate1, predicate2), Optional.of(rangePredicate), table, null);
assertEquals(8, indexRanges.size());
Key indexKey1 = CompoundKey.concat(key1, key3);
Key indexKey2 = CompoundKey.concat(key1, key4);
Key indexKey3 = CompoundKey.concat(key2, key3);
Key indexKey4 = CompoundKey.concat(key2, key4);
Range<TypedKey> baselineRange1 = Range.lessThan(key1);
Range<TypedKey> baselineRange2 = Range.greaterThan(key1);
Set<Key> baselineKeys = ImmutableSet.of(indexKey1, indexKey2, indexKey3, indexKey4);
Set<Range<TypedKey>> baselineRanges = ImmutableSet.of(baselineRange1, baselineRange2);
for (IndexRange range : indexRanges) {
assertTrue(baselineKeys.contains(range.getAccessKey()));
assertTrue(baselineRanges.contains(range.getRange()));
}
}
use of com.pingcap.tikv.expression.Constant in project tispark by pingcap.
the class PartAndFilterExprRewriter method visit.
@Override
public Expression visit(ComparisonBinaryExpression node, Void context) {
NormalizedPredicate predicate = node.normalize();
// predicate maybe null if node's left or right does not have a column ref or a constant.
if (predicate != null) {
if (!columnRefs.contains(predicate.getColumnRef())) {
return node;
}
// we only support year for now.
if (isYear()) {
FuncCallExpr year = new FuncCallExpr(predicate.getValue(), Type.YEAR);
Constant newLiteral = year.eval(predicate.getValue());
return new ComparisonBinaryExpression(node.getComparisonType(), node.getLeft(), newLiteral);
} else if (isColumnRef()) {
return node;
}
unsupportedPartFnFound = true;
return null;
}
// when we find a node in form like [year(y) < 1995], we need rewrite the left child.
Expression left = node.getLeft().accept(this, null);
Expression right = node.getRight().accept(this, null);
return new ComparisonBinaryExpression(node.getComparisonType(), left, right);
}
use of com.pingcap.tikv.expression.Constant 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.Constant 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