Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tsql): SPLIT_PART function and conversion to PARSENAME in tsql #4211

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions sqlglot/dialects/tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,34 @@ def _parse(args: t.List[exp.Expression]) -> exp.Expression:
return _parse


# https://learn.microsoft.com/en-us/sql/t-sql/functions/parsename-transact-sql?view=sql-server-ver16
def _build_parsename(args: t.List) -> t.Optional[exp.SplitPart]:
if len(args) != 2:
return None
arg_this: exp.Literal = seq_get(args, 0) or exp.Literal.string("")
arg_partnum: exp.Literal = seq_get(args, 1) or exp.Literal.number(1)
text = arg_this.this
part_num = int(arg_partnum.this)
length = 1 if isinstance(arg_this, exp.Null) else len(text.split(".")) + 1 # Reverse index
idx = 0 if isinstance(arg_this, exp.Null) else int(part_num)
return exp.SplitPart(
this=arg_this, delimiter=exp.Literal.string("."), part_num=exp.Literal.number(length - idx)
)


def _parsename_sql(self: TSQL.Generator, expression: exp.SplitPart) -> str:
delimiter: exp.Literal = expression.args.get("delimiter") or exp.Literal.string(".")
if delimiter.this != ".":
return str(expression)
arg_this: exp.Literal = expression.args.get("this") or exp.Literal.string("")
arg_part_num: exp.Literal = expression.args.get("part_num") or exp.Literal.number(1)
text = arg_this.this
part_num = int(arg_part_num.this)
length = 1 if isinstance(arg_this, exp.Null) else len(text.split(".")) + 1 # Reverse index
idx = 0 if isinstance(arg_this, exp.Null) else part_num
return self.func("PARSENAME", arg_this, exp.Literal.number(length - idx))


def _build_json_query(args: t.List, dialect: Dialect) -> exp.JSONExtract:
if len(args) == 1:
# The default value for path is '$'. As a result, if you don't provide a
Expand Down Expand Up @@ -543,6 +571,7 @@ class Parser(parser.Parser):
"LEN": _build_with_arg_as_text(exp.Length),
"LEFT": _build_with_arg_as_text(exp.Left),
"RIGHT": _build_with_arg_as_text(exp.Right),
"PARSENAME": _build_parsename,
"REPLICATE": exp.Repeat.from_arg_list,
"SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
"SYSDATETIME": exp.CurrentTimestamp.from_arg_list,
Expand Down Expand Up @@ -886,6 +915,7 @@ class Generator(generator.Generator):
transforms.unnest_generate_date_array_using_recursive_cte,
]
),
exp.SplitPart: _parsename_sql,
exp.Stddev: rename_func("STDEV"),
exp.StrPosition: lambda self, e: self.func(
"CHARINDEX", e.args.get("substr"), e.this, e.args.get("position")
Expand Down
5 changes: 5 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6054,6 +6054,11 @@ class Split(Func):
arg_types = {"this": True, "expression": True, "limit": False}


# https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.split_part.html
class SplitPart(Func):
arg_types = {"this": True, "delimiter": True, "part_num": True}


# Start may be omitted in the case of postgres
# https://www.postgresql.org/docs/9.1/functions-string.html @ Table 9-6
class Substring(Func):
Expand Down
54 changes: 54 additions & 0 deletions tests/dialects/test_tsql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,3 +2009,57 @@ def test_grant(self):
self.validate_identity(
"GRANT EXECUTE ON TestProc TO User2 AS TesterRole", check_command_warning=True
)

def test_parsename(self):
# Test default case
self.validate_all(
"SELECT PARSENAME('1.2.3', 1)",
read={
"spark": "SELECT SPLIT_PART('1.2.3', '.', 3)",
"databricks": "SELECT SPLIT_PART('1.2.3', '.', 3)",
},
write={
"spark": "SELECT SPLIT_PART('1.2.3', '.', 3)",
"databricks": "SELECT SPLIT_PART('1.2.3', '.', 3)",
"tsql": "SELECT PARSENAME('1.2.3', 1)",
},
)
# Test zero index
self.validate_all(
"SELECT PARSENAME('1.2.3', 0)",
read={
"spark": "SELECT SPLIT_PART('1.2.3', '.', 4)",
"databricks": "SELECT SPLIT_PART('1.2.3', '.', 4)",
},
write={
"spark": "SELECT SPLIT_PART('1.2.3', '.', 4)",
"databricks": "SELECT SPLIT_PART('1.2.3', '.', 4)",
"tsql": "SELECT PARSENAME('1.2.3', 0)",
},
)
# Test null value
self.validate_all(
"SELECT PARSENAME(NULL, 1)",
read={
"spark": "SELECT SPLIT_PART(NULL, '.', 1)",
"databricks": "SELECT SPLIT_PART(NULL, '.', 1)",
},
write={
"spark": "SELECT SPLIT_PART(NULL, '.', 1)",
"databricks": "SELECT SPLIT_PART(NULL, '.', 1)",
"tsql": "SELECT PARSENAME(NULL, 1)",
},
)
# Test non-dot delimiter
self.validate_all(
"SELECT SPLIT_PART('1.2.3', ',', 1)",
read={
"spark": "SELECT SPLIT_PART('1.2.3', ',', 1)",
"databricks": "SELECT SPLIT_PART('1.2.3', ',', 1)",
},
write={
"spark": "SELECT SPLIT_PART('1.2.3', ',', 1)",
"databricks": "SELECT SPLIT_PART('1.2.3', ',', 1)",
"tsql": "SELECT SPLIT_PART('1.2.3', ',', 1)",
},
)
Loading