use of org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class LoadRecordFieldsRule method pushFieldAssign.
private static void pushFieldAssign(AssignOperator a2, AbstractLogicalOperator topOp, IOptimizationContext context) throws AlgebricksException {
if (topOp.getInputs().size() == 1 && !topOp.hasNestedPlans()) {
Mutable<ILogicalOperator> topChild = topOp.getInputs().get(0);
// plugAccessAboveOp(a2, topChild, context);
List<Mutable<ILogicalOperator>> a2InptList = a2.getInputs();
a2InptList.clear();
a2InptList.add(topChild);
// and link it as child in the op. tree
topOp.getInputs().set(0, new MutableObject<>(a2));
findAndEliminateRedundantFieldAccess(a2, context);
} else {
// e.g., a join
LinkedList<LogicalVariable> usedInAccess = new LinkedList<LogicalVariable>();
VariableUtilities.getUsedVariables(a2, usedInAccess);
LinkedList<LogicalVariable> produced2 = new LinkedList<LogicalVariable>();
VariableUtilities.getProducedVariables(topOp, produced2);
if (OperatorPropertiesUtil.disjoint(produced2, usedInAccess)) {
for (Mutable<ILogicalOperator> inp : topOp.getInputs()) {
HashSet<LogicalVariable> v2 = new HashSet<LogicalVariable>();
VariableUtilities.getLiveVariables(inp.getValue(), v2);
if (!OperatorPropertiesUtil.disjoint(usedInAccess, v2)) {
pushAccessAboveOpRef(a2, inp, context);
return;
}
}
if (topOp.hasNestedPlans()) {
AbstractOperatorWithNestedPlans nestedOp = (AbstractOperatorWithNestedPlans) topOp;
for (ILogicalPlan plan : nestedOp.getNestedPlans()) {
for (Mutable<ILogicalOperator> root : plan.getRoots()) {
HashSet<LogicalVariable> v2 = new HashSet<LogicalVariable>();
VariableUtilities.getLiveVariables(root.getValue(), v2);
if (!OperatorPropertiesUtil.disjoint(usedInAccess, v2)) {
pushAccessAboveOpRef(a2, root, context);
return;
}
}
}
}
throw new AlgebricksException("Field access " + getFirstExpr(a2) + " does not correspond to any input of operator " + topOp);
}
}
}
use of org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class IntroduceSecondaryIndexInsertDeleteRule method createFilterExpression.
private Mutable<ILogicalExpression> createFilterExpression(List<LogicalVariable> secondaryKeyVars, IVariableTypeEnvironment typeEnv, boolean forceFilter) throws AlgebricksException {
List<Mutable<ILogicalExpression>> filterExpressions = new ArrayList<>();
// condition.
for (LogicalVariable secondaryKeyVar : secondaryKeyVars) {
IAType secondaryKeyType = (IAType) typeEnv.getVarType(secondaryKeyVar);
if (!NonTaggedFormatUtil.isOptional(secondaryKeyType) && !forceFilter) {
continue;
}
ScalarFunctionCallExpression isUnknownFuncExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.IS_UNKOWN), new MutableObject<ILogicalExpression>(new VariableReferenceExpression(secondaryKeyVar)));
ScalarFunctionCallExpression notFuncExpr = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.NOT), new MutableObject<ILogicalExpression>(isUnknownFuncExpr));
filterExpressions.add(new MutableObject<ILogicalExpression>(notFuncExpr));
}
// No nullable secondary keys.
if (filterExpressions.isEmpty()) {
return null;
}
Mutable<ILogicalExpression> filterExpression;
if (filterExpressions.size() > 1) {
// Create a conjunctive condition.
filterExpression = new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.AND), filterExpressions));
} else {
filterExpression = filterExpressions.get(0);
}
return filterExpression;
}
use of org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method addWrapperFunction.
/**
* Inject a function to wrap a variable when necessary
*
* @param requiredRecordType
* the required record type
* @param recordVar
* the record variable
* @param parent
* the current parent operator to be rewritten
* @param context
* the optimization context
* @param fd
* the function to be injected
* @return true if cast is injected; false otherwise.
* @throws AlgebricksException
*/
public static LogicalVariable addWrapperFunction(ARecordType requiredRecordType, LogicalVariable recordVar, ILogicalOperator parent, IOptimizationContext context, FunctionIdentifier fd) throws AlgebricksException {
List<Mutable<ILogicalOperator>> opRefs = parent.getInputs();
for (int index = 0; index < opRefs.size(); index++) {
Mutable<ILogicalOperator> opRef = opRefs.get(index);
ILogicalOperator op = opRef.getValue();
/** get produced vars */
List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
VariableUtilities.getProducedVariables(op, producedVars);
IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context);
for (int i = 0; i < producedVars.size(); i++) {
LogicalVariable var = producedVars.get(i);
if (var.equals(recordVar)) {
/** insert an assign operator to call the function on-top-of the variable */
IAType actualType = (IAType) env.getVarType(var);
AbstractFunctionCallExpression cast = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(fd));
cast.getArguments().add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
/** enforce the required record type */
TypeCastUtils.setRequiredAndInputTypes(cast, requiredRecordType, actualType);
LogicalVariable newAssignVar = context.newVar();
AssignOperator newAssignOperator = new AssignOperator(newAssignVar, new MutableObject<ILogicalExpression>(cast));
newAssignOperator.getInputs().add(new MutableObject<ILogicalOperator>(op));
opRef.setValue(newAssignOperator);
context.computeAndSetTypeEnvironmentForOperator(newAssignOperator);
newAssignOperator.computeOutputTypeEnvironment(context);
VariableUtilities.substituteVariables(parent, recordVar, newAssignVar, context);
return newAssignVar;
}
}
/** recursive descend to the operator who produced the recordVar */
LogicalVariable replacedVar = addWrapperFunction(requiredRecordType, recordVar, op, context, fd);
if (replacedVar != null) {
/** substitute the recordVar by the replacedVar for operators who uses recordVar */
VariableUtilities.substituteVariables(parent, recordVar, replacedVar, context);
return replacedVar;
}
}
return null;
}
use of org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class PullPositionalVariableFromUnnestRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
if (op.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnest = (UnnestOperator) op;
LogicalVariable p = unnest.getPositionalVariable();
if (p == null) {
return false;
}
ArrayList<LogicalVariable> rOpVars = new ArrayList<LogicalVariable>();
rOpVars.add(p);
ArrayList<Mutable<ILogicalExpression>> rOpExprList = new ArrayList<Mutable<ILogicalExpression>>();
StatefulFunctionCallExpression fce = new StatefulFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
rOpExprList.add(new MutableObject<ILogicalExpression>(fce));
RunningAggregateOperator rOp = new RunningAggregateOperator(rOpVars, rOpExprList);
rOp.setExecutionMode(unnest.getExecutionMode());
RunningAggregatePOperator rPop = new RunningAggregatePOperator();
rOp.setPhysicalOperator(rPop);
rOp.getInputs().add(new MutableObject<ILogicalOperator>(unnest));
opRef.setValue(rOp);
unnest.setPositionalVariable(null);
context.computeAndSetTypeEnvironmentForOperator(rOp);
context.computeAndSetTypeEnvironmentForOperator(unnest);
return true;
}
use of org.apache.commons.lang3.mutable.MutableObject in project asterixdb by apache.
the class AccessMethodUtils method createExternalDataLookupUnnestMap.
public static UnnestMapOperator createExternalDataLookupUnnestMap(AbstractDataSourceOperator dataSourceOp, Dataset dataset, ARecordType recordType, ILogicalOperator inputOp, IOptimizationContext context, boolean retainInput, boolean retainNull) throws AlgebricksException {
List<LogicalVariable> primaryKeyVars = AccessMethodUtils.getPrimaryKeyVarsFromSecondaryUnnestMap(dataset, inputOp);
// add a sort on the RID fields before fetching external data.
OrderOperator order = new OrderOperator();
for (LogicalVariable pkVar : primaryKeyVars) {
Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
}
// The secondary-index search feeds into the sort.
order.getInputs().add(new MutableObject<>(inputOp));
order.setExecutionMode(ExecutionMode.LOCAL);
context.computeAndSetTypeEnvironmentForOperator(order);
List<Mutable<ILogicalExpression>> externalLookupArgs = new ArrayList<>();
//Add dataverse to the arguments
AccessMethodUtils.addStringArg(dataset.getDataverseName(), externalLookupArgs);
//Add dataset to the arguments
AccessMethodUtils.addStringArg(dataset.getDatasetName(), externalLookupArgs);
//Add PK vars to the arguments
AccessMethodUtils.writeVarList(primaryKeyVars, externalLookupArgs);
// Variables and types coming out of the external access.
List<LogicalVariable> externalUnnestVars = new ArrayList<>();
List<Object> outputTypes = new ArrayList<>();
// Append output variables/types generated by the data scan (not forwarded from input).
externalUnnestVars.addAll(dataSourceOp.getVariables());
appendExternalRecTypes(dataset, recordType, outputTypes);
IFunctionInfo externalLookup = FunctionUtil.getFunctionInfo(BuiltinFunctions.EXTERNAL_LOOKUP);
AbstractFunctionCallExpression externalLookupFunc = new ScalarFunctionCallExpression(externalLookup, externalLookupArgs);
UnnestMapOperator unnestOp = new UnnestMapOperator(externalUnnestVars, new MutableObject<ILogicalExpression>(externalLookupFunc), outputTypes, retainInput);
// Fed by the order operator or the secondaryIndexUnnestOp.
unnestOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
context.computeAndSetTypeEnvironmentForOperator(unnestOp);
unnestOp.setExecutionMode(ExecutionMode.PARTITIONED);
//set the physical operator
DataSourceId dataSourceId = new DataSourceId(dataset.getDataverseName(), dataset.getDatasetName());
unnestOp.setPhysicalOperator(new ExternalDataLookupPOperator(dataSourceId, dataset, recordType, primaryKeyVars, false, retainInput, retainNull));
return unnestOp;
}
Aggregations