in integration/spark/src/main/scala/org/apache/spark/sql/optimizer/MVMatcher.scala [1306:1385]
private def doTopSelectTranslation(exprE: Expression,
exprListR: Seq[Expression],
subsumee: ModularPlan,
subsumer: ModularPlan,
compensation: Option[ModularPlan]): Option[Expression] = {
(subsumer, subsumee, compensation) match {
// top selects whose children do not match exactly
// for simplicity, we assume outputList of subsumer is 1-1 corresponding to that of its
// immediately group by child
case (
_@modular.Select(
_, _, _, _, _,
Seq(gb_2a@modular.GroupBy(
_, _, _, _, sel_2a@modular.Select(_, _, _, _, _, _, _, _, _, _), _, _, _)),
_, _, _, _),
sel_3q@modular.Select(
_, _, _, _, _, Seq(gb_2q@modular.GroupBy(_, _, _, _, _, _, _, _)), _, _, _, _),
Some(gb_2c@modular.GroupBy(
_, _, _, _, sel_2c@modular.Select(_, _, _, _, _, _, _, _, _, _), _, _, _))
) =>
val distinctGrpByOList = getDistinctOutputList(gb_2q.outputList)
if (sel_3q.predicateList.contains(exprE)) {
val expr1E = exprE.transform {
case attr: Attribute =>
gb_2c.outputList.lift(
distinctGrpByOList.indexWhere {
case alias: Alias if alias.toAttribute.semanticEquals(attr) => true;
case _ => false
}).getOrElse { attr }
}
if (expr1E.eq(exprE)) {
None
} else {
Some(expr1E)
}
}
else if (sel_3q.outputList.contains(exprE)) {
exprE match {
case attr: Attribute => // this subexpression must in subsumee select output list
gb_2c.outputList.lift(
distinctGrpByOList.indexWhere {
case a if a.toAttribute.semanticEquals(attr) => true;
case _ => false
})
case alias: Alias =>
gb_2c.outputList.lift(
distinctGrpByOList.indexWhere {
case a if a.toAttribute.semanticEquals(alias.toAttribute) => true;
case _ => false
})
case _ => None
}
} else if (sel_2c.predicateList.contains(exprE)) {
if (sel_2a.predicateList.exists(_.semanticEquals(exprE)) ||
canEvaluate(exprE, subsumer) || canBeDerived(subsumer, exprE)) {
Some(exprE)
} else {
None
}
} else if (gb_2c.predicateList.contains(exprE)) {
if (gb_2a.outputList.exists {
case a: Alias if a.child.semanticEquals(exprE) => true;
case _ => false
} || canEvaluate(exprE, subsumer)) {
Some(exprE)
} else {
None
}
} else if (sel_2a.predicateList.exists(_.semanticEquals(exprE)) ||
canEvaluate(exprE, subsumer)) {
Some(exprE)
} else {
None
}
case _ => None // TODO: implement this
}
}