-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DelegatingProcedureExecutablePlanBuilder.scala
132 lines (111 loc) · 6.45 KB
/
DelegatingProcedureExecutablePlanBuilder.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
128
129
130
131
/*
* Copyright (c) 2002-2016 "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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cypher.internal.compiler.v3_0.executionplan.procs
import org.neo4j.cypher.internal.compiler.v3_0.executionplan.{ExecutablePlanBuilder, ExecutionPlan, PlanFingerprint, PlanFingerprintReference, SCHEMA_WRITE}
import org.neo4j.cypher.internal.compiler.v3_0.spi.{FieldSignature, PlanContext, ProcedureName, ProcedureSignature, QueryContext}
import org.neo4j.cypher.internal.compiler.v3_0.{CompilationPhaseTracer, PreparedQuery}
import org.neo4j.cypher.internal.frontend.v3_0.ast._
import org.neo4j.cypher.internal.frontend.v3_0.symbols.TypeSpec
import org.neo4j.cypher.internal.frontend.v3_0.{CypherTypeException, InvalidArgumentException, SemanticTable}
/**
* This planner takes on queries that requires no planning such as procedures and schema commands
*
* @param delegate The plan builder to delegate to
*/
case class DelegatingProcedureExecutablePlanBuilder(delegate: ExecutablePlanBuilder) extends ExecutablePlanBuilder {
override def producePlan(inputQuery: PreparedQuery, planContext: PlanContext, tracer: CompilationPhaseTracer,
createFingerprintReference: (Option[PlanFingerprint]) => PlanFingerprintReference): ExecutionPlan = {
inputQuery.statement match {
// CALL foo.bar.baz("arg1", 2)
case call@CallProcedure(namespace, ProcName(name), providedArgs) =>
val signature = planContext.procedureSignature(ProcedureName(namespace, name))
providedArgs.foreach { args =>
if (args.nonEmpty && args.size != signature.inputSignature.size) {
throw new InvalidArgumentException(
s"""Procedure ${signature.name.name} takes ${signature.inputSignature.size}
|arguments but ${args.size} was provided.""".stripMargin)
}
//type check arguments
args.zip(signature.inputSignature).foreach {
case (arg, field) => typeCheck(inputQuery.semanticTable)(arg, field, signature)
}
}
CallProcedureExecutionPlan(signature, providedArgs)
// CREATE CONSTRAINT ON (node:Label) ASSERT node.prop IS UNIQUE
case CreateUniquePropertyConstraint(node, label, prop) =>
PureSideEffectExecutionPlan("CreateUniqueConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.createUniqueConstraint _).tupled(labelProp(ctx)(label, prop.propertyKey))
})
// DROP CONSTRAINT ON (node:Label) ASSERT node.prop IS UNIQUE
case DropUniquePropertyConstraint(_, label, prop) =>
PureSideEffectExecutionPlan("DropUniqueConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.dropUniqueConstraint _).tupled(labelProp(ctx)(label, prop.propertyKey))
})
// CREATE CONSTRAINT ON (node:Label) ASSERT node.prop EXISTS
case CreateNodePropertyExistenceConstraint(_, label, prop) =>
PureSideEffectExecutionPlan("CreateNodePropertyExistenceConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.createNodePropertyExistenceConstraint _).tupled(labelProp(ctx)(label, prop.propertyKey))
})
// DROP CONSTRAINT ON (node:Label) ASSERT node.prop EXISTS
case DropNodePropertyExistenceConstraint(_, label, prop) =>
PureSideEffectExecutionPlan("CreateNodePropertyExistenceConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.dropNodePropertyExistenceConstraint _).tupled(labelProp(ctx)(label, prop.propertyKey))
})
// CREATE CONSTRAINT ON ()-[r:R]-() ASSERT r.prop EXISTS
case CreateRelationshipPropertyExistenceConstraint(_, relType, prop) =>
PureSideEffectExecutionPlan("CreateRelationshipPropertyExistenceConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.createRelationshipPropertyExistenceConstraint _).tupled(typeProp(ctx)(relType, prop.propertyKey))
})
// DROP CONSTRAINT ON ()-[r:R]-() ASSERT r.prop EXISTS
case DropRelationshipPropertyExistenceConstraint(_, relType, prop) =>
PureSideEffectExecutionPlan("DropRelationshipPropertyExistenceConstraint", SCHEMA_WRITE, (ctx) => {
(ctx.dropRelationshipPropertyExistenceConstraint _).tupled(typeProp(ctx)(relType, prop.propertyKey))
})
// CREATE INDEX ON :LABEL(prop)
case CreateIndex(label, prop) =>
PureSideEffectExecutionPlan("CreateIndex", SCHEMA_WRITE, (ctx) => {
(ctx.addIndexRule _).tupled(labelProp(ctx)(label, prop))
})
// DROP INDEX ON :LABEL(prop)
case DropIndex(label, prop) =>
PureSideEffectExecutionPlan("DropIndex", SCHEMA_WRITE, (ctx) => {
(ctx.dropIndexRule _).tupled(labelProp(ctx)(label, prop))
})
case _ => delegate.producePlan(inputQuery, planContext, tracer, createFingerprintReference)
}
}
private def labelProp(ctx: QueryContext)(label: LabelName, prop: PropertyKeyName) =
(ctx.getOrCreateLabelId(label.name), ctx.getOrCreatePropertyKeyId(prop.name))
private def typeProp(ctx: QueryContext)(relType: RelTypeName, prop: PropertyKeyName) =
(ctx.getOrCreateRelTypeId(relType.name), ctx.getOrCreatePropertyKeyId(prop.name))
private def typeCheck(semanticTable: SemanticTable)(exp: Expression, field: FieldSignature, proc: ProcedureSignature) = {
val actual = semanticTable.types(exp).actual
val expected = field.typ
val intersected = actual intersectOrCoerce expected.covariant
if (intersected == TypeSpec.none)
throw new CypherTypeException(
s"""Parameter `${field.name}` for procedure `${proc.name}`
|expects value of type ${semanticTable.types(exp).actual.toShortString} but got value of type ${field.typ}.
|
|Usage: CALL ${proc.name}(${proc.inputSignature.map(s => s"<${s.name}>").mkString(", ")})
|${proc.inputSignature.map(s => s" ${s.name} (type ${s.typ})").mkString("Parameters:\n", "\n","")}
""".stripMargin)
}
}