Search code examples
pythonsnowflake-cloud-data-platformsnowflake-connector

Returning Array in Snowflake Stored procedure


I have a stored procedure with return type as variant in snowflake. SP is written in JavaScript. Basically we are maintaining a array in SP and adding results and info just to maintain logs. The SP works fine when called from snowflake worksheets(UI) and returning

[
  "Using LOY DATABASE",
  "Using STAGE SCHEMA",
  "RUN ID - 20220517070653",
  "Truncating Table LOY.STAGE.MEMBER.",
  "Truncated Members Table.",
  "Data loaded to stage table.",
  "Swapped Stage table with Main Table.",
  "load_status - LOADED",
  "rows_loaded - 13175",
  "Procedure Successfully Completed.",
  "Members table count after loading : 13175"
]

But when the SP is called from a python script this array is converted to a string. I think it is because the array is of multi line and new line chars in present in array(multi lines) causing the result to convert as string. we are getting result like this in the python script

[('[\n "Using LOY DATABASE",\n "Using STAGE SCHEMA",\n "RUN ID - 20220517145428",\n "Truncating Table LOY.STAGE.MEMBER.",\n "Truncated Members Table.", \n "Data loaded to stage table.",\n "Swapped Stage table with Main Table.",\n "load_status - LOADED",\n "rows_loaded - 13175",\n"Members table count after loading : 13175"]')]

I'm using literal_eval to convert the string to list in python currently. Is there a way to get the result as List in python script instead as string?


Solution

  • Seems like the issue is still the case. I solved this issue by using ast.literal_eval() function as follows;

    df["column1"] = df["column1"].apply(lambda x: ast.literal_eval(x))