Search in sources :

Example 1 with Constant

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));
}
Also used : Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) Test(org.junit.Test)

Example 2 with Constant

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()));
    }
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) Range(com.google.common.collect.Range) Key(com.pingcap.tikv.key.Key) CompoundKey(com.pingcap.tikv.key.CompoundKey) TypedKey(com.pingcap.tikv.key.TypedKey) Test(org.junit.Test)

Example 3 with Constant

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);
}
Also used : ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) NormalizedPredicate(com.pingcap.tikv.expression.ComparisonBinaryExpression.NormalizedPredicate) ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) FuncCallExpr(com.pingcap.tikv.expression.FuncCallExpr)

Example 4 with Constant

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));
}
Also used : TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) IndexMatcher(com.pingcap.tikv.expression.visitor.IndexMatcher) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 5 with Constant

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));
}
Also used : TiIndexColumn(com.pingcap.tikv.meta.TiIndexColumn) IndexMatcher(com.pingcap.tikv.expression.visitor.IndexMatcher) Expression(com.pingcap.tikv.expression.Expression) Constant(com.pingcap.tikv.expression.Constant) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ColumnRef(com.pingcap.tikv.expression.ColumnRef) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Aggregations

Constant (com.pingcap.tikv.expression.Constant)7 Expression (com.pingcap.tikv.expression.Expression)6 Test (org.junit.Test)6 ColumnRef (com.pingcap.tikv.expression.ColumnRef)5 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)4 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)2 TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)2 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)2 Range (com.google.common.collect.Range)1 AggregateFunction (com.pingcap.tikv.expression.AggregateFunction)1 ComparisonBinaryExpression (com.pingcap.tikv.expression.ComparisonBinaryExpression)1 NormalizedPredicate (com.pingcap.tikv.expression.ComparisonBinaryExpression.NormalizedPredicate)1 FuncCallExpr (com.pingcap.tikv.expression.FuncCallExpr)1 LogicalBinaryExpression (com.pingcap.tikv.expression.LogicalBinaryExpression)1 CompoundKey (com.pingcap.tikv.key.CompoundKey)1 Key (com.pingcap.tikv.key.Key)1 TypedKey (com.pingcap.tikv.key.TypedKey)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1