Search in sources :

Example 1 with IfCondition

use of org.apache.drill.common.expression.IfExpression.IfCondition in project drill by apache.

the class FunctionGenerationHelper method getTypeComparisonFunction.

/**
   * Wraps the comparison function in an If-statement which compares the types first, evaluating the comaprison function only
   * if the types are equivialent
   *
   * @param comparisonFunction
   * @param args
   * @return
   */
private static LogicalExpression getTypeComparisonFunction(LogicalExpression comparisonFunction, HoldingContainer... args) {
    List<LogicalExpression> argExpressions = Lists.newArrayList();
    List<MajorType> argTypes = Lists.newArrayList();
    for (HoldingContainer c : args) {
        argTypes.add(c.getMajorType());
        argExpressions.add(new HoldingContainerExpression(c));
    }
    FunctionCall call = new FunctionCall("compareType", argExpressions, ExpressionPosition.UNKNOWN);
    List<LogicalExpression> newArgs = Lists.newArrayList();
    newArgs.add(call);
    newArgs.add(new IntExpression(0, ExpressionPosition.UNKNOWN));
    FunctionCall notEqual = new FunctionCall("not_equal", newArgs, ExpressionPosition.UNKNOWN);
    IfExpression.IfCondition ifCondition = new IfCondition(notEqual, call);
    IfExpression ifExpression = IfExpression.newBuilder().setIfCondition(ifCondition).setElse(comparisonFunction).build();
    return ifExpression;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) IntExpression(org.apache.drill.common.expression.ValueExpressions.IntExpression) FunctionCall(org.apache.drill.common.expression.FunctionCall) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition) HoldingContainerExpression(org.apache.drill.exec.expr.HoldingContainerExpression) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 2 with IfCondition

use of org.apache.drill.common.expression.IfExpression.IfCondition in project drill by apache.

the class CloneVisitor method visitIfExpression.

@Override
public LogicalExpression visitIfExpression(IfExpression ifExpr, Void value) throws RuntimeException {
    LogicalExpression ifCondition = ifExpr.ifCondition.condition.accept(this, null);
    LogicalExpression ifExpression = ifExpr.ifCondition.expression.accept(this, null);
    LogicalExpression elseExpression = ifExpr.elseExpression.accept(this, null);
    IfExpression.IfCondition condition = new IfCondition(ifCondition, ifExpression);
    return IfExpression.newBuilder().setIfCondition(condition).setElse(elseExpression).build();
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 3 with IfCondition

use of org.apache.drill.common.expression.IfExpression.IfCondition in project drill by apache.

the class ExpressionStringBuilder method visitIfExpression.

@Override
public Void visitIfExpression(IfExpression ifExpr, StringBuilder sb) throws RuntimeException {
    // serialize the if expression
    sb.append(" ( ");
    IfCondition c = ifExpr.ifCondition;
    sb.append("if (");
    c.condition.accept(this, sb);
    sb.append(" ) then (");
    c.expression.accept(this, sb);
    sb.append(" ) ");
    sb.append(" else (");
    ifExpr.elseExpression.accept(this, sb);
    sb.append(" ) ");
    sb.append(" end ");
    sb.append(" ) ");
    return null;
}
Also used : IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 4 with IfCondition

use of org.apache.drill.common.expression.IfExpression.IfCondition in project drill by apache.

the class ConditionalExprOptimizer method visitIfExpression.

@Override
public LogicalExpression visitIfExpression(IfExpression ifExpr, Void value) throws RuntimeException {
    LogicalExpression newElseExpr = ifExpr.elseExpression.accept(this, value);
    IfCondition conditions = ifExpr.ifCondition;
    LogicalExpression newCondition = conditions.condition.accept(this, value);
    LogicalExpression newExpr = conditions.expression.accept(this, value);
    conditions = new IfExpression.IfCondition(newCondition, newExpr);
    return IfExpression.newBuilder().setElse(newElseExpr).setIfCondition(conditions).build();
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) IfExpression(org.apache.drill.common.expression.IfExpression) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Example 5 with IfCondition

use of org.apache.drill.common.expression.IfExpression.IfCondition in project drill by apache.

the class ExpressionValidator method visitIfExpression.

@Override
public Void visitIfExpression(IfExpression ifExpr, ErrorCollector errors) throws RuntimeException {
    // confirm that all conditions are required boolean values.
    IfCondition cond = ifExpr.ifCondition;
    MajorType majorType = cond.condition.getMajorType();
    if (majorType.getMinorType() != MinorType.BIT) {
        errors.addGeneralError(cond.condition.getPosition(), String.format("Failure composing If Expression.  All conditions must return a boolean type.  Condition was of Type %s.", majorType.getMinorType()));
    }
    // confirm that all outcomes are the same type.
    final MajorType mt = ifExpr.elseExpression.getMajorType();
    cond = ifExpr.ifCondition;
    MajorType innerT = cond.expression.getMajorType();
    if (//
    (innerT.getMode() == DataMode.REPEATED && mt.getMode() != DataMode.REPEATED) || ((innerT.getMinorType() != mt.getMinorType()) && (innerT.getMode() != DataMode.OPTIONAL && mt.getMode() != DataMode.OPTIONAL && (innerT.getMinorType() != MinorType.NULL && mt.getMinorType() != MinorType.NULL)))) {
        errors.addGeneralError(cond.condition.getPosition(), String.format("Failure composing If Expression.  All expressions must return the same MinorType as the else expression.  The if condition returned type type %s but the else expression was of type %s", innerT, mt));
    }
    return null;
}
Also used : MajorType(org.apache.drill.common.types.TypeProtos.MajorType) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Aggregations

IfCondition (org.apache.drill.common.expression.IfExpression.IfCondition)5 IfExpression (org.apache.drill.common.expression.IfExpression)3 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)3 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2 FunctionCall (org.apache.drill.common.expression.FunctionCall)1 IntExpression (org.apache.drill.common.expression.ValueExpressions.IntExpression)1 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)1 HoldingContainerExpression (org.apache.drill.exec.expr.HoldingContainerExpression)1