use of org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression in project asterixdb by apache.
the class InlineUnnestFunctionRule 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;
}
context.addToDontApplySet(this, op1);
if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator unnestOperator = (UnnestOperator) op1;
AbstractFunctionCallExpression expr = (AbstractFunctionCallExpression) unnestOperator.getExpressionRef().getValue();
//we only inline for the scan-collection function
if (expr.getFunctionIdentifier() != BuiltinFunctions.SCAN_COLLECTION) {
return false;
}
// inline all variables from an unnesting function call
AbstractFunctionCallExpression funcExpr = expr;
List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
for (int i = 0; i < args.size(); i++) {
ILogicalExpression argExpr = args.get(i).getValue();
if (argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression varExpr = (VariableReferenceExpression) argExpr;
inlineVariable(varExpr.getVariableReference(), unnestOperator);
}
}
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression in project asterixdb by apache.
the class InlineUnnestFunctionRule method getParentFunctionExpression.
private void getParentFunctionExpression(LogicalVariable usedVar, ILogicalExpression expr, List<Pair<AbstractFunctionCallExpression, Integer>> parentAndIndexList) {
AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
for (int i = 0; i < args.size(); i++) {
ILogicalExpression argExpr = args.get(i).getValue();
if (argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
VariableReferenceExpression varExpr = (VariableReferenceExpression) argExpr;
if (varExpr.getVariableReference().equals(usedVar)) {
parentAndIndexList.add(new Pair<AbstractFunctionCallExpression, Integer>(funcExpr, i));
}
}
if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
getParentFunctionExpression(usedVar, argExpr, parentAndIndexList);
}
}
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression in project asterixdb by apache.
the class IntroduceDynamicTypeCastRule method rewritePost.
@Override
public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
// Depending on the operator type, we need to extract the following pieces of information.
AbstractLogicalOperator op;
ARecordType requiredRecordType;
LogicalVariable recordVar;
// We identify INSERT and DISTRIBUTE_RESULT operators.
AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
switch(op1.getOperatorTag()) {
case SINK:
case DELEGATE_OPERATOR:
{
/**
* pattern match: commit insert assign
* resulting plan: commit-insert-project-assign
*/
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) {
InsertDeleteUpsertOperator insertDeleteOp = (InsertDeleteUpsertOperator) op2;
if (insertDeleteOp.getOperation() == InsertDeleteUpsertOperator.Kind.DELETE) {
return false;
}
// Remember this is the operator we need to modify
op = insertDeleteOp;
// Derive the required ARecordType based on the schema of the DataSource
InsertDeleteUpsertOperator insertDeleteOperator = (InsertDeleteUpsertOperator) op2;
DataSource dataSource = (DataSource) insertDeleteOperator.getDataSource();
requiredRecordType = (ARecordType) dataSource.getItemType();
// Derive the Variable which we will potentially wrap with cast/null functions
ILogicalExpression expr = insertDeleteOperator.getPayloadExpression().getValue();
List<LogicalVariable> payloadVars = new ArrayList<>();
expr.getUsedVariables(payloadVars);
recordVar = payloadVars.get(0);
} else {
return false;
}
break;
}
case DISTRIBUTE_RESULT:
{
// First, see if there was an output-record-type specified
requiredRecordType = (ARecordType) op1.getAnnotations().get("output-record-type");
if (requiredRecordType == null) {
return false;
}
// Remember this is the operator we need to modify
op = op1;
recordVar = ((VariableReferenceExpression) ((DistributeResultOperator) op).getExpressions().get(0).getValue()).getVariableReference();
break;
}
default:
{
return false;
}
}
// Derive the statically-computed type of the record
IVariableTypeEnvironment env = op.computeOutputTypeEnvironment(context);
IAType inputRecordType = (IAType) env.getVarType(recordVar);
/** the input record type can be an union type -- for the case when it comes from a subplan or left-outer join */
boolean checkUnknown = false;
while (NonTaggedFormatUtil.isOptional(inputRecordType)) {
/** while-loop for the case there is a nested multi-level union */
inputRecordType = ((AUnionType) inputRecordType).getActualType();
checkUnknown = true;
}
/** see whether the input record type needs to be casted */
boolean cast = !compatible(requiredRecordType, inputRecordType);
if (checkUnknown) {
recordVar = addWrapperFunction(requiredRecordType, recordVar, op, context, BuiltinFunctions.CHECK_UNKNOWN);
}
if (cast) {
addWrapperFunction(requiredRecordType, recordVar, op, context, BuiltinFunctions.CAST_TYPE);
}
return cast || checkUnknown;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression in project asterixdb by apache.
the class IntroduceTransactionCommitByAssignOpRule 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 selectOperator = (SelectOperator) op;
Mutable<ILogicalOperator> childOfSelect = selectOperator.getInputs().get(0);
//[Direction] SelectOp(cond1)<--ChildOps... ==> SelectOp(booleanValue of cond1)<--NewAssignOp(cond1)<--ChildOps...
//#. Create an assign-operator with a new local variable and the condition of the select-operator.
//#. Set the input(child operator) of the new assign-operator to input(child operator) of the select-operator.
// (Later, the newly created assign-operator will apply the condition on behalf of the select-operator,
// and set the variable of the assign-operator to a boolean value according to the condition evaluation.)
//#. Give the select-operator the result boolean value created by the newly created child assign-operator.
//create an assignOp with a variable and the condition of the select-operator.
LogicalVariable v = context.newVar();
AssignOperator assignOperator = new AssignOperator(v, new MutableObject<ILogicalExpression>(selectOperator.getCondition().getValue()));
//set the input of the new assign-operator to the input of the select-operator.
assignOperator.getInputs().add(childOfSelect);
//set the result value of the assign-operator to the condition of the select-operator
//scalarFunctionCallExpression);
selectOperator.getCondition().setValue(new VariableReferenceExpression(v));
selectOperator.getInputs().set(0, new MutableObject<ILogicalOperator>(assignOperator));
context.computeAndSetTypeEnvironmentForOperator(assignOperator);
//Once this rule is fired, don't apply again.
context.addToDontApplySet(this, selectOperator);
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression in project asterixdb by apache.
the class LoadRecordFieldsRule method findAndEliminateRedundantFieldAccess.
/**
* Rewrite
* assign $x := field-access($y, "field")
* assign $y := record-constructor { "field": Expr, ... }
* into
* assign $x := Expr
* assign $y := record-constructor { "field": Expr, ... }
*/
private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign, IOptimizationContext context) throws AlgebricksException {
ILogicalExpression expr = getFirstExpr(assign);
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
ILogicalExpression arg0 = f.getArguments().get(0).getValue();
if (arg0.getExpressionTag() != LogicalExpressionTag.VARIABLE) {
return false;
}
VariableReferenceExpression vre = (VariableReferenceExpression) arg0;
LogicalVariable recordVar = vre.getVariableReference();
ILogicalExpression arg1 = f.getArguments().get(1).getValue();
if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
return false;
}
IVariableTypeEnvironment typeEnvironment = context.getOutputTypeEnvironment(assign);
ConstantExpression ce = (ConstantExpression) arg1;
ILogicalExpression fldExpr;
if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
fldExpr = findFieldExpression(assign, recordVar, fldName, typeEnvironment, (name, expression, env) -> findFieldByNameFromRecordConstructor(name, expression));
} else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_BY_INDEX)) {
Integer fldIdx = ((AInt32) ((AsterixConstantValue) ce.getValue()).getObject()).getIntegerValue();
fldExpr = findFieldExpression(assign, recordVar, fldIdx, typeEnvironment, LoadRecordFieldsRule::findFieldByIndexFromRecordConstructor);
} else if (f.getFunctionIdentifier().equals(BuiltinFunctions.FIELD_ACCESS_NESTED)) {
return false;
} else {
throw new IllegalStateException();
}
if (fldExpr == null) {
return false;
}
// check the liveness of the new expression
List<LogicalVariable> usedVariables = new ArrayList<>();
fldExpr.getUsedVariables(usedVariables);
List<LogicalVariable> liveInputVars = new ArrayList<>();
VariableUtilities.getLiveVariables(assign, liveInputVars);
usedVariables.removeAll(liveInputVars);
if (usedVariables.isEmpty()) {
assign.getExpressions().get(0).setValue(fldExpr);
return true;
} else {
return false;
}
}
Aggregations