Search in sources :

Example 6 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo 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 7 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo 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)

Example 8 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo 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));
        }
    });
}
Also used : TypedKey(com.pingcap.tikv.key.TypedKey) Range(com.google.common.collect.Range) Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ColumnRef(com.pingcap.tikv.expression.ColumnRef) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 9 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo in project tispark by pingcap.

the class TiKVScanAnalyzerTest method extractConditionsWithPrefixTest.

@Test
public void extractConditionsWithPrefixTest() {
    TiTableInfo table = createTableWithPrefix();
    TiIndexInfo index = table.getIndices().get(0);
    Expression eq1 = equal(ColumnRef.create("c1", table), Constant.create(0, IntegerType.INT));
    Expression eq2 = equal(ColumnRef.create("c2", table), Constant.create("test", StringType.VARCHAR));
    Expression le1 = lessEqual(ColumnRef.create("c3", table), Constant.create("fxxx", StringType.VARCHAR));
    // Last one should be pushed back
    Expression eq3 = equal(ColumnRef.create("c4", table), Constant.create("fxxx", StringType.VARCHAR));
    List<Expression> exprs = ImmutableList.of(eq1, eq2, le1, eq3);
    Set<Expression> baselineSet = ImmutableSet.of(eq2, le1, eq3);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    // 3 remains since c2 condition pushed back as well
    assertEquals(baselineSet, result.getResidualPredicates());
    assertEquals(2, result.getPointPredicates().size());
    assertEquals(eq1, result.getPointPredicates().get(0));
    assertEquals(eq2, result.getPointPredicates().get(1));
    assertFalse(result.getRangePredicate().isPresent());
}
Also used : Expression(com.pingcap.tikv.expression.Expression) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 10 with TiIndexInfo

use of com.pingcap.tikv.meta.TiIndexInfo in project tispark by pingcap.

the class TiKVScanAnalyzerTest method testKeyRangeGenWithNoFilterTest.

@Test
public void testKeyRangeGenWithNoFilterTest() {
    TiTableInfo table = createTableWithPrefix();
    TiIndexInfo index = TiIndexInfo.generateFakePrimaryKeyIndex(table);
    TiKVScanAnalyzer scanBuilder = new TiKVScanAnalyzer();
    TiKVScanAnalyzer.TiKVScanPlan scanPlan = scanBuilder.buildIndexScan(ImmutableList.of(), ImmutableList.of(), index, table, null, false);
    ByteString startKey = RowKey.toRowKey(table.getId(), new IntHandle(Long.MIN_VALUE)).toByteString();
    ByteString endKey = RowKey.createBeyondMax(table.getId()).toByteString();
    assertEquals(1, scanPlan.getKeyRanges().size());
    assertEquals(startKey, scanPlan.getKeyRanges().get(table.getId()).get(0).getStart());
    assertEquals(endKey, scanPlan.getKeyRanges().get(table.getId()).get(0).getEnd());
}
Also used : ByteString(com.google.protobuf.ByteString) TiTableInfo(com.pingcap.tikv.meta.TiTableInfo) IntHandle(com.pingcap.tikv.key.IntHandle) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Aggregations

TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)13 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)10 Test (org.junit.Test)10 Expression (com.pingcap.tikv.expression.Expression)9 ArrayList (java.util.ArrayList)6 TiIndexColumn (com.pingcap.tikv.meta.TiIndexColumn)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ColumnRef (com.pingcap.tikv.expression.ColumnRef)3 IndexMatcher (com.pingcap.tikv.expression.visitor.IndexMatcher)3 TiColumnInfo (com.pingcap.tikv.meta.TiColumnInfo)3 HashMap (java.util.HashMap)3 Range (com.google.common.collect.Range)2 ByteString (com.google.protobuf.ByteString)2 TiClientInternalException (com.pingcap.tikv.exception.TiClientInternalException)2 Constant (com.pingcap.tikv.expression.Constant)2 TypedKey (com.pingcap.tikv.key.TypedKey)2 TiStoreType (com.pingcap.tikv.region.TiStoreType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1