Search in sources :

Example 31 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.

the class DrillDecimalSetScaleFuncHolder method getReturnType.

@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    TypeProtos.DataMode mode = returnValue.type.getMode();
    int scale = 0;
    int precision = 0;
    int i = 0;
    if (nullHandling == NullHandling.NULL_IF_NULL) {
        // if any one of the input types is nullable, then return nullable return type
        for (LogicalExpression e : args) {
            precision = Math.max(precision, e.getMajorType().getPrecision());
            if (e.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                mode = TypeProtos.DataMode.OPTIONAL;
            }
        }
        /* Used by functions like round, truncate which specify the scale for
       * the output as the second argument
       */
        assert (args.size() == 2) && (args.get(1) instanceof ValueExpressions.IntExpression);
        // Get the scale from the second argument which should be a constant
        scale = ((ValueExpressions.IntExpression) args.get(1)).getInt();
    }
    return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(scale).setPrecision(precision).setMode(mode).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 32 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.

the class DrillDecimalZeroScaleFuncHolder method getReturnType.

/* This function scope is used when we need to remove the scale part.
   * trunc and round functions with single argument use this
   */
@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    int precision = 0;
    TypeProtos.DataMode mode = returnValue.type.getMode();
    if (nullHandling == NullHandling.NULL_IF_NULL) {
        // if any one of the input types is nullable, then return nullable return type
        for (LogicalExpression e : args) {
            if (e.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                mode = TypeProtos.DataMode.OPTIONAL;
            }
            precision = Math.max(precision, e.getMajorType().getPrecision());
        }
    }
    return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(0).setPrecision(precision).setMode(mode).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 33 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.

the class FunctionGenerationHelper method getOrderingComparator.

/**
 * Finds ordering comparator ("compare_to...") FunctionHolderExpression with
 * a specified ordering for NULL (and considering NULLS <i>equal</i>).
 * @param  null_high  whether NULL should compare as the lowest value (if
 *                    {@code false}) or the highest value (if {@code true})
 * @param  left  ...
 * @param  right  ...
 * @param  functionLookupContext  ...
 * @return
 *     FunctionHolderExpression containing the found function implementation
 */
public static LogicalExpression getOrderingComparator(boolean null_high, HoldingContainer left, HoldingContainer right, FunctionLookupContext functionLookupContext) {
    final String comparator_name = null_high ? COMPARE_TO_NULLS_HIGH : COMPARE_TO_NULLS_LOW;
    if (!isComparableType(left.getMajorType()) || !isComparableType(right.getMajorType())) {
        throw new UnsupportedOperationException(formatCanNotCompareMsg(left.getMajorType(), right.getMajorType()));
    }
    LogicalExpression comparisonFunctionExpression = getFunctionExpression(comparator_name, left, right);
    ErrorCollector collector = new ErrorCollectorImpl();
    if (!isUnionType(left.getMajorType()) && !isUnionType(right.getMajorType())) {
        return ExpressionTreeMaterializer.materialize(comparisonFunctionExpression, null, collector, functionLookupContext);
    } else {
        LogicalExpression typeComparisonFunctionExpression = getTypeComparisonFunction(comparisonFunctionExpression, left, right);
        return ExpressionTreeMaterializer.materialize(typeComparisonFunctionExpression, null, collector, functionLookupContext);
    }
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ErrorCollector(org.apache.drill.common.expression.ErrorCollector)

Example 34 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.

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 35 with LogicalExpression

use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.

the class ExpressionTreeMaterializer method materializeFilterExpr.

public static LogicalExpression materializeFilterExpr(LogicalExpression expr, Map<SchemaPath, ColumnStatistics> fieldTypes, ErrorCollector errorCollector, FunctionLookupContext functionLookupContext) {
    final FilterMaterializeVisitor filterMaterializeVisitor = new FilterMaterializeVisitor(fieldTypes, errorCollector);
    LogicalExpression out = expr.accept(filterMaterializeVisitor, functionLookupContext);
    return out;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)287 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)78 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)77 FunctionCall (org.apache.drill.common.expression.FunctionCall)44 SchemaPath (org.apache.drill.common.expression.SchemaPath)43 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)42 FieldReference (org.apache.drill.common.expression.FieldReference)39 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)38 RexNode (org.apache.calcite.rex.RexNode)32 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)31 DrillParseContext (org.apache.drill.exec.planner.logical.DrillParseContext)30 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)30 ArrayList (java.util.ArrayList)29 MaterializedField (org.apache.drill.exec.record.MaterializedField)28 JConditional (com.sun.codemodel.JConditional)26 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)23 Ordering (org.apache.drill.common.logical.data.Order.Ordering)23 ValueVector (org.apache.drill.exec.vector.ValueVector)22 Test (org.junit.Test)22 TypeProtos (org.apache.drill.common.types.TypeProtos)20