
    g                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ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 erd d
lmZ d dlmZ d dlmZ d dlmZ dhZ G d ded         Zy)    )annotationsN)ChainMap)contextmanager)TYPE_CHECKINGcast)BaseConnection)SNOWSQL_CONNECTION_FILEload_from_snowsql_config_filerunning_in_sis)StreamlitAPIException)
cache_data)Iterator)	timedelta)	DataFrameSessionaccountc                  f     e Zd ZdZd fdZddZ	 d		 	 	 	 	 d
dZedd       Ze	dd       Z
 xZS )SnowparkConnectiona  A connection to Snowpark using snowflake.snowpark.session.Session. Initialize using
    ``st.connection("<name>", type="snowpark")``.

    In addition to providing access to the Snowpark Session, SnowparkConnection supports
    direct SQL querying using ``query("...")`` and thread safe access using
    ``with conn.safe_session():``. See methods below for more information.
    SnowparkConnections should always be created using ``st.connection()``, **not**
    initialized directly.

    .. note::
        We don't expect this iteration of SnowparkConnection to be able to scale
        well in apps with many concurrent users due to the lock contention that will occur
        over the single underlying Session object under high load.
    c                X    t        j                         | _        t        |   |fi | y N)	threadingRLock_locksuper__init__)selfconnection_namekwargs	__class__s      ^/var/www/openai/venv/lib/python3.12/site-packages/streamlit/connections/snowpark_connection.pyr   zSnowparkConnection.__init__@   s#    __&
3F3    c                   ddl m} ddlm} t	               r |       S t        || j                  j                         t        | j                              }t        |      st        dt         d      t        D ]  }||vst        d|        t        ||j                  j!                  |      j#                               S )Nr   )get_active_sessionr   zZMissing Snowpark connection configuration. Did you forget to set this in `secrets.toml`, `z#`, or as kwargs to `st.connection`?z#Missing Snowpark connection param: )snowflake.snowpark.contextr$   snowflake.snowpark.sessionr   r   r   _secretsto_dictr
   _connection_namelenr   r	   _REQUIRED_CONNECTION_PARAMSr   builderconfigscreate)r   r   r$   r   conn_paramsps         r!   _connectzSnowparkConnection._connectD   s    A6 %''MM!!#)$*?*?@
 ;'BBYAZ [33  -A#+.QRSQT,UVV - GW__44[AHHJKKr"   c           	     0    ddl m} ddlm}m}m}m}  | fd |d      d ||       |d            d fd	       }t        |      j                  d
d      }	|j                   d j                   d|	 |_	         t        d|      |      } ||      S )a  Run a read-only SQL query.

        This method implements both query result caching (with caching behavior
        identical to that of using ``@st.cache_data``) as well as simple error handling/retries.

        .. note::
            Queries that are run without a specified ttl are cached indefinitely.

        Parameters
        ----------
        sql : str
            The read-only SQL query to execute.
        ttl : float, int, timedelta or None
            The maximum number of seconds to keep results in the cache, or
            None if cached results should not expire. The default is None.

        Returns
        -------
        pandas.DataFrame
            The result of running the query, formatted as a pandas DataFrame.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> conn = st.connection("snowpark")
        >>> df = conn.query("SELECT * FROM pet_owners")
        >>> st.dataframe(df)
        r   )SnowparkServerException)retryretry_if_exception_typestop_after_attempt
wait_fixedc                $    j                         S r   )reset)_r   s    r!   <lambda>z*SnowparkConnection.query.<locals>.<lambda>   s    DJJLr"      T   )afterstopreraiser4   waitc                    j                   5  j                  j                  |       j                         cd d d        S # 1 sw Y   y xY wr   )r   	_instancesql	to_pandas)rD   r   s    r!   _queryz(SnowparkConnection.query.<locals>._query   s1     ~~))#.88: s   )AA
.r:   zRunning `snowpark.query(...)`.)show_spinnerttl)rD   strreturnr   )snowflake.snowpark.exceptionsr3   tenacityr4   r5   r6   r7   rJ   replace__qualname__r)   r   )
r   rD   rI   r3   r4   r5   r6   r7   rF   ttl_strs
   `         r!   queryzSnowparkConnection.query`   s    D	
	
 	
 
(#A&)*ABA

	;

	; 

'#s
 	 "(!4!4 5Qt7L7L6MQwiX
9
 
 c{r"   c                    | j                   S )a,  Access the underlying Snowpark session.

        .. note::
            Snowpark sessions are **not** thread safe. Users of this method are
            responsible for ensuring that access to the session returned by this method is
            done in a thread-safe manner. For most users, we recommend using the thread-safe
            safe_session() method and a ``with`` block.

        Information on how to use Snowpark sessions can be found in the `Snowpark documentation
        <https://docs.snowflake.com/en/developer-guide/snowpark/python/working-with-dataframes>`_.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> session = st.connection("snowpark").session
        >>> df = session.table("mytable").limit(10).to_pandas()
        >>> st.dataframe(df)
        )rC   r   s    r!   sessionzSnowparkConnection.session   s    * ~~r"   c              #  j   K   | j                   5  | j                   ddd       y# 1 sw Y   yxY ww)a  Grab the underlying Snowpark session in a thread-safe manner.

        As operations on a Snowpark session are not thread safe, we need to take care
        when using a session in the context of a Streamlit app where each script run
        occurs in its own thread. Using the contextmanager pattern to do this ensures
        that access on this connection's underlying Session is done in a thread-safe
        manner.

        Information on how to use Snowpark sessions can be found in the `Snowpark documentation
        <https://docs.snowflake.com/en/developer-guide/snowpark/python/working-with-dataframes>`_.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> conn = st.connection("snowpark")
        >>> with conn.safe_session() as session:
        ...     df = session.table("mytable").limit(10).to_pandas()
        >>>
        >>> st.dataframe(df)
        N)r   rT   rS   s    r!   safe_sessionzSnowparkConnection.safe_session   s"     . ZZ,, ZZs   3'	303)r   rJ   rK   None)rK   r   r   )rD   rJ   rI   zfloat | int | timedelta | NonerK   r   )rK   zIterator[Session])__name__
__module__rO   __doc__r   r1   rQ   propertyrT   r   rV   __classcell__)r    s   @r!   r   r   0   sf    4L> /3DD ,D 
	DL  ,  r"   r   r   )
__future__r   r   collectionsr   
contextlibr   typingr   r   streamlit.connectionsr   streamlit.connections.utilr	   r
   r   streamlit.errorsr   streamlit.runtime.cachingr   collections.abcr   datetimer   pandasr   r&   r   r+   r    r"   r!   <module>ri      sW   * #    % & 0 
 3 0(" 2  )k e	2 er"   