Skip to content

Using try_cast in SQLAlchemy #473

@dwolfeu

Description

@dwolfeu

Expected behavior

sqlalchemy.sql.try_cast should work.

Actual behavior

It doesn't work.

Steps To Reproduce

from sqlalchemy import create_engine, Column, Integer, String, select, try_cast
from sqlalchemy.orm import declarative_base, Session
from trino.auth import OAuth2Authentication

engine = create_engine(
'<url-of-trino-server>',
    connect_args={
        'auth': OAuth2Authentication(),
        'http_scheme': 'https',
        'catalog': '<catalog-name>'
    }
)

Base = declarative_base()

class MyTable(Base):
    __tablename__ = 'foobar'
    __table_args__ = {'schema': 'baz'}
    id = Column(Integer, primary_key=True)

session = Session(engine)
stmt = select(try_cast(MyTable.id,String)).where(MyTable.id.in_([1,2,3]))

for i in session.scalars(stmt):
    print(i)

Log output

AttributeError: 'TrinoSQLCompiler' object has no attribute 'visit_try_cast'

The above exception was the direct cause of the following exception:

# <Lots of detailed error messages>

UnsupportedCompilationError: Compiler <trino.sqlalchemy.compiler.TrinoSQLCompiler object at 0x1696e8d60> can't render element of type <class 'sqlalchemy.sql.elements.TryCast'> (Background on this error at: https://sqlalche.me/e/20/l7de)

Operating System

macOS Sonoma 14.5

Trino Python client version

0.329.0

Trino Server version

426

Python version

3.10.2

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions