Skip to content

fix(spark): drop DataFrame temp views after inserting from them - #6134

Open
raams95 wants to merge 1 commit into
SQLMesh:mainfrom
raams95:fix/spark-drop-dataframe-temp-views
Open

raams95 wants to merge 1 commit into
SQLMesh:mainfrom
raams95:fix/spark-drop-dataframe-temp-views

Conversation

@raams95

@raams95 raams95 commented Oct 13, 2026 •

Copy link
Copy Markdown

Description

As reported in #6125, every DataFrame insert in the Spark and Databricks (Spark session) adapters registers a temp view (createOrReplaceGlobalTempView / createOrReplaceTempView) that is never dropped. In long-running sessions the views pile up and keep the DataFrames alive on the driver.

The other adapters already clean up their temp tables through the SourceQuery cleanup callback (DuckDB, BigQuery, MSSQL, ClickHouse, Snowflake). This adds the same for Spark and Databricks: the view is dropped with spark.catalog.dropGlobalTempView / spark.catalog.dropTempView once the statement that reads it has run. I used the catalog API rather than DROP VIEW so the drop goes through the same Spark session that created the view. On Databricks this matters because SQL can otherwise go through the SQL connector.

The temp view name is now generated once, outside the query factory, so the cleanup knows what to drop. The generated SQL is the same as before.

Fixes #6125

Test Plan

  • Added test_df_source_query_drops_global_temp_view (tests/core/engine_adapter/test_spark.py) and test_df_source_query_drops_temp_view (tests/core/engine_adapter/test_databricks.py). Each one checks that the view is created and queried inside the source query context and dropped when it exits. Both fail on main (drop never called) and pass with this change.
  • pytest tests/core/engine_adapter tests/core/test_snapshot_evaluator.py --ignore=tests/core/engine_adapter/integration passes, except test_drop_table_ducklake_no_cascade, which failed once in a parallel run on my Windows machine and passes when rerun on its own (it doesn't touch the Spark/Databricks adapters).
  • ruff check, ruff format and mypy are clean on the changed files.
  • I haven't run this against a real Spark or Databricks cluster (no Java on this machine), so the tests use mocked sessions.

Checklist

  • I have run make style and fixed any issues (ruff and ruff-format pass; mypy and the migrations hook report the same environment-only errors on main on my Windows machine)
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (no new failures compared to main on my Windows machine; the Windows-only failures on main are unchanged)
  • My commits are signed off (git commit -s) per the DCO

The Spark and Databricks adapters register a temp view for every
DataFrame insert but never drop it, so long sessions accumulate views
and keep the DataFrames alive on the driver. Drop the view in the
source query cleanup, as the other adapters do for their temp tables.

Fixes SQLMesh#6125

Signed-off-by: JanakiRamChittineni <341781521+raams95@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Databricks: should temp views created for DataFrame inserts be dropped after insert_append?

1 participant