Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/unit/sqlalchemy/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,14 @@ def test_ignore_nulls(dialect, function, element):
assert str(query) == \
f'SELECT {function}("table".id) OVER (PARTITION BY "table".name) AS window ' \
f'\nFROM "table"'


@pytest.mark.skipif(
sqlalchemy_version() < "2.0",
reason="ImportError: cannot import name 'try_cast' from 'sqlalchemy'"
)
def test_try_cast(dialect):
from sqlalchemy import try_cast
statement = select(try_cast(table_without_catalog.c.id, String))
query = statement.compile(dialect=dialect)
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'
3 changes: 3 additions & 0 deletions trino/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def compile_ignore_nulls(element, compiler, **kwargs):
compiled += ' IGNORE NULLS'
return compiled

def visit_try_cast(self, element, **kw):
return f"try_cast({self.process(element.clause, **kw)} as {self.process(element.typeclause, **kw)})"


class TrinoDDLCompiler(compiler.DDLCompiler):
pass
Expand Down