AbstractLogicalOperator subplanOp1 = (AbstractLogicalOperator) subplan.getNestedPlans().get(0).getRoots()
.get(0).getValue();
if (subplanOp1.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
return false;
}
AggregateOperator aggregate = (AggregateOperator) subplanOp1;
// Check to see if the expression is a function and op:sequence.
ILogicalExpression logicalExpression1 = (ILogicalExpression) aggregate.getExpressions().get(0).getValue();
if (logicalExpression1.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression functionCall1 = (AbstractFunctionCallExpression) logicalExpression1;
if (!functionCall1.getFunctionIdentifier().equals(BuiltinOperators.SEQUENCE.getFunctionIdentifier())) {
return false;
}
Mutable<ILogicalExpression> lvm1 = ExpressionToolbox.findVariableExpression(aggregate.getExpressions().get(0));
if (lvm1 == null) {
return false;
}
VariableReferenceExpression vre1 = (VariableReferenceExpression) lvm1.getValue();
// UNNEST($v1, iterate($v0) )
AbstractLogicalOperator subplanOp2 = (AbstractLogicalOperator) subplanOp1.getInputs().get(0).getValue();
if (subplanOp2.getOperatorTag() != LogicalOperatorTag.UNNEST) {
return false;
}
UnnestOperator subplanUnnest = (UnnestOperator) subplanOp2;
// Check to see if the expression is the iterate operator.
ILogicalExpression logicalExpression2 = (ILogicalExpression) subplanUnnest.getExpressionRef().getValue();
if (logicalExpression2.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression functionCall2 = (AbstractFunctionCallExpression) logicalExpression2;
if (!functionCall2.getFunctionIdentifier().equals(BuiltinOperators.ITERATE.getFunctionIdentifier())) {
return false;
}
if (subplanUnnest.getVariable() != vre1.getVariableReference()) {
return false;
}
Mutable<ILogicalExpression> lvm2 = ExpressionToolbox.findVariableExpression(subplanUnnest.getExpressionRef());
if (lvm2 == null) {
return false;
}
VariableReferenceExpression vre2 = (VariableReferenceExpression) lvm2.getValue();
// NESTEDTUPLESOURCE
AbstractLogicalOperator subplanOp3 = (AbstractLogicalOperator) subplanOp2.getInputs().get(0).getValue();
if (subplanOp3.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
return false;
}
// Ensure input is from a UNNEST operator.
AbstractLogicalOperator subplanInput = (AbstractLogicalOperator) subplan.getInputs().get(0).getValue();
if (subplanInput.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
return false;
}
AssignOperator assign = (AssignOperator) subplanInput;
if (!assign.getVariables().contains(vre2.getVariableReference())) {
return false;
}
// Check to see if the expression is the iterate operator.
ILogicalExpression logicalExpression3 = (ILogicalExpression) assign.getExpressions().get(0).getValue();
if (logicalExpression3.getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
return false;
}
AbstractFunctionCallExpression functionCall3 = (AbstractFunctionCallExpression) logicalExpression3;
if (!functionCall3.getFunctionIdentifier().equals(BuiltinOperators.TREAT.getFunctionIdentifier())) {
return false;
}
// Find the treat type.
ILogicalExpression argType = functionCall3.getArguments().get(ARG_TYPE).getValue();
if (argType.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
return false;
}
TaggedValuePointable tvp = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
ExpressionToolbox.getConstantAsPointable((ConstantExpression) argType, tvp);
IntegerPointable pTypeCode = (IntegerPointable) IntegerPointable.FACTORY.createPointable();
tvp.getValue(pTypeCode);
SequenceType sType = dCtx.lookupSequenceType(pTypeCode.getInteger());
if (sType.getQuantifier() != Quantifier.QUANT_ONE) {
return false;
}
// Create replacement assign operator.
lvm1.setValue(vre2);
AssignOperator replacementAssign = new AssignOperator(aggregate.getVariables().get(0), functionCall1
.getArguments().get(0));
replacementAssign.getInputs().addAll(subplan.getInputs());
opRef.setValue(replacementAssign);
return false;