use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class RemoveRedundantListifyRule method applies.
private boolean applies(Mutable<ILogicalOperator> opRef, Set<LogicalVariable> varUsedAbove, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnest1 = (UnnestOperator) op1;
ILogicalExpression expr = unnest1.getExpressionRef().getValue();
if (expr.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
if (((AbstractFunctionCallExpression) expr).getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
return false;
}
AbstractFunctionCallExpression functionCall = (AbstractFunctionCallExpression) expr;
ILogicalExpression functionCallArgExpr = functionCall.getArguments().get(0).getValue();
if (functionCallArgExpr.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable unnestedVar = ((VariableReferenceExpression) functionCallArgExpr).getVariableReference();
if (varUsedAbove.contains(unnestedVar)) {
return false;
}
Mutable<ILogicalOperator> aggregateParentRef = opRef;
AbstractLogicalOperator r = op1;
boolean metAggregate = false;
while (r.getInputs().size() == 1) {
aggregateParentRef = r.getInputs().get(0);
r = (AbstractLogicalOperator) aggregateParentRef.getValue();
if (r.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assign = (AssignOperator) r;
List<LogicalVariable> variables = assign.getVariables();
// The assign operator doesn't produce any variable that is used by the unnest.
if (variables.contains(unnestedVar)) {
return false;
}
} else {
if (r.getOperatorTag() == LogicalOperatorTag.AGGREGATE) {
metAggregate = true;
}
break;
}
}
if (!metAggregate) {
return false;
}
AggregateOperator agg = (AggregateOperator) r;
if (agg.getVariables().size() > 1) {
return false;
}
LogicalVariable aggVar = agg.getVariables().get(0);
ILogicalExpression aggFun = agg.getExpressions().get(0).getValue();
if (!aggVar.equals(unnestedVar) || ((AbstractLogicalExpression) aggFun).getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) aggFun;
if (!BuiltinFunctions.LISTIFY.equals(f.getFunctionIdentifier())) {
return false;
}
if (f.getArguments().size() != 1) {
return false;
}
ILogicalExpression arg0 = f.getArguments().get(0).getValue();
if (((AbstractLogicalExpression) arg0).getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
LogicalVariable paramVar = ((VariableReferenceExpression) arg0).getVariableReference();
List<LogicalVariable> assgnVars = new ArrayList<>(1);
assgnVars.add(unnest1.getVariable());
List<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<>(1);
assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar)));
AssignOperator assign = new AssignOperator(assgnVars, assgnExprs);
assign.getInputs().add(agg.getInputs().get(0));
context.computeAndSetTypeEnvironmentForOperator(assign);
LogicalVariable posVar = unnest1.getPositionalVariable();
if (posVar == null) {
// Removes the aggregate operator.
aggregateParentRef.setValue(assign);
} else {
List<LogicalVariable> raggVars = new ArrayList<>(1);
raggVars.add(posVar);
List<Mutable<ILogicalExpression>> rAggExprs = new ArrayList<>(1);
StatefulFunctionCallExpression tidFun = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
rAggExprs.add(new MutableObject<ILogicalExpression>(tidFun));
RunningAggregateOperator rAgg = new RunningAggregateOperator(raggVars, rAggExprs);
rAgg.getInputs().add(new MutableObject<ILogicalOperator>(assign));
aggregateParentRef.setValue(rAgg);
context.computeAndSetTypeEnvironmentForOperator(rAgg);
}
// Removes the unnest operator.
opRef.setValue(unnest1.getInputs().get(0).getValue());
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class RemoveRedundantSelectRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.SELECT) {
return false;
}
SelectOperator select = (SelectOperator) op;
ILogicalExpression cond = select.getCondition().getValue();
if (alwaysHold(cond)) {
opRef.setValue(select.getInputs().get(0).getValue());
return true;
}
return false;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class ResolveVariableRule method cleanupScanCollectionForDataset.
// Cleans up scan collections on top of a "dataset" function call since "dataset"
// is an unnest function.
private void cleanupScanCollectionForDataset(AbstractFunctionCallExpression funcExpr) {
if (funcExpr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
return;
}
ILogicalExpression arg = funcExpr.getArguments().get(0).getValue();
if (arg.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return;
}
AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) arg;
if (argFuncExpr.getFunctionIdentifier() != BuiltinFunctions.DATASET) {
return;
}
funcExpr.setFunctionInfo(argFuncExpr.getFunctionInfo());
funcExpr.getArguments().clear();
funcExpr.getArguments().addAll(argFuncExpr.getArguments());
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class ResolveVariableRule method extractExprs.
// Extracts all possible expressions from the arguments of the "resolve" function.
private List<ILogicalExpression> extractExprs(List<Mutable<ILogicalExpression>> args) throws AlgebricksException {
List<ILogicalExpression> exprs = new ArrayList<>();
// The first arg is is the name of the undefined variable.
for (int index = 1; index < args.size(); ++index) {
ILogicalExpression argExpr = args.get(index).getValue();
exprs.add(argExpr);
}
return exprs;
}
use of org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression in project asterixdb by apache.
the class RemoveLeftOuterUnnestForLeftOuterJoinRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
ILogicalOperator op1 = opRef.getValue();
// Checks the plan pattern.
if (!checkOperatorPattern(op1)) {
return false;
}
LeftOuterUnnestOperator outerUnnest = (LeftOuterUnnestOperator) op1;
GroupByOperator gbyOperator = (GroupByOperator) outerUnnest.getInputs().get(0).getValue();
LeftOuterJoinOperator lojOperator = (LeftOuterJoinOperator) gbyOperator.getInputs().get(0).getValue();
// Checks whether the left outer unnest and the group-by operator are qualified for rewriting.
Triple<Boolean, ILogicalExpression, ILogicalExpression> checkGbyResult = checkUnnestAndGby(outerUnnest, gbyOperator);
// The argument for listify and not(is-missing(...)) check should be variables.
if (!isVariableReference(checkGbyResult.second) || !isVariableReference(checkGbyResult.third)) {
return false;
}
// Checks whether both the listify variable and the condition test variable are from the right input
// branch of the left outer join.
LogicalVariable listifyVar = ((VariableReferenceExpression) checkGbyResult.second).getVariableReference();
LogicalVariable conditionTestVar = ((VariableReferenceExpression) checkGbyResult.third).getVariableReference();
if (!checkListifyAndConditionVar(lojOperator, listifyVar, conditionTestVar)) {
return false;
}
// Does the rewrite.
removeGroupByAndOuterUnnest(opRef, context, outerUnnest, gbyOperator, lojOperator, listifyVar);
return true;
}
Aggregations