use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.
the class FunctionGenerationHelper method getFunctionExpression.
private static LogicalExpression getFunctionExpression(String name, HoldingContainer... args) {
List<MajorType> argTypes = new ArrayList<MajorType>(args.length);
List<LogicalExpression> argExpressions = new ArrayList<LogicalExpression>(args.length);
for (HoldingContainer c : args) {
argTypes.add(c.getMajorType());
argExpressions.add(new HoldingContainerExpression(c));
}
return new FunctionCall(name, argExpressions, ExpressionPosition.UNKNOWN);
}
use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.
the class DrillAggFuncHolder method renderEnd.
@Override
public HoldingContainer renderEnd(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, JVar[] workspaceJVars, FunctionHolderExpression holderExpr) {
HoldingContainer out = null;
JVar internalOutput = null;
if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
out = classGenerator.declare(getReturnType(), false);
}
JBlock sub = new JBlock();
if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
internalOutput = sub.decl(JMod.FINAL, classGenerator.getHolderType(getReturnType()), getReturnValue().getName(), JExpr._new(classGenerator.getHolderType(getReturnType())));
}
classGenerator.getEvalBlock().add(sub);
addProtectedBlock(classGenerator, sub, output(), null, workspaceJVars, false);
if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
sub.assign(out.getHolder(), internalOutput);
}
// hash aggregate uses workspace vectors. Initialization is done in "setup" and does not require "reset" block.
if (!classGenerator.getMappingSet().isHashAggMapping()) {
generateBody(classGenerator, BlockType.RESET, reset(), null, workspaceJVars, false);
}
generateBody(classGenerator, BlockType.CLEANUP, cleanup(), null, workspaceJVars, false);
return out;
}
use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.
the class TopNBatch method createNewPriorityQueue.
public static PriorityQueue createNewPriorityQueue(MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping, List<Ordering> orderings, VectorAccessible batch, boolean unionTypeEnabled, boolean codegenDump, int limit, BufferAllocator allocator, SelectionVectorMode mode, FragmentContext context) {
OptionSet optionSet = context.getOptions();
FunctionLookupContext functionLookupContext = context.getFunctionRegistry();
CodeGenerator<PriorityQueue> cg = CodeGenerator.get(PriorityQueue.TEMPLATE_DEFINITION, optionSet);
cg.plainJavaCapable(true);
cg.saveCodeForDebugging(codegenDump);
// Uncomment out this line to debug the generated code.
// cg.saveCodeForDebugging(true);
ClassGenerator<PriorityQueue> 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, functionLookupContext, unionTypeEnabled);
collector.reportErrors(logger);
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, functionLookupContext);
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));
PriorityQueue q = context.getImplementationClass(cg);
try {
q.init(limit, allocator, mode == BatchSchema.SelectionVectorMode.TWO_BYTE);
} catch (SchemaChangeException e) {
throw TopNBatch.schemaChangeException(e, "Top N", logger);
}
return q;
}
use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.
the class ChainedHashTable method setupIsKeyMatchInternal.
private void setupIsKeyMatchInternal(ClassGenerator<HashTable> cg, MappingSet incomingMapping, MappingSet htableMapping, LogicalExpression[] keyExprs, List<Comparator> comparators, TypedFieldId[] htKeyFieldIds, SetupWork work) {
boolean checkIfBothNulls = work == SetupWork.CHECK_BOTH_NULLS;
// Regular key matching may return false in the middle (i.e., some pair of columns did not match), and true only if all matched;
// but "both nulls" check returns the opposite logic (i.e., true when one pair of nulls is found, need check no more)
JExpression midPointResult = checkIfBothNulls ? JExpr.TRUE : JExpr.FALSE;
JExpression finalResult = checkIfBothNulls ? JExpr.FALSE : JExpr.TRUE;
cg.setMappingSet(incomingMapping);
if (keyExprs == null || keyExprs.length == 0 || checkIfBothNulls && !comparators.contains(Comparator.EQUALS)) {
// e.g. for Hash-Aggr, or non-equi join
cg.getEvalBlock()._return(JExpr.FALSE);
return;
}
for (int i = 0; i < keyExprs.length; i++) {
final LogicalExpression expr = keyExprs[i];
cg.setMappingSet(incomingMapping);
HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND);
cg.setMappingSet(htableMapping);
ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
HoldingContainer right = cg.addExpr(vvrExpr, ClassGenerator.BlkCreateMode.FALSE);
JConditional jc;
if (work != SetupWork.DO_BUILD) {
// codegen for the special case when both columns are null (i.e., return early with midPointResult)
if (comparators.get(i) == Comparator.EQUALS && left.isOptional() && right.isOptional()) {
jc = cg.getEvalBlock()._if(left.getIsSet().eq(JExpr.lit(0)).cand(right.getIsSet().eq(JExpr.lit(0))));
jc._then()._return(midPointResult);
}
}
if (!checkIfBothNulls) {
// generate comparison code (at least one of the two columns' values is non-null)
final LogicalExpression f = FunctionGenerationHelper.getOrderingComparatorNullsHigh(left, right, context.getFunctionRegistry());
HoldingContainer out = cg.addExpr(f, ClassGenerator.BlkCreateMode.FALSE);
// check if two values are not equal (comparator result != 0)
jc = cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
jc._then()._return(midPointResult);
}
}
// All key expressions compared the same way, so return the appropriate final result
cg.getEvalBlock()._return(finalResult);
}
use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.
the class DrillSimpleFuncHolder method generateEvalBody.
protected HoldingContainer generateEvalBody(ClassGenerator<?> g, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FieldReference ref) {
g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0]));
JBlock sub = new JBlock(true, true);
JBlock topSub = sub;
HoldingContainer out = null;
MajorType returnValueType = getReturnType();
// add outside null handling if it is defined.
if (getNullHandling() == NullHandling.NULL_IF_NULL) {
JExpression e = null;
for (HoldingContainer v : inputVariables) {
if (v.isOptional()) {
JExpression isNullExpr;
if (v.isReader()) {
isNullExpr = JOp.cond(v.getHolder().invoke("isSet"), JExpr.lit(1), JExpr.lit(0));
} else {
isNullExpr = v.getIsSet();
}
if (e == null) {
e = isNullExpr;
} else {
e = e.mul(isNullExpr);
}
}
}
if (e != null) {
// if at least one expression must be checked, set up the conditional.
returnValueType = getReturnType().toBuilder().setMode(DataMode.OPTIONAL).build();
out = g.declare(returnValueType);
e = e.eq(JExpr.lit(0));
JConditional jc = sub._if(e);
jc._then().assign(out.getIsSet(), JExpr.lit(0));
sub = jc._else();
}
}
if (out == null) {
out = g.declare(returnValueType);
}
// add the subblock after the out declaration.
g.getEvalBlock().add(topSub);
JVar internalOutput = sub.decl(JMod.FINAL, g.getHolderType(returnValueType), getReturnValue().getName(), JExpr._new(g.getHolderType(returnValueType)));
addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false);
if (sub != topSub) {
// Assign null if NULL_IF_NULL mode
sub.assign(internalOutput.ref("isSet"), JExpr.lit(1));
}
sub.assign(out.getHolder(), internalOutput);
if (sub != topSub) {
// Assign null if NULL_IF_NULL mode
sub.assign(internalOutput.ref("isSet"), JExpr.lit(1));
}
g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0]));
return out;
}
Aggregations