use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.
the class AbstractPropagatePropertiesForUsedVariablesPOperator method computeDeliveredPropertiesForUsedVariables.
public void computeDeliveredPropertiesForUsedVariables(ILogicalOperator op, List<LogicalVariable> usedVariables) {
ILogicalOperator op2 = op.getInputs().get(0).getValue();
IPartitioningProperty pp = op2.getDeliveredPhysicalProperties().getPartitioningProperty();
List<ILocalStructuralProperty> downPropsLocal = op2.getDeliveredPhysicalProperties().getLocalProperties();
List<ILocalStructuralProperty> propsLocal = new ArrayList<ILocalStructuralProperty>();
if (downPropsLocal != null) {
for (ILocalStructuralProperty lsp : downPropsLocal) {
LinkedList<LogicalVariable> cols = new LinkedList<LogicalVariable>();
lsp.getColumns(cols);
ILocalStructuralProperty propagatedProp = lsp.retainVariables(usedVariables);
if (propagatedProp != null) {
propsLocal.add(propagatedProp);
}
}
}
deliveredProperties = new StructuralPropertiesVector(pp, propsLocal);
}
use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.
the class InjectTypeCastForUnionRule method injectCast.
// Injects a type cast function on one input (indicated by childIndex) of the union all operator if necessary.
private boolean injectCast(UnionAllOperator op, int childIndex, IOptimizationContext context) throws AlgebricksException {
// Gets the type environments for the union all operator and its child operator with the right child index.
IVariableTypeEnvironment env = context.getOutputTypeEnvironment(op);
Mutable<ILogicalOperator> branchOpRef = op.getInputs().get(childIndex);
IVariableTypeEnvironment childEnv = context.getOutputTypeEnvironment(branchOpRef.getValue());
// The two lists are used for the assign operator that calls cast functions.
List<LogicalVariable> varsToCast = new ArrayList<>();
List<Mutable<ILogicalExpression>> castFunctionsForLeft = new ArrayList<>();
// Iterate through all triples.
List<Triple<LogicalVariable, LogicalVariable, LogicalVariable>> triples = op.getVariableMappings();
for (Triple<LogicalVariable, LogicalVariable, LogicalVariable> triple : triples) {
LogicalVariable producedVar = triple.third;
IAType producedType = (IAType) env.getVarType(producedVar);
LogicalVariable varToCast = childIndex == 0 ? triple.first : triple.second;
IAType inputType = (IAType) childEnv.getVarType(varToCast);
if (!TypeResolverUtil.needsCast(producedType, inputType)) {
// Continues to the next triple if no cast is neeeded.
continue;
}
LogicalVariable castedVar = context.newVar();
// Resets triple variables to new variables that bind to the results of type casting.
triple.first = childIndex == 0 ? castedVar : triple.first;
triple.second = childIndex > 0 ? castedVar : triple.second;
ScalarFunctionCallExpression castFunc = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CAST_TYPE), new ArrayList<>(Collections.singletonList(new MutableObject<>(new VariableReferenceExpression(varToCast)))));
TypeCastUtils.setRequiredAndInputTypes(castFunc, producedType, inputType);
// Adds the variable and function expression into lists, for the assign operator.
varsToCast.add(castedVar);
castFunctionsForLeft.add(new MutableObject<>(castFunc));
}
if (castFunctionsForLeft.isEmpty()) {
return false;
}
// Injects an assign operator to perform type casts.
AssignOperator assignOp = new AssignOperator(varsToCast, castFunctionsForLeft);
assignOp.getInputs().add(new MutableObject<>(branchOpRef.getValue()));
branchOpRef.setValue(assignOp);
context.computeAndSetTypeEnvironmentForOperator(assignOp);
// Returns true to indicate that rewriting happens.
return true;
}
use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable in project asterixdb by apache.
the class FeedScanCollectionToUnnest method findVarOriginExpression.
private ILogicalExpression findVarOriginExpression(LogicalVariable v, ILogicalOperator op) throws AlgebricksException {
boolean searchInputs = false;
if (!(op instanceof AbstractAssignOperator)) {
searchInputs = true;
} else {
AbstractAssignOperator aao = (AbstractAssignOperator) op;
List<LogicalVariable> producedVars = new ArrayList<>();
VariableUtilities.getProducedVariables(op, producedVars);
int exprIndex = producedVars.indexOf(v);
if (exprIndex == -1) {
searchInputs = true;
} else {
ILogicalExpression originalCandidate = aao.getExpressions().get(exprIndex).getValue();
if (originalCandidate.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
searchInputs = true;
} else {
return originalCandidate;
}
}
}
if (searchInputs) {
for (Mutable<ILogicalOperator> childOp : op.getInputs()) {
ILogicalExpression ret = findVarOriginExpression(v, childOp.getValue());
if (ret != null) {
return ret;
}
}
}
throw new IllegalStateException("Unable to find the original expression that produced variable " + v);
}
use of org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable 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.base.LogicalVariable 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;
}
Aggregations