Skip to content

col_expression does not apply to UNION and set ops correctly #4787

Closed
@zzzeek

Description

@zzzeek
Member

when a compoundselect is invoked directly, the columns of all the subsequent SELECTs need to have col_expression applied:


from sqlalchemy import cast
from sqlalchemy import Column
from sqlalchemy import DateTime
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import TypeDecorator


class StringDate(TypeDecorator):
    impl = DateTime

    def column_expression(self, col):
        return cast(col, String)


meta = MetaData()
t = Table(
    "t",
    meta,
    Column("Id", Integer, primary_key=True),
    Column("x", StringDate),
)

t_history = Table(
    "t_history",
    meta,
    Column("Id", Integer),
    Column("x", StringDate),
)

print(t.select().union_all(t_history.select()))

returns:

SELECT t."Id", CAST(t.x AS VARCHAR) AS x 
FROM t UNION ALL SELECT t_history."Id", t_history.x 
FROM t_history

workaround is of course to call select() from that:

print(t.select().union_all(t_history.select()))

which is why this is not an ORM issue

Activity

added this to the 1.3.xx milestone on Jul 29, 2019
sqla-tester

sqla-tester commented on Jul 29, 2019

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the master branch:

Invoke column_expression() for subsequent SELECTs in CompoundSelect https://gerrit.sqlalchemy.org/1383

sqla-tester

sqla-tester commented on Jul 29, 2019

@sqla-tester
Collaborator

Mike Bayer has proposed a fix for this issue in the rel_1_3 branch:

Invoke column_expression() for subsequent SELECTs in CompoundSelect https://gerrit.sqlalchemy.org/1384

added a commit that references this issue on Jul 30, 2019
50bd166
modified the milestones: 1.3.xx, 1.3.x on Dec 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @zzzeek@sqla-tester

        Issue actions

          col_expression does not apply to UNION and set ops correctly · Issue #4787 · sqlalchemy/sqlalchemy