Skip to content

Commit 97c5a7f

Browse files
dwolfeuhashhar
authored andcommitted
Add visit_try_cast to sqlalchemy compiler
1 parent 560556b commit 97c5a7f

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

tests/unit/sqlalchemy/test_compiler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,14 @@ def test_ignore_nulls(dialect, function, element):
151151
assert str(query) == \
152152
f'SELECT {function}("table".id) OVER (PARTITION BY "table".name) AS window ' \
153153
f'\nFROM "table"'
154+
155+
156+
@pytest.mark.skipif(
157+
sqlalchemy_version() < "2.0",
158+
reason="ImportError: cannot import name 'try_cast' from 'sqlalchemy'"
159+
)
160+
def test_try_cast(dialect):
161+
from sqlalchemy import try_cast
162+
statement = select(try_cast(table_without_catalog.c.id, String))
163+
query = statement.compile(dialect=dialect)
164+
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'

trino/sqlalchemy/compiler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def compile_ignore_nulls(element, compiler, **kwargs):
175175
compiled += ' IGNORE NULLS'
176176
return compiled
177177

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

179182
class TrinoDDLCompiler(compiler.DDLCompiler):
180183
pass

0 commit comments

Comments
 (0)