Search code examples
pythonstored-proceduressnowflake-cloud-data-platform

Get name of the current Snowpark Python Stored Procedure


is it possible to retrieve the name of the currently running python sproc?

For example if I have a python proc

CREATE OR REPLACE PROCEDURE copydata(fromtable VARCHAR, totable VARCHAR, count INT)
RETURNS STRING
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
PACKAGES = ('snowflake-snowpark-python')
HANDLER = 'run'
AS
$$
def run(session, from_table, to_table, count):

 session.table(from_table).limit(count).write.save_as_table(to_table)

 return "SUCCESS"
$$;

I would like to return "SUCCESS " + PROC_NAME


Solution

  • Well I found two different ways:

    create or replace procedure
        some_sproc()
    returns
        string
    language
        python
    runtime_version = 3.8
    packages = ('snowflake-snowpark-python')
    handler = 'some_sproc'
    as $$
    import inspect
    from snowflake.snowpark import Session
    
    def some_sproc(session: Session) -> str:
        return inspect.currentframe().f_code.co_name
    $$;
    
    call some_sproc();
    

    And the other one:

    create or replace procedure
        some_sproc_h()
    returns
        string
    language
        python
    runtime_version = 3.8
    packages = ('snowflake-snowpark-python')
    handler = 'handler'
    as $$
    from snowflake.snowpark import Session
    SPROC_NAME = 'some_sproc_h'
    
    def handler(session: Session) -> str:
        return SPROC_NAME
    $$;
    
    call some_sproc_h();