Skip to content

Commit

Permalink
fix: include dialect when parsing inside cast (tobymao#3960)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored Aug 23, 2024
1 parent 794dc4c commit c4e5be7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7249,8 +7249,8 @@ def cast(
Returns:
The new Cast instance.
"""
expr = maybe_parse(expression, copy=copy, **opts)
data_type = DataType.build(to, copy=copy, **opts)
expr = maybe_parse(expression, copy=copy, dialect=dialect, **opts)
data_type = DataType.build(to, copy=copy, dialect=dialect, **opts)

# dont re-cast if the expression is already a cast to the correct type
if isinstance(expr, Cast):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ def test_cast(self):
self.assertEqual(len(list(recast.find_all(exp.Cast))), 2)
self.assertEqual(recast.sql(), "CAST(CAST(x AS DATE) AS VARCHAR)")

# check that dialect is used when casting strings
self.assertEqual(
exp.cast("x", to="regtype", dialect="postgres").sql(), "CAST(x AS REGTYPE)"
)
self.assertEqual(exp.cast("`x`", to="date", dialect="hive").sql(), 'CAST("x" AS DATE)')

def test_ctes(self):
expression = parse_one("SELECT a FROM x")
self.assertEqual(expression.ctes, [])
Expand Down

0 comments on commit c4e5be7

Please sign in to comment.