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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
|
import django
from django.db import connections
from django.db.models import Expression, F, QuerySet, Value, Window
from django.db.models.functions import RowNumber
from django.db.models.sql.compiler import SQLCompiler
from django.db.models.sql.query import Query
SEPARATOR = "\x1f"
def _find_tree_model(cls):
return cls._meta.get_field("parent").model
class TreeQuery(Query):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._setup_query()
def _setup_query(self):
"""
Run on initialization and at the end of chaining. Any attributes that
would normally be set in __init__() should go here instead.
"""
# We add the variables for `sibling_order` and `rank_table_query` here so they
# act as instance variables which do not persist between user queries
# the way class variables do
# Only add the sibling_order attribute if the query doesn't already have one to preserve cloning behavior
if not hasattr(self, "sibling_order"):
# Add an attribute to control the ordering of siblings within trees
opts = _find_tree_model(self.model)._meta
self.sibling_order = opts.ordering if opts.ordering else opts.pk.attname
# Only add the rank_table_query attribute if the query doesn't already have one to preserve cloning behavior
if not hasattr(self, "rank_table_query"):
# Create a default QuerySet for the rank_table to use
# so we can avoid recursion
self.rank_table_query = QuerySet(model=_find_tree_model(self.model))
if not hasattr(self, "tree_fields"):
self.tree_fields = {}
def get_compiler(self, using=None, connection=None, **kwargs):
# 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.
# **kwargs passes on elide_empty from Django 4.0 onwards
return TreeCompiler(self, connection, using, **kwargs)
def get_sibling_order(self):
return self.sibling_order
def get_rank_table_query(self):
return self.rank_table_query
def get_tree_fields(self):
return self.tree_fields
class TreeCompiler(SQLCompiler):
CTE_POSTGRESQL = """
WITH RECURSIVE __rank_table(
{tree_fields_columns}
"{pk}",
"{parent}",
"rank_order"
) AS (
{rank_table}
),
__tree (
{tree_fields_names}
"tree_depth",
"tree_path",
"tree_ordering",
"tree_pk"
) AS (
SELECT
{tree_fields_initial}
0,
array[T.{pk}],
array[T.rank_order],
T."{pk}"
FROM __rank_table T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
{tree_fields_recursive}
__tree.tree_depth + 1,
__tree.tree_path || T.{pk},
__tree.tree_ordering || T.rank_order,
T."{pk}"
FROM __rank_table T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
CTE_MYSQL = """
WITH RECURSIVE __rank_table(
{tree_fields_columns}
{pk},
{parent},
rank_order
) AS (
{rank_table}
),
__tree(
{tree_fields_names}
tree_depth,
tree_path,
tree_ordering,
tree_pk
) AS (
SELECT
{tree_fields_initial}
0,
-- Limit to max. 50 levels...
CAST(CONCAT("{sep}", {pk}, "{sep}") AS char(1000)),
CAST(CONCAT("{sep}", LPAD(CONCAT(T.rank_order, "{sep}"), 20, "0"))
AS char(1000)),
T.{pk}
FROM __rank_table T
WHERE T.{parent} IS NULL
UNION ALL
SELECT
{tree_fields_recursive}
__tree.tree_depth + 1,
CONCAT(__tree.tree_path, T2.{pk}, "{sep}"),
CONCAT(__tree.tree_ordering, LPAD(CONCAT(T2.rank_order, "{sep}"), 20, "0")),
T2.{pk}
FROM __tree, __rank_table T2
WHERE __tree.tree_pk = T2.{parent}
)
"""
CTE_SQLITE = """
WITH RECURSIVE __rank_table(
{tree_fields_columns}
{pk},
{parent},
rank_order
) AS (
{rank_table}
),
__tree(
{tree_fields_names}
tree_depth,
tree_path,
tree_ordering,
tree_pk
) AS (
SELECT
{tree_fields_initial}
0,
printf("{sep}%%s{sep}", {pk}),
printf("{sep}%%020s{sep}", T.rank_order),
T."{pk}" tree_pk
FROM __rank_table T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
{tree_fields_recursive}
__tree.tree_depth + 1,
__tree.tree_path || printf("%%s{sep}", T.{pk}),
__tree.tree_ordering || printf("%%020s{sep}", T.rank_order),
T."{pk}"
FROM __rank_table T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
# Optimized CTEs without rank table for simple cases
CTE_POSTGRESQL_SIMPLE = """
WITH RECURSIVE __tree (
{tree_fields_names}"tree_depth",
"tree_path",
"tree_ordering",
"tree_pk"
) AS (
SELECT
{tree_fields_initial}0,
array[T.{pk}],
array[T."{order_field}"],
T.{pk}
FROM {db_table} T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
{tree_fields_recursive}__tree.tree_depth + 1,
__tree.tree_path || T.{pk},
__tree.tree_ordering || T."{order_field}",
T.{pk}
FROM {db_table} T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
CTE_MYSQL_SIMPLE = """
WITH RECURSIVE __tree(
{tree_fields_names}tree_depth,
tree_path,
tree_ordering,
tree_pk
) AS (
SELECT
{tree_fields_initial}0,
CAST(CONCAT("{sep}", T.{pk}, "{sep}") AS char(1000)),
CAST(CONCAT("{sep}", LPAD(CONCAT(T.`{order_field}`, "{sep}"), 20, "0")) AS char(1000)),
T.{pk}
FROM {db_table} T
WHERE T.`{parent}` IS NULL
UNION ALL
SELECT
{tree_fields_recursive}__tree.tree_depth + 1,
CONCAT(__tree.tree_path, T.{pk}, "{sep}"),
CONCAT(__tree.tree_ordering, LPAD(CONCAT(T.`{order_field}`, "{sep}"), 20, "0")),
T.{pk}
FROM {db_table} T, __tree
WHERE __tree.tree_pk = T.`{parent}`
)
"""
CTE_SQLITE_SIMPLE = """
WITH RECURSIVE __tree(
{tree_fields_names}tree_depth,
tree_path,
tree_ordering,
tree_pk
) AS (
SELECT
{tree_fields_initial}0,
"{sep}" || T."{pk}" || "{sep}",
"{sep}" || printf("%%020s", T."{order_field}") || "{sep}",
T."{pk}"
FROM {db_table} T
WHERE T."{parent}" IS NULL
UNION ALL
SELECT
{tree_fields_recursive}__tree.tree_depth + 1,
__tree.tree_path || T."{pk}" || "{sep}",
__tree.tree_ordering || printf("%%020s", T."{order_field}") || "{sep}",
T."{pk}"
FROM {db_table} T
JOIN __tree ON T."{parent}" = __tree.tree_pk
)
"""
def _can_skip_rank_table(self):
"""
Determine if we can skip the rank table optimization.
We can skip it when:
1. No tree filters are applied (rank_table_query is unchanged)
2. Simple ordering (single field, ascending)
3. No custom tree fields
"""
# Check if tree filters have been applied
original_query = QuerySet(model=_find_tree_model(self.query.model))
if str(self.query.get_rank_table_query().query) != str(original_query.query):
return False
# Check if custom tree fields are simple column references
tree_fields = self.query.get_tree_fields()
if tree_fields:
model = _find_tree_model(self.query.model)
for name, column in tree_fields.items():
# Only allow simple column names (no complex expressions)
if not isinstance(column, str):
return False
# Check if it's a valid field on the model
try:
model._meta.get_field(column)
except FieldDoesNotExist:
return False
# Check for complex ordering
sibling_order = self.query.get_sibling_order()
if isinstance(sibling_order, (list, tuple)):
if len(sibling_order) > 1:
return False
order_field = sibling_order[0]
else:
order_field = sibling_order
# Check for descending order or complex expressions
if (
isinstance(order_field, str)
and order_field.startswith("-")
or not isinstance(order_field, str)
):
return False
# Check for related field lookups (contains __)
if "__" in order_field:
return False
# Check if the ordering field is numeric/integer
# For string fields, the optimization might not preserve correct order
# because we bypass the ROW_NUMBER() ranking that the complex CTE uses
field = _find_tree_model(self.query.model)._meta.get_field(order_field)
if not hasattr(field, "get_internal_type"):
return False
field_type = field.get_internal_type()
if field_type not in (
"AutoField",
"BigAutoField",
"IntegerField",
"BigIntegerField",
"PositiveIntegerField",
"PositiveSmallIntegerField",
"SmallIntegerField",
):
return False
return True
def get_rank_table(self):
# Get and validate sibling_order
sibling_order = self.query.get_sibling_order()
if isinstance(sibling_order, (list, tuple)):
order_fields = sibling_order
elif isinstance(sibling_order, str):
order_fields = [sibling_order]
else:
raise ValueError(
"Sibling order must be a string or a list or tuple of strings."
)
# Convert strings to expressions. This is to maintain backwards compatibility
# with Django versions < 4.1
if django.VERSION < (4, 1):
base_order = []
for field in order_fields:
if isinstance(field, Expression):
base_order.append(field)
elif isinstance(field, str):
if field[0] == "-":
base_order.append(F(field[1:]).desc())
else:
base_order.append(F(field).asc())
order_fields = base_order
# Get the rank table query
rank_table_query = self.query.get_rank_table_query()
rank_table_query = (
rank_table_query.order_by() # Ensure there is no ORDER BY at the end of the SQL
# Values allows us to both limit and specify the order of
# the columns selected so that they match the CTE
.values(
*self.query.get_tree_fields().values(),
"pk",
"parent",
rank_order=Window(
expression=RowNumber(),
order_by=order_fields,
),
)
)
rank_table_sql, rank_table_params = rank_table_query.query.sql_with_params()
return rank_table_sql, rank_table_params
def as_sql(self, *args, **kwargs):
# Try detecting if we're used in a EXISTS(1 as "a") subquery like
# Django's sql.Query.exists() generates. If we detect such a query
# we're skipping the tree generation since it's not necessary in the
# best case and references unused table aliases (leading to SQL errors)
# in the worst case. See GitHub issue #63.
if (
self.query.subquery
and (ann := self.query.annotations)
and ann == {"a": Value(1)}
):
return super().as_sql(*args, **kwargs)
# The general idea is that if we have a summary query (e.g. .count())
# then we do not want to ask Django to add the tree fields to the query
# using .query.add_extra. The way to determine whether we have a
# summary query on our hands is to check the is_summary attribute of
# all annotations.
#
# A new case appeared in the GitHub issue #26: Queries using
# .distinct().count() crashed. The reason for this is that Django uses
# a distinct subquery *without* annotations -- the annotations are kept
# in the surrounding query. Because of this we look at the distinct and
# subquery attributes.
#
# I am not confident that this is the perfect way to approach this
# problem but I just gotta stop worrying and trust the testsuite.
skip_tree_fields = (
self.query.distinct and self.query.subquery
) or any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
)
opts = _find_tree_model(self.query.model)._meta
params = {
"parent": "parent_id", # XXX Hardcoded.
"pk": opts.pk.attname,
"db_table": opts.db_table,
"sep": SEPARATOR,
}
# Check if we can use the optimized path without rank table
use_rank_table = not self._can_skip_rank_table()
if use_rank_table:
# Get the rank_table SQL and params
rank_table_sql, rank_table_params = self.get_rank_table()
params["rank_table"] = rank_table_sql
else:
# Use optimized path - get the order field for simple CTE
sibling_order = self.query.get_sibling_order()
if isinstance(sibling_order, (list, tuple)):
order_field = sibling_order[0]
else:
order_field = sibling_order
params["order_field"] = order_field
rank_table_params = []
# Set database-specific CTE template and column reference format
if self.connection.vendor == "postgresql":
cte = (
self.CTE_POSTGRESQL_SIMPLE
if not use_rank_table
else self.CTE_POSTGRESQL
)
cte_initial = "array[{column}]::text[], "
cte_recursive = "__tree.{name} || {column}::text, "
elif self.connection.vendor == "sqlite":
cte = self.CTE_SQLITE_SIMPLE if not use_rank_table else self.CTE_SQLITE
cte_initial = 'printf("{sep}%%s{sep}", {column}), '
cte_recursive = '__tree.{name} || printf("%%s{sep}", {column}), '
elif self.connection.vendor == "mysql":
cte = self.CTE_MYSQL_SIMPLE if not use_rank_table else self.CTE_MYSQL
cte_initial = 'CAST(CONCAT("{sep}", {column}, "{sep}") AS char(1000)), '
cte_recursive = 'CONCAT(__tree.{name}, {column}, "{sep}"), '
tree_fields = self.query.get_tree_fields()
qn = self.connection.ops.quote_name
# Generate tree field parameters using unified templates
# Set column reference format based on CTE type
if use_rank_table:
# Complex CTE uses rank table references
column_ref_format = "{column}"
params.update({
"tree_fields_columns": "".join(
f"{qn(column)}, " for column in tree_fields.values()
),
})
else:
# Simple CTE uses direct table references
column_ref_format = "T.{column}"
# Generate unified tree field parameters
params.update({
"tree_fields_names": "".join(f"{qn(name)}, " for name in tree_fields),
"tree_fields_initial": "".join(
cte_initial.format(
column=column_ref_format.format(column=qn(column)),
name=qn(name),
sep=SEPARATOR,
)
for name, column in tree_fields.items()
),
"tree_fields_recursive": "".join(
cte_recursive.format(
column=column_ref_format.format(column=qn(column)),
name=qn(name),
sep=SEPARATOR,
)
for name, column in tree_fields.items()
),
})
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
# When using tree queries in subqueries our base table may use
# an alias. Let's hope using the first alias is correct.
aliases = self.query.table_map.get(tree_params["db_table"])
if aliases:
tree_params["db_table"] = aliases[0]
select = {
"tree_depth": "__tree.tree_depth",
"tree_path": "__tree.tree_path",
"tree_ordering": "__tree.tree_ordering",
}
# Add custom tree fields for both simple and complex CTEs
select.update({name: f"__tree.{name}" for name in tree_fields})
self.query.add_extra(
# Do not add extra fields to the select statement when it is a
# summary query or when using .values() or .values_list()
select={} if skip_tree_fields or self.query.values_select else select,
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 skip_tree_fields or self.query.extra_order_by
else ["__tree.tree_ordering"] # DFS is the only true way
),
)
sql_0, sql_1 = super().as_sql(*args, **kwargs)
explain = ""
if sql_0.startswith("EXPLAIN "):
explain, sql_0 = sql_0.split(" ", 1)
# Pass any additional rank table sql paramaters so that the db backend can handle them.
# This only works because we know that the CTE is at the start of the query.
return (
"".join([explain, cte.format(**params), sql_0]),
(*rank_table_params, *sql_1),
)
def get_converters(self, expressions):
converters = super().get_converters(expressions)
tree_fields = {"__tree.tree_path", "__tree.tree_ordering"} | {
f"__tree.{name}" for name in self.query.tree_fields
}
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_fields:
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
|