-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
compiler.py
216 lines (186 loc) · 7.08 KB
/
compiler.py
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from __future__ import unicode_literals
from django.db import connections, models
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.query import Query
SEPARATOR = "\x1f"
class TreeQuery(Query):
def get_compiler(self, using=None, connection=None):
# Copied from django/db/models/sql/query.py
if using is None and connection is None:
raise ValueError("Need either using or connection")
if using:
connection = connections[using]
# Difference: Not connection.ops.compiler, but our own compiler which
# adds the CTE.
return TreeCompiler(self, connection, using)
class TreeCompiler(SQLCompiler):
CTE_POSTGRESQL_WITH_TEXT_ORDERING = """
WITH RECURSIVE __tree (
"tree_depth",
"tree_path",
"tree_ordering",
"tree_pk"
) AS (
SELECT
0 AS tree_depth,
array[T.{pk}] AS tree_path,
array[LPAD(CONCAT({order_by}), 20, '0')]::text[] AS tree_ordering,
T."{pk}"
FROM {db_table} T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
__tree.tree_depth + 1 AS tree_depth,
__tree.tree_path || T.{pk},
__tree.tree_ordering || LPAD(CONCAT({order_by}), 20, '0')::text,
T."{pk}"
FROM {db_table} T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
CTE_POSTGRESQL_WITH_INTEGER_ORDERING = """
WITH RECURSIVE __tree (
"tree_depth",
"tree_path",
"tree_ordering",
"tree_pk"
) AS (
SELECT
0 AS tree_depth,
array[T.{pk}] AS tree_path,
array[{order_by}] AS tree_ordering,
T."{pk}"
FROM {db_table} T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
__tree.tree_depth + 1 AS tree_depth,
__tree.tree_path || T.{pk},
__tree.tree_ordering || {order_by},
T."{pk}"
FROM {db_table} T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
CTE_MYSQL = """
WITH RECURSIVE __tree(tree_depth, tree_path, tree_ordering, tree_pk) AS (
SELECT
0,
-- Limit to max. 50 levels...
CAST(CONCAT("{sep}", {pk}, "{sep}") AS char(1000)),
CAST(CONCAT("{sep}", LPAD(CONCAT({order_by}, "{sep}"), 20, "0"))
AS char(1000)),
T.{pk}
FROM {db_table} T
WHERE T.{parent} IS NULL
UNION ALL
SELECT
__tree.tree_depth + 1,
CONCAT(__tree.tree_path, T2.{pk}, "{sep}"),
CONCAT(__tree.tree_ordering, LPAD(CONCAT(T2.{order_by}, "{sep}"), 20, "0")),
T2.{pk}
FROM __tree, {db_table} T2
WHERE __tree.tree_pk = T2.{parent}
)
"""
CTE_SQLITE3 = """
WITH RECURSIVE __tree(tree_depth, tree_path, tree_ordering, tree_pk) AS (
SELECT
0 tree_depth,
printf("{sep}%%s{sep}", {pk}) tree_path,
printf("{sep}%%020s{sep}", {order_by}) tree_ordering,
T."{pk}" tree_pk
FROM {db_table} T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
__tree.tree_depth + 1,
__tree.tree_path || printf("%%s{sep}", T.{pk}),
__tree.tree_ordering || printf("%%020s{sep}", T.{order_by}),
T."{pk}"
FROM {db_table} T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
def as_sql(self, *args, **kwargs):
# Summary queries are aggregates (not annotations)
is_summary = any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
)
opts = self.query.model._meta
params = {
"parent": "parent_id", # XXX Hardcoded.
"pk": opts.pk.attname,
"db_table": opts.db_table,
"order_by": opts.ordering[0] if opts.ordering else opts.pk.attname,
"sep": SEPARATOR,
}
if "__tree" not in self.query.extra_tables: # pragma: no branch - unlikely
tree_params = params.copy()
# use aliased table name (U0, U1, U2)
base_table = self.query.__dict__.get("base_table")
if base_table is not None:
tree_params["db_table"] = base_table
self.query.add_extra(
# Do not add extra fields to the select statement when it is a
# summary query
select={}
if is_summary
else {
"tree_depth": "__tree.tree_depth",
"tree_path": "__tree.tree_path",
"tree_ordering": "__tree.tree_ordering",
},
select_params=None,
where=["__tree.tree_pk = {db_table}.{pk}".format(**tree_params)],
params=None,
tables=["__tree"],
order_by=(
[]
# Do not add ordering for aggregates, or if the ordering
# has already been specified using .extra()
if is_summary or self.query.extra_order_by
else ["__tree.tree_ordering"] # DFS is the only true way
),
)
sql = super(TreeCompiler, self).as_sql(*args, **kwargs)
if self.connection.vendor == "postgresql":
CTE = (
self.CTE_POSTGRESQL_WITH_INTEGER_ORDERING
if _ordered_by_integer(opts, params)
else self.CTE_POSTGRESQL_WITH_TEXT_ORDERING
)
elif self.connection.vendor == "sqlite":
CTE = self.CTE_SQLITE3
elif self.connection.vendor == "mysql":
CTE = self.CTE_MYSQL
return ("".join([CTE.format(**params), sql[0]]), sql[1])
def get_converters(self, expressions):
converters = super(TreeCompiler, self).get_converters(expressions)
for i, expression in enumerate(expressions):
# We care about tree fields and annotations only
if not hasattr(expression, "sql"):
continue
if expression.sql in {"__tree.tree_path", "__tree.tree_ordering"}:
converters[i] = ([converter], expression)
return converters
def converter(value, expression, connection, context=None):
# context can be removed as soon as we only support Django>=2.0
if isinstance(value, str):
# MySQL/MariaDB and sqlite3 do not support arrays. Split the value on
# the ASCII unit separator (chr(31)).
# NOTE: The representation of array is NOT part of the API.
value = value.split(SEPARATOR)[1:-1]
try:
# Either all values are convertible to int or don't bother
return [int(v) for v in value] # Maybe Field.to_python()?
except ValueError:
return value
def _ordered_by_integer(opts, params):
try:
ordering_field = opts.get_field(params["order_by"])
return isinstance(ordering_field, models.IntegerField)
except Exception:
return False