// flatten(distinct({(1), (1)})) is (1), (1)
// in both cases correctness is not affected
LOForEach foreach = (LOForEach)matched.getSources().get(0);
LOGenerate gen = OptimizerUtils.findGenerate( foreach );
if( !OptimizerUtils.hasFlatten( gen ) )
return false;
List<Operator> succs = currentPlan.getSuccessors( foreach );
if( succs == null || succs.size() != 1 )
return false;
List<Long> uids = getNonFlattenFieldUids( gen );
Operator succ = succs.get( 0 );
if( !( succ instanceof LOSort || succ instanceof LOJoin || succ instanceof LOCross ) )
return false;
if( succ instanceof LOSort ) {
// Check if the expressions for the foreach generate are purely projection including flatten fields.
List<LogicalExpressionPlan> exprs = gen.getOutputPlans();
for( LogicalExpressionPlan expr : exprs ) {
if( !isPureProjection( expr ) )
return false;
}