Search in sources :

Example 6 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiParserTest method TestParseExpression.

@Test
public void TestParseExpression() {
    String sql = "1.0";
    TiParser parser = new TiParser();
    Expression constant = parser.parseExpression(sql);
    Assert.assertEquals(Constant.create(1.0, RealType.REAL), constant);
    sql = "true";
    Expression ex1 = parser.parseExpression(sql);
    Assert.assertEquals("1", ex1.toString());
    sql = "true and id < 4";
    ex1 = parser.parseExpression(sql);
    Assert.assertEquals("[1 AND [[id] LESS_THAN 4]]", ex1.toString());
    sql = "true and x <= 4";
    ex1 = parser.parseExpression(sql);
    Assert.assertEquals("[1 AND [[x] LESS_EQUAL 4]]", ex1.toString());
    sql = "true and x = 4";
    ex1 = parser.parseExpression(sql);
    Assert.assertEquals("[1 AND [[x] EQUAL 4]]", ex1.toString());
    sql = "true and x > 4";
    ex1 = parser.parseExpression(sql);
    Assert.assertEquals("[1 AND [[x] GREATER_THAN 4]]", ex1.toString());
    sql = "1.4;";
    Expression cst2 = parser.parseExpression(sql);
    Assert.assertEquals(Constant.create(1.4), cst2);
    sql = "id;";
    Expression colRef = parser.parseExpression(sql);
    Assert.assertEquals(ColumnRef.create("id"), colRef);
    sql = "id+1";
    colRef = parser.parseExpression(sql);
    Assert.assertEquals(ArithmeticBinaryExpression.plus(ColumnRef.create("id"), Constant.create(1, IntegerType.INT)), colRef);
    sql = "id*1";
    colRef = parser.parseExpression(sql);
    Assert.assertEquals(ArithmeticBinaryExpression.multiply(ColumnRef.create("id"), Constant.create(1, IntegerType.INT)), colRef);
    sql = "id-1";
    colRef = parser.parseExpression(sql);
    Assert.assertEquals(ArithmeticBinaryExpression.minus(ColumnRef.create("id"), Constant.create(1)), colRef);
    sql = "id/1";
    colRef = parser.parseExpression(sql);
    Assert.assertEquals(ArithmeticBinaryExpression.divide(ColumnRef.create("id"), Constant.create(1)), colRef);
    sql = "id div 1";
    colRef = parser.parseExpression(sql);
    Assert.assertEquals(ArithmeticBinaryExpression.divide(ColumnRef.create("id"), Constant.create(1)), colRef);
    sql = "'abc'";
    Expression stringLiteral = parser.parseExpression(sql);
    Assert.assertEquals(Constant.create("'abc'"), stringLiteral);
    sql = "id < 1 and id >= 3";
    Expression and = parser.parseExpression(sql);
    Assert.assertEquals(and.toString(), "[[[id] LESS_THAN 1] AND [[id] GREATER_EQUAL 3]]");
    sql = "id < 1 and id >= 0";
    and = parser.parseExpression(sql);
    Assert.assertEquals(and.toString(), "[[[id] LESS_THAN 1] AND [[id] GREATER_EQUAL 0]]");
    sql = "''";
    stringLiteral = parser.parseExpression(sql);
    Assert.assertEquals(stringLiteral, Constant.create("''"));
    sql = "\"abc\"";
    stringLiteral = parser.parseExpression(sql);
    Assert.assertEquals(stringLiteral, Constant.create("abc"));
}
Also used : Expression(com.pingcap.tikv.expression.Expression) ArithmeticBinaryExpression(com.pingcap.tikv.expression.ArithmeticBinaryExpression) Test(org.junit.Test)

Example 7 with Expression

use of com.pingcap.tikv.expression.Expression 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 8 with Expression

use of com.pingcap.tikv.expression.Expression 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 9 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiKVScanAnalyzerTest method buildIndexScanKeyRangeTest.

@Test
public void buildIndexScanKeyRangeTest() {
    TiTableInfo table = createTableWithIndex(6, 5);
    TiIndexInfo index = table.getIndices().get(0);
    Expression eq1 = equal(ColumnRef.create("c1", table), Constant.create(0));
    Expression eq2 = lessEqual(ColumnRef.create("c2", table), Constant.create("wtf"));
    List<Expression> exprs = ImmutableList.of(eq1);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    List<IndexRange> irs = expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, index);
    TiKVScanAnalyzer scanAnalyzer = new TiKVScanAnalyzer();
    Map<Long, List<Coprocessor.KeyRange>> keyRanges = scanAnalyzer.buildIndexScanKeyRange(table, index, irs, null);
    assertEquals(keyRanges.size(), 1);
    Coprocessor.KeyRange keyRange = keyRanges.get(table.getId()).get(0);
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getStart());
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 1 }), keyRange.getEnd());
    exprs = ImmutableList.of(eq1, eq2);
    result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    irs = expressionToIndexRanges(result.getPointPredicates(), result.getRangePredicate(), table, index);
    keyRanges = scanAnalyzer.buildIndexScanKeyRange(table, index, irs, null);
    assertEquals(keyRanges.size(), 1);
    keyRange = keyRanges.get(table.getId()).get(0);
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0, 0 }), keyRange.getStart());
    assertEquals(ByteString.copyFrom(new byte[] { 116, -128, 0, 0, 0, 0, 0, 0, 6, 95, 105, -128, 0, 0, 0, 0, 0, 0, 5, 3, -128, 0, 0, 0, 0, 0, 0, 0, 1, 119, 116, 102, 0, 0, 0, 0, 0, -5 }), keyRange.getEnd());
}
Also used : Coprocessor(org.tikv.kvproto.Coprocessor) 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) TiIndexInfo(com.pingcap.tikv.meta.TiIndexInfo) Test(org.junit.Test)

Example 10 with Expression

use of com.pingcap.tikv.expression.Expression in project tispark by pingcap.

the class TiKVScanAnalyzerTest method extractConditionsWithPrimaryKeyTest.

@Test
public void extractConditionsWithPrimaryKeyTest() {
    TiTableInfo table = createTableWithPrefix();
    TiIndexInfo index = TiIndexInfo.generateFakePrimaryKeyIndex(table);
    requireNonNull(index);
    assertEquals(1, index.getIndexColumns().size());
    assertEquals("c1", index.getIndexColumns().get(0).getName());
    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);
    ScanSpec result = TiKVScanAnalyzer.extractConditions(exprs, table, index);
    Set<Expression> baselineSet = ImmutableSet.of(eq2, le1, eq3);
    // 3 remains since c2 condition pushed back as well
    assertEquals(baselineSet, result.getResidualPredicates());
    assertEquals(1, result.getPointPredicates().size());
    assertEquals(eq1, result.getPointPredicates().get(0));
    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)

Aggregations

Expression (com.pingcap.tikv.expression.Expression)28 Test (org.junit.Test)16 TiTableInfo (com.pingcap.tikv.meta.TiTableInfo)11 ArithmeticBinaryExpression (com.pingcap.tikv.expression.ArithmeticBinaryExpression)10 ComparisonBinaryExpression (com.pingcap.tikv.expression.ComparisonBinaryExpression)9 LogicalBinaryExpression (com.pingcap.tikv.expression.LogicalBinaryExpression)9 TiIndexInfo (com.pingcap.tikv.meta.TiIndexInfo)8 ColumnRef (com.pingcap.tikv.expression.ColumnRef)7 Constant (com.pingcap.tikv.expression.Constant)6 FuncCallExpr (com.pingcap.tikv.expression.FuncCallExpr)6 Expr (com.pingcap.tidb.tipb.Expr)5 StringRegExpression (com.pingcap.tikv.expression.StringRegExpression)4 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)3 Range (com.google.common.collect.Range)3 TiExpressionException (com.pingcap.tikv.exception.TiExpressionException)3 TypedKey (com.pingcap.tikv.key.TypedKey)3 List (java.util.List)3 ScalarFuncSig (com.pingcap.tidb.tipb.ScalarFuncSig)2 UnsupportedSyntaxException (com.pingcap.tikv.exception.UnsupportedSyntaxException)2