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);
}
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;
}
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));
}
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);
}
}
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;
}
Aggregations