if( !union.isOnSchema() )
return;
LogicalSchema outputSchema = union.getSchema();
int fieldCount = outputSchema.size();
OperatorPlan plan = union.getPlan();
List<Operator> preds = new ArrayList<Operator>();
preds.addAll( plan.getPredecessors( union ) );
List<LogicalSchema> fieldSchemas = new ArrayList<LogicalSchema>( fieldCount );
for( LogicalFieldSchema fs : outputSchema.getFields() ) {
LogicalSchema ls = new LogicalSchema();
ls.addField( new LogicalFieldSchema( fs.alias, null, DataType.NULL ) );
fieldSchemas.add( ls );
}
for( Operator pred : preds ) {
LogicalRelationalOperator op = (LogicalRelationalOperator)pred;
LogicalSchema opSchema = op.getSchema();
if( opSchema.isEqual( outputSchema , true) )
continue;
LOForEach foreach = new LOForEach( plan );
LogicalPlan innerPlan = new LogicalPlan();
LOGenerate gen = new LOGenerate( innerPlan );
boolean[] flattenFlags = new boolean[fieldCount];
List<LogicalExpressionPlan> exprPlans = new ArrayList<LogicalExpressionPlan>( fieldCount );
List<Operator> genInputs = new ArrayList<Operator>();
// Get exprPlans, and genInputs
for( LogicalFieldSchema fs : outputSchema.getFields() ) {
LogicalExpressionPlan exprPlan = new LogicalExpressionPlan();
exprPlans.add( exprPlan );
int pos = -1;
//do a match with subname also
LogicalFieldSchema matchFS = opSchema.getFieldSubNameMatch(fs.alias);
if(matchFS != null){
pos = opSchema.getFieldPosition(matchFS.alias);
}
if( pos == -1 ) {
ConstantExpression constExp = new ConstantExpression( exprPlan, null);
if(fs.type != DataType.BYTEARRAY){
LogicalSchema.LogicalFieldSchema constFs = fs.deepCopy();
constFs.resetUid();
new CastExpression(exprPlan, constExp, constFs);
}
} else {
ProjectExpression projExpr =
new ProjectExpression( exprPlan, genInputs.size(), 0, gen );
if( opSchema.getField( pos ).type != fs.type ) {
new CastExpression( exprPlan, projExpr, fs );
}
genInputs.add( new LOInnerLoad( innerPlan, foreach, pos ) );
}
}
gen.setFlattenFlags( flattenFlags );
gen.setOutputPlans( exprPlans );
gen.setUserDefinedSchema( fieldSchemas );
innerPlan.add( gen );
for( Operator input : genInputs ) {
innerPlan.add(input);
innerPlan.connect( input, gen );
}
foreach.setInnerPlan( innerPlan );
foreach.setAlias(union.getAlias());
Pair<Integer, Integer> pair = plan.disconnect( pred, union );
plan.add( foreach );
plan.connect( pred, pair.first, foreach, 0 );
plan.connect( foreach, 0, union, pair.second );
}
union.setUnionOnSchema(false);
}