Search code examples
pythonsnowflake-cloud-data-platform

How can I write/show a Python Snowpark DataFrame with more than 64 rows?


I'm using Python Snowpark to create a dataframe and show it or write it to my SnowFlake database but I'm having a strange error while doing so.

The error raises when I ".show()" or ".write()" the DataFrame and when this contains more than 64 rows. The error says "Cannot perform DROP. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.", which I don't get the session actually is connected to a database, and it can write on it but the problem comes from the length of the data.

Here I show an example in which with the same data but different slice lengths the output is correct or is an error.

writing_df_50 = db.create_dataframe(data[:50], schema=self.model.schema)
writing_df_50_2 = db.create_dataframe(data[50:100], schema=self.model.schema)
writing_df_100 = db.create_dataframe(data[:100], schema=self.model.schema)

# OK
writing_df_50.show()

# OK
writing_df_50_2.show()

# ERROR
writing_df_100.show()

Here I write the error traceback:

Traceback (most recent call last):
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3548, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-86bf889c01ec>", line 1, in <module>
    db.create_dataframe(writing_data, schema=self.model.schema).show()
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/telemetry.py", line 139, in wrap
    result = func(*args, **kwargs)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/dataframe.py", line 2861, in show
    self._show_string(
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/dataframe.py", line 2979, in _show_string
    result, meta = self._session._conn.get_result_and_metadata(
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 593, in get_result_and_metadata
    result_set, result_meta = self.get_result_set(plan, **kwargs)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/analyzer/snowflake_plan.py", line 187, in wrap
    raise ne.with_traceback(tb) from None
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/analyzer/snowflake_plan.py", line 116, in wrap
    return func(*args, **kwargs)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 576, in get_result_set
    self.run_query(
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 103, in wrap
    raise ex
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 97, in wrap
    return func(*args, **kwargs)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 367, in run_query
    raise ex
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/snowpark/_internal/server_connection.py", line 348, in run_query
    results_cursor = self._cursor.execute(query, params=params, **kwargs)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/connector/cursor.py", line 920, in execute
    Error.errorhandler_wrapper(self.connection, self, error_class, errvalue)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/connector/errors.py", line 290, in errorhandler_wrapper
    handed_over = Error.hand_to_other_handler(
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/connector/errors.py", line 345, in hand_to_other_handler
    cursor.errorhandler(connection, cursor, error_class, error_value)
  File "/home/alex/.cache/pypoetry/virtualenvs/marilyn-vOmyxDUH-py3.10/lib/python3.10/site-packages/snowflake/connector/errors.py", line 221, in default_errorhandler
    raise error_class(
snowflake.snowpark.exceptions.SnowparkSQLException: (1304): 01b0a765-0303-21c9-0001-474606530ad2: 090105 (22000): Cannot perform DROP. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.

Does anybody understand what's happening here and how can I solve it?


Solution

  • The error just means you do not have a context set to your session. While you are connected to Snowflake you don't have a database/schema set in your current session.

    >>> connection_parameters = {
    ...    "account": "<your snowflake account>",
    ...    "user": "<your snowflake user>",
    ...    "password": "<your snowflake password>",
    ...    "role": "<your snowflake role>",  # optional
    ...    "warehouse": "<your snowflake warehouse>",  # optional
    ...    "database": "<your snowflake database>",  # optional
    ...    "schema": "<your snowflake schema>",  # optional
    ...  } 
    

    Not only that, but your user also doesn't have a DEFAULT_NAMESPACE set. This means you can only do statements that contain fully qualified name of objects, as in <database>.<schema>.<table> for example.

    Adjust your connection string to include a database/schema and you should not get the error anymore.