|
| 1 | +package org.partiql.eval.impl |
| 2 | + |
| 3 | +import org.partiql.eval.impl.expression.Collection |
| 4 | +import org.partiql.eval.impl.expression.Literal |
| 5 | +import org.partiql.eval.impl.expression.Select |
| 6 | +import org.partiql.eval.impl.expression.Struct |
| 7 | +import org.partiql.eval.impl.expression.Variable |
| 8 | +import org.partiql.eval.impl.relation.Projection |
| 9 | +import org.partiql.eval.impl.relation.Scan |
| 10 | +import org.partiql.plan.PartiQLPlan |
| 11 | +import org.partiql.plan.PlanNode |
| 12 | +import org.partiql.plan.Rel |
| 13 | +import org.partiql.plan.Rex |
| 14 | +import org.partiql.plan.Statement |
| 15 | +import org.partiql.plan.visitor.PlanBaseVisitor |
| 16 | +import org.partiql.value.PartiQLValueExperimental |
| 17 | + |
| 18 | +internal object PlanToPhysical { |
| 19 | + |
| 20 | + fun convert(plan: PartiQLPlan): PhysicalNode.Expression { |
| 21 | + return PlanToCodeTransformer.visitPartiQLPlan(plan, Unit) |
| 22 | + } |
| 23 | + |
| 24 | + private object PlanToCodeTransformer : PlanBaseVisitor<PhysicalNode, Unit>() { |
| 25 | + override fun defaultReturn(node: PlanNode, ctx: Unit): PhysicalNode { |
| 26 | + TODO("Not yet implemented") |
| 27 | + } |
| 28 | + |
| 29 | + override fun visitRexOpStruct(node: Rex.Op.Struct, ctx: Unit): PhysicalNode { |
| 30 | + val fields = node.fields.map { |
| 31 | + Struct.Field(visitRex(it.k, ctx), visitRex(it.v, ctx)) |
| 32 | + } |
| 33 | + return Struct(fields) |
| 34 | + } |
| 35 | + |
| 36 | + override fun visitRexOpVar(node: Rex.Op.Var, ctx: Unit): PhysicalNode { |
| 37 | + return Variable(node.ref) |
| 38 | + } |
| 39 | + |
| 40 | + override fun visitRexOpCollection(node: Rex.Op.Collection, ctx: Unit): PhysicalNode { |
| 41 | + val values = node.values.map { visitRex(it, ctx) } |
| 42 | + return Collection(values) |
| 43 | + } |
| 44 | + |
| 45 | + override fun visitRelOpProject(node: Rel.Op.Project, ctx: Unit): PhysicalNode { |
| 46 | + val input = visitRel(node.input, ctx) |
| 47 | + val projections = node.projections.map { visitRex(it, ctx) } |
| 48 | + return Projection(input, projections) |
| 49 | + } |
| 50 | + |
| 51 | + override fun visitRexOpSelect(node: Rex.Op.Select, ctx: Unit): PhysicalNode { |
| 52 | + val rel = visitRel(node.rel, ctx) |
| 53 | + val constructor = visitRex(node.constructor, ctx) |
| 54 | + return Select(rel, constructor) |
| 55 | + } |
| 56 | + |
| 57 | + override fun visitRelOpScan(node: Rel.Op.Scan, ctx: Unit): PhysicalNode { |
| 58 | + val rex = visitRex(node.rex, ctx) |
| 59 | + return Scan(rex) |
| 60 | + } |
| 61 | + |
| 62 | + @OptIn(PartiQLValueExperimental::class) |
| 63 | + override fun visitRexOpLit(node: Rex.Op.Lit, ctx: Unit): PhysicalNode { |
| 64 | + return Literal(node.value) |
| 65 | + } |
| 66 | + |
| 67 | + override fun visitRel(node: Rel, ctx: Unit): PhysicalNode.Relation { |
| 68 | + return super.visitRelOp(node.op, ctx) as PhysicalNode.Relation |
| 69 | + } |
| 70 | + |
| 71 | + override fun visitRex(node: Rex, ctx: Unit): PhysicalNode.Expression { |
| 72 | + return super.visitRexOp(node.op, ctx) as PhysicalNode.Expression |
| 73 | + } |
| 74 | + |
| 75 | + // TODO: Re-look at |
| 76 | + override fun visitPartiQLPlan(node: PartiQLPlan, ctx: Unit): PhysicalNode.Expression { |
| 77 | + return visitStatement(node.statement, ctx) as PhysicalNode.Expression |
| 78 | + } |
| 79 | + |
| 80 | + // TODO: Re-look at |
| 81 | + override fun visitStatementQuery(node: Statement.Query, ctx: Unit): PhysicalNode.Expression { |
| 82 | + return visitRex(node.root, ctx) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments