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

SNOW-1857250: More fixes to AST generation in functions.py #2793

Open
wants to merge 1 commit 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
38 changes: 32 additions & 6 deletions src/snowflake/snowpark/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,17 +752,31 @@ def convert_timezone(
)
target_tz = _to_col_if_str(target_timezone, "convert_timezone")
source_time_to_convert = _to_col_if_str(source_time, "convert_timezone")

# Build AST here to prevent rearrangement of args in the encoded AST.
ast = (
build_function_expr(
"convert_timezone",
[target_timezone, source_time, source_timezone],
ignore_null_args=True,
)
if _emit_ast
else None
)
if source_timezone is None:
return call_builtin(
"convert_timezone", target_tz, source_time_to_convert, _emit_ast=_emit_ast
"convert_timezone",
target_tz,
source_time_to_convert,
_ast=ast,
_emit_ast=False,
)
return call_builtin(
"convert_timezone",
source_tz,
target_tz,
source_time_to_convert,
_emit_ast=_emit_ast,
_ast=ast,
_emit_ast=False,
)


Expand Down Expand Up @@ -888,7 +902,7 @@ def count_distinct(*cols: ColumnOrName, _emit_ast: bool = True) -> Column:
return Column(
FunctionExpression("count", [c._expression for c in cs], is_distinct=True),
_ast=ast,
_emit_ast=_emit_ast,
_emit_ast=False,
)


Expand Down Expand Up @@ -4262,7 +4276,12 @@ def next_day(
[Row(NEXT_DAY("A", 'FR')=datetime.date(2020, 8, 7)), Row(NEXT_DAY("A", 'FR')=datetime.date(2020, 12, 4))]
"""
c = _to_col_if_str(date, "next_day")
return builtin("next_day", _emit_ast=_emit_ast)(c, Column._to_expr(day_of_week))
# Build AST here to prevent `date` from being recorded as a Column instead of a literal and
# `day_of_week` from being recorded as a literal instead of Column.
ast = build_function_expr("next_day", [date, day_of_week]) if _emit_ast else None
return builtin("next_day", _ast=ast, _emit_ast=False)(
c, Column._to_expr(day_of_week)
)


@publicapi
Expand All @@ -4285,7 +4304,14 @@ def previous_day(
[Row(PREVIOUS_DAY("A", 'FR')=datetime.date(2020, 7, 31)), Row(PREVIOUS_DAY("A", 'FR')=datetime.date(2020, 11, 27))]
"""
c = _to_col_if_str(date, "previous_day")
return builtin("previous_day", _emit_ast=_emit_ast)(c, Column._to_expr(day_of_week))
# Build AST here to prevent `date` from being recorded as a Column instead of a literal and
# `day_of_week` from being recorded as a literal instead of Column.
ast = (
build_function_expr("previous_day", [date, day_of_week]) if _emit_ast else None
)
return builtin("previous_day", _ast=ast, _emit_ast=False)(
c, Column._to_expr(day_of_week)
)


@publicapi
Expand Down
Loading
Loading