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;
}
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();
}
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;
}
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();
}
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;
}
Aggregations