-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
inlineProjections.scala
106 lines (92 loc) · 4.4 KB
/
inlineProjections.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
/*
* 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 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_3.ast.rewriters
import org.neo4j.cypher.internal.frontend.v3_4.ast._
import org.neo4j.cypher.internal.frontend.v3_4.helpers.fixedPoint
import org.neo4j.cypher.internal.frontend.v3_4.{InternalException, Rewriter, TypedRewriter, topDown}
case object inlineProjections extends Rewriter {
def apply(in: AnyRef): AnyRef = instance(in)
private val instance = Rewriter.lift { case input: Statement =>
val context = inliningContextCreator(input)
val inlineVariables = TypedRewriter[ASTNode](context.variableRewriter)
val inlinePatterns = TypedRewriter[Pattern](context.patternRewriter)
val inlineReturnItemsInWith = Rewriter.lift(aliasedReturnItemRewriter(inlineVariables.narrowed, context, inlineAliases = true))
val inlineReturnItemsInReturn = Rewriter.lift(aliasedReturnItemRewriter(inlineVariables.narrowed, context, inlineAliases = false))
val inliningRewriter: Rewriter = Rewriter.lift {
case withClause: With if !withClause.distinct =>
withClause.copy(
returnItems = withClause.returnItems.rewrite(inlineReturnItemsInWith).asInstanceOf[ReturnItems],
where = withClause.where.map(inlineVariables.narrowed)
)(withClause.position)
case returnClause: Return =>
returnClause.copy(
returnItems = returnClause.returnItems.rewrite(inlineReturnItemsInReturn).asInstanceOf[ReturnItems]
)(returnClause.position)
case m @ Match(_, mPattern, mHints, mOptWhere) =>
val newOptWhere = mOptWhere.map(inlineVariables.narrowed)
val newHints = mHints.map(inlineVariables.narrowed)
// no need to inline expressions in patterns since all expressions have been moved to WHERE prior to
// calling inlineProjections
val newPattern = inlinePatterns(mPattern)
m.copy(pattern = newPattern, hints = newHints, where = newOptWhere)(m.position)
case _: UpdateClause =>
throw new InternalException("Update clauses not excepted here")
case clause: Clause =>
inlineVariables.narrowed(clause)
}
input.endoRewrite(topDown(inliningRewriter, _.isInstanceOf[Expression]))
}
private def findAllDependencies(variable: Variable, context: InliningContext): Set[Variable] = {
val (dependencies, _) = fixedPoint[(Set[Variable], List[Variable])]({
case (deps, Nil) =>
(deps, Nil)
case (deps, queue) =>
val id :: tail = queue
context.projections.get(id) match {
case Some(expr) =>
val exprDependencies = expr.dependencies
(deps - id ++ exprDependencies, (exprDependencies -- deps).toList ++ tail)
case None =>
(deps + id, queue)
}
})((Set(variable), List(variable)))
dependencies
}
private def aliasedReturnItemRewriter(inlineExpressions: Expression => Expression, context: InliningContext,
inlineAliases: Boolean): PartialFunction[AnyRef, AnyRef] = {
case ri: ReturnItems =>
val newItems = ri.items.flatMap {
case item: AliasedReturnItem
if context.okToRewrite(item.variable) && inlineAliases =>
val dependencies = findAllDependencies(item.variable, context)
if (dependencies == Set(item.variable)) {
IndexedSeq(item)
} else {
dependencies.map { id =>
AliasedReturnItem(id.copyId, id.copyId)(item.position)
}.toIndexedSeq
}
case item: AliasedReturnItem => IndexedSeq(
item.copy(expression = inlineExpressions(item.expression))(item.position)
)
}
ri.copy(items = newItems)(ri.position)
}
}