From e028d984cc5631c66aac5f42c29200410caca47e Mon Sep 17 00:00:00 2001 From: Jo <46752250+GeorgeSittas@users.noreply.github.com> Date: Thu, 8 Jun 2023 03:23:23 +0300 Subject: [PATCH] Feat(redshift,presto): transpile FROM_BASE to STRTOL and vice versa (#1744) * Feat(redshift,presto): transpile FROM_BASE to STRTOL and vice versa * Formatting --- sqlglot/dialects/redshift.py | 3 +++ sqlglot/expressions.py | 4 ++++ tests/dialects/test_redshift.py | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/sqlglot/dialects/redshift.py b/sqlglot/dialects/redshift.py index fda191bfe1..afed1d1ea4 100644 --- a/sqlglot/dialects/redshift.py +++ b/sqlglot/dialects/redshift.py @@ -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 @@ -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 @@ -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 diff --git a/sqlglot/expressions.py b/sqlglot/expressions.py index b061fd4b91..3c6ba2a338 100644 --- a/sqlglot/expressions.py +++ b/sqlglot/expressions.py @@ -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 diff --git a/tests/dialects/test_redshift.py b/tests/dialects/test_redshift.py index b3dc912a86..db5d72a9e1 100644 --- a/tests/dialects/test_redshift.py +++ b/tests/dialects/test_redshift.py @@ -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={