compiler.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. # ext/compiler.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. r"""Provides an API for creation of custom ClauseElements and compilers.
  8. Synopsis
  9. ========
  10. Usage involves the creation of one or more
  11. :class:`~sqlalchemy.sql.expression.ClauseElement` subclasses and one or
  12. more callables defining its compilation::
  13. from sqlalchemy.ext.compiler import compiles
  14. from sqlalchemy.sql.expression import ColumnClause
  15. class MyColumn(ColumnClause):
  16. inherit_cache = True
  17. @compiles(MyColumn)
  18. def compile_mycolumn(element, compiler, **kw):
  19. return "[%s]" % element.name
  20. Above, ``MyColumn`` extends :class:`~sqlalchemy.sql.expression.ColumnClause`,
  21. the base expression element for named column objects. The ``compiles``
  22. decorator registers itself with the ``MyColumn`` class so that it is invoked
  23. when the object is compiled to a string::
  24. from sqlalchemy import select
  25. s = select(MyColumn("x"), MyColumn("y"))
  26. print(str(s))
  27. Produces:
  28. .. sourcecode:: sql
  29. SELECT [x], [y]
  30. Dialect-specific compilation rules
  31. ==================================
  32. Compilers can also be made dialect-specific. The appropriate compiler will be
  33. invoked for the dialect in use::
  34. from sqlalchemy.schema import DDLElement
  35. class AlterColumn(DDLElement):
  36. inherit_cache = False
  37. def __init__(self, column, cmd):
  38. self.column = column
  39. self.cmd = cmd
  40. @compiles(AlterColumn)
  41. def visit_alter_column(element, compiler, **kw):
  42. return "ALTER COLUMN %s ..." % element.column.name
  43. @compiles(AlterColumn, "postgresql")
  44. def visit_alter_column(element, compiler, **kw):
  45. return "ALTER TABLE %s ALTER COLUMN %s ..." % (
  46. element.table.name,
  47. element.column.name,
  48. )
  49. The second ``visit_alter_table`` will be invoked when any ``postgresql``
  50. dialect is used.
  51. .. _compilerext_compiling_subelements:
  52. Compiling sub-elements of a custom expression construct
  53. =======================================================
  54. The ``compiler`` argument is the
  55. :class:`~sqlalchemy.engine.interfaces.Compiled` object in use. This object
  56. can be inspected for any information about the in-progress compilation,
  57. including ``compiler.dialect``, ``compiler.statement`` etc. The
  58. :class:`~sqlalchemy.sql.compiler.SQLCompiler` and
  59. :class:`~sqlalchemy.sql.compiler.DDLCompiler` both include a ``process()``
  60. method which can be used for compilation of embedded attributes::
  61. from sqlalchemy.sql.expression import Executable, ClauseElement
  62. class InsertFromSelect(Executable, ClauseElement):
  63. inherit_cache = False
  64. def __init__(self, table, select):
  65. self.table = table
  66. self.select = select
  67. @compiles(InsertFromSelect)
  68. def visit_insert_from_select(element, compiler, **kw):
  69. return "INSERT INTO %s (%s)" % (
  70. compiler.process(element.table, asfrom=True, **kw),
  71. compiler.process(element.select, **kw),
  72. )
  73. insert = InsertFromSelect(t1, select(t1).where(t1.c.x > 5))
  74. print(insert)
  75. Produces (formatted for readability):
  76. .. sourcecode:: sql
  77. INSERT INTO mytable (
  78. SELECT mytable.x, mytable.y, mytable.z
  79. FROM mytable
  80. WHERE mytable.x > :x_1
  81. )
  82. .. note::
  83. The above ``InsertFromSelect`` construct is only an example, this actual
  84. functionality is already available using the
  85. :meth:`_expression.Insert.from_select` method.
  86. Cross Compiling between SQL and DDL compilers
  87. ---------------------------------------------
  88. SQL and DDL constructs are each compiled using different base compilers -
  89. ``SQLCompiler`` and ``DDLCompiler``. A common need is to access the
  90. compilation rules of SQL expressions from within a DDL expression. The
  91. ``DDLCompiler`` includes an accessor ``sql_compiler`` for this reason, such as
  92. below where we generate a CHECK constraint that embeds a SQL expression::
  93. @compiles(MyConstraint)
  94. def compile_my_constraint(constraint, ddlcompiler, **kw):
  95. kw["literal_binds"] = True
  96. return "CONSTRAINT %s CHECK (%s)" % (
  97. constraint.name,
  98. ddlcompiler.sql_compiler.process(constraint.expression, **kw),
  99. )
  100. Above, we add an additional flag to the process step as called by
  101. :meth:`.SQLCompiler.process`, which is the ``literal_binds`` flag. This
  102. indicates that any SQL expression which refers to a :class:`.BindParameter`
  103. object or other "literal" object such as those which refer to strings or
  104. integers should be rendered **in-place**, rather than being referred to as
  105. a bound parameter; when emitting DDL, bound parameters are typically not
  106. supported.
  107. Changing the default compilation of existing constructs
  108. =======================================================
  109. The compiler extension applies just as well to the existing constructs. When
  110. overriding the compilation of a built in SQL construct, the @compiles
  111. decorator is invoked upon the appropriate class (be sure to use the class,
  112. i.e. ``Insert`` or ``Select``, instead of the creation function such
  113. as ``insert()`` or ``select()``).
  114. Within the new compilation function, to get at the "original" compilation
  115. routine, use the appropriate visit_XXX method - this
  116. because compiler.process() will call upon the overriding routine and cause
  117. an endless loop. Such as, to add "prefix" to all insert statements::
  118. from sqlalchemy.sql.expression import Insert
  119. @compiles(Insert)
  120. def prefix_inserts(insert, compiler, **kw):
  121. return compiler.visit_insert(insert.prefix_with("some prefix"), **kw)
  122. The above compiler will prefix all INSERT statements with "some prefix" when
  123. compiled.
  124. .. _type_compilation_extension:
  125. Changing Compilation of Types
  126. =============================
  127. ``compiler`` works for types, too, such as below where we implement the
  128. MS-SQL specific 'max' keyword for ``String``/``VARCHAR``::
  129. @compiles(String, "mssql")
  130. @compiles(VARCHAR, "mssql")
  131. def compile_varchar(element, compiler, **kw):
  132. if element.length == "max":
  133. return "VARCHAR('max')"
  134. else:
  135. return compiler.visit_VARCHAR(element, **kw)
  136. foo = Table("foo", metadata, Column("data", VARCHAR("max")))
  137. Subclassing Guidelines
  138. ======================
  139. A big part of using the compiler extension is subclassing SQLAlchemy
  140. expression constructs. To make this easier, the expression and
  141. schema packages feature a set of "bases" intended for common tasks.
  142. A synopsis is as follows:
  143. * :class:`~sqlalchemy.sql.expression.ClauseElement` - This is the root
  144. expression class. Any SQL expression can be derived from this base, and is
  145. probably the best choice for longer constructs such as specialized INSERT
  146. statements.
  147. * :class:`~sqlalchemy.sql.expression.ColumnElement` - The root of all
  148. "column-like" elements. Anything that you'd place in the "columns" clause of
  149. a SELECT statement (as well as order by and group by) can derive from this -
  150. the object will automatically have Python "comparison" behavior.
  151. :class:`~sqlalchemy.sql.expression.ColumnElement` classes want to have a
  152. ``type`` member which is expression's return type. This can be established
  153. at the instance level in the constructor, or at the class level if its
  154. generally constant::
  155. class timestamp(ColumnElement):
  156. type = TIMESTAMP()
  157. inherit_cache = True
  158. * :class:`~sqlalchemy.sql.functions.FunctionElement` - This is a hybrid of a
  159. ``ColumnElement`` and a "from clause" like object, and represents a SQL
  160. function or stored procedure type of call. Since most databases support
  161. statements along the line of "SELECT FROM <some function>"
  162. ``FunctionElement`` adds in the ability to be used in the FROM clause of a
  163. ``select()`` construct::
  164. from sqlalchemy.sql.expression import FunctionElement
  165. class coalesce(FunctionElement):
  166. name = "coalesce"
  167. inherit_cache = True
  168. @compiles(coalesce)
  169. def compile(element, compiler, **kw):
  170. return "coalesce(%s)" % compiler.process(element.clauses, **kw)
  171. @compiles(coalesce, "oracle")
  172. def compile(element, compiler, **kw):
  173. if len(element.clauses) > 2:
  174. raise TypeError(
  175. "coalesce only supports two arguments on " "Oracle Database"
  176. )
  177. return "nvl(%s)" % compiler.process(element.clauses, **kw)
  178. * :class:`.ExecutableDDLElement` - The root of all DDL expressions,
  179. like CREATE TABLE, ALTER TABLE, etc. Compilation of
  180. :class:`.ExecutableDDLElement` subclasses is issued by a
  181. :class:`.DDLCompiler` instead of a :class:`.SQLCompiler`.
  182. :class:`.ExecutableDDLElement` can also be used as an event hook in
  183. conjunction with event hooks like :meth:`.DDLEvents.before_create` and
  184. :meth:`.DDLEvents.after_create`, allowing the construct to be invoked
  185. automatically during CREATE TABLE and DROP TABLE sequences.
  186. .. seealso::
  187. :ref:`metadata_ddl_toplevel` - contains examples of associating
  188. :class:`.DDL` objects (which are themselves :class:`.ExecutableDDLElement`
  189. instances) with :class:`.DDLEvents` event hooks.
  190. * :class:`~sqlalchemy.sql.expression.Executable` - This is a mixin which
  191. should be used with any expression class that represents a "standalone"
  192. SQL statement that can be passed directly to an ``execute()`` method. It
  193. is already implicit within ``DDLElement`` and ``FunctionElement``.
  194. Most of the above constructs also respond to SQL statement caching. A
  195. subclassed construct will want to define the caching behavior for the object,
  196. which usually means setting the flag ``inherit_cache`` to the value of
  197. ``False`` or ``True``. See the next section :ref:`compilerext_caching`
  198. for background.
  199. .. _compilerext_caching:
  200. Enabling Caching Support for Custom Constructs
  201. ==============================================
  202. SQLAlchemy as of version 1.4 includes a
  203. :ref:`SQL compilation caching facility <sql_caching>` which will allow
  204. equivalent SQL constructs to cache their stringified form, along with other
  205. structural information used to fetch results from the statement.
  206. For reasons discussed at :ref:`caching_caveats`, the implementation of this
  207. caching system takes a conservative approach towards including custom SQL
  208. constructs and/or subclasses within the caching system. This includes that
  209. any user-defined SQL constructs, including all the examples for this
  210. extension, will not participate in caching by default unless they positively
  211. assert that they are able to do so. The :attr:`.HasCacheKey.inherit_cache`
  212. attribute when set to ``True`` at the class level of a specific subclass
  213. will indicate that instances of this class may be safely cached, using the
  214. cache key generation scheme of the immediate superclass. This applies
  215. for example to the "synopsis" example indicated previously::
  216. class MyColumn(ColumnClause):
  217. inherit_cache = True
  218. @compiles(MyColumn)
  219. def compile_mycolumn(element, compiler, **kw):
  220. return "[%s]" % element.name
  221. Above, the ``MyColumn`` class does not include any new state that
  222. affects its SQL compilation; the cache key of ``MyColumn`` instances will
  223. make use of that of the ``ColumnClause`` superclass, meaning it will take
  224. into account the class of the object (``MyColumn``), the string name and
  225. datatype of the object::
  226. >>> MyColumn("some_name", String())._generate_cache_key()
  227. CacheKey(
  228. key=('0', <class '__main__.MyColumn'>,
  229. 'name', 'some_name',
  230. 'type', (<class 'sqlalchemy.sql.sqltypes.String'>,
  231. ('length', None), ('collation', None))
  232. ), bindparams=[])
  233. For objects that are likely to be **used liberally as components within many
  234. larger statements**, such as :class:`_schema.Column` subclasses and custom SQL
  235. datatypes, it's important that **caching be enabled as much as possible**, as
  236. this may otherwise negatively affect performance.
  237. An example of an object that **does** contain state which affects its SQL
  238. compilation is the one illustrated at :ref:`compilerext_compiling_subelements`;
  239. this is an "INSERT FROM SELECT" construct that combines together a
  240. :class:`_schema.Table` as well as a :class:`_sql.Select` construct, each of
  241. which independently affect the SQL string generation of the construct. For
  242. this class, the example illustrates that it simply does not participate in
  243. caching::
  244. class InsertFromSelect(Executable, ClauseElement):
  245. inherit_cache = False
  246. def __init__(self, table, select):
  247. self.table = table
  248. self.select = select
  249. @compiles(InsertFromSelect)
  250. def visit_insert_from_select(element, compiler, **kw):
  251. return "INSERT INTO %s (%s)" % (
  252. compiler.process(element.table, asfrom=True, **kw),
  253. compiler.process(element.select, **kw),
  254. )
  255. While it is also possible that the above ``InsertFromSelect`` could be made to
  256. produce a cache key that is composed of that of the :class:`_schema.Table` and
  257. :class:`_sql.Select` components together, the API for this is not at the moment
  258. fully public. However, for an "INSERT FROM SELECT" construct, which is only
  259. used by itself for specific operations, caching is not as critical as in the
  260. previous example.
  261. For objects that are **used in relative isolation and are generally
  262. standalone**, such as custom :term:`DML` constructs like an "INSERT FROM
  263. SELECT", **caching is generally less critical** as the lack of caching for such
  264. a construct will have only localized implications for that specific operation.
  265. Further Examples
  266. ================
  267. "UTC timestamp" function
  268. -------------------------
  269. A function that works like "CURRENT_TIMESTAMP" except applies the
  270. appropriate conversions so that the time is in UTC time. Timestamps are best
  271. stored in relational databases as UTC, without time zones. UTC so that your
  272. database doesn't think time has gone backwards in the hour when daylight
  273. savings ends, without timezones because timezones are like character
  274. encodings - they're best applied only at the endpoints of an application
  275. (i.e. convert to UTC upon user input, re-apply desired timezone upon display).
  276. For PostgreSQL and Microsoft SQL Server::
  277. from sqlalchemy.sql import expression
  278. from sqlalchemy.ext.compiler import compiles
  279. from sqlalchemy.types import DateTime
  280. class utcnow(expression.FunctionElement):
  281. type = DateTime()
  282. inherit_cache = True
  283. @compiles(utcnow, "postgresql")
  284. def pg_utcnow(element, compiler, **kw):
  285. return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
  286. @compiles(utcnow, "mssql")
  287. def ms_utcnow(element, compiler, **kw):
  288. return "GETUTCDATE()"
  289. Example usage::
  290. from sqlalchemy import Table, Column, Integer, String, DateTime, MetaData
  291. metadata = MetaData()
  292. event = Table(
  293. "event",
  294. metadata,
  295. Column("id", Integer, primary_key=True),
  296. Column("description", String(50), nullable=False),
  297. Column("timestamp", DateTime, server_default=utcnow()),
  298. )
  299. "GREATEST" function
  300. -------------------
  301. The "GREATEST" function is given any number of arguments and returns the one
  302. that is of the highest value - its equivalent to Python's ``max``
  303. function. A SQL standard version versus a CASE based version which only
  304. accommodates two arguments::
  305. from sqlalchemy.sql import expression, case
  306. from sqlalchemy.ext.compiler import compiles
  307. from sqlalchemy.types import Numeric
  308. class greatest(expression.FunctionElement):
  309. type = Numeric()
  310. name = "greatest"
  311. inherit_cache = True
  312. @compiles(greatest)
  313. def default_greatest(element, compiler, **kw):
  314. return compiler.visit_function(element)
  315. @compiles(greatest, "sqlite")
  316. @compiles(greatest, "mssql")
  317. @compiles(greatest, "oracle")
  318. def case_greatest(element, compiler, **kw):
  319. arg1, arg2 = list(element.clauses)
  320. return compiler.process(case((arg1 > arg2, arg1), else_=arg2), **kw)
  321. Example usage::
  322. Session.query(Account).filter(
  323. greatest(Account.checking_balance, Account.savings_balance) > 10000
  324. )
  325. "false" expression
  326. ------------------
  327. Render a "false" constant expression, rendering as "0" on platforms that
  328. don't have a "false" constant::
  329. from sqlalchemy.sql import expression
  330. from sqlalchemy.ext.compiler import compiles
  331. class sql_false(expression.ColumnElement):
  332. inherit_cache = True
  333. @compiles(sql_false)
  334. def default_false(element, compiler, **kw):
  335. return "false"
  336. @compiles(sql_false, "mssql")
  337. @compiles(sql_false, "mysql")
  338. @compiles(sql_false, "oracle")
  339. def int_false(element, compiler, **kw):
  340. return "0"
  341. Example usage::
  342. from sqlalchemy import select, union_all
  343. exp = union_all(
  344. select(users.c.name, sql_false().label("enrolled")),
  345. select(customers.c.name, customers.c.enrolled),
  346. )
  347. """
  348. from __future__ import annotations
  349. from typing import Any
  350. from typing import Callable
  351. from typing import Dict
  352. from typing import Type
  353. from typing import TYPE_CHECKING
  354. from typing import TypeVar
  355. from .. import exc
  356. from ..sql import sqltypes
  357. if TYPE_CHECKING:
  358. from ..sql.compiler import SQLCompiler
  359. _F = TypeVar("_F", bound=Callable[..., Any])
  360. def compiles(class_: Type[Any], *specs: str) -> Callable[[_F], _F]:
  361. """Register a function as a compiler for a
  362. given :class:`_expression.ClauseElement` type."""
  363. def decorate(fn: _F) -> _F:
  364. # get an existing @compiles handler
  365. existing = class_.__dict__.get("_compiler_dispatcher", None)
  366. # get the original handler. All ClauseElement classes have one
  367. # of these, but some TypeEngine classes will not.
  368. existing_dispatch = getattr(class_, "_compiler_dispatch", None)
  369. if not existing:
  370. existing = _dispatcher()
  371. if existing_dispatch:
  372. def _wrap_existing_dispatch(
  373. element: Any, compiler: SQLCompiler, **kw: Any
  374. ) -> Any:
  375. try:
  376. return existing_dispatch(element, compiler, **kw)
  377. except exc.UnsupportedCompilationError as uce:
  378. raise exc.UnsupportedCompilationError(
  379. compiler,
  380. type(element),
  381. message="%s construct has no default "
  382. "compilation handler." % type(element),
  383. ) from uce
  384. existing.specs["default"] = _wrap_existing_dispatch
  385. # TODO: why is the lambda needed ?
  386. setattr(
  387. class_,
  388. "_compiler_dispatch",
  389. lambda *arg, **kw: existing(*arg, **kw),
  390. )
  391. setattr(class_, "_compiler_dispatcher", existing)
  392. if specs:
  393. for s in specs:
  394. existing.specs[s] = fn
  395. else:
  396. existing.specs["default"] = fn
  397. return fn
  398. return decorate
  399. def deregister(class_: Type[Any]) -> None:
  400. """Remove all custom compilers associated with a given
  401. :class:`_expression.ClauseElement` type.
  402. """
  403. if hasattr(class_, "_compiler_dispatcher"):
  404. class_._compiler_dispatch = class_._original_compiler_dispatch
  405. del class_._compiler_dispatcher
  406. class _dispatcher:
  407. def __init__(self) -> None:
  408. self.specs: Dict[str, Callable[..., Any]] = {}
  409. def __call__(self, element: Any, compiler: SQLCompiler, **kw: Any) -> Any:
  410. # TODO: yes, this could also switch off of DBAPI in use.
  411. fn = self.specs.get(compiler.dialect.name, None)
  412. if not fn:
  413. try:
  414. fn = self.specs["default"]
  415. except KeyError as ke:
  416. raise exc.UnsupportedCompilationError(
  417. compiler,
  418. type(element),
  419. message="%s construct has no default "
  420. "compilation handler." % type(element),
  421. ) from ke
  422. # if compilation includes add_to_result_map, collect add_to_result_map
  423. # arguments from the user-defined callable, which are probably none
  424. # because this is not public API. if it wasn't called, then call it
  425. # ourselves.
  426. arm = kw.get("add_to_result_map", None)
  427. if arm:
  428. arm_collection = []
  429. kw["add_to_result_map"] = lambda *args: arm_collection.append(args)
  430. expr = fn(element, compiler, **kw)
  431. if arm:
  432. if not arm_collection:
  433. arm_collection.append(
  434. (None, None, (element,), sqltypes.NULLTYPE)
  435. )
  436. for tup in arm_collection:
  437. arm(*tup)
  438. return expr