use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method processSelectClause.
// Generates the return expression for a select clause.
private Pair<ILogicalOperator, LogicalVariable> processSelectClause(SelectBlock selectBlock, Mutable<ILogicalOperator> tupSrc) throws CompilationException {
SelectClause selectClause = selectBlock.getSelectClause();
Expression returnExpr;
if (selectClause.selectElement()) {
returnExpr = selectClause.getSelectElement().getExpression();
} else {
returnExpr = generateReturnExpr(selectClause, selectBlock);
}
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(returnExpr, tupSrc);
LogicalVariable returnVar;
ILogicalOperator returnOperator;
if (returnExpr.getKind() == Kind.VARIABLE_EXPRESSION) {
VariableExpr varExpr = (VariableExpr) returnExpr;
returnOperator = eo.second.getValue();
returnVar = context.getVar(varExpr.getVar().getId());
} else {
returnVar = context.newVar();
returnOperator = new AssignOperator(returnVar, new MutableObject<ILogicalExpression>(eo.first));
returnOperator.getInputs().add(eo.second);
}
if (selectClause.distinct()) {
DistinctOperator distinctOperator = new DistinctOperator(mkSingletonArrayList(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(returnVar))));
distinctOperator.getInputs().add(new MutableObject<ILogicalOperator>(returnOperator));
return new Pair<>(distinctOperator, returnVar);
} else {
return new Pair<>(returnOperator, returnVar);
}
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(Query q, Mutable<ILogicalOperator> tupSource) throws CompilationException {
Expression queryBody = q.getBody();
if (queryBody.getKind() == Kind.SELECT_EXPRESSION) {
SelectExpression selectExpr = (SelectExpression) queryBody;
if (q.isTopLevel()) {
selectExpr.setSubquery(false);
}
return queryBody.accept(this, tupSource);
} else {
LogicalVariable var = context.newVar();
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(queryBody, tupSource);
AssignOperator assignOp = new AssignOperator(var, new MutableObject<ILogicalExpression>(eo.first));
assignOp.getInputs().add(eo.second);
ProjectOperator projectOp = new ProjectOperator(var);
projectOp.getInputs().add(new MutableObject<ILogicalOperator>(assignOp));
return new Pair<>(projectOp, var);
}
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator in project asterixdb by apache.
the class SqlppExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(FromTerm fromTerm, Mutable<ILogicalOperator> tupSource) throws CompilationException {
LogicalVariable fromVar = context.newVarFromExpression(fromTerm.getLeftVariable());
Expression fromExpr = fromTerm.getLeftExpression();
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(fromExpr, tupSource);
ILogicalOperator unnestOp;
if (fromTerm.hasPositionalVariable()) {
LogicalVariable pVar = context.newVarFromExpression(fromTerm.getPositionalVariable());
// We set the positional variable type as BIGINT type.
unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)), pVar, BuiltinType.AINT64, new PositionWriter());
} else {
unnestOp = new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)));
}
unnestOp.getInputs().add(eo.second);
// Processes joins, unnests, and nests.
Mutable<ILogicalOperator> topOpRef = new MutableObject<>(unnestOp);
if (fromTerm.hasCorrelateClauses()) {
for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
if (correlateClause.getClauseType() == ClauseType.UNNEST_CLAUSE) {
// Correlation is allowed.
topOpRef = new MutableObject<>(correlateClause.accept(this, topOpRef).first);
} else {
// Correlation is dis-allowed.
uncorrelatedLeftBranchStack.push(topOpRef);
topOpRef = new MutableObject<>(correlateClause.accept(this, tupSource).first);
}
}
}
return new Pair<>(topOpRef.getValue(), fromVar);
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator in project asterixdb by apache.
the class EnforceOrderByAfterSubplan method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
if (context.checkIfInDontApplySet(this, op1)) {
return false;
}
List<Mutable<ILogicalOperator>> inputs = op1.getInputs();
context.addToDontApplySet(this, op1);
if (op1.getOperatorTag() == LogicalOperatorTag.ORDER || inputs == null) {
/**
* does not apply if
* 1. there is yet-another order operator on-top-of the subplan, because the downstream order operator's ordering will be broken anyway
* 2. the input operator(s) is null
*/
return false;
}
boolean changed = false;
for (int i = 0; i < inputs.size(); i++) {
Mutable<ILogicalOperator> inputOpRef = inputs.get(i);
AbstractLogicalOperator op = (AbstractLogicalOperator) inputOpRef.getValue();
context.addToDontApplySet(this, op);
if (op.getOperatorTag() != LogicalOperatorTag.SUBPLAN) {
continue;
}
/**
* check the order operators whose ordering is not broken before the subplan operator, and then
* duplicate them on-top-of the subplan operator
*/
boolean foundTarget = true;
boolean orderSensitive = false;
Mutable<ILogicalOperator> childRef = op.getInputs().get(0);
AbstractLogicalOperator child = (AbstractLogicalOperator) childRef.getValue();
while (child.getOperatorTag() != LogicalOperatorTag.ORDER) {
context.addToDontApplySet(this, child);
if (orderBreakingOps.contains(child.getOperatorTag())) {
foundTarget = false;
break;
}
if (child.getOperatorTag() == LogicalOperatorTag.GROUP) {
foundTarget = false;
break;
}
if (orderSensitiveOps.contains(child.getOperatorTag())) {
orderSensitive = true;
}
List<Mutable<ILogicalOperator>> childInputs = child.getInputs();
if (childInputs == null || childInputs.size() > 2 || childInputs.size() < 1) {
foundTarget = false;
break;
} else {
childRef = childInputs.get(0);
child = (AbstractLogicalOperator) childRef.getValue();
}
}
/** the target order-by operator has not been found. */
if (!foundTarget) {
return false;
}
/** copy the original order-by operator and insert on-top-of the subplan operator */
context.addToDontApplySet(this, child);
OrderOperator sourceOrderOp = (OrderOperator) child;
for (Pair<IOrder, Mutable<ILogicalExpression>> expr : sourceOrderOp.getOrderExpressions()) {
if (!expr.second.getValue().isFunctional()) {
return false;
}
}
List<Pair<IOrder, Mutable<ILogicalExpression>>> orderExprs = deepCopyOrderAndExpression(sourceOrderOp.getOrderExpressions());
OrderOperator newOrderOp = new OrderOperator(orderExprs);
context.addToDontApplySet(this, newOrderOp);
inputs.set(i, new MutableObject<ILogicalOperator>(newOrderOp));
newOrderOp.getInputs().add(inputOpRef);
context.computeAndSetTypeEnvironmentForOperator(newOrderOp);
if (!orderSensitive) {
/** remove the original order-by */
childRef.setValue(sourceOrderOp.getInputs().get(0).getValue());
}
changed = true;
}
return changed;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator in project asterixdb by apache.
the class EnforceStructuralPropertiesRule method physOptimizePlan.
private boolean physOptimizePlan(ILogicalPlan plan, IPhysicalPropertiesVector pvector, boolean nestedPlan, IOptimizationContext context) throws AlgebricksException {
boolean changed = false;
for (Mutable<ILogicalOperator> root : plan.getRoots()) {
if (physOptimizeOp(root, pvector, nestedPlan, context)) {
changed = true;
}
AbstractLogicalOperator op = (AbstractLogicalOperator) root.getValue();
op.computeDeliveredPhysicalProperties(context);
AlgebricksConfig.ALGEBRICKS_LOGGER.finest(">>>> Structural properties for " + op.getPhysicalOperator() + ": " + op.getDeliveredPhysicalProperties() + "\n");
}
return changed;
}
Aggregations