Search in sources :

Example 1 with Order

use of org.apache.drill.common.logical.data.Order 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)

Aggregations

FunctionCall (org.apache.drill.common.expression.FunctionCall)1 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)1 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)1 Order (org.apache.drill.common.logical.data.Order)1