Search in sources :

Example 71 with LogicalExpression

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

the class DrillAggregateRel method implement.

@Override
public LogicalOperator implement(DrillImplementor implementor) {
    GroupingAggregate.Builder builder = GroupingAggregate.builder();
    builder.setInput(implementor.visitChild(this, 0, getInput()));
    final List<String> childFields = getInput().getRowType().getFieldNames();
    final List<String> fields = getRowType().getFieldNames();
    for (int group : BitSets.toIter(groupSet)) {
        FieldReference fr = new FieldReference(childFields.get(group), ExpressionPosition.UNKNOWN);
        builder.addKey(fr, fr);
    }
    for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
        FieldReference ref = new FieldReference(fields.get(groupSet.cardinality() + aggCall.i));
        LogicalExpression expr = toDrill(aggCall.e, childFields, implementor);
        builder.addExpr(ref, expr);
    }
    return builder.build();
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) GroupingAggregate(org.apache.drill.common.logical.data.GroupingAggregate)

Example 72 with LogicalExpression

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

the class ParquetPushDownFilter method doOnMatch.

protected void doOnMatch(RelOptRuleCall call, FilterPrel filter, ProjectPrel project, ScanPrel scan) {
    ParquetGroupScan groupScan = (ParquetGroupScan) scan.getGroupScan();
    if (groupScan.getFilter() != null && !groupScan.getFilter().equals(ValueExpressions.BooleanExpression.TRUE)) {
        return;
    }
    RexNode condition = null;
    if (project == null) {
        condition = filter.getCondition();
    } else {
        // get the filter as if it were below the projection.
        condition = RelOptUtil.pushFilterPastProject(filter.getCondition(), project);
    }
    if (condition == null || condition.equals(ValueExpressions.BooleanExpression.TRUE)) {
        return;
    }
    // get a conjunctions of the filter condition. For each conjunction, if it refers to ITEM or FLATTEN expression
    // then we could not pushed down. Otherwise, it's qualified to be pushed down.
    final List<RexNode> predList = RelOptUtil.conjunctions(condition);
    final List<RexNode> qualifiedPredList = Lists.newArrayList();
    for (final RexNode pred : predList) {
        if (DrillRelOptUtil.findItemOrFlatten(pred, ImmutableList.<RexNode>of()) == null) {
            qualifiedPredList.add(pred);
        }
    }
    final RexNode qualifedPred = RexUtil.composeConjunction(filter.getCluster().getRexBuilder(), qualifiedPredList, true);
    if (qualifedPred == null) {
        return;
    }
    LogicalExpression conditionExp = DrillOptiq.toDrill(new DrillParseContext(PrelUtil.getPlannerSettings(call.getPlanner())), scan, qualifedPred);
    Stopwatch timer = Stopwatch.createStarted();
    final GroupScan newGroupScan = groupScan.applyFilter(conditionExp, optimizerContext, optimizerContext.getFunctionRegistry(), optimizerContext.getPlannerSettings().getOptions());
    logger.info("Took {} ms to apply filter on parquet row groups. ", timer.elapsed(TimeUnit.MILLISECONDS));
    if (newGroupScan == null) {
        return;
    }
    final ScanPrel newScanRel = ScanPrel.create(scan, scan.getTraitSet(), newGroupScan, scan.getRowType());
    RelNode inputRel = newScanRel;
    if (project != null) {
        inputRel = project.copy(project.getTraitSet(), ImmutableList.of(inputRel));
    }
    final RelNode newFilter = filter.copy(filter.getTraitSet(), ImmutableList.<RelNode>of(inputRel));
    call.transformTo(newFilter);
}
Also used : GroupScan(org.apache.drill.exec.physical.base.GroupScan) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ScanPrel(org.apache.drill.exec.planner.physical.ScanPrel) RelNode(org.apache.calcite.rel.RelNode) Stopwatch(com.google.common.base.Stopwatch) DrillParseContext(org.apache.drill.exec.planner.logical.DrillParseContext) RexNode(org.apache.calcite.rex.RexNode)

Example 73 with LogicalExpression

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

the class ParquetFilterBuilder method handleCompareFunction.

private LogicalExpression handleCompareFunction(FunctionHolderExpression functionHolderExpression, Set<LogicalExpression> value) {
    List<LogicalExpression> newArgs = new ArrayList();
    for (LogicalExpression arg : functionHolderExpression.args) {
        LogicalExpression newArg = arg.accept(this, value);
        if (newArg == null) {
            return null;
        }
        newArgs.add(newArg);
    }
    String funcName = ((DrillSimpleFuncHolder) functionHolderExpression.getHolder()).getRegisteredNames()[0];
    switch(funcName) {
        case FunctionGenerationHelper.EQ:
            return new ParquetPredicates.EqualPredicate(newArgs.get(0), newArgs.get(1));
        case FunctionGenerationHelper.GT:
            return new ParquetPredicates.GTPredicate(newArgs.get(0), newArgs.get(1));
        case FunctionGenerationHelper.GE:
            return new ParquetPredicates.GEPredicate(newArgs.get(0), newArgs.get(1));
        case FunctionGenerationHelper.LT:
            return new ParquetPredicates.LTPredicate(newArgs.get(0), newArgs.get(1));
        case FunctionGenerationHelper.LE:
            return new ParquetPredicates.LEPredicate(newArgs.get(0), newArgs.get(1));
        case FunctionGenerationHelper.NE:
            return new ParquetPredicates.NEPredicate(newArgs.get(0), newArgs.get(1));
        default:
            return null;
    }
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ArrayList(java.util.ArrayList)

Example 74 with LogicalExpression

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

the class ParquetRGFilterEvaluator method canDrop.

public static boolean canDrop(LogicalExpression expr, Map<SchemaPath, ColumnStatistics> columnStatisticsMap, long rowCount, UdfUtilities udfUtilities, FunctionImplementationRegistry functionImplementationRegistry) {
    ErrorCollector errorCollector = new ErrorCollectorImpl();
    LogicalExpression materializedFilter = ExpressionTreeMaterializer.materializeFilterExpr(expr, columnStatisticsMap, errorCollector, functionImplementationRegistry);
    if (errorCollector.hasErrors()) {
        logger.error("{} error(s) encountered when materialize filter expression : {}", errorCollector.getErrorCount(), errorCollector.toErrorString());
        return false;
    }
    Set<LogicalExpression> constantBoundaries = ConstantExpressionIdentifier.getConstantExpressionSet(materializedFilter);
    ParquetFilterPredicate parquetPredicate = (ParquetFilterPredicate) ParquetFilterBuilder.buildParquetFilterPredicate(materializedFilter, constantBoundaries, udfUtilities);
    return canDrop(parquetPredicate, columnStatisticsMap, rowCount);
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ParquetFilterPredicate(org.apache.drill.exec.expr.stat.ParquetFilterPredicate) ErrorCollector(org.apache.drill.common.expression.ErrorCollector)

Example 75 with LogicalExpression

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

the class ExternalSortBatch method createNewMSorter.

private MSorter createNewMSorter(FragmentContext context, List<Ordering> orderings, VectorAccessible batch, MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping) throws ClassTransformationException, IOException, SchemaChangeException {
    CodeGenerator<MSorter> cg = CodeGenerator.get(MSorter.TEMPLATE_DEFINITION, context.getFunctionRegistry(), context.getOptions());
    ClassGenerator<MSorter> g = cg.getRoot();
    g.setMappingSet(mainMapping);
    for (Ordering od : orderings) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        if (collector.hasErrors()) {
            throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
        }
        g.setMappingSet(leftMapping);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(rightMapping);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(mainMapping);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
        g.rotateBlock();
    }
    g.rotateBlock();
    g.getEvalBlock()._return(JExpr.lit(0));
    // This class can generate plain-old Java.
    cg.plainJavaCapable(true);
    //    cg.saveCodeForDebugging(true);
    return context.getImplementationClass(cg);
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)90 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)32 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)31 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)18 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)17 FieldReference (org.apache.drill.common.expression.FieldReference)14 FunctionCall (org.apache.drill.common.expression.FunctionCall)14 SchemaPath (org.apache.drill.common.expression.SchemaPath)12 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)11 MaterializedField (org.apache.drill.exec.record.MaterializedField)11 JConditional (com.sun.codemodel.JConditional)10 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)10 ValueVector (org.apache.drill.exec.vector.ValueVector)10 IOException (java.io.IOException)9 Ordering (org.apache.drill.common.logical.data.Order.Ordering)9 DrillParseContext (org.apache.drill.exec.planner.logical.DrillParseContext)9 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)8 QuotedString (org.apache.drill.common.expression.ValueExpressions.QuotedString)8 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)8 ArrayList (java.util.ArrayList)7