-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
MasterCompiler.scala
175 lines (153 loc) · 7.97 KB
/
MasterCompiler.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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
import java.time.Clock
import org.neo4j.cypher.internal.compiler.v3_5.{StatsDivergenceCalculator, _}
import org.neo4j.cypher.{InvalidArgumentException, _}
import org.neo4j.graphdb.factory.GraphDatabaseSettings
import org.neo4j.kernel.GraphDatabaseQueryService
import org.neo4j.kernel.configuration.Config
import org.neo4j.kernel.impl.query.TransactionalContext
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.logging.LogProvider
import org.neo4j.values.virtual.MapValue
import org.opencypher.v9_0.frontend.phases.{CompilationPhaseTracer, RecordingNotificationLogger}
import org.opencypher.v9_0.util.{DeprecatedStartNotification, SyntaxException => InternalSyntaxException}
object MasterCompiler {
val DEFAULT_QUERY_CACHE_SIZE: Int = 128
val DEFAULT_QUERY_PLAN_TTL: Long = 1000 // 1 second
val DEFAULT_QUERY_PLAN_TARGET: Long = 1000 * 60 * 60 * 7 // 7 hours
val CLOCK: Clock = Clock.systemUTC()
val DEFAULT_STATISTICS_DIVERGENCE_THRESHOLD = 0.5
val DEFAULT_STATISTICS_DIVERGENCE_TARGET = 0.1
val DEFAULT_DIVERGENCE_ALGORITHM: String = StatsDivergenceCalculator.inverse
val DEFAULT_NON_INDEXED_LABEL_WARNING_THRESHOLD = 10000
}
/**
* Selects the correct cypher implementation based on a pre-parsed query.
*/
class MasterCompiler(graph: GraphDatabaseQueryService,
kernelMonitors: KernelMonitors,
config: CypherConfiguration,
logProvider: LogProvider,
compilerLibrary: CompilerLibrary) {
import org.neo4j.cypher.internal.MasterCompiler._
/**
* Clear all compiler caches.
*
* @return the maximum number of entries clear from any cache
*/
def clearCaches(): Long = {
compilerLibrary.clearCaches()
}
/**
* Compile pre-parsed query into executable query.
*
* @param preParsedQuery pre-parsed query to convert
* @param tracer compilation tracer to which events of the compilation process are reported
* @param transactionalContext transactional context to use during compilation (in logical and physical planning)
* @return a compiled and executable query
*/
def compile(preParsedQuery: PreParsedQuery,
tracer: CompilationPhaseTracer,
transactionalContext: TransactionalContext,
params: MapValue
): ExecutableQuery = {
val logger = new RecordingNotificationLogger(Some(preParsedQuery.offset))
val supportedRuntimes3_1 = Seq(CypherRuntimeOption.interpreted, CypherRuntimeOption.default)
val inputPosition = preParsedQuery.offset
def assertSupportedRuntime(ex: InternalSyntaxException, runtime: CypherRuntimeOption): Unit = {
if (!supportedRuntimes3_1.contains(runtime)) {
if (config.useErrorsOverWarnings) {
throw new InvalidArgumentException("The given query is not currently supported in the selected runtime")
} else {
logger.log(RuntimeUnsupportedNotification)
}
}
}
/**
* Compile query or recursively fallback to 3.1 in some cases.
*
* @param preParsedQuery the query to compile
* @return the compiled query
*/
def innerCompile(preParsedQuery: PreParsedQuery, params: MapValue): ExecutableQuery = {
if ((preParsedQuery.version == CypherVersion.v3_4 || preParsedQuery.version == CypherVersion.v3_5) && preParsedQuery.planner == CypherPlannerOption.rule) {
logger.log(RulePlannerUnavailableFallbackNotification)
innerCompile(preParsedQuery.copy(version = CypherVersion.v3_1), params)
} else if (preParsedQuery.version == CypherVersion.v3_5) {
val compiler3_5 = compilerLibrary.selectCompiler(preParsedQuery.version,
preParsedQuery.planner,
preParsedQuery.runtime,
preParsedQuery.updateStrategy)
try {
compiler3_5.compile(preParsedQuery, tracer, logger.notifications, transactionalContext, params)
} catch {
case ex: SyntaxException if ex.getMessage.startsWith("CREATE UNIQUE") =>
val ex3_5 = ex.getCause.asInstanceOf[InternalSyntaxException]
logger.log(CreateUniqueUnavailableFallback(ex3_5.pos.get))
assertSupportedRuntime(ex3_5, preParsedQuery.runtime)
innerCompile(preParsedQuery.copy(version = CypherVersion.v3_1, runtime = CypherRuntimeOption.interpreted), params)
case ex: SyntaxException if ex.getMessage.startsWith("START is deprecated") =>
val ex3_5 = ex.getCause.asInstanceOf[InternalSyntaxException]
logger.log(StartUnavailableFallback)
logger.log(DeprecatedStartNotification(inputPosition, ex.getMessage))
assertSupportedRuntime(ex3_5, preParsedQuery.runtime)
innerCompile(preParsedQuery.copy(version = CypherVersion.v3_1, runtime = CypherRuntimeOption.interpreted), params)
}
} else {
val compiler = compilerLibrary.selectCompiler(preParsedQuery.version,
preParsedQuery.planner,
preParsedQuery.runtime,
preParsedQuery.updateStrategy)
compiler.compile(preParsedQuery, tracer, logger.notifications, transactionalContext, params)
}
}
// Do the compilation
innerCompile(preParsedQuery, params)
}
private def getStatisticsDivergenceCalculator: StatsDivergenceCalculator = {
val divergenceThreshold = getSetting(graph,
config => config.get(GraphDatabaseSettings.query_statistics_divergence_threshold).doubleValue(),
DEFAULT_STATISTICS_DIVERGENCE_THRESHOLD)
val targetThreshold = getSetting(graph,
config => config.get(GraphDatabaseSettings.query_statistics_divergence_target).doubleValue(),
DEFAULT_STATISTICS_DIVERGENCE_TARGET)
val minReplanTime = getSetting(graph,
config => config.get(GraphDatabaseSettings.cypher_min_replan_interval).toMillis.longValue(),
DEFAULT_QUERY_PLAN_TTL)
val targetReplanTime = getSetting(graph,
config => config.get(GraphDatabaseSettings.cypher_replan_interval_target).toMillis.longValue(),
DEFAULT_QUERY_PLAN_TARGET)
val divergenceAlgorithm = getSetting(graph,
config => config.get(GraphDatabaseSettings.cypher_replan_algorithm),
DEFAULT_DIVERGENCE_ALGORITHM)
StatsDivergenceCalculator.divergenceCalculatorFor(divergenceAlgorithm, divergenceThreshold, targetThreshold, minReplanTime, targetReplanTime)
}
private def getNonIndexedLabelWarningThreshold: Long = {
val setting: (Config) => Long = config => config.get(GraphDatabaseSettings.query_non_indexed_label_warning_threshold).longValue()
getSetting(graph, setting, DEFAULT_NON_INDEXED_LABEL_WARNING_THRESHOLD)
}
private def getSetting[A](gds: GraphDatabaseQueryService, configLookup: Config => A, default: A): A = gds match {
// TODO: Cypher should not be pulling out components from casted interfaces, it should ask for Config as a dep
case gdbApi:GraphDatabaseQueryService => configLookup(gdbApi.getDependencyResolver.resolveDependency(classOf[Config]))
case _ => default
}
}