Search in sources :

Example 1 with NamedExpression

use of org.apache.drill.common.logical.data.NamedExpression in project drill by apache.

the class DrillProjectRelBase method getProjectExpressions.

protected List<NamedExpression> getProjectExpressions(DrillParseContext context) {
    List<NamedExpression> expressions = Lists.newArrayList();
    HashMap<String, String> starColPrefixes = new HashMap<String, String>();
    // This will allow us to differentiate the regular expanded from *, and the regular column referenced in the query.
    for (Pair<RexNode, String> pair : projects()) {
        if (StarColumnHelper.isPrefixedStarColumn(pair.right)) {
            String prefix = StarColumnHelper.extractStarColumnPrefix(pair.right);
            if (!starColPrefixes.containsKey(prefix)) {
                starColPrefixes.put(prefix, pair.right);
            }
        }
    }
    for (Pair<RexNode, String> pair : projects()) {
        if (!StarColumnHelper.subsumeColumn(starColPrefixes, pair.right)) {
            LogicalExpression expr = DrillOptiq.toDrill(context, getInput(), pair.left);
            expressions.add(new NamedExpression(expr, FieldReference.getWithQuotedRef(pair.right)));
        }
    }
    return expressions;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HashMap(java.util.HashMap) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 2 with NamedExpression

use of org.apache.drill.common.logical.data.NamedExpression in project drill by apache.

the class ChainedHashTable method createAndSetupHashTable.

public HashTable createAndSetupHashTable(TypedFieldId[] outKeyFieldIds) throws ClassTransformationException, IOException, SchemaChangeException {
    CodeGenerator<HashTable> top = CodeGenerator.get(HashTable.TEMPLATE_DEFINITION, context.getFunctionRegistry(), context.getOptions());
    top.plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // This code is called from generated code, so to step into this code,
    // persist the code generated in HashAggBatch also.
    //  top.saveCodeForDebugging(true);
    ClassGenerator<HashTable> cg = top.getRoot();
    ClassGenerator<HashTable> cgInner = cg.getInnerGenerator("BatchHolder");
    LogicalExpression[] keyExprsBuild = new LogicalExpression[htConfig.getKeyExprsBuild().size()];
    LogicalExpression[] keyExprsProbe = null;
    boolean isProbe = (htConfig.getKeyExprsProbe() != null);
    if (isProbe) {
        keyExprsProbe = new LogicalExpression[htConfig.getKeyExprsProbe().size()];
    }
    ErrorCollector collector = new ErrorCollectorImpl();
    // original ht container from which others may be cloned
    VectorContainer htContainerOrig = new VectorContainer();
    LogicalExpression[] htKeyExprs = new LogicalExpression[htConfig.getKeyExprsBuild().size()];
    TypedFieldId[] htKeyFieldIds = new TypedFieldId[htConfig.getKeyExprsBuild().size()];
    int i = 0;
    for (NamedExpression ne : htConfig.getKeyExprsBuild()) {
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(ne.getExpr(), incomingBuild, collector, context.getFunctionRegistry());
        if (collector.hasErrors()) {
            throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
        }
        if (expr == null) {
            continue;
        }
        keyExprsBuild[i] = expr;
        i++;
    }
    if (isProbe) {
        i = 0;
        for (NamedExpression ne : htConfig.getKeyExprsProbe()) {
            final LogicalExpression expr = ExpressionTreeMaterializer.materialize(ne.getExpr(), incomingProbe, collector, context.getFunctionRegistry());
            if (collector.hasErrors()) {
                throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
            }
            if (expr == null) {
                continue;
            }
            keyExprsProbe[i] = expr;
            i++;
        }
        JoinUtils.addLeastRestrictiveCasts(keyExprsProbe, incomingProbe, keyExprsBuild, incomingBuild, context);
    }
    i = 0;
    /*
     * Once the implicit casts have been added, create the value vectors for the corresponding
     * type and add it to the hash table's container.
     * Note: Adding implicit casts may have a minor impact on the memory foot print. For example
     * if we have a join condition with bigint on the probe side and int on the build side then
     * after this change we will be allocating a bigint vector in the hashtable instead of an int
     * vector.
     */
    for (NamedExpression ne : htConfig.getKeyExprsBuild()) {
        LogicalExpression expr = keyExprsBuild[i];
        final MaterializedField outputField = MaterializedField.create(ne.getRef().getAsUnescapedPath(), expr.getMajorType());
        @SuppressWarnings("resource") ValueVector vv = TypeHelper.getNewVector(outputField, allocator);
        htKeyFieldIds[i] = htContainerOrig.add(vv);
        i++;
    }
    // generate code for isKeyMatch(), setValue(), getHash() and outputRecordKeys()
    setupIsKeyMatchInternal(cgInner, KeyMatchIncomingBuildMapping, KeyMatchHtableMapping, keyExprsBuild, htConfig.getComparators(), htKeyFieldIds);
    setupIsKeyMatchInternal(cgInner, KeyMatchIncomingProbeMapping, KeyMatchHtableProbeMapping, keyExprsProbe, htConfig.getComparators(), htKeyFieldIds);
    setupSetValue(cgInner, keyExprsBuild, htKeyFieldIds);
    if (outgoing != null) {
        if (outKeyFieldIds.length > htConfig.getKeyExprsBuild().size()) {
            throw new IllegalArgumentException("Mismatched number of output key fields.");
        }
    }
    setupOutputRecordKeys(cgInner, htKeyFieldIds, outKeyFieldIds);
    setupGetHash(cg, /* use top level code generator for getHash */
    GetHashIncomingBuildMapping, incomingBuild, keyExprsBuild, false);
    setupGetHash(cg, /* use top level code generator for getHash */
    GetHashIncomingProbeMapping, incomingProbe, keyExprsProbe, true);
    HashTable ht = context.getImplementationClass(top);
    ht.setup(htConfig, context, allocator, incomingBuild, incomingProbe, outgoing, htContainerOrig);
    return ht;
}
Also used : ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MaterializedField(org.apache.drill.exec.record.MaterializedField) VectorContainer(org.apache.drill.exec.record.VectorContainer) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVector(org.apache.drill.exec.vector.ValueVector) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) TypedFieldId(org.apache.drill.exec.record.TypedFieldId)

Example 3 with NamedExpression

use of org.apache.drill.common.logical.data.NamedExpression in project drill by apache.

the class StreamingAggBatch method createAggregatorInternal.

private StreamingAggregator createAggregatorInternal() throws SchemaChangeException, ClassTransformationException, IOException {
    ClassGenerator<StreamingAggregator> cg = CodeGenerator.getRoot(StreamingAggTemplate.TEMPLATE_DEFINITION, context.getFunctionRegistry(), context.getOptions());
    cg.getCodeGenerator().plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    //    cg.getCodeGenerator().saveCodeForDebugging(true);
    container.clear();
    LogicalExpression[] keyExprs = new LogicalExpression[popConfig.getKeys().size()];
    LogicalExpression[] valueExprs = new LogicalExpression[popConfig.getExprs().size()];
    TypedFieldId[] keyOutputIds = new TypedFieldId[popConfig.getKeys().size()];
    ErrorCollector collector = new ErrorCollectorImpl();
    for (int i = 0; i < keyExprs.length; i++) {
        final NamedExpression ne = popConfig.getKeys().get(i);
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(ne.getExpr(), incoming, collector, context.getFunctionRegistry());
        if (expr == null) {
            continue;
        }
        keyExprs[i] = expr;
        final MaterializedField outputField = MaterializedField.create(ne.getRef().getAsUnescapedPath(), expr.getMajorType());
        @SuppressWarnings("resource") final ValueVector vector = TypeHelper.getNewVector(outputField, oContext.getAllocator());
        keyOutputIds[i] = container.add(vector);
    }
    for (int i = 0; i < valueExprs.length; i++) {
        final NamedExpression ne = popConfig.getExprs().get(i);
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(ne.getExpr(), incoming, collector, context.getFunctionRegistry());
        if (expr instanceof IfExpression) {
            throw UserException.unsupportedError(new UnsupportedOperationException("Union type not supported in aggregate functions")).build(logger);
        }
        if (expr == null) {
            continue;
        }
        final MaterializedField outputField = MaterializedField.create(ne.getRef().getAsUnescapedPath(), expr.getMajorType());
        @SuppressWarnings("resource") ValueVector vector = TypeHelper.getNewVector(outputField, oContext.getAllocator());
        TypedFieldId id = container.add(vector);
        valueExprs[i] = new ValueVectorWriteExpression(id, expr, true);
    }
    if (collector.hasErrors()) {
        throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
    }
    setupIsSame(cg, keyExprs);
    setupIsSameApart(cg, keyExprs);
    addRecordValues(cg, valueExprs);
    outputRecordKeys(cg, keyOutputIds, keyExprs);
    outputRecordKeysPrev(cg, keyOutputIds, keyExprs);
    cg.getBlock("resetValues")._return(JExpr.TRUE);
    getIndex(cg);
    container.buildSchema(SelectionVectorMode.NONE);
    StreamingAggregator agg = context.getImplementationClass(cg);
    agg.setup(oContext, incoming, this);
    return agg;
}
Also used : IfExpression(org.apache.drill.common.expression.IfExpression) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MaterializedField(org.apache.drill.exec.record.MaterializedField) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVector(org.apache.drill.exec.vector.ValueVector) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression)

Example 4 with NamedExpression

use of org.apache.drill.common.logical.data.NamedExpression in project drill by apache.

the class ProjectAllowDupPrel method getProjectExpressions.

@Override
protected List<NamedExpression> getProjectExpressions(DrillParseContext context) {
    List<NamedExpression> expressions = Lists.newArrayList();
    for (Pair<RexNode, String> pair : Pair.zip(exps, getRowType().getFieldNames())) {
        LogicalExpression expr = DrillOptiq.toDrill(context, getInput(), pair.left);
        expressions.add(new NamedExpression(expr, FieldReference.getWithQuotedRef(pair.right)));
    }
    return expressions;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 5 with NamedExpression

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

NamedExpression (org.apache.drill.common.logical.data.NamedExpression)17 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)11 FieldReference (org.apache.drill.common.expression.FieldReference)7 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)6 MaterializedField (org.apache.drill.exec.record.MaterializedField)6 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)5 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)5 ValueVector (org.apache.drill.exec.vector.ValueVector)5 ValueVectorWriteExpression (org.apache.drill.exec.expr.ValueVectorWriteExpression)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 RexNode (org.apache.calcite.rex.RexNode)3 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AggregateCall (org.apache.calcite.rel.core.AggregateCall)2 FunctionCall (org.apache.drill.common.expression.FunctionCall)2 IfExpression (org.apache.drill.common.expression.IfExpression)2 Order (org.apache.drill.common.logical.data.Order)2 ClassTransformationException (org.apache.drill.exec.exception.ClassTransformationException)2