Search in sources :

Example 1 with FunctionCall

use of org.apache.drill.common.expression.FunctionCall in project drill by apache.

the class WindowFrameRecordBatch method createFramers.

private void createFramers(VectorAccessible batch) throws SchemaChangeException, IOException, ClassTransformationException {
    assert framers == null : "createFramer should only be called once";
    logger.trace("creating framer(s)");
    final List<LogicalExpression> keyExprs = Lists.newArrayList();
    final List<LogicalExpression> orderExprs = Lists.newArrayList();
    boolean requireFullPartition = false;
    // at least one window function uses the DefaultFrameTemplate
    boolean useDefaultFrame = false;
    // at least one window function uses the CustomFrameTemplate
    boolean useCustomFrame = false;
    hasOrderBy = popConfig.getOrderings().size() > 0;
    // all existing vectors will be transferred to the outgoing container in framer.doWork()
    for (final VectorWrapper<?> wrapper : batch) {
        container.addOrGet(wrapper.getField());
    }
    // add aggregation vectors to the container, and materialize corresponding expressions
    for (final NamedExpression ne : popConfig.getAggregations()) {
        if (!(ne.getExpr() instanceof FunctionCall)) {
            throw UserException.functionError().message("Unsupported window function '%s'", ne.getExpr()).build(logger);
        }
        final FunctionCall call = (FunctionCall) ne.getExpr();
        final WindowFunction winfun = WindowFunction.fromExpression(call);
        if (winfun.materialize(ne, container, context.getFunctionRegistry())) {
            functions.add(winfun);
            requireFullPartition |= winfun.requiresFullPartition(popConfig);
            if (winfun.supportsCustomFrames()) {
                useCustomFrame = true;
            } else {
                useDefaultFrame = true;
            }
        }
    }
    container.buildSchema(BatchSchema.SelectionVectorMode.NONE);
    container.setRecordCount(0);
    // materialize partition by expressions
    for (final NamedExpression ne : popConfig.getWithins()) {
        keyExprs.add(ExpressionTreeMaterializer.materializeAndCheckErrors(ne.getExpr(), batch, context.getFunctionRegistry()));
    }
    // materialize order by expressions
    for (final Order.Ordering oe : popConfig.getOrderings()) {
        orderExprs.add(ExpressionTreeMaterializer.materializeAndCheckErrors(oe.getExpr(), batch, context.getFunctionRegistry()));
    }
    // count how many framers we need
    int numFramers = useDefaultFrame ? 1 : 0;
    numFramers += useCustomFrame ? 1 : 0;
    assert numFramers > 0 : "No framer was needed!";
    framers = new WindowFramer[numFramers];
    int index = 0;
    if (useDefaultFrame) {
        framers[index] = generateFramer(keyExprs, orderExprs, functions, false);
        framers[index].setup(batches, container, oContext, requireFullPartition, popConfig);
        index++;
    }
    if (useCustomFrame) {
        framers[index] = generateFramer(keyExprs, orderExprs, functions, true);
        framers[index].setup(batches, container, oContext, requireFullPartition, popConfig);
    }
}
Also used : Order(org.apache.drill.common.logical.data.Order) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Example 2 with FunctionCall

use of org.apache.drill.common.expression.FunctionCall in project drill by apache.

the class TypeInferenceUtils method convertSqlOperatorBindingToFunctionCall.

/**
   * Given a SqlOperatorBinding, convert it to FunctionCall
   * @param  opBinding    the given SqlOperatorBinding
   * @return FunctionCall the converted FunctionCall
   */
public static FunctionCall convertSqlOperatorBindingToFunctionCall(final SqlOperatorBinding opBinding) {
    final List<LogicalExpression> args = Lists.newArrayList();
    for (int i = 0; i < opBinding.getOperandCount(); ++i) {
        final RelDataType type = opBinding.getOperandType(i);
        final TypeProtos.MinorType minorType = getDrillTypeFromCalciteType(type);
        final TypeProtos.MajorType majorType;
        if (type.isNullable()) {
            majorType = Types.optional(minorType);
        } else {
            majorType = Types.required(minorType);
        }
        args.add(new MajorTypeInLogicalExpression(majorType));
    }
    final String drillFuncName = FunctionCallFactory.replaceOpWithFuncName(opBinding.getOperator().getName());
    final FunctionCall functionCall = new FunctionCall(drillFuncName, args, ExpressionPosition.UNKNOWN);
    return functionCall;
}
Also used : MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FunctionCall(org.apache.drill.common.expression.FunctionCall) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 3 with FunctionCall

use of org.apache.drill.common.expression.FunctionCall in project drill by apache.

the class TypeInferenceUtils method resolveDrillFuncHolder.

private static DrillFuncHolder resolveDrillFuncHolder(final SqlOperatorBinding opBinding, final List<DrillFuncHolder> functions) {
    final FunctionCall functionCall = convertSqlOperatorBindingToFunctionCall(opBinding);
    final FunctionResolver functionResolver = FunctionResolverFactory.getResolver(functionCall);
    final DrillFuncHolder func = functionResolver.getBestMatch(functions, functionCall);
    // if no DrillFuncHolder matched for the given list of operand types
    if (func == null) {
        String operandTypes = "";
        for (int i = 0; i < opBinding.getOperandCount(); ++i) {
            operandTypes += opBinding.getOperandType(i).getSqlTypeName();
            if (i < opBinding.getOperandCount() - 1) {
                operandTypes += ",";
            }
        }
        throw UserException.functionError().message(String.format("%s does not support operand types (%s)", opBinding.getOperator().getName(), operandTypes)).build(logger);
    }
    return func;
}
Also used : DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) FunctionCall(org.apache.drill.common.expression.FunctionCall) FunctionResolver(org.apache.drill.exec.resolver.FunctionResolver)

Example 4 with FunctionCall

use of org.apache.drill.common.expression.FunctionCall in project drill by apache.

the class ExpressionTreeMaterializer method addCastExpression.

public static LogicalExpression addCastExpression(LogicalExpression fromExpr, MajorType toType, FunctionLookupContext functionLookupContext, ErrorCollector errorCollector, boolean exactResolver) {
    String castFuncName = CastFunctions.getCastFunc(toType.getMinorType());
    List<LogicalExpression> castArgs = Lists.newArrayList();
    //input_expr
    castArgs.add(fromExpr);
    if (fromExpr.getMajorType().getMinorType() == MinorType.UNION && toType.getMinorType() == MinorType.UNION) {
        return fromExpr;
    }
    if (!Types.isFixedWidthType(toType) && !Types.isUnion(toType)) {
        /* We are implicitly casting to VARCHAR so we don't have a max length,
       * using an arbitrary value. We trim down the size of the stored bytes
       * to the actual size so this size doesn't really matter.
       */
        castArgs.add(new ValueExpressions.LongExpression(Types.MAX_VARCHAR_LENGTH, null));
    } else if (CoreDecimalUtility.isDecimalType(toType)) {
        // Add the scale and precision to the arguments of the implicit cast
        castArgs.add(new ValueExpressions.LongExpression(toType.getPrecision(), null));
        castArgs.add(new ValueExpressions.LongExpression(toType.getScale(), null));
    }
    FunctionCall castCall = new FunctionCall(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
    FunctionResolver resolver;
    if (exactResolver) {
        resolver = FunctionResolverFactory.getExactResolver(castCall);
    } else {
        resolver = FunctionResolverFactory.getResolver(castCall);
    }
    DrillFuncHolder matchedCastFuncHolder = functionLookupContext.findDrillFunction(resolver, castCall);
    if (matchedCastFuncHolder == null) {
        logFunctionResolutionError(errorCollector, castCall);
        return NullExpression.INSTANCE;
    }
    return matchedCastFuncHolder.getExpr(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) LongExpression(org.apache.drill.common.expression.ValueExpressions.LongExpression) QuotedString(org.apache.drill.common.expression.ValueExpressions.QuotedString) LongExpression(org.apache.drill.common.expression.ValueExpressions.LongExpression) FunctionCall(org.apache.drill.common.expression.FunctionCall) FunctionResolver(org.apache.drill.exec.resolver.FunctionResolver)

Example 5 with FunctionCall

use of org.apache.drill.common.expression.FunctionCall in project drill by apache.

the class DrillWindowRel method toDrill.

protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
    List<LogicalExpression> args = Lists.newArrayList();
    for (Integer i : call.getArgList()) {
        args.add(new FieldReference(fn.get(i)));
    }
    // for count(1).
    if (args.isEmpty()) {
        args.add(new ValueExpressions.LongExpression(1l));
    }
    LogicalExpression expr = new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
    return expr;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Aggregations

FunctionCall (org.apache.drill.common.expression.FunctionCall)15 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)14 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)5 FieldReference (org.apache.drill.common.expression.FieldReference)4 QuotedString (org.apache.drill.common.expression.ValueExpressions.QuotedString)4 DrillFuncHolder (org.apache.drill.exec.expr.fn.DrillFuncHolder)4 FunctionResolver (org.apache.drill.exec.resolver.FunctionResolver)4 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 IntExpression (org.apache.drill.common.expression.ValueExpressions.IntExpression)3 LongExpression (org.apache.drill.common.expression.ValueExpressions.LongExpression)3 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)3 ByteBuf (io.netty.buffer.ByteBuf)2 ArrayList (java.util.ArrayList)2 BooleanExpression (org.apache.drill.common.expression.ValueExpressions.BooleanExpression)2 DateExpression (org.apache.drill.common.expression.ValueExpressions.DateExpression)2 DoubleExpression (org.apache.drill.common.expression.ValueExpressions.DoubleExpression)2 FloatExpression (org.apache.drill.common.expression.ValueExpressions.FloatExpression)2 TimeExpression (org.apache.drill.common.expression.ValueExpressions.TimeExpression)2 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)2 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)2