-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectionPushDown.scala
66 lines (58 loc) · 2.47 KB
/
ProjectionPushDown.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package core.planner.volcano.rules.transform
import core.planner.volcano.VolcanoPlannerContext
import core.planner.volcano.logicalplan.{Join, LogicalPlan, Project, Scan}
import core.planner.volcano.memo.GroupExpression
import core.planner.volcano.rules.TransformationRule
import core.ql.FieldID
import scala.collection.mutable
class ProjectionPushDown extends TransformationRule {
override def `match`(expression: GroupExpression)(implicit ctx: VolcanoPlannerContext): Boolean = {
val plan = expression.plan
plan match {
case Project(_, child) => check(child)
case _ => false
}
}
// check if the tree only contains SCAN and JOIN nodes
private def check(node: LogicalPlan): Boolean = {
node match {
case Scan(_, _) => true
case Join(left, right, _) => check(left) && check(right)
case _ => false
}
}
private def extractProjections(node: LogicalPlan, buffer: mutable.ListBuffer[FieldID]): Unit = {
node match {
case Scan(_, _) => (): Unit
case Project(fields, parent) =>
buffer ++= fields
extractProjections(parent, buffer)
case Join(left, right, on) =>
buffer ++= on.map(_._1) ++ on.map(_._2)
extractProjections(left, buffer)
extractProjections(right, buffer)
}
}
override def transform(expression: GroupExpression)(implicit ctx: VolcanoPlannerContext): GroupExpression = {
val plan = expression.plan.asInstanceOf[Project]
val pushDownProjection = mutable.ListBuffer[FieldID]()
extractProjections(plan, pushDownProjection)
val newPlan = Project(plan.fields, pushDown(pushDownProjection.distinct, plan.child))
ctx.memo.getOrCreateGroupExpression(newPlan)
}
private def pushDown(pushDownProjection: Seq[FieldID], node: LogicalPlan): LogicalPlan = {
node match {
case Scan(table, tableProjection) =>
val filteredPushDownProjection = pushDownProjection.filter(_.table == table).map(_.id)
val updatedProjection =
if (filteredPushDownProjection.contains("*") || filteredPushDownProjection.contains("*.*")) {
Seq.empty
} else {
(tableProjection ++ filteredPushDownProjection).distinct
}
Scan(table, updatedProjection)
case Join(left, right, on) => Join(pushDown(pushDownProjection, left), pushDown(pushDownProjection, right), on)
case _ => throw new Exception("should never happen")
}
}
}