
    ɯei"                        d Z ddlZddlZddlmZ ddlmZmZmZmZm	Z	m
Z
mZ ddlZddlmc mc mc mc mZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ dd
l m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ejn                  dk  rddlm8Z8 nddl9m8Z8  G d d      Z: G d d      Z;y)zStored procedures in Snowpark. Refer to :class:`~snowflake.snowpark.stored_procedure.StoredProcedure` for details and sample code.    N)
ModuleType)AnyCallableDictListOptionalTupleUnion)ProgrammingError)build_sprocbuild_sproc_applywith_src_position)SnowparkClientExceptionMessages)"open_telemetry_udf_context_manager)TelemetryField)convert_sp_to_sf_type)	UDFColumnRegistrationType&add_snowpark_package_to_sproc_packagescheck_execute_as_argcheck_python_runtime_versioncheck_register_args%cleanup_failed_permanent_registrationcreate_python_udf_or_sp generate_anonymous_python_sp_sqlgenerate_call_python_sp_sqlprocess_file_pathprocess_registration_inputsresolve_imports_and_packages)TempObjectTypecheck_imports_type	publicapi)DataType
StructType)   	   )Iterablec                   4   e Zd ZdZ	 	 	 	 	 	 ddededee   dedej                  d   d	e
e   d
e
eeeef         de
ej                     de
e   de
ej                      ddfdZ	 ddee   de
d   fdZeddddddede
d   de
eeef      dededefd       Zy)StoredProcedurea%  
    Encapsulates a user defined lambda or function that is returned by
    :func:`~snowflake.snowpark.functions.sproc`, :meth:`StoredProcedureRegistration.register`
    or :meth:`StoredProcedureRegistration.register_from_file`. The constructor
    of this class is not supposed to be called directly.

    Call an instance of :class:`StoredProcedure` to invoke a stored procedure.
    The input should be Python literal values.

    See Also:
        - :class:`StoredProcedureRegistration`
        - :func:`~snowflake.snowpark.functions.sproc`
    Nfuncreturn_typeinput_typesname
execute_ascallerownerzrestricted calleranonymous_sp_sqlpackages_ast_ast_id	_ast_stmtreturnc                     || _         || _        || _        || _        || _        || _        t        |t              | _        || _	        || _
        |	| _        |
| _        y N)r*   r-   _return_type_input_types_execute_as_anonymous_sp_sql
isinstancer$   _is_return_table	_packagesr4   r5   r6   )selfr*   r+   r,   r-   r.   r2   r3   r4   r5   r6   s              e/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/snowpark/stored_procedure.py__init__zStoredProcedure.__init__H   s_     #		''%!1 *;
 C! 	"    argssession"snowflake.snowpark.session.Sessionc                    |rIt        |d   t        j                  j                  j                        r|rt        d      |d   }|dd  }n,|xs( t        j                  j                  j                         }t        | j                        t        |      k  r-t        dt        | j                         dt        |             ||fS )Nr   zTwo sessions specified in arguments. Session should either be the first argument or as a named argument at the end, but not both   zJIncorrect number of arguments passed to the stored procedure. Expected: <=z	, Found: )	r>   	snowflakesnowparkrF   Session
ValueError_get_active_sessionlenr;   )rA   rE   rF   s      rB   _validate_callzStoredProcedure._validate_callg   s    
 JtAw	(:(:(B(B(J(JK @  1gG8DQ!3!3!;!;!O!O!QGt  !CI-"4#4#456iD	{L 
 W}rD   T)rF   statement_paramsblock	_emit_astrQ   rR   rS   c                ,   | j                  ||      \  }}|j                  j                  j                  dt        j
                  j                         d }|r^| j                  R| j                  J d       | j                  J d       t        j                         }t        || j                  |g|  | j                  rUt        || j                  g| }| j                   | }|j                  || j                   ||| j"                        }	|s9|	S  |j$                  | j                  g|| j                   || j                   |d}	| j                   r	||	_        |	S | j"                  %|j&                  j)                  | j"                         |	S )NzStoredProcedure.__call__zENeed to ensure _emit_ast is True when registering a stored procedure.z-Need to assign an ID to the stored procedure.)queryis_return_tablerR   rQ   ast_stmt)rV   rQ   rS   rR   )rP   _conn_telemetry_clientsend_function_usage_telemetryr   FUNC_CAT_USAGEvaluer4   r5   protoExprr   r=   r   r-   _execute_sproc_internalr?   r6   _call
_ast_batcheval)
rA   rF   rQ   rR   rS   rE   
sproc_exprcall_sqlrU   ress
             rB   __call__zStoredProcedure.__call__   s    ++D':g''EE&(E(E(K(K	
 
.		%WVW% (?>?(Jj$,,8HP4P!!27DIIMMH--.xj9E11 $ 5 5!1 2 C 
'--		 !% 5 5!1//C    "CH
 
	 ^^'##DNN3
rD   )r1   NNNNNr9   )__name__
__module____qualname____doc__r   r#   r   strtypingLiteralr   r
   r   r]   r)   intBindrC   r   rP   r"   r   boolrf    rD   rB   r)   r)   9   se   ( NU*.;?04!%*.## # (^	#
 # NN#IJ# #3-# 4c:o 678# u,,-# ## EJJ'# 
#D CG3i >?0  CG5999 >?9 #4S>2	9
 9 9 
9 9rD   r)   c            9       ~   e Zd ZdZded   ddfdZdeddfd	Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2ddddde	e
eeef   f   dee   deee      dee	eee   f      dedee   deee	eeeef   f         deee	eef         dedededej(                  d   dedeee      deeeef      dee   ded ee   d!eeeef      d"eeeef      d#ed$edef.d%       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2ddd
dd&d'ed(edee   deee      dee	eee   f      dedee   deee	eeeef   f         deee	eef         dedededej(                  d   dedeee      deeeef      dee   ded ee   d!eeeef      d"eeeef      d#ed)ed$edef2d*       Zd
ddd
d
d
ddd
ddd
dddd+de	e
eeef   f   dedee   d,edee   deee	eeeef   f         deee	eef         dedededed#ed"eeeef      dej(                  d   d-ed.ed)ededeee      deeeef      d/edee   d0eeeef      ded ee   d!eeeef      d$edef8d1Zy)3StoredProcedureRegistrationak;  
    Provides methods to register lambdas and functions as stored procedures in the Snowflake database.
    For more information about Snowflake Python stored procedures, see `Python stored procedures <https://docs.snowflake.com/en/sql-reference/stored-procedures-python.html>`__.

    :attr:`session.sproc <snowflake.snowpark.Session.sproc>` returns an object of this class.
    You can use this object to register stored procedures that you plan to use in the current session or
    permanently. The methods that register a stored procedure return a :class:`StoredProcedure` object.

    Note that the first parameter of your function should be a snowpark Session. Also, you need to add
    `snowflake-snowpark-python` package (version >= 0.4.0) to your session before trying to create a
    stored procedure.

    There are two ways to register a stored procedure with Snowpark:

        - Use :func:`~snowflake.snowpark.functions.sproc` or :meth:`register`. By pointing to a
          `runtime Python function`, Snowpark uses `cloudpickle <https://github.com/cloudpipe/cloudpickle>`_
          to serialize this function to bytecode, and deserialize the bytecode to a Python
          function on the Snowflake server during stored procedure creation. During the serialization, the
          global variables used in the Python function will be serialized into the bytecode,
          but only the name of the module object or any objects from a module that are used in the
          Python function will be serialized. If the size of the serialized bytecode is over 8K bytes, it will be uploaded to a stage location as a Python file.
          If it's under 8K, it will be added to the `Stored Procedure in-line code <https://docs.snowflake.com/en/sql-reference/stored-procedures-python.html#choosing-to-create-a-stored-procedure-with-in-line-code-or-with-code-uploaded-from-a-stage>`__.

          During the deserialization, Python will look up the
          corresponding modules and objects by names.

          Details could be found in :class:`snowflake.snowpark.udf.UDFRegistration`.

        - Use :meth:`register_from_file`. By pointing to a `Python file` or a `zip file containing
          Python source code` and the target function name, Snowpark uploads this file to a stage
          (which can also be customized), and load the corresponding function from this file to
          the Python runtime on the Snowflake server during stored procedure creation. Then this
          function will be invoked when calling this stored procedure. This approach can address
          the deficiency of the previous approach that uses cloudpickle, because the source code
          in this file other than the target function will be loaded during stored procedure creation.
          Therefore, this approach is useful and efficient when all your Python code is already in
          source files.

    Snowflake supports the following data types for the parameters for a stored procedure:

    =============================================  ================================================  =========
    Python Type                                    Snowpark Type                                     SQL Type
    =============================================  ================================================  =========
    ``int``                                        :class:`~snowflake.snowpark.types.LongType`       NUMBER
    ``decimal.Decimal``                            :class:`~snowflake.snowpark.types.DecimalType`    NUMBER
    ``float``                                      :class:`~snowflake.snowpark.types.FloatType`      FLOAT
    ``str``                                        :class:`~snowflake.snowpark.types.StringType`     STRING
    ``bool``                                       :class:`~snowflake.snowpark.types.BooleanType`    BOOL
    ``datetime.time``                              :class:`~snowflake.snowpark.types.TimeType`       TIME
    ``datetime.date``                              :class:`~snowflake.snowpark.types.DateType`       DATE
    ``datetime.datetime``                          :class:`~snowflake.snowpark.types.TimestampType`  TIMESTAMP
    ``bytes`` or ``bytearray``                     :class:`~snowflake.snowpark.types.BinaryType`     BINARY
    ``list``                                       :class:`~snowflake.snowpark.types.ArrayType`      ARRAY
    ``dict``                                       :class:`~snowflake.snowpark.types.MapType`        OBJECT
    Dynamically mapped to the native Python type   :class:`~snowflake.snowpark.types.VariantType`    VARIANT
    ``dict``                                       :class:`~snowflake.snowpark.types.GeographyType`  GEOGRAPHY
    =============================================  ================================================  =========

    Note:
        1. Data with the VARIANT SQL type will be converted to a Python type
        dynamically inside a stored procedure. The following SQL types are converted to :class:`str`
        in stored procedures rather than native Python types: TIME, DATE, TIMESTAMP and BINARY.

        2. Data returned as :class:`~snowflake.snowpark.types.ArrayType` (``list``),
        :class:`~snowflake.snowpark.types.MapType` (``dict``) or
        :class:`~snowflake.snowpark.types.VariantType` (:attr:`~snowflake.snowpark.types.Variant`)
        by a stored procedure will be represented as a json string. You can call ``eval()`` or ``json.loads()``
        to convert the result to a native Python object. Data returned as
        :class:`~snowflake.snowpark.types.GeographyType` (:attr:`~snowflake.snowpark.types.Geography`)
        by a stored procedure will be represented as a `GeoJSON <https://datatracker.ietf.org/doc/html/rfc7946>`_
        string.

        3. Currently calling stored procedure that requires VARIANT and GEOGRAPHY input types is not supported
        in snowpark API.

        4. Dataframe returned from :meth:`~snowflake.snowpark.Session.call` does not support stacking dataframe
        operations when sql simplifier is disabled, and output columns in return type for the table stored
        procedure are not defined.

    Example 1
        Use stored procedure to copy data from one table to another::

            >>> import snowflake.snowpark
            >>> from snowflake.snowpark.functions import sproc
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>>
            >>> def my_copy(session: snowflake.snowpark.Session, from_table: str, to_table: str, count: int) -> str:
            ...     session.table(from_table).limit(count).write.save_as_table(to_table)
            ...     return "SUCCESS"
            >>>
            >>> my_copy_sp = session.sproc.register(my_copy, name="my_copy_sp", replace=True)
            >>> _ = session.sql("create or replace temp table test_from(test_str varchar) as select randstr(20, random()) from table(generator(rowCount => 100))").collect()
            >>>
            >>> # call using sql
            >>> _ = session.sql("drop table if exists test_to").collect()
            >>> session.sql("call my_copy_sp('test_from', 'test_to', 10)").collect()
            [Row(MY_COPY_SP='SUCCESS')]
            >>> session.table("test_to").count()
            10
            >>> # call using session.call API
            >>> _ = session.sql("drop table if exists test_to").collect()
            >>> session.call("my_copy_sp", "test_from", "test_to", 10)
            'SUCCESS'
            >>> session.table("test_to").count()
            10

    Example 2
        Create a temporary stored procedure from a lambda and call it::

            >>> from snowflake.snowpark.functions import sproc
            >>> from snowflake.snowpark.types import IntegerType
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>> add_one_sp = sproc(
            ...     lambda session_, x: session_.sql(f"select {x} + 1").collect()[0][0],
            ...     return_type=IntegerType(),
            ...     input_types=[IntegerType()]
            ... )
            >>> add_one_sp(1)
            2

    Example 3
        Create a stored procedure with type hints and ``@sproc`` decorator and call it::

            >>> import snowflake.snowpark
            >>> from snowflake.snowpark.functions import sproc
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>> @sproc
            ... def add_sp(session_: snowflake.snowpark.Session, x: int, y: int) -> int:
            ...    return session_.sql(f"select {x} + {y}").collect()[0][0]
            >>> add_sp(1, 2)
            3

    Example 4
        Create a permanent stored procedure with a name and call it in SQL::

            >>> from snowflake.snowpark.types import IntegerType
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>> _ = session.sql("create or replace temp stage mystage").collect()
            >>> _ = session.sproc.register(
            ...     lambda session_, x, y: session_.sql(f"SELECT {x} * {y}").collect()[0][0],
            ...     return_type=IntegerType(),
            ...     input_types=[IntegerType(), IntegerType()],
            ...     is_permanent=True,
            ...     name="mul_sp",
            ...     replace=True,
            ...     stage_location="@mystage",
            ... )
            >>> session.sql("call mul_sp(5, 6)").collect()
            [Row(MUL_SP=30)]
            >>> # skip stored proc creation if it already exists
            >>> _ = session.sproc.register(
            ...     lambda session_, x, y: session_.sql(f"SELECT {x} * {y} + 1").collect()[0][0],
            ...     return_type=IntegerType(),
            ...     input_types=[IntegerType(), IntegerType()],
            ...     is_permanent=True,
            ...     name="mul_sp",
            ...     if_not_exists=True,
            ...     stage_location="@mystage",
            ... )
            >>> session.sql("call mul_sp(5, 6)").collect()
            [Row(MUL_SP=30)]
            >>> # overwrite stored procedure
            >>> _ = session.sproc.register(
            ...     lambda session_, x, y: session_.sql(f"SELECT {x} * {y} + 1").collect()[0][0],
            ...     return_type=IntegerType(),
            ...     input_types=[IntegerType(), IntegerType()],
            ...     is_permanent=True,
            ...     name="mul_sp",
            ...     replace=True,
            ...     stage_location="@mystage",
            ... )
            >>> session.sql("call mul_sp(5, 6)").collect()
            [Row(MUL_SP=31)]

    Example 5
        Create a stored procedure with stored-procedure-level imports and call it::

            >>> import snowflake.snowpark
            >>> from resources.test_sp_dir.test_sp_file import mod5
            >>> from snowflake.snowpark.functions import sproc
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>> @sproc(imports=[("tests/resources/test_sp_dir/test_sp_file.py", "resources.test_sp_dir.test_sp_file")])
            ... def mod5_and_plus1_sp(session_: snowflake.snowpark.Session, x: int) -> int:
            ...     return mod5(session_, x) + 1
            >>> mod5_and_plus1_sp(2)
            3

    Example 6
        Create a stored procedure with stored-procedure-level packages and call it::

            >>> import snowflake.snowpark
            >>> from snowflake.snowpark.functions import sproc
            >>> import numpy as np
            >>> import math
            >>>
            >>> @sproc(packages=["snowflake-snowpark-python", "numpy"])
            ... def sin_sp(_: snowflake.snowpark.Session, x: float) -> float:
            ...     return np.sin(x)
            >>> sin_sp(0.5 * math.pi)
            1.0

    Example 7
        Creating a stored procedure from a local Python file::

            >>> session.add_packages('snowflake-snowpark-python')
            >>> # mod5() in that file has type hints
            >>> mod5_sp = session.sproc.register_from_file(
            ...     file_path="tests/resources/test_sp_dir/test_sp_file.py",
            ...     func_name="mod5",
            ... )
            >>> mod5_sp(2)
            2

    Example 8
        Creating a stored procedure from a Python file on an internal stage::

            >>> from snowflake.snowpark.types import IntegerType
            >>>
            >>> session.add_packages('snowflake-snowpark-python')
            >>> _ = session.sql("create or replace temp stage mystage").collect()
            >>> _ = session.file.put("tests/resources/test_sp_dir/test_sp_file.py", "@mystage", auto_compress=False)
            >>> mod5_sp = session.sproc.register_from_file(
            ...     file_path="@mystage/test_sp_file.py",
            ...     func_name="mod5",
            ...     return_type=IntegerType(),
            ...     input_types=[IntegerType()],
            ... )
            >>> mod5_sp(2)
            2

    Example 9
        Creating a table stored procedure with return type while defining return columns and datatypes::

            >>> from snowflake.snowpark.types import IntegerType, StructField, StructType
            >>> @sproc(return_type=StructType([StructField("A", IntegerType()), StructField("B", IntegerType())]), input_types=[IntegerType(), IntegerType()])
            ... def select_sp(session_, x, y):
            ...     return session_.sql(f"SELECT {x} as A, {y} as B")
            ...
            >>> select_sp(1, 2).show()
            -------------
            |"A"  |"B"  |
            -------------
            |1    |2    |
            -------------
            <BLANKLINE>

    Example 10
        Creating a table stored procedure with return type with free return columns::

            >>> from snowflake.snowpark.types import IntegerType, StructType
            >>> @sproc(return_type=StructType(), input_types=[IntegerType(), IntegerType()])
            ... def select_sp(session_, x, y):
            ...     return session_.sql(f"SELECT {x} as A, {y} as B")
            ...
            >>> select_sp(1, 2).show()
            -------------
            |"A"  |"B"  |
            -------------
            |1    |2    |
            -------------
            <BLANKLINE>

    Example 11
        Creating a table stored procedure using implicit type hints::

            >>> from snowflake.snowpark.dataframe import DataFrame
            >>> @sproc
            ... def select_sp(session_: snowflake.snowpark.Session, x: int, y: int) -> DataFrame:
            ...     return session_.sql(f"SELECT {x} as A, {y} as B")
            ...
            >>> select_sp(1, 2).show()
            -------------
            |"A"  |"B"  |
            -------------
            |1    |2    |
            -------------
            <BLANKLINE>

    See Also:
        - :class:`snowflake.snowpark.udf.UDFRegistration`
        - :func:`~snowflake.snowpark.functions.sproc`
        - :meth:`register`
        - :meth:`register_from_file`
        - :meth:`~snowflake.snowpark.Session.add_import`
        - :meth:`~snowflake.snowpark.Session.add_packages`
    rF   rG   r7   Nc                     || _         y r9   )_session)rA   rF   s     rB   rC   z$StoredProcedureRegistration.__init__  s	    rD   	sproc_objz&snowflake.snowpark.dataframe.DataFramec                     |j                   D cg c]  }t        |       }}| j                  j                  d|j                   ddj                  |       d      S c c}w )a  
        Returns a :class:`~snowflake.snowpark.DataFrame` that describes the properties of a stored procedure.

        Args:
            sproc_obj: A :class:`StoredProcedure` returned by
                :func:`~snowflake.snowpark.functions.sproc` or :meth:`register`.
        zdescribe procedure (,))r;   r   ru   sqlr-   join)rA   rv   t	func_argss       rB   describez$StoredProcedureRegistration.describe  sb     8A7M7MN!*1-N	N}}  !)..!1388I3F2GqI
 	
 Os   AFr1   T)rQ   source_code_displayrS   r*   r+   r,   r-   is_permanentstage_locationimportsr3   replaceif_not_existsparallelr.   r/   strictexternal_access_integrationssecretscommentcopy_grantsartifact_repositoryresource_constraintrQ   r   rS   c                   t        | j                  ||      5  t        |      s(|j                  d      t	        dt        |             t        |       t        t        j                  ||||        | j                  ||||||||	|
||f||||||d||j                  dd      ||j                  dd      |j                  d	d      |||d
|cddd       S # 1 sw Y   yxY w)a  
        Registers a Python function as a Snowflake Python stored procedure and returns the stored procedure.
        The usage, input arguments, and return value of this method are the same as
        they are for :func:`~snowflake.snowpark.functions.sproc`, but :meth:`register`
        cannot be used as a decorator. See examples in
        :class:`~snowflake.snowpark.stored_procedure.StoredProcedureRegistration`.

        Args:
            func: A Python function used for creating the stored procedure. Note that the first parameter
                of your function should be a snowpark Session.
            return_type: A :class:`~snowflake.snowpark.types.DataType` representing the return data
                type of the stored procedure. Optional if type hints are provided.
            input_types: A list of :class:`~snowflake.snowpark.types.DataType`
                representing the input data types of the stored procedure. Optional if
                type hints are provided.
            name: A string or list of strings that specify the name or fully-qualified
                object identifier (database name, schema name, and function name) for
                the stored procedure in Snowflake, which allows you to call this stored procedure in a SQL
                command or via :meth:`~snowflake.snowpark.Session.call`.
                If it is not provided, a name will be automatically generated for the stored procedure.
                A name must be specified when ``is_permanent`` is ``True``.
            is_permanent: Whether to create a permanent stored procedure. The default is ``False``.
                If it is ``True``, a valid ``stage_location`` must be provided.
            stage_location: The stage location where the Python file for the stored procedure
                and its dependencies should be uploaded. The stage location must be specified
                when ``is_permanent`` is ``True``, and it will be ignored when
                ``is_permanent`` is ``False``. It can be any stage other than temporary
                stages and external stages.
            imports: A list of imports that only apply to this stored procedure. You can use a string to
                represent a file path (similar to the ``path`` argument in
                :meth:`~snowflake.snowpark.Session.add_import`) in this list, or a tuple of two
                strings to represent a file path and an import path (similar to the ``import_path``
                argument in :meth:`~snowflake.snowpark.Session.add_import`). These stored procedure-level imports
                will override the session-level imports added by
                :meth:`~snowflake.snowpark.Session.add_import`.
            packages: A list of packages that only apply to this stored procedure.
                These stored procedure-level packages will override the session-level packages added by
                :meth:`~snowflake.snowpark.Session.add_packages` and
                :meth:`~snowflake.snowpark.Session.add_requirements`.
            replace: Whether to replace a stored procedure that already was registered. The default is ``False``.
                If it is ``False``, attempting to register a stored procedure with a name that already exists
                results in a ``SnowparkSQLException`` exception being thrown. If it is ``True``,
                an existing stored procedure with the same name is overwritten.
            if_not_exists: Whether to skip creation of a stored procedure the same procedure is already registered.
                The default is ``False``. ``if_not_exists`` and ``replace`` are mutually exclusive and a ``ValueError``
                is raised when both are set. If it is ``True`` and a stored procedure is already registered, the registration is skipped.
            parallel: The number of threads to use for uploading stored procedure files with the
                `PUT <https://docs.snowflake.com/en/sql-reference/sql/put.html#put>`_
                command. The default value is 4 and supported values are from 1 to 99.
                Increasing the number of threads can improve performance when uploading
                large stored procedure files.
            execute_as: What permissions should the procedure have while executing. This
                supports caller, or owner for now.
            strict: Whether the created stored procedure is strict. A strict stored procedure will not invoke
                the stored procedure if any input is null. Instead, a null value will always be returned. Note
                that the stored procedure might still return null for non-null inputs.
            statement_params: Dictionary of statement level parameters to be set while executing this action.
            source_code_display: Display the source code of the stored procedure `func` as comments in the generated script.
                The source code is dynamically generated therefore it may not be identical to how the
                `func` is originally defined. The default is ``True``.
                If it is ``False``, source code will not be generated or displayed.
            external_access_integrations: The names of one or more external access integrations. Each
                integration you specify allows access to the external network locations and secrets
                the integration specifies.
            secrets: The key-value pairs of string types of secrets used to authenticate the external network location.
                The secrets can be accessed from handler code. The secrets specified as values must
                also be specified in the external access integration and the keys are strings used to
                retrieve the secrets using secret API.
            comment: Adds a comment for the created object. See
                `COMMENT <https://docs.snowflake.com/en/sql-reference/sql/comment>`_
            copy_grants: Specifies to retain the access privileges from the original function when a new function is
                created using CREATE OR REPLACE PROCEDURE.
            artifact_repository: The name of an artifact_repository that packages are found in. If unspecified, packages are
                pulled from Anaconda.
            resource_constraint: A dictionary containing a resource properties of a warehouse and then
                constraints needed to run this function. Eg ``{"architecture": "x86"}`` requires an x86
                warehouse be used for execution.

        See Also:
            - :func:`~snowflake.snowpark.functions.sproc`
            - :meth:`register_from_file`
        )r*   r-   _registered_object_nameNzHInvalid function: not a function or callable (__call__ is not defined): z$StoredProcedureRegistration.register	anonymousFforce_inline_codenative_app_params)r   r   r   r   rQ   r.   api_call_sourcer   r   r   r   r   r   r   rS   )r   registercallableget	TypeErrortyper   r   r    	PROCEDURE_do_register_sppop)rA   r*   r+   r,   r-   r   r   r   r3   r   r   r   r.   r   r   r   r   r   r   r   rQ   r   rS   kwargss                           rB   r   z$StoredProcedureRegistration.register  s   \ 0DtT ,	D>fjj1J&K&S226t*? 
 !,(($nh
 (4'' .J'!1% F$7 **[%8) #)**-@%"H"(**-@$"G$7$7#;< =,	 ,	 ,	s   B8CC$)rQ   r   skip_upload_on_content_matchrS   	file_path	func_namer   c                    t        | j                  |||      5  t        |      }t        t        j
                  ||||       t        |        | j                  ||f||||||	|
|||f||||||d||||||d|cddd       S # 1 sw Y   yxY w)a  
        Registers a Python function as a Snowflake Python stored procedure from a Python or zip file,
        and returns the stored procedure. Apart from ``file_path`` and ``func_name``, the input arguments
        of this method are the same as :meth:`register`. See examples in
        :class:`~snowflake.snowpark.stored_procedure.StoredProcedureRegistration`.

        Args:
            file_path: The path of a local file or a remote file in the stage. See
                more details on ``path`` argument of
                :meth:`session.add_import() <snowflake.snowpark.Session.add_import>`.
                Note that unlike ``path`` argument of
                :meth:`session.add_import() <snowflake.snowpark.Session.add_import>`,
                here the file can only be a Python file or a compressed file
                (e.g., .zip file) containing Python modules.
            func_name: The Python function name in the file that will be created
                as a stored procedure.
            return_type: A :class:`~snowflake.snowpark.types.DataType` representing the return data
                type of the stored procedure. Optional if type hints are provided.
            input_types: A list of :class:`~snowflake.snowpark.types.DataType`
                representing the input data types of the stored procedure. Optional if
                type hints are provided.
            name: A string or list of strings that specify the name or fully-qualified
                object identifier (database name, schema name, and function name) for
                the stored procedure in Snowflake, which allows you to call this stored procedure in a SQL
                command or via :meth:`~snowflake.snowpark.Session.call`.
                If it is not provided, a name will be automatically generated for the stored procedure.
                A name must be specified when ``is_permanent`` is ``True``.
            is_permanent: Whether to create a permanent stored procedure. The default is ``False``.
                If it is ``True``, a valid ``stage_location`` must be provided.
            stage_location: The stage location where the Python file for the stored procedure
                and its dependencies should be uploaded. The stage location must be specified
                when ``is_permanent`` is ``True``, and it will be ignored when
                ``is_permanent`` is ``False``. It can be any stage other than temporary
                stages and external stages.
            imports: A list of imports that only apply to this stored procedure. You can use a string to
                represent a file path (similar to the ``path`` argument in
                :meth:`~snowflake.snowpark.Session.add_import`) in this list, or a tuple of two
                strings to represent a file path and an import path (similar to the ``import_path``
                argument in :meth:`~snowflake.snowpark.Session.add_import`). These stored procedure-level imports
                will override the session-level imports added by
                :meth:`~snowflake.snowpark.Session.add_import`.
            packages: A list of packages that only apply to this stored procedure.
                These stored procedure-level packages will override the session-level packages added by
                :meth:`~snowflake.snowpark.Session.add_packages` and
                :meth:`~snowflake.snowpark.Session.add_requirements`.
            replace: Whether to replace a stored procedure that already was registered. The default is ``False``.
                If it is ``False``, attempting to register a stored procedure with a name that already exists
                results in a ``SnowparkSQLException`` exception being thrown. If it is ``True``,
                an existing stored procedure with the same name is overwritten.
            if_not_exists: Whether to skip creation of a stored procedure the same procedure is already registered.
                The default is ``False``. ``if_not_exists`` and ``replace`` are mutually exclusive and a ``ValueError``
                is raised when both are set. If it is ``True`` and a stored procedure is already registered, the registration is skipped.
            parallel: The number of threads to use for uploading stored procedure files with the
                `PUT <https://docs.snowflake.com/en/sql-reference/sql/put.html#put>`_
                command. The default value is 4 and supported values are from 1 to 99.
                Increasing the number of threads can improve performance when uploading
                large stored procedure files.
            execute_as: What permissions should the procedure have while executing. This
                supports ``caller``, ``owner`` or ``restricted caller`` for now.
            strict: Whether the created stored procedure is strict. A strict stored procedure will not invoke
                the stored procedure if any input is null. Instead, a null value will always be returned. Note
                that the stored procedure might still return null for non-null inputs.
            statement_params: Dictionary of statement level parameters to be set while executing this action.
            source_code_display: Display the source code of the stored procedure `func` as comments in the generated script.
                The source code is dynamically generated therefore it may not be identical to how the
                `func` is originally defined. The default is ``True``.
                If it is ``False``, source code will not be generated or displayed.
            skip_upload_on_content_match: When set to ``True`` and a version of source file already exists on stage, the given source
                file will be uploaded to stage only if the contents of the current file differ from the remote file on stage. Defaults
                to ``False``.
            external_access_integrations: The names of one or more external access integrations. Each
                integration you specify allows access to the external network locations and secrets
                the integration specifies.
            secrets: The key-value pairs of string types of secrets used to authenticate the external network location.
                The secrets can be accessed from handler code. The secrets specified as values must
                also be specified in the external access integration and the keys are strings used to
                retrieve the secrets using secret API.
            comment: Adds a comment for the created object. See
                `COMMENT <https://docs.snowflake.com/en/sql-reference/sql/comment>`_
            copy_grants: Specifies to retain the access privileges from the original function when a new function is
                created using CREATE OR REPLACE PROCEDURE.
            artifact_repository: The name of an artifact_repository that packages are found in. If unspecified, packages are
                pulled from Anaconda.
            resource_constraint: A dictionary containing a resource properties of a warehouse and then
                constraints needed to run this function. Eg ``{"architecture": "x86"}`` requires an x86
                warehouse be used for execution.

        Note::
            The type hints can still be extracted from the source Python file if they
            are provided, but currently are not working for a zip file. Therefore,
            you have to provide ``return_type`` and ``input_types`` when ``path``
            points to a zip file.

        See Also:
            - :func:`~snowflake.snowpark.functions.sproc`
            - :meth:`register`
        )r   r   r-   z.StoredProcedureRegistration.register_from_file)r   r   r   r   rQ   r.   r   r   r   r   r   r   rS   N)r   register_from_filer   r   r    r   r   r   )rA   r   r   r+   r,   r-   r   r   r   r3   r   r   r   r.   r   r   r   r   r   r   r   rQ   r   r   rS   r   s                             rB   r   z.StoredProcedureRegistration.register_from_file  s    ~ 0##yITX
 $	 *)4I(($nh !, (4''I& .J'!1% P$7-I)$7$7#12 3$	 $	 $	s   A BB)r   rQ   r.   r   r   r   r   r   r   r   r   r   r   r   rS   sp_namer   r   r   r   c                b   d\  }}}|j                  d      i|rP| j                  j                  j                         }t	        |j
                  j                  |      }|j                  }t        ||||d   |||||	      S t        |d       t        | j                  t        j                  |||||      \  } }!}"}}}#|r| j                  j                  j                         }t	        |j
                  j                  |      }|j                  }t        ||fi d|d|d|d|d	|d
|d|d|	d|
d|d|d|d|d|d|d|d|d| j                  d| | |!rt        d      dgt        t!        |            D $cg c]
  }$d|$dz     c}$z   }%t#        ||%dd        D &'cg c]  \  }&}'t%        |&|'       }(}&}'t'        | j                  |      }t)        | j                  t        j                  ||%| ||||
|||||||j                  dd            \  })}*}+},}-}.d }/| j                  | j                  j*                  }/|.st-        |/       d }0|rt/        |||(|)| |+|,||*||/|||      }0nd}1	 t1        d3i d| j                  d|d|d |(d!|#d"|)d#t        j                  d$| d%|+d&|,d'|d(t2        j4                  d|d|d|	d)|*d|d*|d|d|d|d|d|d+|d,|d-|/d.|d/| 	 |1rtE        | j                  |-|       	 t        |||| ||0||||2
      }5|5S c c}$w c c}'}&w # t6        $ rE}2d0}1t9        j:                         d1   }3t=        j>                  |2      }4|4jA                  |3      d d }2~2wtB        $ r d0}1 w xY w# |1rtE        | j                  |-|       w w xY w)4N)NNNr   )r.   r3   r4   r5   r6   zstored-proc-levelr+   r,   r-   r   r   r3   r   r   r   rQ   r.   r   r   r   r   r   r   rF   z(pandas stored procedure is not supportedargrI   )rF   r3    _suppress_local_package_warningsF)rQ   r   r   r   r   r   r   )r*   r+   
input_argshandlerobject_nameall_importsall_packagesraw_importsinline_python_coder   runtime_versionr   r   r   r*   r   opt_arg_defaultsr   object_typer   r   r   r   registration_typer   r   r   r   r   r   r   T   )r.   r2   r3   r4   r5   r6   rq   )#r   ru   ra   bindr   exprstored_procedureuidr)   r!   r   r    r   r   r   rangerO   zipr   r   r   !_runtime_version_from_requirementr   r   r   r   SPROCr   sysexc_infor   $SQL_EXCEPTION_FROM_PROGRAMMING_ERRORwith_tracebackBaseExceptionr   )6rA   r*   r+   r,   r   r   r   r3   r   r   r   r   r   rQ   r.   r   r   r   r   r   r   r   r   r   r   r   r   rS   r   stmtastast_id
sproc_nameis_pandas_udfis_dataframe_inputr   i	arg_namesdtarg_namer   r   coder   r   upload_file_stage_location%custom_python_runtime_version_allowed runtime_version_from_requirementr2   raisedpetbnesprocs6                                                         rB   r   z+StoredProcedureRegistration._do_register_sp4  sU   @ -c6::/0<}}//446'		(B(BDI"01%!
 
 	7$78 (MM$$
	
 ==++002D#DII$>$>ECXXF ( (	
   .   "   , " "2 &  %8  .J!"  #$  %& *'( )* )3-2 FGGKc+>N8O"P1S1;"PP	8;KSTSU8W
(4HIb(#

 

 :MM
 )MM$$- 3)E%/ 3-3ZZ2E.!
	
&1. ,0(==$?? - 5()IJ?'%&')##' @-I"3 " F1'  MM !,  *	
 &6 $ !/ 8 8 !+ !, ". !( '7&<&< ". $ #0  (,!"  *#$ %4%& "'( 2N)* $+, &6-. $/0 '812 !,34 %E56 )<78 )<9Z 9'A>  !-
 K #Q
N $ 6\\^A&4YY ''+5   9'A> s2   L+*L06BL6 6	N?A M??NN N.)NNNFNNNFF   r1   FNNNFNN)rg   rh   ri   rj   r   rC   r)   r   r"   r
   r   r	   rk   r#   r   r'   rp   r   rn   rl   rm   r   r   r   r   r   rq   rD   rB   rs   rs      sD   bH	 )M N  SW  
(
	1
  +/0448"(,?C;?#MT<@,0!%!-18<)Y, 6:$(1YHeCHo-.Y h'Y d8n-	Y
 uS(3-/01Y Y !Y $uS%S/%9:;<Y 4c:o 678Y Y Y Y NN#IJY Y '/tCy&9Y  $sCx.)!Y" ##Y$ %Y& &c]'Y( &d38n5)Y, #4S>2-Y. "/Y0 1Y4 
5Y Yv 
 +/0448"(,?C;?#MT<@,0!%!-18<+b. 6:$(-25bb b h'	b
 d8n-b uS(3-/01b b !b $uS%S/%9:;<b 4c:o 678b b b b NN#IJb b  '/tCy&9!b" $sCx.)#b$ #%b& 'b( &c])b* &d38n5+b. #4S>2/b0 "1b2 '+3b4 5b8 
9b bd %*59MT-2"<@,0"'!%6:!-18<;mHeCHo-.m m (^	m
 m !m $uS%S/%9:;<m 4c:o 678m m m m m "m #4S>2m  NN#IJ!m" #m$ %m& '+'m( )m* '/tCy&9+m, $sCx.)-m.  /m0 #1m2 $DcN33m4 5m6 &c]7m8 &d38n59m: ;m> 
?mrD   rs   )<rj   r   rl   typesr   r   r   r   r   r   r	   r
   snowflake.snowparkrJ   4snowflake.snowpark._internal.proto.generated.ast_pb2rK   	_internalr]   	generatedast_pb2snowflake.connectorr   &snowflake.snowpark._internal.ast.utilsr   r   r   *snowflake.snowpark._internal.error_messager   +snowflake.snowpark._internal.open_telemetryr   &snowflake.snowpark._internal.telemetryr   'snowflake.snowpark._internal.type_utilsr   &snowflake.snowpark._internal.udf_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   "snowflake.snowpark._internal.utilsr    r!   r"   snowflake.snowpark.typesr#   r$   version_infor'   collections.abcr)   rs   rq   rD   rB   <module>r      s    I 
   D D D  D D D 0 
 W B I    
 :
 v(@ @Fe erD   