From d3a4e96196cd47858de072ae589c6554088edc24 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 22 Nov 2021 10:59:06 -0500 Subject: Support lightweight compiler column elements w/ slots the _CompileLabel class included ``__slots__`` but these weren't used as the superclasses included slots. Create a ``__slots__`` superclass for ``ClauseElement``, creating a new class of compilable SQL elements that don't include heavier features like caching, annotations and cloning, which are meant to be used only in an ad-hoc compiler fashion. Create new ``CompilerColumnElement`` from that which serves in column-oriented contexts, but similarly does not include any expression operator support as it is intended to be used only to generate a string. Apply this to both ``_CompileLabel`` as well as PostgreSQL ``_ColonCast``, which does not actually subclass ``ColumnElement`` as this class has memoized attributes that aren't worth changing, and does not include SQL operator capabilities as these are not needed for these compiler-only objects. this allows us to more inexpensively add new ad-hoc labels / casts etc. at compile time, as we will be seeking to expand out the typecasts that are needed for PostgreSQL dialects in a subsequent patch. Change-Id: I52973ae3295cb6e2eb0d7adc816c678a626643ed --- test/sql/test_compiler.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/sql') diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 4eef97369..23a2833ca 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -79,6 +79,7 @@ from sqlalchemy.sql import table from sqlalchemy.sql import util as sql_util from sqlalchemy.sql.elements import BooleanClauseList from sqlalchemy.sql.elements import ColumnElement +from sqlalchemy.sql.elements import CompilerColumnElement from sqlalchemy.sql.expression import ClauseElement from sqlalchemy.sql.expression import ClauseList from sqlalchemy.sql.expression import HasPrefixes @@ -215,6 +216,25 @@ class TestCompilerFixture(fixtures.TestBase, AssertsCompiledSQL): class SelectTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "default" + def test_compiler_column_element_is_slots(self): + class SomeColThing(CompilerColumnElement): + __slots__ = ("name",) + __visit_name__ = "some_col_thing" + + def __init__(self, name): + self.name = name + + c1 = SomeColThing("some name") + eq_(c1.name, "some name") + assert not hasattr(c1, "__dict__") + + def test_compile_label_is_slots(self): + + c1 = compiler._CompileLabel(column("q"), "somename") + + eq_(c1.name, "somename") + assert not hasattr(c1, "__dict__") + def test_attribute_sanity(self): assert hasattr(table1, "c") assert hasattr(table1.select().subquery(), "c") -- cgit v1.2.1