Search in sources :

Example 26 with LogicalExpression

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

the class DrillDecimalAddFuncHolder method getReturnType.

/*
   * This function scope is used by add and subtract functions for decimal data type.
   * DecimalScalePrecisionAddFunction is used to compute the output types'
   * scale and precision
   */
@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    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;
                break;
            }
        }
    }
    /* Get the result's scale and precision. This is a function scope for add function, assert we have
     * only two inputs
     */
    assert args.size() == 2;
    DecimalScalePrecisionAddFunction outputScalePrec = new DecimalScalePrecisionAddFunction(args.get(0).getMajorType().getPrecision(), args.get(0).getMajorType().getScale(), args.get(1).getMajorType().getPrecision(), args.get(1).getMajorType().getScale());
    return (TypeProtos.MajorType.newBuilder().setMinorType(DecimalUtility.getDecimalDataType(outputScalePrec.getOutputPrecision())).setScale(outputScalePrec.getOutputScale()).setPrecision(outputScalePrec.getOutputPrecision()).setMode(mode).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) DecimalScalePrecisionAddFunction(org.apache.drill.common.util.DecimalScalePrecisionAddFunction) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 27 with LogicalExpression

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

the class DrillDecimalAggFuncHolder method getReturnType.

@Override
public TypeProtos.MajorType getReturnType(List<LogicalExpression> args) {
    int scale = 0;
    int precision = 0;
    // Get the max scale and precision from the inputs
    for (LogicalExpression e : args) {
        scale = Math.max(scale, e.getMajorType().getScale());
        precision = Math.max(precision, e.getMajorType().getPrecision());
    }
    return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(scale).setPrecision(precision).setMode(TypeProtos.DataMode.REQUIRED).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression)

Example 28 with LogicalExpression

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

the class DrillDecimalCastFuncHolder method getReturnType.

@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    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;
                break;
            }
        }
    }
    if (args.size() != 3) {
        StringBuilder err = new StringBuilder();
        for (int i = 0; i < args.size(); i++) {
            err.append("arg" + i + ": " + args.get(i).getMajorType().getMinorType());
        }
        throw new DrillRuntimeException("Decimal cast function invoked with incorect arguments" + err);
    }
    int scale = (int) ((ValueExpressions.LongExpression) (args.get(args.size() - 1))).getLong();
    int precision = (int) ((ValueExpressions.LongExpression) (args.get(args.size() - 2))).getLong();
    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) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 29 with LogicalExpression

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

the class DrillDecimalDivScaleFuncHolder method getReturnType.

/*
   * This function scope is used by divide functions for decimal data type.
   * DecimalScalePrecisionDivideFunction is used to compute the output types'
   * scale and precision
   */
@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    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;
                break;
            }
        }
    }
    /* Get the result's scale and precision. This is a function scope for Divide function, assert we have
     * only two inputs
     */
    assert args.size() == 2;
    DecimalScalePrecisionDivideFunction outputScalePrec = new DecimalScalePrecisionDivideFunction(args.get(0).getMajorType().getPrecision(), args.get(0).getMajorType().getScale(), args.get(1).getMajorType().getPrecision(), args.get(1).getMajorType().getScale());
    return (TypeProtos.MajorType.newBuilder().setMinorType(DecimalUtility.getDecimalDataType(outputScalePrec.getOutputPrecision())).setScale(outputScalePrec.getOutputScale()).setPrecision(outputScalePrec.getOutputPrecision()).setMode(mode).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) DecimalScalePrecisionDivideFunction(org.apache.drill.common.util.DecimalScalePrecisionDivideFunction) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 30 with LogicalExpression

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

the class DrillDecimalMaxScaleFuncHolder method getReturnType.

@Override
public MajorType getReturnType(List<LogicalExpression> args) {
    TypeProtos.DataMode mode = returnValue.type.getMode();
    boolean nullInput = false;
    int scale = 0;
    int precision = 0;
    for (LogicalExpression e : args) {
        if (e.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
            nullInput = true;
        }
        scale = Math.max(scale, e.getMajorType().getScale());
        precision = Math.max(precision, e.getMajorType().getPrecision());
    }
    if (nullHandling == NullHandling.NULL_IF_NULL && nullInput) {
        mode = TypeProtos.DataMode.OPTIONAL;
    }
    return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(scale).setPrecision(precision).setMode(mode).build());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) TypeProtos(org.apache.drill.common.types.TypeProtos)

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