use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class IntroduceRandomPartitioningFeedComputationRule method rewritePre.
@Override
public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
ILogicalOperator op = opRef.getValue();
if (!op.getOperatorTag().equals(LogicalOperatorTag.ASSIGN)) {
return false;
}
ILogicalOperator opChild = op.getInputs().get(0).getValue();
if (!opChild.getOperatorTag().equals(LogicalOperatorTag.DATASOURCESCAN)) {
return false;
}
DataSourceScanOperator scanOp = (DataSourceScanOperator) opChild;
DataSource dataSource = (DataSource) scanOp.getDataSource();
if (dataSource.getDatasourceType() != DataSource.Type.FEED) {
return false;
}
final FeedDataSource feedDataSource = (FeedDataSource) dataSource;
FeedConnection feedConnection = feedDataSource.getFeedConnection();
if (feedConnection.getAppliedFunctions() == null || feedConnection.getAppliedFunctions().size() == 0) {
return false;
}
ExchangeOperator exchangeOp = new ExchangeOperator();
INodeDomain domain = new INodeDomain() {
@Override
public boolean sameAs(INodeDomain domain) {
return domain == this;
}
@Override
public Integer cardinality() {
return feedDataSource.getComputeCardinality();
}
};
exchangeOp.setPhysicalOperator(new RandomPartitionExchangePOperator(domain));
op.getInputs().get(0).setValue(exchangeOp);
exchangeOp.getInputs().add(new MutableObject<ILogicalOperator>(scanOp));
ExecutionMode em = ((AbstractLogicalOperator) scanOp).getExecutionMode();
exchangeOp.setExecutionMode(em);
exchangeOp.computeDeliveredPhysicalProperties(context);
context.computeAndSetTypeEnvironmentForOperator(exchangeOp);
AssignOperator assignOp = (AssignOperator) opRef.getValue();
AssignPOperator assignPhyOp = (AssignPOperator) assignOp.getPhysicalOperator();
assignPhyOp.setCardinalityConstraint(domain.cardinality());
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class PushAggregateIntoNestedSubplanRule method collectVarsBottomUp.
private boolean collectVarsBottomUp(Mutable<ILogicalOperator> opRef, IOptimizationContext context, Map<LogicalVariable, Integer> nspListifyVarsCount, Map<LogicalVariable, AbstractOperatorWithNestedPlans> nspWithAgg, Map<LogicalVariable, Integer> nspAggVarToPlanIndex, Map<ILogicalExpression, ILogicalExpression> aggregateExprToVarExpr) throws AlgebricksException {
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
context.addToDontApplySet(this, op1);
boolean change = false;
for (Mutable<ILogicalOperator> child : op1.getInputs()) {
if (collectVarsBottomUp(child, context, nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, aggregateExprToVarExpr)) {
change = true;
}
}
Set<LogicalVariable> used = new HashSet<>();
VariableUtilities.getUsedVariables(op1, used);
switch(op1.getOperatorTag()) {
case ASSIGN:
case SELECT:
boolean found = false;
// Do some prefiltering: check if the Assign uses any nsp vars.
for (LogicalVariable v : used) {
if (nspListifyVarsCount.get(v) != null) {
found = true;
break;
}
}
if (!found) {
break;
}
if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assign = (AssignOperator) op1;
for (Mutable<ILogicalExpression> exprRef : assign.getExpressions()) {
Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, nspWithAgg, aggregateExprToVarExpr, context);
if (p.first) {
change = true;
exprRef.setValue(p.second);
}
}
}
if (op1.getOperatorTag() == LogicalOperatorTag.SELECT) {
SelectOperator select = (SelectOperator) op1;
Mutable<ILogicalExpression> exprRef = select.getCondition();
Pair<Boolean, ILogicalExpression> p = extractAggFunctionsFromExpression(exprRef, nspWithAgg, aggregateExprToVarExpr, context);
if (p.first) {
change = true;
exprRef.setValue(p.second);
}
}
used.clear();
VariableUtilities.getUsedVariables(op1, used);
// increment the count for the ones which are still used
for (LogicalVariable v : used) {
Integer m = nspListifyVarsCount.get(v);
if (m != null) {
nspListifyVarsCount.put(v, m + 1);
}
}
break;
case SUBPLAN:
// Try to push the subplan into a group-by operator if possible.
for (LogicalVariable v : used) {
Integer m = nspListifyVarsCount.get(v);
if (m != null) {
AbstractOperatorWithNestedPlans nspOp = nspWithAgg.get(v);
if (pushSubplanAsAggIntoNestedSubplan(opRef, nspOp, v, nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, context)) {
change = true;
} else {
nspListifyVarsCount.put(v, m + 1);
}
}
}
if (!change) {
// Collect aggregate variables for pushing aggregates into the subplan (if possible).
collectAggregateVars(nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, (AbstractOperatorWithNestedPlans) op1);
}
break;
case GROUP:
// Collect aggregate variables for pushing aggregates into the nested subplan
// of the group by operator (if possible).
collectAggregateVars(nspListifyVarsCount, nspWithAgg, nspAggVarToPlanIndex, (AbstractOperatorWithNestedPlans) op1);
break;
default:
for (LogicalVariable v : used) {
Integer m = nspListifyVarsCount.get(v);
if (m != null) {
nspListifyVarsCount.put(v, m + 1);
}
}
}
return change;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class IntroduceAutogenerateIDRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
// match: commit OR distribute-result OR SINK - ... followed by:
// [insert to internal dataset with autogenerated id] - assign - project
// produce: insert - assign - assign* - project
// **
// OR [insert to internal dataset with autogenerated id] - assign - [datasource scan]
// produce insert - assign - assign* - datasource scan
AbstractLogicalOperator currentOp = (AbstractLogicalOperator) opRef.getValue();
if (currentOp.getOperatorTag() == LogicalOperatorTag.DELEGATE_OPERATOR) {
DelegateOperator dOp = (DelegateOperator) currentOp;
if (!(dOp.getDelegate() instanceof CommitOperator)) {
return false;
} else if (!((CommitOperator) dOp.getDelegate()).isSink()) {
return false;
}
} else if (currentOp.getOperatorTag() != LogicalOperatorTag.DISTRIBUTE_RESULT && currentOp.getOperatorTag() != LogicalOperatorTag.SINK) {
return false;
}
ArrayDeque<AbstractLogicalOperator> opStack = new ArrayDeque<>();
opStack.push(currentOp);
while (currentOp.getInputs().size() == 1) {
currentOp = (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue();
if (currentOp.getOperatorTag() == LogicalOperatorTag.INSERT_DELETE_UPSERT) {
break;
}
opStack.push(currentOp);
}
if (currentOp.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
return false;
}
InsertDeleteUpsertOperator insertOp = (InsertDeleteUpsertOperator) currentOp;
if (insertOp.getOperation() != Kind.INSERT && insertOp.getOperation() != Kind.UPSERT) {
return false;
}
DatasetDataSource dds = (DatasetDataSource) insertOp.getDataSource();
boolean autogenerated = ((InternalDatasetDetails) dds.getDataset().getDatasetDetails()).isAutogenerated();
if (!autogenerated) {
return false;
}
if (((DataSource) insertOp.getDataSource()).getDatasourceType() != Type.INTERNAL_DATASET) {
return false;
}
AbstractLogicalOperator parentOp = (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue();
if (parentOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
return false;
}
AssignOperator assignOp = (AssignOperator) parentOp;
LogicalVariable inputRecord;
//TODO: bug here. will not work for internal datasets with filters since the pattern becomes
//[project-assign-assign-insert]
AbstractLogicalOperator grandparentOp = (AbstractLogicalOperator) parentOp.getInputs().get(0).getValue();
if (grandparentOp.getOperatorTag() == LogicalOperatorTag.PROJECT) {
ProjectOperator projectOp = (ProjectOperator) grandparentOp;
inputRecord = projectOp.getVariables().get(0);
} else if (grandparentOp.getOperatorTag() == LogicalOperatorTag.DATASOURCESCAN) {
DataSourceScanOperator dssOp = (DataSourceScanOperator) grandparentOp;
inputRecord = dssOp.getVariables().get(0);
} else {
return false;
}
List<String> pkFieldName = ((InternalDatasetDetails) dds.getDataset().getDatasetDetails()).getPrimaryKey().get(0);
ILogicalExpression rec0 = new VariableReferenceExpression(inputRecord);
ILogicalExpression rec1 = createPrimaryKeyRecordExpression(pkFieldName);
ILogicalExpression mergedRec = createRecordMergeFunction(rec0, rec1);
ILogicalExpression nonNullMergedRec = createNotNullFunction(mergedRec);
LogicalVariable v = context.newVar();
AssignOperator newAssign = new AssignOperator(v, new MutableObject<ILogicalExpression>(nonNullMergedRec));
newAssign.getInputs().add(new MutableObject<ILogicalOperator>(grandparentOp));
assignOp.getInputs().set(0, new MutableObject<ILogicalOperator>(newAssign));
VariableUtilities.substituteVariables(assignOp, inputRecord, v, context);
VariableUtilities.substituteVariables(insertOp, inputRecord, v, context);
context.computeAndSetTypeEnvironmentForOperator(newAssign);
context.computeAndSetTypeEnvironmentForOperator(assignOp);
context.computeAndSetTypeEnvironmentForOperator(insertOp);
;
for (AbstractLogicalOperator op : opStack) {
VariableUtilities.substituteVariables(op, inputRecord, v, context);
context.computeAndSetTypeEnvironmentForOperator(op);
}
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class LoadRecordFieldsRule method findFieldExpression.
// Finds a field expression.
private static ILogicalExpression findFieldExpression(AbstractLogicalOperator op, LogicalVariable recordVar, Object accessKey, IVariableTypeEnvironment typeEnvironment, FieldResolver resolver) throws AlgebricksException {
for (Mutable<ILogicalOperator> child : op.getInputs()) {
AbstractLogicalOperator opChild = (AbstractLogicalOperator) child.getValue();
if (opChild.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator op2 = (AssignOperator) opChild;
int i = op2.getVariables().indexOf(recordVar);
if (i >= 0) {
AbstractLogicalExpression constr = (AbstractLogicalExpression) op2.getExpressions().get(i).getValue();
return resolveFieldExpression(constr, accessKey, typeEnvironment, resolver);
}
} else if (opChild.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE) {
NestedTupleSourceOperator nts = (NestedTupleSourceOperator) opChild;
AbstractLogicalOperator opBelowNestedPlan = (AbstractLogicalOperator) nts.getDataSourceReference().getValue().getInputs().get(0).getValue();
ILogicalExpression expr1 = findFieldExpression(opBelowNestedPlan, recordVar, accessKey, typeEnvironment, resolver);
if (expr1 != null) {
return expr1;
}
}
ILogicalExpression expr2 = findFieldExpression(opChild, recordVar, accessKey, typeEnvironment, resolver);
if (expr2 != null) {
return expr2;
}
}
return null;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class InlineUnnestFunctionRule method findUsedVarOrigin.
private ILogicalExpression findUsedVarOrigin(LogicalVariable usedVar, AbstractLogicalOperator parentOp, AbstractLogicalOperator currentOp) throws AlgebricksException {
ILogicalExpression ret = null;
if (currentOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
VariableUtilities.getProducedVariables(currentOp, producedVars);
if (producedVars.contains(usedVar)) {
AssignOperator assignOp = (AssignOperator) currentOp;
int index = assignOp.getVariables().indexOf(usedVar);
ILogicalExpression returnedExpr = assignOp.getExpressions().get(index).getValue();
if (returnedExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) returnedExpr;
if (BuiltinFunctions.isBuiltinUnnestingFunction(funcExpr.getFunctionIdentifier())) {
// we only inline for unnest functions
removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
ret = returnedExpr;
}
} else if (returnedExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
//recusively inline
VariableReferenceExpression varExpr = (VariableReferenceExpression) returnedExpr;
LogicalVariable var = varExpr.getVariableReference();
ILogicalExpression finalExpr = findUsedVarOrigin(var, currentOp, (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue());
if (finalExpr != null) {
removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
ret = finalExpr;
}
}
}
} else {
for (Mutable<ILogicalOperator> child : currentOp.getInputs()) {
ILogicalExpression expr = findUsedVarOrigin(usedVar, currentOp, (AbstractLogicalOperator) child.getValue());
if (expr != null) {
ret = expr;
}
}
}
return ret;
}
Aggregations