Search in sources :

Example 11 with Expression

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

the class PartAndFilterExprRewriterTest method TestRewrite.

@Test
public void TestRewrite() {
    Expression col = ColumnRef.create("y", DateType.DATE);
    Expression col2 = ColumnRef.create("a", IntegerType.INT);
    Expression partExpr = new FuncCallExpr(col, Type.YEAR);
    DateTime date = DateTime.parse("1995-10-10");
    // rewrite right hand side's constant. Apply year to it.
    Expression exprToBeRewrited = LogicalBinaryExpression.or(ComparisonBinaryExpression.equal(col, Constant.create(date, DateType.DATE)), ComparisonBinaryExpression.greaterEqual(col2, Constant.create(5, IntegerType.INT)));
    PartAndFilterExprRewriter expressionRewriter = new PartAndFilterExprRewriter(partExpr);
    Expression rewrote = expressionRewriter.rewrite(exprToBeRewrited);
    assertEquals("[[y@DATE EQUAL 1995] OR [a@LONG GREATER_EQUAL 5]]", rewrote.toString());
    // not support case
    partExpr = new Not(col);
    exprToBeRewrited = ComparisonBinaryExpression.equal(col, Constant.create("1995-10-10"));
    expressionRewriter = new PartAndFilterExprRewriter(partExpr);
    rewrote = expressionRewriter.rewrite(exprToBeRewrited);
    assertNull(rewrote);
    assertTrue(expressionRewriter.isUnsupportedPartFnFound());
    // rewrite left hand. Rewrite year(y) to y.
    Expression year = new FuncCallExpr(col, Type.YEAR);
    exprToBeRewrited = ComparisonBinaryExpression.lessEqual(year, Constant.create("1995", IntegerType.INT));
    rewrote = expressionRewriter.rewrite(exprToBeRewrited);
    assertEquals("[y@DATE LESS_EQUAL \"1995\"]", rewrote.toString());
    // simple column case. No rewriting happens.
    exprToBeRewrited = ComparisonBinaryExpression.lessEqual(col, Constant.create(1, IntegerType.INT));
    expressionRewriter = new PartAndFilterExprRewriter(col);
    rewrote = expressionRewriter.rewrite(exprToBeRewrited);
    assertEquals("[y@DATE LESS_EQUAL 1]", rewrote.toString());
}
Also used : Not(com.pingcap.tikv.expression.Not) Expression(com.pingcap.tikv.expression.Expression) ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) DateTime(org.joda.time.DateTime) FuncCallExpr(com.pingcap.tikv.expression.FuncCallExpr) Test(org.junit.Test)

Example 12 with Expression

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

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

the class PartAndFilterExprRewriter method visit.

public Expression visit(LogicalBinaryExpression node, Void context) {
    Expression left = node.getLeft().accept(this, null);
    Expression right = node.getRight().accept(this, null);
    return new LogicalBinaryExpression(node.getCompType(), left, right);
}
Also used : LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) Expression(com.pingcap.tikv.expression.Expression)

Example 14 with Expression

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

the class ProtoConverter method visit.

@Override
protected Expr visit(ArithmeticBinaryExpression node, Object context) {
    // assume after type coerce, children should be compatible
    Expression child = node.getLeft();
    String typeSignature = getTypeSignature(child);
    ScalarFuncSig protoSig;
    switch(node.getCompType()) {
        // TODO: Add test for bitwise push down
        case BIT_AND:
            protoSig = ScalarFuncSig.BitAndSig;
            break;
        case BIT_OR:
            protoSig = ScalarFuncSig.BitOrSig;
            break;
        case BIT_XOR:
            protoSig = ScalarFuncSig.BitXorSig;
            break;
        case DIVIDE:
            protoSig = ScalarFuncSig.valueOf("Divide" + typeSignature);
            break;
        case MINUS:
            protoSig = ScalarFuncSig.valueOf("Minus" + typeSignature);
            break;
        case MULTIPLY:
            protoSig = ScalarFuncSig.valueOf("Multiply" + typeSignature);
            break;
        case PLUS:
            protoSig = ScalarFuncSig.valueOf("Plus" + typeSignature);
            break;
        default:
            throw new TiExpressionException(String.format("Unknown comparison type %s", node.getCompType()));
    }
    Expr.Builder builder = scalarToPartialProto(node, context);
    builder.setSig(protoSig);
    builder.setFieldType(toPBFieldType(getType(node)));
    return builder.build();
}
Also used : Expr(com.pingcap.tidb.tipb.Expr) FuncCallExpr(com.pingcap.tikv.expression.FuncCallExpr) Expression(com.pingcap.tikv.expression.Expression) ArithmeticBinaryExpression(com.pingcap.tikv.expression.ArithmeticBinaryExpression) StringRegExpression(com.pingcap.tikv.expression.StringRegExpression) ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) TiExpressionException(com.pingcap.tikv.exception.TiExpressionException) ScalarFuncSig(com.pingcap.tidb.tipb.ScalarFuncSig)

Example 15 with Expression

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

the class ProtoConverter method visit.

@Override
protected Expr visit(AggregateFunction node, Object context) {
    Expr.Builder builder = Expr.newBuilder();
    FunctionType type = node.getType();
    switch(type) {
        case Max:
            builder.setTp(ExprType.Max);
            break;
        case Sum:
            builder.setTp(ExprType.Sum);
            break;
        case Min:
            builder.setTp(ExprType.Min);
            break;
        case First:
            builder.setTp(ExprType.First);
            break;
        case Count:
            builder.setTp(ExprType.Count);
            break;
    }
    for (Expression arg : node.getChildren()) {
        Expr exprProto = arg.accept(this, context);
        builder.addChildren(exprProto);
    }
    builder.setFieldType(toPBFieldType(getType(node)));
    return builder.build();
}
Also used : Expr(com.pingcap.tidb.tipb.Expr) FuncCallExpr(com.pingcap.tikv.expression.FuncCallExpr) Expression(com.pingcap.tikv.expression.Expression) ArithmeticBinaryExpression(com.pingcap.tikv.expression.ArithmeticBinaryExpression) StringRegExpression(com.pingcap.tikv.expression.StringRegExpression) ComparisonBinaryExpression(com.pingcap.tikv.expression.ComparisonBinaryExpression) LogicalBinaryExpression(com.pingcap.tikv.expression.LogicalBinaryExpression) FunctionType(com.pingcap.tikv.expression.AggregateFunction.FunctionType)

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