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