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);
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.
the class ExternalSortBatch method generateComparisons.
private void generateComparisons(ClassGenerator<?> g, VectorAccessible batch) throws SchemaChangeException {
g.setMappingSet(MAIN_MAPPING);
for (Ordering od : popConfig.getOrderings()) {
// 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(LEFT_MAPPING);
HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
g.setMappingSet(RIGHT_MAPPING);
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.
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));
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.
the class ExpressionTreeMaterializerTest method testMaterializingLateboundTreeValidated.
@Test
public void testMaterializingLateboundTreeValidated(@Injectable final RecordBatch batch) throws SchemaChangeException {
ErrorCollector ec = new ErrorCollector() {
int errorCount = 0;
@Override
public void addGeneralError(ExpressionPosition expr, String s) {
errorCount++;
}
@Override
public void addUnexpectedArgumentType(ExpressionPosition expr, String name, MajorType actual, MajorType[] expected, int argumentIndex) {
errorCount++;
}
@Override
public void addUnexpectedArgumentCount(ExpressionPosition expr, int actual, Range<Integer> expected) {
errorCount++;
}
@Override
public void addUnexpectedArgumentCount(ExpressionPosition expr, int actual, int expected) {
errorCount++;
}
@Override
public void addNonNumericType(ExpressionPosition expr, MajorType actual) {
errorCount++;
}
@Override
public void addUnexpectedType(ExpressionPosition expr, int index, MajorType actual) {
errorCount++;
}
@Override
public void addExpectedConstantValue(ExpressionPosition expr, int actual, String s) {
errorCount++;
}
@Override
public boolean hasErrors() {
return errorCount > 0;
}
@Override
public String toErrorString() {
return String.format("Found %s errors.", errorCount);
}
@Override
public int getErrorCount() {
return errorCount;
}
};
new NonStrictExpectations() {
{
batch.getValueVectorId(new SchemaPath("test", ExpressionPosition.UNKNOWN));
result = new TypedFieldId(Types.required(MinorType.BIGINT), -5);
}
};
new MockUp<RemoteFunctionRegistry>() {
@Mock
long getRegistryVersion() {
return 0L;
}
};
LogicalExpression functionCallExpr = new FunctionCall("testFunc", ImmutableList.of((LogicalExpression) new FieldReference("test", ExpressionPosition.UNKNOWN)), ExpressionPosition.UNKNOWN);
LogicalExpression newExpr = ExpressionTreeMaterializer.materialize(functionCallExpr, batch, ec, registry);
assertTrue(newExpr instanceof TypedNullConstant);
assertEquals(1, ec.getErrorCount());
System.out.println(ec.toErrorString());
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.
the class ConditionalExprOptimizer method visitIfExpression.
@Override
public LogicalExpression visitIfExpression(IfExpression ifExpr, Void value) throws RuntimeException {
LogicalExpression newElseExpr = ifExpr.elseExpression.accept(this, value);
IfCondition conditions = ifExpr.ifCondition;
LogicalExpression newCondition = conditions.condition.accept(this, value);
LogicalExpression newExpr = conditions.expression.accept(this, value);
conditions = new IfExpression.IfCondition(newCondition, newExpr);
return IfExpression.newBuilder().setElse(newElseExpr).setIfCondition(conditions).build();
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by apache.
the class TreeTest method testExpressionParsing.
/**
* Attempt to parse an expression. Once parsed, convert it to a string and then parse it again to make sure serialization works.
* @param expr
* @throws RecognitionException
* @throws IOException
*/
private void testExpressionParsing(String expr) throws RecognitionException, IOException {
logger.debug("-----" + expr + "-----");
LogicalExpression e = parseExpression(expr);
String newStringExpr = serializeExpression(e);
logger.debug(newStringExpr);
LogicalExpression e2 = parseExpression(newStringExpr);
//Assert.assertEquals(e, e2);
}
Aggregations