use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class LoadRecordFieldsRule 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;
}
if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator a1 = (AssignOperator) op1;
ILogicalExpression expr = getFirstExpr(a1);
if (AnalysisUtil.isAccessToFieldRecord(expr)) {
boolean res = findAndEliminateRedundantFieldAccess(a1, context);
context.addToDontApplySet(this, op1);
return res;
}
}
exprVisitor.setTopOp(op1);
exprVisitor.setContext(context);
boolean res = op1.acceptExpressionTransform(exprVisitor);
if (!res) {
context.addToDontApplySet(this, op1);
}
if (res && op1.getOperatorTag() == LogicalOperatorTag.SELECT) {
// checking if we can annotate a Selection as using just one field
// access
SelectOperator sigma = (SelectOperator) op1;
LinkedList<LogicalVariable> vars = new LinkedList<LogicalVariable>();
VariableUtilities.getUsedVariables(sigma, vars);
if (vars.size() == 1) {
// we can annotate Selection
AssignOperator assign1 = (AssignOperator) op1.getInputs().get(0).getValue();
AbstractLogicalExpression expr1 = (AbstractLogicalExpression) getFirstExpr(assign1);
if (expr1.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr1;
// f should be a call to a field/data access kind of
// function
sigma.getAnnotations().put(OperatorAnnotation.FIELD_ACCESS, f.getArguments().get(0));
}
}
}
// TODO: avoid having to recompute type env. here
if (res) {
OperatorPropertiesUtil.typeOpRec(opRef, context);
}
return res;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class IntroduceRapidFrameFlushProjectAssignRule method changeRule.
private boolean changeRule(AbstractLogicalOperator op) {
boolean planModified = false;
for (int i = 0; i < op.getInputs().size(); ++i) {
AbstractLogicalOperator descendantOp = (AbstractLogicalOperator) op.getInputs().get(i).getValue();
if (descendantOp.getOperatorTag() == LogicalOperatorTag.PROJECT) {
ProjectOperator projectOp = (ProjectOperator) descendantOp;
StreamProjectPOperator physicalOp = (StreamProjectPOperator) projectOp.getPhysicalOperator();
physicalOp.setRapidFrameFlush(true);
planModified = true;
} else if (descendantOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assignOp = (AssignOperator) descendantOp;
AssignPOperator physicalOp = (AssignPOperator) assignOp.getPhysicalOperator();
physicalOp.setRapidFrameFlush(true);
planModified = true;
}
changeRule(descendantOp);
}
return planModified;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class IntroduceSecondaryIndexInsertDeleteRule method getRecordVar.
private LogicalVariable getRecordVar(IOptimizationContext context, AbstractLogicalOperator inputOp, ILogicalExpression recordExpr, int expectedRecordIndex) throws AlgebricksException {
if (exprIsRecord(context.getOutputTypeEnvironment(inputOp), recordExpr)) {
return ((VariableReferenceExpression) recordExpr).getVariableReference();
} else {
/**
* For the case primary key-assignment expressions are constant
* expressions, find assign op that creates record to be
* inserted/deleted.
*/
FunctionIdentifier fid = null;
AbstractLogicalOperator currentInputOp = inputOp;
while (fid != BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR) {
if (currentInputOp.getInputs().isEmpty()) {
return null;
}
currentInputOp = (AbstractLogicalOperator) currentInputOp.getInputs().get(0).getValue();
if (currentInputOp.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
continue;
}
AssignOperator assignOp = (AssignOperator) currentInputOp;
ILogicalExpression assignExpr = assignOp.getExpressions().get(expectedRecordIndex).getValue();
if (assignExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
ScalarFunctionCallExpression funcExpr = (ScalarFunctionCallExpression) assignOp.getExpressions().get(expectedRecordIndex).getValue();
fid = funcExpr.getFunctionIdentifier();
}
}
return ((AssignOperator) currentInputOp).getVariables().get(0);
}
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator in project asterixdb by apache.
the class IntroduceStaticTypeCastForInsertRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
/**
* pattern match: sink/insert/assign record type is propagated from
* insert data source to the record-constructor expression
*/
if (context.checkIfInDontApplySet(this, opRef.getValue())) {
return false;
}
context.addToDontApplySet(this, opRef.getValue());
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
List<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
LogicalVariable oldRecordVariable;
if (op1.getOperatorTag() != LogicalOperatorTag.DELEGATE_OPERATOR && op1.getOperatorTag() != LogicalOperatorTag.SINK) {
return false;
}
if (op1.getOperatorTag() == LogicalOperatorTag.DELEGATE_OPERATOR) {
DelegateOperator eOp = (DelegateOperator) op1;
if (!(eOp.getDelegate() instanceof CommitOperator)) {
return false;
}
}
AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
if (op2.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE_UPSERT) {
return false;
}
InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op2;
if (insertDeleteOp.getOperation() == InsertDeleteUpsertOperator.Kind.DELETE) {
return false;
}
/**
* get required record type
*/
InsertDeleteUpsertOperator insertDeleteOperator = (InsertDeleteUpsertOperator) op2;
DataSource dataSource = (DataSource) insertDeleteOperator.getDataSource();
IAType requiredRecordType = dataSource.getItemType();
List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
insertDeleteOperator.getPayloadExpression().getValue().getUsedVariables(usedVariables);
// empty
if (usedVariables.size() == 0) {
return false;
}
oldRecordVariable = usedVariables.get(0);
LogicalVariable inputRecordVar = usedVariables.get(0);
IVariableTypeEnvironment env = insertDeleteOperator.computeOutputTypeEnvironment(context);
IAType inputRecordType = (IAType) env.getVarType(inputRecordVar);
AbstractLogicalOperator currentOperator = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
/**
* find the assign operator for the "input record" to the insert_delete
* operator
*/
do {
context.addToDontApplySet(this, currentOperator);
if (currentOperator.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
AssignOperator assignOp = (AssignOperator) currentOperator;
producedVariables.clear();
VariableUtilities.getProducedVariables(currentOperator, producedVariables);
int position = producedVariables.indexOf(oldRecordVariable);
/**
* set the top-down propagated type
*/
if (position >= 0) {
AssignOperator originalAssign = (AssignOperator) currentOperator;
List<Mutable<ILogicalExpression>> expressionRefs = originalAssign.getExpressions();
ILogicalExpression expr = expressionRefs.get(position).getValue();
if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
// fail but just return false
if (TypeCastUtils.getRequiredType(funcExpr) != null) {
context.computeAndSetTypeEnvironmentForOperator(assignOp);
return false;
}
IVariableTypeEnvironment assignEnv = assignOp.computeOutputTypeEnvironment(context);
StaticTypeCastUtil.rewriteFuncExpr(funcExpr, requiredRecordType, inputRecordType, assignEnv);
}
context.computeAndSetTypeEnvironmentForOperator(originalAssign);
}
}
if (currentOperator.getInputs().size() > 0) {
currentOperator = (AbstractLogicalOperator) currentOperator.getInputs().get(0).getValue();
} else {
break;
}
} while (currentOperator != null);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator 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;
}
Aggregations