Skip to content

Commit

Permalink
Convert SIGNUM function to SIGN (#3007)
Browse files Browse the repository at this point in the history
  • Loading branch information
acreux committed Feb 22, 2024
1 parent 9840496 commit 9079ead
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sqlglot/dataframe/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def sec(col: ColumnOrName) -> Column:


def signum(col: ColumnOrName) -> Column:
return Column.invoke_anonymous_function(col, "SIGNUM")
return Column.invoke_expression_over_column(col, expression.Sign)


def sin(col: ColumnOrName) -> Column:
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5313,6 +5313,10 @@ class SHA2(Func):
arg_types = {"this": True, "length": False}


class Sign(Func):
_sql_names = ["SIGN", "SIGNUM"]


class SortArray(Func):
arg_types = {"this": True, "asc": False}

Expand Down
1 change: 1 addition & 0 deletions sqlglot/optimizer/annotate_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ class TypeAnnotator(metaclass=_TypeAnnotator):
exp.DateToDi,
exp.Floor,
exp.Levenshtein,
exp.Sign,
exp.StrPosition,
exp.TsOrDiToDi,
},
Expand Down
4 changes: 2 additions & 2 deletions tests/dataframe/unit/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ def test_sec(self):

def test_signum(self):
col_str = SF.signum("cola")
self.assertEqual("SIGNUM(cola)", col_str.sql())
self.assertEqual("SIGN(cola)", col_str.sql())
col = SF.signum(SF.col("cola"))
self.assertEqual("SIGNUM(cola)", col.sql())
self.assertEqual("SIGN(cola)", col.sql())

def test_sin(self):
col_str = SF.sin("cola")
Expand Down
15 changes: 15 additions & 0 deletions tests/dialects/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,3 +1142,18 @@ def test_to_char(self):
"presto": "DATE_FORMAT(ts, '%y')",
},
)

def test_signum(self):
self.validate_all(
"SIGN(x)",
read={
"presto": "SIGN(x)",
"spark": "SIGNUM(x)",
"starrocks": "SIGN(x)",
},
write={
"presto": "SIGN(x)",
"spark": "SIGN(x)",
"starrocks": "SIGN(x)",
},
)

0 comments on commit 9079ead

Please sign in to comment.