if (schema != null) {
ArrayList<LogicalFieldSchema> fields = (ArrayList<LogicalFieldSchema>) schema
.getFields();
for (int i = 0; i < fields.size(); i++) {
LogicalExpressionPlan lEplan = new LogicalExpressionPlan();
new ProjectExpression(lEplan, i, fields.get(i).alias, null, gen);
allExprPlan.add(lEplan);
}
}
}
// iterate over all operations and generate corresponding UDFs
for (int operIdx = 0; operIdx < operations.size(); operIdx++) {
List<LogicalExpressionPlan> lexpPlanList = new ArrayList<LogicalExpressionPlan>();
List<LogicalExpression> lexpList = new ArrayList<LogicalExpression>();
lexpPlanList.addAll(expressionPlans.get(operIdx));
// If duplicates exists in the dimension list then exception is
// thrown
checkDuplicateProject(lexpPlanList);
// Construct ProjectExpression from the LogicalExpressionPlans
lexpList = getProjectExpList(lexpPlanList, gen);
for (int i = 0; i < lexpList.size(); i++) {
// Retain the columns that needs to be pushed down.
// Remove the dimension columns from the input column list
// as it will be attached to CubeDimension UDF
for (int j = 0; j < allExprPlan.size(); j++) {
LogicalExpression lexp = (LogicalExpression) allExprPlan.get(j).getSources()
.get(0);
String colAlias = ((ProjectExpression) lexpList.get(i)).getColAlias();
if (colAlias == null) {
colAlias = ((ProjectExpression) lexpList.get(i)).getFieldSchema().alias;
}
String projExpAlias = null;
try {
projExpAlias = ((ProjectExpression) lexp).getColAlias();
} catch (ClassCastException e) {
// if it is not projection then it should be
// UserFuncExpr.
// ignore and continue till next ProjExpr is encountered
continue;
}
if (colAlias.equals(projExpAlias) == true) {
allExprPlan.remove(j);
} else {
// if projected exp alias is a namespaced alias
if (projExpAlias.lastIndexOf(":") != -1) {
projExpAlias = projExpAlias.substring(
projExpAlias.lastIndexOf(":") + 1, projExpAlias.length());
if (colAlias.equals(projExpAlias) == true) {
allExprPlan.remove(j);
}
}
}
}
}
// Create UDF with user specified dimensions
LogicalExpressionPlan uexpPlan = new LogicalExpressionPlan();
if (operations.get(operIdx).equals("CUBE")) {
new UserFuncExpression(uexpPlan, new FuncSpec(CubeDimensions.class.getName()),
lexpList);
} else {
new UserFuncExpression(uexpPlan, new FuncSpec(RollupDimensions.class.getName()),
lexpList);
}
for (LogicalExpressionPlan lexp : lexpPlanList) {
Iterator<Operator> it = lexp.getOperators();
while (it.hasNext()) {
uexpPlan.add(it.next());
}
}
// Add the UDF to logical expression plan that contains dependent
// attributes (pushed down from input columns)
allExprPlan.add(operIdx, uexpPlan);
}
// If the operator is a UserFuncExpression then set the flatten flags.
List<Boolean> flattenFlags = new ArrayList<Boolean>();
for (int idx = 0; idx < allExprPlan.size(); idx++) {
List<Operator> opers = allExprPlan.get(idx).getSources();
for (Operator oper : opers) {
if (oper instanceof ProjectExpression) {
flattenFlags.add(false);
} else if (oper instanceof UserFuncExpression) {
flattenFlags.add(true);
}
}
}
// Generate and Foreach operator creation
String falias = null;
try {
buildGenerateOp(loc, (LOForEach) foreach, (LOGenerate) gen, allExprPlan,
flattenFlags, getUserDefinedSchema(allExprPlan));
falias = buildForeachOp(loc, (LOForEach) foreach, "cube", inputAlias, innerPlan);
} catch (ParserValidationException pve) {
throw new FrontendException(pve);
}
List<Boolean> innerFlags = new ArrayList<Boolean>();
List<String> inpAliases = new ArrayList<String>();
inpAliases.add(falias);
innerFlags.add(false);
// Get the output schema of foreach operator and reconstruct the
// LogicalExpressionPlan for each dimensional attributes
MultiMap<Integer, LogicalExpressionPlan> exprPlansCopy = new MultiMap<Integer, LogicalExpressionPlan>();
for (LogicalExpressionPlan exp : expressionPlans.values()) {
LogicalExpression lexp = (LogicalExpression) exp.getSources().get(0);
LogicalExpressionPlan epGrp = new LogicalExpressionPlan();
new ProjectExpression(epGrp, 0, lexp.getFieldSchema().alias, null, groupby);
exprPlansCopy.put(0, epGrp);
}
// build group by operator