Skip to content

A failed CREATE OR REPLACE, or DROP SCHEMA CASCADE, leaves a prepared query reading old rows #326

Closed

Description

@alxmrs

🌀 Claude: Two statements change a table without a prepared query noticing. The prepared query then returns old rows and doesn't raise. CodeRabbit found both while reviewing #323. I reproduced each on main at 447791a (0.7.1), with DataFusion 55.0.0.

import numpy as np, xarray as xr
from xarray_sql import Context

def ds(v):
    return xr.Dataset({"v": ("i", v)}, coords={"i": np.arange(len(v))})

def split(v):  # two groups of dims, so from_dataset makes tables g.i and g.i_j
    return xr.Dataset(
        {"v": ("i", v), "t": (("i", "j"), np.ones((len(v), 2)))},
        coords={"i": np.arange(len(v)), "j": np.arange(2)},
    )

1. A CREATE OR REPLACE TABLE that fails while it runs. DataFusion drops the old table before it computes the new rows. If computing them fails, the old table is gone, but Context.sql raised before it could count the replacement:

ctx = Context(); ctx.from_dataset("a", ds(np.arange(3.0)))
q = ctx.prepare("SELECT v FROM a ORDER BY i")
ctx.sql("CREATE OR REPLACE TABLE a AS SELECT 1 / (v - v) AS v FROM (VALUES (CAST(1 AS INT))) t(v)")
# Arrow error: Divide by zero error
ctx.table_exist("a")  # False
q()["v"]              # [0.0, 1.0, 2.0], from a table that no longer exists

A statement that fails while it's planned, such as one that reads a missing table, leaves the old table in place.

2. DROP SCHEMA ... CASCADE. It drops every table in the schema, but only DROP TABLE and CREATE OR REPLACE are counted:

ctx = Context(); ctx.from_dataset("g", split(np.arange(3.0)))
q = ctx.prepare("SELECT v FROM g.i ORDER BY i")
ctx.sql("DROP SCHEMA g CASCADE")
ctx.sql("CREATE SCHEMA g"); ctx.sql("CREATE TABLE g.i AS SELECT 9.0 AS v, 0 AS i")
q()["v"]  # [0.0, 1.0, 2.0], with no error

#323 will fix both.

Activity

  1. added a commit that references this issue on Nov 18, 2026
    567f6c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions