use of org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass 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.base.EquivalenceClass 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;
}
use of org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass in project asterixdb by apache.
the class FDsAndEquivClassesVisitor method visitGroupByOperator.
@Override
public Void visitGroupByOperator(GroupByOperator 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);
List<FunctionalDependency> inheritedFDs = new ArrayList<FunctionalDependency>();
for (ILogicalPlan p : op.getNestedPlans()) {
for (Mutable<ILogicalOperator> r : p.getRoots()) {
ILogicalOperator op2 = r.getValue();
equivalenceClasses.putAll(getOrComputeEqClasses(op2, ctx));
inheritedFDs.addAll(getOrComputeFDs(op2, ctx));
}
}
ILogicalOperator op0 = op.getInputs().get(0).getValue();
inheritedFDs.addAll(getOrComputeFDs(op0, ctx));
Map<LogicalVariable, EquivalenceClass> inheritedEcs = getOrComputeEqClasses(op0, ctx);
for (FunctionalDependency inherited : inheritedFDs) {
boolean isCoveredByGbyOrDecorVars = true;
List<LogicalVariable> newHead = new ArrayList<LogicalVariable>(inherited.getHead().size());
for (LogicalVariable v : inherited.getHead()) {
LogicalVariable vnew = getNewGbyVar(op, v);
if (vnew == null) {
vnew = getNewDecorVar(op, v);
if (vnew == null) {
isCoveredByGbyOrDecorVars = false;
}
break;
}
newHead.add(vnew);
}
if (isCoveredByGbyOrDecorVars) {
List<LogicalVariable> newTail = new ArrayList<LogicalVariable>();
for (LogicalVariable v2 : inherited.getTail()) {
LogicalVariable v3 = getNewGbyVar(op, v2);
if (v3 != null) {
newTail.add(v3);
}
}
if (!newTail.isEmpty()) {
FunctionalDependency newFd = new FunctionalDependency(newHead, newTail);
functionalDependencies.add(newFd);
}
}
}
List<LogicalVariable> premiseGby = new LinkedList<LogicalVariable>();
List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> gByList = op.getGroupByList();
for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
premiseGby.add(p.first);
}
List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> decorList = op.getDecorList();
LinkedList<LogicalVariable> conclDecor = new LinkedList<LogicalVariable>();
for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : decorList) {
conclDecor.add(GroupByOperator.getDecorVariable(p));
}
if (!conclDecor.isEmpty()) {
functionalDependencies.add(new FunctionalDependency(premiseGby, conclDecor));
}
Set<LogicalVariable> gbySet = new HashSet<LogicalVariable>();
for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
ILogicalExpression expr = p.second.getValue();
if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression v = (VariableReferenceExpression) expr;
gbySet.add(v.getVariableReference());
}
}
LocalGroupingProperty lgp = new LocalGroupingProperty(gbySet);
ILocalStructuralProperty normalizedLgp = lgp.normalize(inheritedEcs, inheritedFDs);
Set<LogicalVariable> normSet = new ListSet<>();
normalizedLgp.getColumns(normSet);
List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> newGbyList = new ArrayList<>();
boolean changed = false;
for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gByList) {
ILogicalExpression expr = p.second.getValue();
if (expr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression varRef = (VariableReferenceExpression) expr;
LogicalVariable v2 = varRef.getVariableReference();
EquivalenceClass ec2 = inheritedEcs.get(v2);
LogicalVariable v3;
if (ec2 != null && !ec2.representativeIsConst()) {
v3 = ec2.getVariableRepresentative();
} else {
v3 = v2;
}
if (normSet.contains(v3)) {
newGbyList.add(p);
} else {
changed = true;
decorList.add(p);
}
} else {
newGbyList.add(p);
}
}
if (changed) {
AlgebricksConfig.ALGEBRICKS_LOGGER.fine(">>>> Group-by list changed from " + GroupByOperator.veListToString(gByList) + " to " + GroupByOperator.veListToString(newGbyList) + ".\n");
}
gByList.clear();
gByList.addAll(newGbyList);
return null;
}
use of org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass in project asterixdb by apache.
the class AddEquivalenceClassForRecordConstructorRule method propagateEquivalenceClassesForRecordConstructor.
@SuppressWarnings("unchecked")
private boolean propagateEquivalenceClassesForRecordConstructor(LogicalVariable recordVar, AbstractFunctionCallExpression funcExpr, AssignOperator assignOp, IOptimizationContext context) {
List<Mutable<ILogicalExpression>> argRefs = funcExpr.getArguments();
boolean changed = false;
// Only odd position arguments are field value expressions.
for (int parameterIndex = 1; parameterIndex < argRefs.size(); parameterIndex += 2) {
ILogicalExpression fieldExpr = argRefs.get(parameterIndex).getValue();
// Adds equivalent classes if a field is from a variable reference.
if (fieldExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression varExpr = (VariableReferenceExpression) fieldExpr;
LogicalVariable fieldVar = varExpr.getVariableReference();
Map<LogicalVariable, EquivalenceClass> ecs = context.getEquivalenceClassMap(assignOp);
if (ecs == null) {
ecs = new HashMap<LogicalVariable, EquivalenceClass>();
context.putEquivalenceClassMap(assignOp, ecs);
}
ILogicalExpression expr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_INDEX), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(recordVar)), new MutableObject<ILogicalExpression>(// Every two parameters corresponds to a field.
new ConstantExpression(new AsterixConstantValue(new AInt32(parameterIndex / 2)))));
EquivalenceClass equivClass = new EquivalenceClass(Collections.singletonList(fieldVar), fieldVar, Collections.singletonList(expr));
ecs.put(fieldVar, equivClass);
changed = true;
}
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.base.EquivalenceClass in project asterixdb by apache.
the class AbstractFunctionCallExpression method getFDsAndEquivClassesForEqWithConstant.
private static final void getFDsAndEquivClassesForEqWithConstant(ConstantExpression c, VariableReferenceExpression v, Collection<FunctionalDependency> fds, Map<LogicalVariable, EquivalenceClass> equivClasses) {
LogicalVariable var = v.getVariableReference();
LinkedList<LogicalVariable> head = new LinkedList<LogicalVariable>();
// empty set in the head
LinkedList<LogicalVariable> tail = new LinkedList<LogicalVariable>();
tail.add(var);
FunctionalDependency fd = new FunctionalDependency(head, tail);
fds.add(fd);
EquivalenceClass ec = equivClasses.get(var);
if (ec == null) {
LinkedList<LogicalVariable> members = new LinkedList<LogicalVariable>();
members.add(var);
EquivalenceClass eclass = new EquivalenceClass(members, c);
equivClasses.put(var, eclass);
} else {
if (ec.representativeIsConst()) {
ILogicalExpression c1 = ec.getConstRepresentative();
if (!c1.equals(c)) {
// here I could also rewrite to FALSE
return;
}
}
ec.setConstRepresentative(c);
}
}
Aggregations