Search in sources :

Example 96 with LogicalExpression

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

the class DruidFilterBuilderTest method visitBooleanOperatorWithOrOperator.

@Test
public void visitBooleanOperatorWithOrOperator() {
    LogicalExpression logicalExpression2 = mock(LogicalExpression.class);
    try {
        when(logicalExpression.accept(any(), any())).thenReturn(druidScanSpecLeft);
        when(logicalExpression2.accept(any(), any())).thenReturn(druidScanSpecRight);
    } catch (Exception ignored) {
    }
    BooleanOperator booleanOperator = new BooleanOperator(FunctionNames.OR, Stream.of(logicalExpression, logicalExpression2).collect(Collectors.toList()), null);
    DruidScanSpec druidScanSpec = druidFilterBuilder.visitBooleanOperator(booleanOperator, null);
    String expectedFilterJson = "{\"type\":\"or\",\"fields\":[{\"type\":\"selector\",\"dimension\":\"some dimension\",\"value\":\"some value\"},{\"type\":\"selector\",\"dimension\":\"some other dimension\",\"value\":\"some other value\"}]}";
    String actual = druidScanSpec.getFilter().toJson();
    assertThat(actual).isEqualTo(expectedFilterJson);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) BooleanOperator(org.apache.drill.common.expression.BooleanOperator) Test(org.junit.Test)

Example 97 with LogicalExpression

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

the class DruidFilterBuilder method visitBooleanOperator.

@Override
public DruidScanSpec visitBooleanOperator(BooleanOperator op, Void value) {
    List<LogicalExpression> args = op.args();
    DruidScanSpec nodeScanSpec = null;
    String functionName = op.getName();
    logger.debug("visitBooleanOperator Called. FunctionName - {}", functionName);
    for (LogicalExpression arg : args) {
        switch(functionName) {
            case FunctionNames.AND:
            case FunctionNames.OR:
                if (nodeScanSpec == null) {
                    nodeScanSpec = arg.accept(this, null);
                } else {
                    DruidScanSpec scanSpec = arg.accept(this, null);
                    if (scanSpec != null) {
                        nodeScanSpec = mergeScanSpecs(functionName, nodeScanSpec, scanSpec);
                    } else {
                        allExpressionsConverted = false;
                    }
                }
                break;
        }
    }
    return nodeScanSpec;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression)

Example 98 with LogicalExpression

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

the class MergingRecordBatch method generateComparisons.

private void generateComparisons(final ClassGenerator<?> g, final VectorAccessible batch) throws SchemaChangeException {
    g.setMappingSet(MAIN_MAPPING);
    for (final Ordering od : popConfig.getOrderings()) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        final 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(LEFT_MAPPING);
        final HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(RIGHT_MAPPING);
        final HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(MAIN_MAPPING);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        final LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
        final HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        final 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.getEvalBlock()._return(JExpr.lit(0));
}
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)

Example 99 with LogicalExpression

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

the class OrderedPartitionRecordBatch method setupNewSchema.

/**
 * Sets up projection that will transfer all of the columns in batch, and also
 * populate the partition column based on which partition a record falls into
 * in the partition table
 *
 * @param batch
 */
protected void setupNewSchema(VectorAccessible batch) {
    container.clear();
    ErrorCollector collector = new ErrorCollectorImpl();
    List<TransferPair> transfers = Lists.newArrayList();
    ClassGenerator<OrderedPartitionProjector> cg = CodeGenerator.getRoot(OrderedPartitionProjector.TEMPLATE_DEFINITION, context.getOptions());
    for (VectorWrapper<?> vw : batch) {
        TransferPair tp = vw.getValueVector().getTransferPair(oContext.getAllocator());
        transfers.add(tp);
        container.add(tp.getTo());
    }
    cg.setMappingSet(mainMapping);
    int count = 0;
    for (Ordering od : popConfig.getOrderings()) {
        LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        collector.reportErrors(logger);
        cg.setMappingSet(incomingMapping);
        ClassGenerator.HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        cg.setMappingSet(partitionMapping);
        TypedFieldId fieldId = new TypedFieldId.Builder().finalType(expr.getMajorType()).addId(count++).build();
        ClassGenerator.HoldingContainer right = cg.addExpr(new ValueVectorReadExpression(fieldId), ClassGenerator.BlkCreateMode.FALSE);
        cg.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());
        ClassGenerator.HoldingContainer out = cg.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = cg.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());
        }
    }
    cg.getEvalBlock()._return(JExpr.lit(0));
    container.add(this.partitionKeyVector);
    container.buildSchema(batch.getSchema().getSelectionVectorMode());
    projector = context.getImplementationClass(cg);
    try {
        projector.setup(context, batch, this, transfers, partitionVectors, partitions, popConfig.getRef());
    } catch (SchemaChangeException e) {
        throw UserException.schemaChangeError(e).addContext("Unexpected schema change in the Ordered Partitioner").build(logger);
    }
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) Ordering(org.apache.drill.common.logical.data.Order.Ordering) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JConditional(com.sun.codemodel.JConditional)

Example 100 with LogicalExpression

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

the class OutputWidthVisitor method visitValueVectorWriteExpression.

/**
 * Records a variable width write expression. This will be converted to a
 * {@link FixedLenExpr} expression by walking the tree of expression attached
 * to the write expression.
 */
@Override
public OutputWidthExpression visitValueVectorWriteExpression(ValueVectorWriteExpression writeExpr, OutputWidthVisitorState state) throws RuntimeException {
    TypedFieldId fieldId = writeExpr.getFieldId();
    ProjectMemoryManager manager = state.getManager();
    OutputWidthExpression outputExpr;
    if (manager.isFixedWidth(fieldId)) {
        outputExpr = getFixedLenExpr(fieldId.getFinalType());
    } else {
        LogicalExpression writeArg = writeExpr.getChild();
        outputExpr = writeArg.accept(this, state);
    }
    return outputExpr;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) TypedFieldId(org.apache.drill.exec.record.TypedFieldId)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)287 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)78 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)77 FunctionCall (org.apache.drill.common.expression.FunctionCall)44 SchemaPath (org.apache.drill.common.expression.SchemaPath)43 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)42 FieldReference (org.apache.drill.common.expression.FieldReference)39 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)38 RexNode (org.apache.calcite.rex.RexNode)32 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)31 DrillParseContext (org.apache.drill.exec.planner.logical.DrillParseContext)30 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)30 ArrayList (java.util.ArrayList)29 MaterializedField (org.apache.drill.exec.record.MaterializedField)28 JConditional (com.sun.codemodel.JConditional)26 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)23 Ordering (org.apache.drill.common.logical.data.Order.Ordering)23 ValueVector (org.apache.drill.exec.vector.ValueVector)22 Test (org.junit.Test)22 TypeProtos (org.apache.drill.common.types.TypeProtos)20