-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogicalPlan.scala
65 lines (50 loc) · 1.87 KB
/
LogicalPlan.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
package core.planner.volcano.logicalplan
import core.ql
sealed trait LogicalPlan {
def describe(): String
def children(): Seq[LogicalPlan]
}
case class Scan(table: ql.TableID, projection: Seq[String]) extends LogicalPlan {
override def describe(): String = {
if (projection.isEmpty) {
s"SCAN ${table.id}"
} else {
s"SCAN ${table.id} (${projection.mkString(", ")})"
}
}
override def children(): Seq[LogicalPlan] = Seq.empty
}
case class Project(fields: Seq[ql.FieldID], child: LogicalPlan) extends LogicalPlan {
override def describe(): String = s"PROJECT ${fields.map(f => s"${f.table.id}.${f.id}").mkString(", ")}"
override def canEqual(other: Any): Boolean = other.isInstanceOf[Project]
override def equals(other: Any): Boolean = other match {
case that: Project =>
that.canEqual(this) &&
fields.sortBy(x => (x.table.id, x.id)) == that.fields.sortBy(x => (x.table.id, x.id)) &&
child == that.child
case _ => false
}
override def children(): Seq[LogicalPlan] = Seq(child)
}
case class Join(left: LogicalPlan, right: LogicalPlan, on: Seq[(ql.FieldID, ql.FieldID)]) extends LogicalPlan {
override def describe(): String = "JOIN"
override def canEqual(that: Any): Boolean = that.isInstanceOf[Join]
override def equals(other: Any): Boolean = other match {
case that: Join =>
that.canEqual(this) && (
left == that.left && right == that.right ||
left == that.right && right == that.left
)
case _ => false
}
override def children(): Seq[LogicalPlan] = Seq(left, right)
}
object LogicalPlan {
def toPlan(node: ql.Statement): LogicalPlan = {
node match {
case ql.Table(table) => Scan(table, Seq.empty)
case ql.Join(left, right, on) => Join(toPlan(left), toPlan(right), on)
case ql.Select(fields, from) => Project(fields, toPlan(from))
}
}
}