Skip to content

Commit

Permalink
Feat(redshift,presto): transpile FROM_BASE to STRTOL and vice versa (#…
Browse files Browse the repository at this point in the history
…1744)

* Feat(redshift,presto): transpile FROM_BASE to STRTOL and vice versa

* Formatting
  • Loading branch information
georgesittas authored Jun 8, 2023
1 parent 6168fbf commit e028d98
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sqlglot/dialects/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import typing as t

from sqlglot import exp, transforms
from sqlglot.dialects.dialect import rename_func
from sqlglot.dialects.postgres import Postgres
from sqlglot.helper import seq_get
from sqlglot.tokens import TokenType
Expand Down Expand Up @@ -34,6 +35,7 @@ class Parser(Postgres.Parser):
unit=seq_get(args, 0),
),
"NVL": exp.Coalesce.from_arg_list,
"STRTOL": exp.FromBase.from_arg_list,
}

CONVERT_TYPE_FIRST = True
Expand Down Expand Up @@ -105,6 +107,7 @@ class Generator(Postgres.Generator):
exp.JSONExtractScalar: _json_sql,
exp.Select: transforms.preprocess([transforms.eliminate_distinct_on]),
exp.SortKeyProperty: lambda self, e: f"{'COMPOUND ' if e.args['compound'] else ''}SORTKEY({self.format_args(*e.this)})",
exp.FromBase: rename_func("STRTOL"),
}

# Postgres maps exp.Pivot to no_pivot_sql, but Redshift support pivots
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,10 @@ class NumberToStr(Func):
arg_types = {"this": True, "format": True}


class FromBase(Func):
arg_types = {"this": True, "expression": True}


class Struct(Func):
arg_types = {"expressions": True}
is_var_len_args = True
Expand Down
10 changes: 10 additions & 0 deletions tests/dialects/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ def test_redshift(self):
self.validate_identity("foo$")
self.validate_identity("$foo")

self.validate_all(
"SELECT STRTOL('abc', 16)",
read={
"trino": "SELECT FROM_BASE('abc', 16)",
},
write={
"redshift": "SELECT STRTOL('abc', 16)",
"trino": "SELECT FROM_BASE('abc', 16)",
},
)
self.validate_all(
"SELECT SNAPSHOT, type",
write={
Expand Down

0 comments on commit e028d98

Please sign in to comment.