-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CodeGenerator.scala
127 lines (110 loc) · 6.15 KB
/
CodeGenerator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.compiled.codegen
import java.time.Clock
import java.util
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.compiled.ExecutionPlanBuilder.DescriptionProvider
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.compiled.codegen.ir._
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.compiled.codegen.spi.{CodeStructure, CodeStructureResult}
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.compiled.{CompiledExecutionResult, CompiledPlan, RunnablePlan}
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.executionplan.{InternalExecutionResult, PlanFingerprint, Provider}
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.{ExecutionMode, TaskCloser}
import org.neo4j.cypher.internal.compiler.v3_3.planDescription.{Id, InternalPlanDescription}
import org.neo4j.cypher.internal.compiler.v3_3.planner.CantCompileQueryException
import org.neo4j.cypher.internal.compiler.v3_3.planner.logical.plans.{LogicalPlan, ProduceResult}
import org.neo4j.cypher.internal.compiler.v3_3.planner.logical.{LogicalPlan2PlanDescription, LogicalPlanIdentificationBuilder}
import org.neo4j.cypher.internal.compiler.v3_3.spi.{InstrumentedGraphStatistics, PlanContext}
import org.neo4j.cypher.internal.frontend.v3_3.helpers.Eagerly
import org.neo4j.cypher.internal.frontend.v3_3.{PlannerName, SemanticTable}
import org.neo4j.cypher.internal.spi.v3_3.QueryContext
import org.neo4j.cypher.internal.v3_3.codegen.QueryExecutionTracer
import org.neo4j.cypher.internal.v3_3.executionplan.{GeneratedQuery, GeneratedQueryExecution}
class CodeGenerator(val structure: CodeStructure[GeneratedQuery], clock: Clock, conf: CodeGenConfiguration = CodeGenConfiguration() ) {
import CodeGenerator.generateCode
type PlanDescriptionProvider =
(InternalPlanDescription) => (Provider[InternalPlanDescription], Option[QueryExecutionTracer])
def generate(plan: LogicalPlan, planContext: PlanContext, semanticTable: SemanticTable, plannerName: PlannerName): CompiledPlan = {
plan match {
case res: ProduceResult =>
val idMap = LogicalPlanIdentificationBuilder(plan)
val query: CodeStructureResult[GeneratedQuery] = try {
generateQuery(plan, semanticTable, idMap, res.columns, conf)
} catch {
case e: CantCompileQueryException => throw e
case e: Exception => throw new CantCompileQueryException(cause = e)
}
val fp = planContext.statistics match {
case igs: InstrumentedGraphStatistics =>
Some(PlanFingerprint(clock.millis(), planContext.txIdProvider(), igs.snapshot.freeze))
case _ =>
None
}
val descriptionTree = LogicalPlan2PlanDescription(plan, idMap)
val description: InternalPlanDescription = query.code.foldLeft(descriptionTree) {
case (descriptionRoot, code) => descriptionRoot.addArgument(code)
}
val builder = new RunnablePlan {
def apply(queryContext: QueryContext, execMode: ExecutionMode,
descriptionProvider: DescriptionProvider, params: Map[String, Any],
closer: TaskCloser): InternalExecutionResult = {
val (provider, tracer) = descriptionProvider(description)
val execution: GeneratedQueryExecution = query.query.execute(closer, queryContext, execMode,
provider, tracer.getOrElse(QueryExecutionTracer.NONE), asJavaHashMap(params))
new CompiledExecutionResult(closer, queryContext, execution, provider)
}
}
CompiledPlan(updating = false, None, fp, plannerName, description, res.columns, builder, plan.indexUsage)
case _ => throw new CantCompileQueryException("Can only compile plans with ProduceResult on top")
}
}
private def generateQuery(plan: LogicalPlan, semantics: SemanticTable, ids: Map[LogicalPlan, Id],
columns: Seq[String], conf: CodeGenConfiguration): CodeStructureResult[GeneratedQuery] = {
import LogicalPlanConverter._
implicit val context = new CodeGenContext(semantics, ids)
val (_, instructions) = asCodeGenPlan(plan).produce(context)
generateCode(structure)(instructions, context.operatorIds.map {
case (id: Id, field: String) => field -> id
}.toMap, columns, conf)
}
private def asJavaHashMap(params: scala.collection.Map[String, Any]) = {
val jMap = new util.HashMap[String, Object]()
params.foreach {
case (key, value) => jMap.put(key, javaValue(value))
}
jMap
}
import scala.collection.JavaConverters._
private def javaValue(value: Any): Object = value match {
case null => null
case iter: Seq[_] => iter.map(javaValue).asJava
case iter: scala.collection.Map[_, _] => Eagerly.immutableMapValues(iter, javaValue).asJava
case x: Any => x.asInstanceOf[AnyRef]
}
}
object CodeGenerator {
type SourceSink = Option[(String, String) => Unit]
def generateCode[T](structure: CodeStructure[T])(instructions: Seq[Instruction], operatorIds: Map[String, Id],
columns: Seq[String], conf: CodeGenConfiguration)(implicit context: CodeGenContext): CodeStructureResult[T] = {
structure.generateQuery(Namer.newClassName(), columns, operatorIds, conf) { accept =>
instructions.foreach(insn => insn.init(accept))
instructions.foreach(insn => insn.body(accept))
}
}
}