Search in sources :

Example 1 with OutputWidthCalculator

use of org.apache.drill.exec.expr.fn.output.OutputWidthCalculator in project drill by apache.

the class OutputWidthVisitor method visitFunctionCallExpr.

/**
 * Converts a {@link FunctionCallExpr} to a {@link FixedLenExpr} by passing
 * the the args of the function to the width calculator for this function.
 */
@Override
public OutputWidthExpression visitFunctionCallExpr(FunctionCallExpr functionCallExpr, OutputWidthVisitorState state) throws RuntimeException {
    ArrayList<OutputWidthExpression> args = functionCallExpr.getArgs();
    ArrayList<FixedLenExpr> estimatedArgs = null;
    if (args != null && args.size() != 0) {
        estimatedArgs = new ArrayList<>(args.size());
        for (OutputWidthExpression expr : args) {
            // Once the args are visited, they will all become FixedWidthExpr
            FixedLenExpr fixedLenExpr = (FixedLenExpr) expr.accept(this, state);
            estimatedArgs.add(fixedLenExpr);
        }
    }
    OutputWidthCalculator estimator = functionCallExpr.getCalculator();
    int estimatedSize = estimator.getOutputWidth(estimatedArgs);
    return new FixedLenExpr(estimatedSize);
}
Also used : FixedLenExpr(org.apache.drill.exec.physical.impl.project.OutputWidthExpression.FixedLenExpr) OutputWidthCalculator(org.apache.drill.exec.expr.fn.output.OutputWidthCalculator)

Example 2 with OutputWidthCalculator

use of org.apache.drill.exec.expr.fn.output.OutputWidthCalculator in project drill by apache.

the class OutputWidthVisitor method visitFunctionHolderExpression.

/**
 * Handles a {@link FunctionHolderExpression}. Functions that produce
 * fixed-width output are trivially converted to a {@link FixedLenExpr}. For
 * functions that produce variable width output, the output width calculator
 * annotation is looked-up and recorded in a {@link FunctionCallExpr}. This
 * calculator will later be used to convert the FunctionCallExpr to a
 * {@link FixedLenExpr} expression
 */
@Override
public OutputWidthExpression visitFunctionHolderExpression(FunctionHolderExpression holderExpr, OutputWidthVisitorState state) throws RuntimeException {
    OutputWidthExpression fixedWidth = getFixedLenExpr(holderExpr.getMajorType());
    if (fixedWidth != null) {
        return fixedWidth;
    }
    // will default to a fixed value
    if (!(holderExpr instanceof DrillFuncHolderExpr)) {
        // Use a default if this is not a DrillFunc
        return new FixedLenExpr(OutputSizeEstimateConstants.NON_DRILL_FUNCTION_OUTPUT_SIZE_ESTIMATE);
    }
    final DrillFuncHolder holder = ((DrillFuncHolderExpr) holderExpr).getHolder();
    // If the user has provided a size estimate, use it
    int estimate = holder.variableOutputSizeEstimate();
    if (estimate != FunctionTemplate.OUTPUT_SIZE_ESTIMATE_DEFAULT) {
        return new FixedLenExpr(estimate);
    }
    // Use the calculator provided by the user or use the default
    OutputWidthCalculator widthCalculator = holder.getOutputWidthCalculator();
    final int argSize = holderExpr.args.size();
    ArrayList<OutputWidthExpression> arguments = null;
    if (argSize != 0) {
        arguments = new ArrayList<>(argSize);
        for (LogicalExpression expr : holderExpr.args) {
            arguments.add(expr.accept(this, state));
        }
    }
    return new FunctionCallExpr(holderExpr, widthCalculator, arguments);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FixedLenExpr(org.apache.drill.exec.physical.impl.project.OutputWidthExpression.FixedLenExpr) DrillFuncHolder(org.apache.drill.exec.expr.fn.DrillFuncHolder) FunctionCallExpr(org.apache.drill.exec.physical.impl.project.OutputWidthExpression.FunctionCallExpr) OutputWidthCalculator(org.apache.drill.exec.expr.fn.output.OutputWidthCalculator) DrillFuncHolderExpr(org.apache.drill.exec.expr.DrillFuncHolderExpr)

Aggregations

OutputWidthCalculator (org.apache.drill.exec.expr.fn.output.OutputWidthCalculator)2 FixedLenExpr (org.apache.drill.exec.physical.impl.project.OutputWidthExpression.FixedLenExpr)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)1 DrillFuncHolderExpr (org.apache.drill.exec.expr.DrillFuncHolderExpr)1 DrillFuncHolder (org.apache.drill.exec.expr.fn.DrillFuncHolder)1 FunctionCallExpr (org.apache.drill.exec.physical.impl.project.OutputWidthExpression.FunctionCallExpr)1