in mv/plan/src/main/scala/org/apache/carbondata/mv/plans/util/SQLBuilder.scala [143:225]
override def apply(tree: ModularPlan): ModularPlan = {
tree transformUp {
case s@modular.Select(_, _, _, _, _,
Seq(g@modular.GroupBy(_, _, _, _,
s2@modular.Select(_, _, _, _, _, _, _, _, _, _), _, _, _)), _, _, _, _)
if s2.children.forall { _.isInstanceOf[modular.LeafNode] } => s
case g@modular.GroupBy(_, _, _, _,
s2@modular.Select(_, _, _, _, _, _, _, _, _, _), _, _, _)
if s2.children.forall { _.isInstanceOf[modular.LeafNode] } => g
case s@modular.Select(_, _, _, _, _, _, _, _, _, _)
if !s.rewritten && !s.children.forall { _.isInstanceOf[modular.LeafNode] } =>
var list: List[(Int, String)] = List()
var newS = s.copy()
s.children.zipWithIndex.filterNot { _._1.isInstanceOf[modular.LeafNode] }.foreach {
case (child: ModularPlan, index) if !s.aliasMap.contains(index) =>
val subqueryName = newSubqueryName()
val windowAttributeSet = child match {
case select: Select =>
val windowExprs = select.windowSpec
.map { case Seq(expr) => expr.asInstanceOf[Seq[NamedExpression]] }
.foldLeft(Seq.empty.asInstanceOf[Seq[NamedExpression]])(_ ++ _)
SQLBuilder.collectAttributeSet(windowExprs)
case _ =>
AttributeSet.empty
}
val subqueryAttributeSet = child.outputSet ++ windowAttributeSet
// TODO: how to use alias to avoid duplicate names with distinct
// exprIds
// val duplicateNames = collectDuplicateNames(subqueryAttributeSet)
// .toSet
if (SQLBuilder.collectDuplicateNames(subqueryAttributeSet).nonEmpty) {
new UnsupportedOperationException(s"duplicate name(s): ${
child.output
.map(_.toString + ", ")
}")
}
list = list :+ ((index, subqueryName))
newS = newS.transformExpressions {
case ref: Attribute if subqueryAttributeSet.contains(ref) =>
ExpressionHelper.createReference(
ref.name, ref.dataType, nullable = true, Metadata.empty,
ref.exprId, Some(subqueryName))
case alias: Alias if subqueryAttributeSet.contains(alias.toAttribute) =>
ExpressionHelper.createAlias(
alias.child, alias.name, alias.exprId, Some(subqueryName))
}
case _ =>
}
if (list.nonEmpty) {
list = list ++ s.aliasMap.toSeq
newS.copy(aliasMap = list.toMap)
} else {
newS
}
case g@modular.GroupBy(_, _, _, _, _, _, _, _) if !g.rewritten && g.alias.isEmpty =>
val newG = if (g.outputList.isEmpty) {
val ol = g.predicateList.map { case a: Attribute => a }
g.copy(outputList = ol)
} else {
g
}
val subqueryName = newSubqueryName()
val subqueryAttributeSet = newG.child.outputSet
if (SQLBuilder.collectDuplicateNames(subqueryAttributeSet).nonEmpty) {
new UnsupportedOperationException(s"duplicate name(s): ${
newG.child.output.map(_.toString + ", ")
}")
}
newG.transformExpressions {
case ref: AttributeReference if subqueryAttributeSet.contains(ref) =>
ExpressionHelper.createReference(
ref.name, ref.dataType, nullable = true, Metadata.empty,
ref.exprId, Some(subqueryName))
case alias: Alias if subqueryAttributeSet.contains(alias.toAttribute) =>
ExpressionHelper.createAlias(
alias.child, alias.name, alias.exprId, Some(subqueryName))
}.copy(alias = Some(subqueryName))
}
}