Skip to content

Commit

Permalink
Fix(teradata): support TRYCAST (#2496)
Browse files Browse the repository at this point in the history
* Fix(teradata): support `TRYCAST`

See https://docs.teradata.com/r/SQL-Functions-Operators-Expressions-and-Predicates/June-2017/Data-Type-Conversions/TRYCAST

* Update sqlglot/dialects/teradata.py

* Update sqlglot/dialects/teradata.py

---------

Co-authored-by: Toby Mao <[email protected]>
  • Loading branch information
hsheth2 and tobymao authored Oct 31, 2023
1 parent 416a959 commit 471591c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sqlglot/dialects/teradata.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Parser(parser.Parser):

FUNCTION_PARSERS = {
**parser.Parser.FUNCTION_PARSERS,
# https://docs.teradata.com/r/SQL-Functions-Operators-Expressions-and-Predicates/June-2017/Data-Type-Conversions/TRYCAST
"TRYCAST": parser.Parser.FUNCTION_PARSERS["TRY_CAST"],
"RANGE_N": lambda self: self._parse_rangen(),
"TRANSLATE": lambda self: self._parse_translate(self.STRICT_CAST),
}
Expand Down Expand Up @@ -192,6 +194,9 @@ def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) ->

return super().cast_sql(expression, safe_prefix=safe_prefix)

def trycast_sql(self, expression: exp.TryCast) -> str:
return self.cast_sql(expression, safe_prefix="TRY")

def tablesample_sql(
self, expression: exp.TableSample, seed_prefix: str = "SEED", sep=" AS "
) -> str:
Expand Down
11 changes: 11 additions & 0 deletions tests/dialects/test_teradata.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,14 @@ def test_cast(self):
},
)
self.validate_identity("CAST('1992-01' AS FORMAT 'YYYY-DD')")

self.validate_all(
"TRYCAST('-2.5' AS DECIMAL(5, 2))",
read={
"snowflake": "TRY_CAST('-2.5' AS DECIMAL(5, 2))",
},
write={
"snowflake": "TRY_CAST('-2.5' AS DECIMAL(5, 2))",
"teradata": "TRYCAST('-2.5' AS DECIMAL(5, 2))",
},
)

0 comments on commit 471591c

Please sign in to comment.