use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.
the class EquivalenceClassUtils method findOrCreatePrimaryKeyOpAndVariables.
/**
* Find the header variables that can imply all subplan-local live variables at <code>operator</code>.
*
* @param operator
* the operator of interest.
* @param usedForCorrelationJoin
* whether the generated primary key will be used for a join that recovers the correlation.
* @param context
* the optimization context.
* @return Pair<ILogicalOperator, Set<LogicalVariable>>, an operator (which is either the original parameter
* <code>operator</code> or a newly created operator) and
* a set of primary key variables at the operator.
* @throws AlgebricksException
*/
public static Pair<ILogicalOperator, Set<LogicalVariable>> findOrCreatePrimaryKeyOpAndVariables(ILogicalOperator operator, boolean usedForCorrelationJoin, IOptimizationContext context) throws AlgebricksException {
computePrimaryKeys(operator, context);
Set<LogicalVariable> liveVars = new HashSet<>();
VariableUtilities.getSubplanLocalLiveVariables(operator, liveVars);
Set<LogicalVariable> primaryKeyVars = new HashSet<>();
Set<LogicalVariable> noKeyVars = new HashSet<>();
for (LogicalVariable liveVar : liveVars) {
List<LogicalVariable> keyVars = context.findPrimaryKey(liveVar);
if (keyVars != null) {
keyVars.retainAll(liveVars);
}
if ((keyVars == null || keyVars.isEmpty())) {
noKeyVars.add(liveVar);
} else {
primaryKeyVars.addAll(keyVars);
}
}
primaryKeyVars.retainAll(liveVars);
if (primaryKeyVars.containsAll(noKeyVars)) {
return new Pair<ILogicalOperator, Set<LogicalVariable>>(operator, primaryKeyVars);
} else {
LogicalVariable assignVar = context.newVar();
ILogicalOperator assignOp = new AssignOperator(assignVar, new MutableObject<ILogicalExpression>(new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_QUERY_UID), null)));
OperatorPropertiesUtil.markMovable(assignOp, !usedForCorrelationJoin);
assignOp.getInputs().add(new MutableObject<ILogicalOperator>(operator));
context.addPrimaryKey(new FunctionalDependency(Collections.singletonList(assignVar), new ArrayList<LogicalVariable>(liveVars)));
context.computeAndSetTypeEnvironmentForOperator(assignOp);
return new Pair<ILogicalOperator, Set<LogicalVariable>>(assignOp, Collections.singleton(assignVar));
}
}
use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.
the class AbstractDecorrelationRule method computeGbyVarsUsingPksOnly.
protected Set<LogicalVariable> computeGbyVarsUsingPksOnly(Set<LogicalVariable> varSet, AbstractLogicalOperator op, IOptimizationContext context) throws AlgebricksException {
PhysicalOptimizationsUtil.computeFDsAndEquivalenceClasses(op, context);
List<FunctionalDependency> fdList = context.getFDList(op);
if (fdList == null) {
return null;
}
// check if any of the FDs is a key
for (FunctionalDependency fd : fdList) {
if (fd.getTail().containsAll(varSet)) {
return new HashSet<LogicalVariable>(fd.getHead());
}
}
return null;
}
use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.
the class EnforceStructuralPropertiesRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
// wait for the physical operators to be set first
if (op.getPhysicalOperator() == null) {
return false;
}
if (context.checkIfInDontApplySet(this, op)) {
return false;
}
List<FunctionalDependency> fds = context.getFDList(op);
if (fds != null && !fds.isEmpty()) {
return false;
}
// These are actually logical constraints, so they could be pre-computed
// somewhere else, too.
physicalOptimizationConfig = context.getPhysicalOptimizationConfig();
AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Optimizing operator " + op.getPhysicalOperator() + ".\n");
PhysicalOptimizationsUtil.computeFDsAndEquivalenceClasses(op, context);
StructuralPropertiesVector pvector = new StructuralPropertiesVector(new RandomPartitioningProperty(context.getComputationNodeDomain()), new LinkedList<ILocalStructuralProperty>());
boolean changed = physOptimizeOp(opRef, pvector, false, context);
op.computeDeliveredPhysicalProperties(context);
AlgebricksConfig.ALGEBRICKS_LOGGER.finest(">>>> Structural properties for " + op.getPhysicalOperator() + ": " + op.getDeliveredPhysicalProperties() + "\n");
context.addToDontApplySet(this, opRef.getValue());
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.
the class FDsAndEquivClassesVisitor method visitSubplanOperator.
@Override
public Void visitSubplanOperator(SubplanOperator op, IOptimizationContext ctx) throws AlgebricksException {
Map<LogicalVariable, EquivalenceClass> equivalenceClasses = new HashMap<LogicalVariable, EquivalenceClass>();
List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
ctx.putEquivalenceClassMap(op, equivalenceClasses);
ctx.putFDList(op, functionalDependencies);
for (ILogicalPlan p : op.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
ILogicalOperator op2 = r.getValue();
equivalenceClasses.putAll(getOrComputeEqClasses(op2, ctx));
functionalDependencies.addAll(getOrComputeFDs(op2, ctx));
}
}
return null;
}
use of org.apache.hyracks.algebricks.core.algebra.properties.FunctionalDependency in project asterixdb by apache.
the class FDsAndEquivClassesVisitor method visitInnerJoinOperator.
@Override
public Void visitInnerJoinOperator(InnerJoinOperator op, IOptimizationContext ctx) throws AlgebricksException {
Map<LogicalVariable, EquivalenceClass> equivalenceClasses = new HashMap<LogicalVariable, EquivalenceClass>();
List<FunctionalDependency> functionalDependencies = new ArrayList<FunctionalDependency>();
ctx.putEquivalenceClassMap(op, equivalenceClasses);
ctx.putFDList(op, functionalDependencies);
ILogicalOperator op0 = op.getInputs().get(0).getValue();
ILogicalOperator op1 = op.getInputs().get(1).getValue();
functionalDependencies.addAll(getOrComputeFDs(op0, ctx));
functionalDependencies.addAll(getOrComputeFDs(op1, ctx));
equivalenceClasses.putAll(getOrComputeEqClasses(op0, ctx));
equivalenceClasses.putAll(getOrComputeEqClasses(op1, ctx));
ILogicalExpression expr = op.getCondition().getValue();
expr.getConstraintsAndEquivClasses(functionalDependencies, equivalenceClasses);
return null;
}
Aggregations