Skip to content

Commit 9e7f158

Browse files
NO-SNOW: Support udxf decorators without an initialized session
Signed-off-by: Devin Petersohn <devin.petersohn@snowflake.com>
1 parent 22d1174 commit 9e7f158

2 files changed

Lines changed: 39 additions & 17 deletions

File tree

src/snowflake/snowpark/functions.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
check_create_map_parameter,
219219
deprecated,
220220
private_preview,
221+
warning,
221222
)
222223
from snowflake.snowpark._functions.scalar_functions import * # noqa: F403,F401
223224
from snowflake.snowpark.column import (
@@ -228,10 +229,7 @@
228229
_to_col_if_str,
229230
_to_col_if_str_or_int,
230231
)
231-
from snowflake.snowpark.stored_procedure import (
232-
StoredProcedure,
233-
StoredProcedureRegistration,
234-
)
232+
from snowflake.snowpark.stored_procedure import StoredProcedure
235233
from snowflake.snowpark.types import (
236234
ArrayType,
237235
DataType,
@@ -242,9 +240,9 @@
242240
TimestampTimeZone,
243241
TimestampType,
244242
)
245-
from snowflake.snowpark.udaf import UDAFRegistration, UserDefinedAggregateFunction
246-
from snowflake.snowpark.udf import UDFRegistration, UserDefinedFunction
247-
from snowflake.snowpark.udtf import UDTFRegistration, UserDefinedTableFunction
243+
from snowflake.snowpark.udaf import UserDefinedAggregateFunction
244+
from snowflake.snowpark.udf import UserDefinedFunction
245+
from snowflake.snowpark.udtf import UserDefinedTableFunction
248246

249247
# Python 3.8 needs to use typing.Iterable because collections.abc.Iterable is not subscriptable
250248
# Python 3.9 can use both
@@ -9295,7 +9293,7 @@ def udf(
92959293
resource_constraint: Optional[Dict[str, str]] = None,
92969294
_emit_ast: bool = True,
92979295
**kwargs,
9298-
) -> Union[UserDefinedFunction, functools.partial]:
9296+
) -> Union[UserDefinedFunction, functools.partial, Callable]:
92999297
"""Registers a Python function as a Snowflake Python UDF and returns the UDF.
93009298

93019299
It can be used as either a function call or a decorator. In most cases you work with a single session.
@@ -9462,7 +9460,13 @@ def udf(
94629460
)
94639461

94649462
if session is None:
9465-
udf_registration_method = UDFRegistration(session=session).register
9463+
warning(
9464+
name=f"udf_no_active_session_{func.__name__ if func else 'unknown'}",
9465+
text="WARN: UDF registration requires an active session. "
9466+
"This function was not registered.",
9467+
warning_times=1,
9468+
)
9469+
return func
94669470
else:
94679471
udf_registration_method = session.udf.register
94689472

@@ -9548,7 +9552,7 @@ def udtf(
95489552
resource_constraint: Optional[Dict[str, str]] = None,
95499553
_emit_ast: bool = True,
95509554
**kwargs,
9551-
) -> Union[UserDefinedTableFunction, functools.partial]:
9555+
) -> Union[UserDefinedTableFunction, functools.partial, Callable]:
95529556
"""Registers a Python class as a Snowflake Python UDTF and returns the UDTF.
95539557

95549558
It can be used as either a function call or a decorator. In most cases you work with a single session.
@@ -9712,7 +9716,13 @@ def udtf(
97129716
session
97139717
)
97149718
if session is None:
9715-
udtf_registration_method = UDTFRegistration(session=session).register
9719+
warning(
9720+
name=f"udtf_no_active_session_{handler.__name__ if handler else 'unknown'}",
9721+
text="WARN: UDTF registration requires an active session. "
9722+
"This function was not registered.",
9723+
warning_times=1,
9724+
)
9725+
return handler
97169726
else:
97179727
udtf_registration_method = session.udtf.register
97189728

@@ -9964,7 +9974,13 @@ def udaf(
99649974
session
99659975
)
99669976
if session is None:
9967-
udaf_registration_method = UDAFRegistration(session=session).register
9977+
warning(
9978+
name=f"udaf_no_active_session_{handler.__name__ if handler else 'unknown'}",
9979+
text="WARN: UDAF registration requires an active session. "
9980+
"This function was not registered.",
9981+
warning_times=1,
9982+
)
9983+
return handler
99689984
else:
99699985
udaf_registration_method = session.udaf.register
99709986

@@ -10660,9 +10676,13 @@ def sproc(
1066010676
session
1066110677
)
1066210678
if session is None:
10663-
sproc_registration_method = StoredProcedureRegistration(
10664-
session=session
10665-
).register
10679+
warning(
10680+
name=f"sproc_no_active_session_{func.__name__ if func else 'unknown'}",
10681+
text="WARN: SPROC registration requires an active session. "
10682+
"This function was not registered.",
10683+
warning_times=1,
10684+
)
10685+
return func
1066610686
else:
1066710687
sproc_registration_method = session.sproc.register
1066810688

src/snowflake/snowpark/session.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,14 @@ def _add_session(session: "Session") -> None:
342342
_active_sessions.add(session)
343343

344344

345-
def _get_sandbox_conditional_active_session(session: "Session") -> "Session":
345+
def _get_sandbox_conditional_active_session(session: "Session") -> Optional["Session"]:
346346
# Precedence to checking sandbox to avoid any side effects
347347
if _is_execution_environment_sandboxed_for_client:
348348
session = None
349349
else:
350-
session = session or _get_active_session()
350+
session = (
351+
session or (len(_active_sessions) > 0 and _get_active_session()) or None
352+
)
351353
return session
352354

353355

0 commit comments

Comments
 (0)