Search in sources :

Example 1 with ValueHolder

use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.

the class DrillConstExecutor method reduce.

@Override
public void reduce(final RexBuilder rexBuilder, List<RexNode> constExps, final List<RexNode> reducedValues) {
    for (final RexNode newCall : constExps) {
        LogicalExpression logEx = DrillOptiq.toDrill(new DrillParseContext(plannerSettings), (RelNode) null, /* input rel */
        newCall);
        ErrorCollectorImpl errors = new ErrorCollectorImpl();
        final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(logEx, null, errors, funcImplReg);
        if (errors.getErrorCount() != 0) {
            String message = String.format("Failure while materializing expression in constant expression evaluator [%s].  Errors: %s", newCall.toString(), errors.toString());
            throw UserException.planError().message(message).build(logger);
        }
        if (NON_REDUCIBLE_TYPES.contains(materializedExpr.getMajorType().getMinorType())) {
            logger.debug("Constant expression not folded due to return type {}, complete expression: {}", materializedExpr.getMajorType(), ExpressionStringBuilder.toString(materializedExpr));
            reducedValues.add(newCall);
            continue;
        }
        ValueHolder output = InterpreterEvaluator.evaluateConstantExpr(udfUtilities, materializedExpr);
        final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
        if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL && TypeHelper.isNull(output)) {
            SqlTypeName sqlTypeName = TypeInferenceUtils.getCalciteTypeFromDrillType(materializedExpr.getMajorType().getMinorType());
            if (sqlTypeName == null) {
                String message = String.format("Error reducing constant expression, unsupported type: %s.", materializedExpr.getMajorType().getMinorType());
                throw UserException.unsupportedError().message(message).build(logger);
            }
            reducedValues.add(rexBuilder.makeNullLiteral(sqlTypeName));
            continue;
        }
        Function<ValueHolder, RexNode> literator = new Function<ValueHolder, RexNode>() {

            @Override
            public RexNode apply(ValueHolder output) {
                switch(materializedExpr.getMajorType().getMinorType()) {
                    case INT:
                        {
                            int value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableIntHolder) output).value : ((IntHolder) output).value;
                            return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTEGER, newCall.getType().isNullable()), false);
                        }
                    case BIGINT:
                        {
                            long value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableBigIntHolder) output).value : ((BigIntHolder) output).value;
                            return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.BIGINT, newCall.getType().isNullable()), false);
                        }
                    case FLOAT4:
                        {
                            float value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableFloat4Holder) output).value : ((Float4Holder) output).value;
                            return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.FLOAT, newCall.getType().isNullable()), false);
                        }
                    case FLOAT8:
                        {
                            double value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableFloat8Holder) output).value : ((Float8Holder) output).value;
                            return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DOUBLE, newCall.getType().isNullable()), false);
                        }
                    case VARCHAR:
                        {
                            String value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? StringFunctionHelpers.getStringFromVarCharHolder((NullableVarCharHolder) output) : StringFunctionHelpers.getStringFromVarCharHolder((VarCharHolder) output);
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.VARCHAR, newCall.getType().isNullable()), false);
                        }
                    case BIT:
                        {
                            boolean value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableBitHolder) output).value == 1 : ((BitHolder) output).value == 1;
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.BOOLEAN, newCall.getType().isNullable()), false);
                        }
                    case DATE:
                        {
                            Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableDateHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((DateHolder) output).value, DateTimeZone.UTC).toCalendar(null);
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DATE, newCall.getType().isNullable()), false);
                        }
                    case DECIMAL9:
                        {
                            long value;
                            int scale;
                            if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                                NullableDecimal9Holder decimal9Out = (NullableDecimal9Holder) output;
                                value = decimal9Out.value;
                                scale = decimal9Out.scale;
                            } else {
                                Decimal9Holder decimal9Out = (Decimal9Holder) output;
                                value = decimal9Out.value;
                                scale = decimal9Out.scale;
                            }
                            return rexBuilder.makeLiteral(new BigDecimal(BigInteger.valueOf(value), scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
                        }
                    case DECIMAL18:
                        {
                            long value;
                            int scale;
                            if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                                NullableDecimal18Holder decimal18Out = (NullableDecimal18Holder) output;
                                value = decimal18Out.value;
                                scale = decimal18Out.scale;
                            } else {
                                Decimal18Holder decimal18Out = (Decimal18Holder) output;
                                value = decimal18Out.value;
                                scale = decimal18Out.scale;
                            }
                            return rexBuilder.makeLiteral(new BigDecimal(BigInteger.valueOf(value), scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
                        }
                    case DECIMAL28SPARSE:
                        {
                            DrillBuf buffer;
                            int start;
                            int scale;
                            if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                                NullableDecimal28SparseHolder decimal28Out = (NullableDecimal28SparseHolder) output;
                                buffer = decimal28Out.buffer;
                                start = decimal28Out.start;
                                scale = decimal28Out.scale;
                            } else {
                                Decimal28SparseHolder decimal28Out = (Decimal28SparseHolder) output;
                                buffer = decimal28Out.buffer;
                                start = decimal28Out.start;
                                scale = decimal28Out.scale;
                            }
                            return rexBuilder.makeLiteral(org.apache.drill.exec.util.DecimalUtility.getBigDecimalFromSparse(buffer, start * 20, 5, scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
                        }
                    case DECIMAL38SPARSE:
                        {
                            DrillBuf buffer;
                            int start;
                            int scale;
                            if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                                NullableDecimal38SparseHolder decimal38Out = (NullableDecimal38SparseHolder) output;
                                buffer = decimal38Out.buffer;
                                start = decimal38Out.start;
                                scale = decimal38Out.scale;
                            } else {
                                Decimal38SparseHolder decimal38Out = (Decimal38SparseHolder) output;
                                buffer = decimal38Out.buffer;
                                start = decimal38Out.start;
                                scale = decimal38Out.scale;
                            }
                            return rexBuilder.makeLiteral(org.apache.drill.exec.util.DecimalUtility.getBigDecimalFromSparse(buffer, start * 24, 6, scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
                        }
                    case TIME:
                        {
                            Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableTimeHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((TimeHolder) output).value, DateTimeZone.UTC).toCalendar(null);
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.TIME, newCall.getType().isNullable()), false);
                        }
                    case TIMESTAMP:
                        {
                            Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableTimeStampHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((TimeStampHolder) output).value, DateTimeZone.UTC).toCalendar(null);
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.TIMESTAMP, newCall.getType().isNullable()), false);
                        }
                    case INTERVALYEAR:
                        {
                            BigDecimal value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new BigDecimal(((NullableIntervalYearHolder) output).value) : new BigDecimal(((IntervalYearHolder) output).value);
                            return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTERVAL_YEAR_MONTH, newCall.getType().isNullable()), false);
                        }
                    case INTERVALDAY:
                        {
                            int days;
                            int milliseconds;
                            if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
                                NullableIntervalDayHolder intervalDayOut = (NullableIntervalDayHolder) output;
                                days = intervalDayOut.days;
                                milliseconds = intervalDayOut.milliseconds;
                            } else {
                                IntervalDayHolder intervalDayOut = (IntervalDayHolder) output;
                                days = intervalDayOut.days;
                                milliseconds = intervalDayOut.milliseconds;
                            }
                            return rexBuilder.makeLiteral(new BigDecimal(days * DateUtility.daysToStandardMillis + milliseconds), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTERVAL_DAY_TIME, newCall.getType().isNullable()), false);
                        }
                    // as new types may be added in the future.
                    default:
                        logger.debug("Constant expression not folded due to return type {}, complete expression: {}", materializedExpr.getMajorType(), ExpressionStringBuilder.toString(materializedExpr));
                        return newCall;
                }
            }
        };
        reducedValues.add(literator.apply(output));
    }
}
Also used : SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NullableFloat8Holder(org.apache.drill.exec.expr.holders.NullableFloat8Holder) NullableDecimal9Holder(org.apache.drill.exec.expr.holders.NullableDecimal9Holder) NullableFloat4Holder(org.apache.drill.exec.expr.holders.NullableFloat4Holder) Float4Holder(org.apache.drill.exec.expr.holders.Float4Holder) Decimal9Holder(org.apache.drill.exec.expr.holders.Decimal9Holder) NullableDecimal9Holder(org.apache.drill.exec.expr.holders.NullableDecimal9Holder) DateTime(org.joda.time.DateTime) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) Function(com.google.common.base.Function) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) NullableBigIntHolder(org.apache.drill.exec.expr.holders.NullableBigIntHolder) NullableTimeStampHolder(org.apache.drill.exec.expr.holders.NullableTimeStampHolder) TimeStampHolder(org.apache.drill.exec.expr.holders.TimeStampHolder) NullableIntHolder(org.apache.drill.exec.expr.holders.NullableIntHolder) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) NullableBigIntHolder(org.apache.drill.exec.expr.holders.NullableBigIntHolder) IntHolder(org.apache.drill.exec.expr.holders.IntHolder) NullableIntHolder(org.apache.drill.exec.expr.holders.NullableIntHolder) NullableDateHolder(org.apache.drill.exec.expr.holders.NullableDateHolder) DrillBuf(io.netty.buffer.DrillBuf) NullableTimeStampHolder(org.apache.drill.exec.expr.holders.NullableTimeStampHolder) NullableIntervalDayHolder(org.apache.drill.exec.expr.holders.NullableIntervalDayHolder) Decimal18Holder(org.apache.drill.exec.expr.holders.Decimal18Holder) NullableDecimal18Holder(org.apache.drill.exec.expr.holders.NullableDecimal18Holder) DateHolder(org.apache.drill.exec.expr.holders.DateHolder) NullableDateHolder(org.apache.drill.exec.expr.holders.NullableDateHolder) NullableTimeHolder(org.apache.drill.exec.expr.holders.NullableTimeHolder) NullableIntervalDayHolder(org.apache.drill.exec.expr.holders.NullableIntervalDayHolder) IntervalDayHolder(org.apache.drill.exec.expr.holders.IntervalDayHolder) Calendar(java.util.Calendar) NullableDecimal38SparseHolder(org.apache.drill.exec.expr.holders.NullableDecimal38SparseHolder) ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder) NullableFloat8Holder(org.apache.drill.exec.expr.holders.NullableFloat8Holder) Float8Holder(org.apache.drill.exec.expr.holders.Float8Holder) BigDecimal(java.math.BigDecimal) NullableDecimal28SparseHolder(org.apache.drill.exec.expr.holders.NullableDecimal28SparseHolder) NullableBigIntHolder(org.apache.drill.exec.expr.holders.NullableBigIntHolder) NullableDecimal18Holder(org.apache.drill.exec.expr.holders.NullableDecimal18Holder) Decimal38SparseHolder(org.apache.drill.exec.expr.holders.Decimal38SparseHolder) NullableDecimal38SparseHolder(org.apache.drill.exec.expr.holders.NullableDecimal38SparseHolder) TimeHolder(org.apache.drill.exec.expr.holders.TimeHolder) NullableTimeHolder(org.apache.drill.exec.expr.holders.NullableTimeHolder) NullableFloat4Holder(org.apache.drill.exec.expr.holders.NullableFloat4Holder) Decimal28SparseHolder(org.apache.drill.exec.expr.holders.Decimal28SparseHolder) NullableDecimal28SparseHolder(org.apache.drill.exec.expr.holders.NullableDecimal28SparseHolder) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with ValueHolder

use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.

the class QueryContext method getConstantValueHolder.

@Override
public ValueHolder getConstantValueHolder(String value, MinorType type, Function<DrillBuf, ValueHolder> holderInitializer) {
    if (!constantValueHolderCache.containsKey(value)) {
        constantValueHolderCache.put(value, Maps.<MinorType, ValueHolder>newHashMap());
    }
    Map<MinorType, ValueHolder> holdersByType = constantValueHolderCache.get(value);
    ValueHolder valueHolder = holdersByType.get(type);
    if (valueHolder == null) {
        valueHolder = holderInitializer.apply(getManagedBuffer());
        holdersByType.put(type, valueHolder);
    }
    return valueHolder;
}
Also used : MinorType(org.apache.drill.common.types.TypeProtos.MinorType) ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder)

Example 3 with ValueHolder

use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.

the class ParquetFilterBuilder method visitFunctionHolderExpression.

@Override
public LogicalExpression visitFunctionHolderExpression(FunctionHolderExpression funcHolderExpr, Set<LogicalExpression> value) throws RuntimeException {
    FuncHolder holder = funcHolderExpr.getHolder();
    if (!(holder instanceof DrillSimpleFuncHolder)) {
        return null;
    }
    if (value.contains(funcHolderExpr)) {
        ValueHolder result;
        try {
            result = InterpreterEvaluator.evaluateConstantExpr(udfUtilities, funcHolderExpr);
        } catch (Exception e) {
            logger.warn("Error in evaluating function of {}", funcHolderExpr.getName());
            return null;
        }
        logger.debug("Reduce a constant function expression into a value expression");
        return getValueExpressionFromConst(result, funcHolderExpr.getMajorType().getMinorType());
    }
    final String funcName = ((DrillSimpleFuncHolder) holder).getRegisteredNames()[0];
    if (isCompareFunction(funcName)) {
        return handleCompareFunction(funcHolderExpr, value);
    }
    if (CastFunctions.isCastFunction(funcName)) {
        List<LogicalExpression> newArgs = new ArrayList();
        for (LogicalExpression arg : funcHolderExpr.args) {
            final LogicalExpression newArg = arg.accept(this, value);
            if (newArg == null) {
                return null;
            }
            newArgs.add(newArg);
        }
        return funcHolderExpr.copy(newArgs);
    } else {
        return null;
    }
}
Also used : DrillSimpleFuncHolder(org.apache.drill.exec.expr.fn.DrillSimpleFuncHolder) FuncHolder(org.apache.drill.common.expression.fn.FuncHolder) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ArrayList(java.util.ArrayList) ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder) DrillSimpleFuncHolder(org.apache.drill.exec.expr.fn.DrillSimpleFuncHolder)

Example 4 with ValueHolder

use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.

the class InterpreterEvaluator method evaluate.

public static void evaluate(int recordCount, UdfUtilities udfUtilities, VectorAccessible incoming, ValueVector outVV, LogicalExpression expr) {
    InitVisitor initVisitor = new InitVisitor(udfUtilities);
    EvalVisitor evalVisitor = new EvalVisitor(incoming, udfUtilities);
    expr.accept(initVisitor, incoming);
    for (int i = 0; i < recordCount; i++) {
        ValueHolder out = expr.accept(evalVisitor, i);
        TypeHelper.setValueSafe(outVV, i, out);
    }
    outVV.getMutator().setValueCount(recordCount);
}
Also used : ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder)

Example 5 with ValueHolder

use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.

the class InterpreterEvaluator method evaluateFunction.

public static ValueHolder evaluateFunction(DrillSimpleFunc interpreter, ValueHolder[] args, String funcName) throws Exception {
    Preconditions.checkArgument(interpreter != null, "interpreter could not be null when use interpreted model to evaluate function " + funcName);
    // the current input index to assign into the next available parameter, found using the @Param notation
    // the order parameters are declared in the java class for the DrillFunc is meaningful
    int currParameterIndex = 0;
    Field outField = null;
    try {
        Field[] fields = interpreter.getClass().getDeclaredFields();
        for (Field f : fields) {
            // if this is annotated as a parameter to the function
            if (f.getAnnotation(Param.class) != null) {
                f.setAccessible(true);
                if (currParameterIndex < args.length) {
                    f.set(interpreter, args[currParameterIndex]);
                }
                currParameterIndex++;
            } else if (f.getAnnotation(Output.class) != null) {
                f.setAccessible(true);
                outField = f;
                // create an instance of the holder for the output to be stored in
                f.set(interpreter, f.getType().newInstance());
            }
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
    if (args.length != currParameterIndex) {
        throw new DrillRuntimeException(String.format("Wrong number of parameters provided to interpreted expression evaluation " + "for function %s, expected %d parameters, but received %d.", funcName, currParameterIndex, args.length));
    }
    if (outField == null) {
        throw new DrillRuntimeException("Malformed DrillFunction without a return type: " + funcName);
    }
    interpreter.setup();
    interpreter.eval();
    ValueHolder out = (ValueHolder) outField.get(interpreter);
    return out;
}
Also used : Field(java.lang.reflect.Field) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) Param(org.apache.drill.exec.expr.annotations.Param) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) ValueHolder(org.apache.drill.exec.expr.holders.ValueHolder)

Aggregations

ValueHolder (org.apache.drill.exec.expr.holders.ValueHolder)8 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)3 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)2 Param (org.apache.drill.exec.expr.annotations.Param)2 DrillSimpleFuncHolder (org.apache.drill.exec.expr.fn.DrillSimpleFuncHolder)2 Function (com.google.common.base.Function)1 DrillBuf (io.netty.buffer.DrillBuf)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Inject (javax.inject.Inject)1 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)1 RexNode (org.apache.calcite.rex.RexNode)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)1 FuncHolder (org.apache.drill.common.expression.fn.FuncHolder)1 FieldDescriptor (org.apache.drill.common.scanner.persistence.FieldDescriptor)1