Question

How to solve 'SQL compilation error: Object 'SNOWPARK_TEMP_STAGE_FLGVIWVUC' already exists.' issue in snowflake?

I have been using Snowflake to do ML works. I have built a Multiple linear regression. I am writing an output df as a table using session.write_pandas. I get this error

'SQL compilation error: Object 'SNOWPARK_TEMP_STAGE_XXXXX' already exists.' 

in snowflake streamlit app. This goes if I refresh the page. But why it occurs? I could not find issues related to this in web.

I get this error only if i write dataframe as pandas. Below is the code where i get this error.


from snowflake.snowpark.context import get_active_session
session = get_active_session()
session.write_pandas(df_final, "TEMPTABLE",database="db",schema="schema", auto_create_table=True, overwrite=True)

I have tried to clear or drop the stage every time before write_pandas code.

import uuid
    
# Drop the stage if it already exists
session.sql(f"DROP STAGE IF EXISTS {stage_name}").collect()

# Create a new temporary stage
session.sql(f"CREATE TEMPORARY STAGE {stage_name}").collect()

This does not work and what is the real reason I get this error?

Please help I have no idea why this happens.

 2  97  2
1 Jan 1970

Solution

 1

Trying to patch away two types of errors when writing from pandas Dataframe() to tables in Snowflake.

  1. SNOWPARK_TEMP_FILE_FORMAT_XXXX already exists
  2. SNOWPARK_TEMP_STAGE_XXXX already exists

There is also a race condition which ever may occur first, so we have to write try-except within a try-except to resolve this issue.

This issue seen in all SNOWPARK related tools like Streamlit in Snowflake, Python-workbook and Python Stored procedures etc.

# EXAMPLE:
# Modify this fix to your own use accordingly ✅
# Snowflake may patch this BUG in the future.
# This BUG is really annoying. 

import re
from snowflake.snowpark.context import get_active_session
session = get_active_session()

def try_to_write_pd_to_table(my_session, my_df, my_tablename, my_db, my_sch):
    try:
        my_session.write_pandas(my_df, my_tablename, database=f"{my_db}", schema="{my_sch}", auto_create_table=True, overwrite=True)
    except Exception as e1:
        try:
            tmp_file_format=re.findall('SNOWPARK_TEMP_FILE_FORMAT_[A-Z]{10}', str(e1))
            if(len(tmp_file_format)>0):
                my_session.sql(f'DROP FILE FORMAT IF EXISTS {my_db}.{my_sch}.{tmp_file_format[0]};').collect();
            tmp_stage=re.findall('SNOWPARK_TEMP_STAGE_[A-Z]{10}', str(e1))
            if(len(tmp_stage)>0):
                my_session.sql(f'DROP STAGE IF EXISTS {my_db}.{my_sch}.{tmp_stage[0]};').collect();
            my_session.write_pandas(my_df, my_tablename, database=f"{my_db}", schema="{my_sch}", auto_create_table=True, overwrite=True)
        except Exception as e2:
            tmp_file_format=re.findall('SNOWPARK_TEMP_FILE_FORMAT_[A-Z]{10}', str(e2))
            if(len(tmp_file_format)>0):
                my_session.sql(f'DROP FILE FORMAT IF EXISTS {my_db}.{my_sch}.{tmp_file_format[0]};').collect();
            tmp_stage=re.findall('SNOWPARK_TEMP_STAGE_[A-Z]{10}', str(e2))
            if(len(tmp_stage)>0):
                my_session.sql(f'DROP STAGE IF EXISTS {my_db}.{my_sch}.{tmp_stage[0]};').collect();
            my_session.write_pandas(my_df, my_tablename, database=f"{my_db}", schema="{my_sch}", auto_create_table=True, overwrite=True)

try_to_write_pd_to_table(session, df_final, "TEMPTABLE", 'LLM_DATABASE', 'ML_SCHEMA');

2024-07-05
Kharthigeyan