use of org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator in project asterixdb by apache.
the class IntroduceProjectsRule method introduceProjects.
protected boolean introduceProjects(AbstractLogicalOperator parentOp, int parentInputIndex, Mutable<ILogicalOperator> opRef, Set<LogicalVariable> parentUsedVars, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
boolean modified = false;
usedVars.clear();
VariableUtilities.getUsedVariables(op, usedVars);
// In the top-down pass, maintain a set of variables that are used in op and all its parents.
// This is a necessary step for the newly created project operator during this optimization,
// since we already have all information from collectUsedVars() method for the other operators.
HashSet<LogicalVariable> parentsUsedVars = new HashSet<>();
parentsUsedVars.addAll(parentUsedVars);
parentsUsedVars.addAll(usedVars);
if (allUsedVarsAfterOpMap.get(op) != null) {
parentsUsedVars.addAll(allUsedVarsAfterOpMap.get(op));
}
// Descend into children.
for (int i = 0; i < op.getInputs().size(); i++) {
Mutable<ILogicalOperator> inputOpRef = op.getInputs().get(i);
if (introduceProjects(op, i, inputOpRef, parentsUsedVars, context)) {
modified = true;
}
}
if (modified) {
context.computeAndSetTypeEnvironmentForOperator(op);
}
// In the bottom-up pass, determine which live variables are not used by op's parents.
// Such variables are be projected away.
liveVars.clear();
VariableUtilities.getLiveVariables(op, liveVars);
producedVars.clear();
VariableUtilities.getProducedVariables(op, producedVars);
liveVars.removeAll(producedVars);
projectVars.clear();
for (LogicalVariable liveVar : liveVars) {
if (parentsUsedVars.contains(liveVar)) {
projectVars.add(liveVar);
}
}
// Some of the variables that are live at this op are not used above.
if (projectVars.size() != liveVars.size()) {
// Add a project operator under each of op's qualifying input branches.
for (int i = 0; i < op.getInputs().size(); i++) {
ILogicalOperator childOp = op.getInputs().get(i).getValue();
liveVars.clear();
VariableUtilities.getLiveVariables(childOp, liveVars);
List<LogicalVariable> vars = new ArrayList<>();
vars.addAll(projectVars);
// Only retain those variables that are live in the i-th input branch.
vars.retainAll(liveVars);
if (vars.size() != liveVars.size()) {
ProjectOperator projectOp = new ProjectOperator(vars);
projectOp.getInputs().add(new MutableObject<ILogicalOperator>(childOp));
op.getInputs().get(i).setValue(projectOp);
context.computeAndSetTypeEnvironmentForOperator(projectOp);
modified = true;
}
}
} else if (op.getOperatorTag() == LogicalOperatorTag.PROJECT) {
// Check if the existing project has become useless.
liveVars.clear();
VariableUtilities.getLiveVariables(op.getInputs().get(0).getValue(), liveVars);
ProjectOperator projectOp = (ProjectOperator) op;
List<LogicalVariable> projectVarsTemp = projectOp.getVariables();
if (liveVars.size() == projectVarsTemp.size() && liveVars.containsAll(projectVarsTemp)) {
boolean eliminateProject = true;
// For UnionAll the variables must also be in exactly the correct order.
if (parentOp.getOperatorTag() == LogicalOperatorTag.UNIONALL) {
eliminateProject = canEliminateProjectBelowUnion((UnionAllOperator) parentOp, projectOp, parentInputIndex);
}
if (eliminateProject) {
// The existing project has become useless. Remove it.
parentOp.getInputs().get(parentInputIndex).setValue(op.getInputs().get(0).getValue());
}
}
}
if (modified) {
context.computeAndSetTypeEnvironmentForOperator(op);
}
return modified;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator in project asterixdb by apache.
the class StreamProjectPOperator method contributeRuntimeOperator.
@Override
public void contributeRuntimeOperator(IHyracksJobBuilder builder, JobGenContext context, ILogicalOperator op, IOperatorSchema propagatedSchema, IOperatorSchema[] inputSchemas, IOperatorSchema outerPlanSchema) throws AlgebricksException {
ProjectOperator project = (ProjectOperator) op;
int[] projectionList = new int[project.getVariables().size()];
int i = 0;
for (LogicalVariable v : project.getVariables()) {
int pos = inputSchemas[0].findVariable(v);
if (pos < 0) {
throw new AlgebricksException("Could not find variable " + v + ".");
}
projectionList[i++] = pos;
}
StreamProjectRuntimeFactory runtime = new StreamProjectRuntimeFactory(projectionList, flushFramesRapidly);
RecordDescriptor recDesc = JobGenHelper.mkRecordDescriptor(context.getTypeEnvironment(op), propagatedSchema, context);
builder.contributeMicroOperator(project, runtime, recDesc);
ILogicalOperator src = project.getInputs().get(0).getValue();
builder.contributeGraphEdge(src, 0, project, 0);
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator in project asterixdb by apache.
the class PushProjectDownRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.PROJECT) {
return false;
}
ProjectOperator pi = (ProjectOperator) op;
Mutable<ILogicalOperator> opRef2 = pi.getInputs().get(0);
HashSet<LogicalVariable> toPush = new HashSet<LogicalVariable>();
toPush.addAll(pi.getVariables());
Pair<Boolean, Boolean> p = pushThroughOp(toPush, opRef2, op, context);
boolean smthWasPushed = p.first;
if (p.second) {
// the original projection is redundant
opRef.setValue(op.getInputs().get(0).getValue());
smthWasPushed = true;
}
return smthWasPushed;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator in project asterixdb by apache.
the class PushProjectIntoDataSourceScanRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getInputs().size() <= 0)
return false;
AbstractLogicalOperator project = (AbstractLogicalOperator) op.getInputs().get(0).getValue();
if (project.getOperatorTag() != LogicalOperatorTag.PROJECT)
return false;
AbstractLogicalOperator exchange = (AbstractLogicalOperator) project.getInputs().get(0).getValue();
if (exchange.getOperatorTag() != LogicalOperatorTag.EXCHANGE)
return false;
AbstractLogicalOperator inputOp = (AbstractLogicalOperator) exchange.getInputs().get(0).getValue();
if (inputOp.getOperatorTag() != LogicalOperatorTag.DATASOURCESCAN)
return false;
DataSourceScanOperator scanOp = (DataSourceScanOperator) inputOp;
ProjectOperator projectOp = (ProjectOperator) project;
scanOp.addProjectVariables(projectOp.getVariables());
if (op.getOperatorTag() != LogicalOperatorTag.EXCHANGE) {
op.getInputs().set(0, project.getInputs().get(0));
} else {
op.getInputs().set(0, exchange.getInputs().get(0));
}
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.ProjectOperator in project asterixdb by apache.
the class RemoveRedundantProjectionRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
if (op1.getOperatorTag() == LogicalOperatorTag.PROJECT) {
Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0);
AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.PROJECT) {
return false;
}
ProjectOperator pi2 = (ProjectOperator) op2;
opRef2.setValue(pi2.getInputs().get(0).getValue());
} else {
if (op1.getInputs().size() <= 0)
return false;
Mutable<ILogicalOperator> opRef2 = op1.getInputs().get(0);
AbstractLogicalOperator op2 = (AbstractLogicalOperator) opRef2.getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.PROJECT) {
return false;
}
if (op2.getInputs().size() <= 0)
return false;
Mutable<ILogicalOperator> opRef3 = op2.getInputs().get(0);
AbstractLogicalOperator op3 = (AbstractLogicalOperator) opRef3.getValue();
List<LogicalVariable> liveVars2 = new ArrayList<LogicalVariable>();
List<LogicalVariable> liveVars3 = new ArrayList<LogicalVariable>();
VariableUtilities.getLiveVariables(op2, liveVars2);
VariableUtilities.getLiveVariables(op3, liveVars3);
if (!VariableUtilities.varListEqualUnordered(liveVars2, liveVars3))
return false;
opRef2.setValue(op3);
}
return true;
}
Aggregations