Repository navigation
Closed
Labels
bugSomething isn't working
Description
🌀 Claude: A prepared query can return stale results after a table it reads is replaced, when it reads that table only in a subquery or through a view. There's no error; the answer is just old. The datafusion-accelerated agent found this on #314, and I reproduced it against main (b6f13b1's Python code, DataFusion 55.0.0).
Cause
PreparedQuery plans once and then checks that nothing it reads has been replaced. It finds what it reads with _tables_read, and it learns about replacements from _deregistrations. Each has a gap:
_tables_readfollowsLogicalPlan.inputs(), which doesn't reach a subquery. A subquery lives inside an expression (ScalarSubquery,InSubquery,Exists), not among a node's inputs._TABLE_DDLmatches onlyCREATE OR REPLACE TABLEandDROP TABLE, so replacing or dropping a view isn't counted. And_tables_readsees through the view to the tables beneath it, so it never lists the view itself.
Repro
import numpy as np, xarray as xr
from xarray_sql import Context
from xarray_sql.sql import _tables_read
def ds(vals):
return xr.Dataset({"v": ("k", np.ones(len(vals)))}, coords={"k": np.array(vals)})
ctx = Context()
ctx.from_dataset("a", ds([1, 2]))
ctx.from_dataset("b", ds([9]))
# 1. A table read only in a subquery.
q = "SELECT k, (SELECT max(k) FROM b) AS m FROM a ORDER BY k"
print(sorted(_tables_read(ctx.sql(q).logical_plan()))) # ['a']
p = ctx.prepare(q)
print(p().column("m").to_pylist()) # [9, 9]
ctx.deregister_table("b"); ctx.from_dataset("b", ds([100]))
print(ctx.sql(q).to_arrow_table().column("m").to_pylist()) # [100, 100]
print(p().column("m").to_pylist()) # [9, 9], stale
# 2. A view replaced.
ctx.sql("CREATE VIEW vw AS SELECT k FROM b")
pv = ctx.prepare("SELECT k FROM vw ORDER BY k")
print(pv().column("k").to_pylist()) # [100]
ctx.sql("CREATE OR REPLACE VIEW vw AS SELECT k FROM a")
print(ctx.sql("SELECT k FROM vw ORDER BY k").to_arrow_table().column("k").to_pylist()) # [1, 2]
print(pv().column("k").to_pylist()) # [100], staleIN (SELECT ...) and EXISTS (SELECT ...) miss b the same way. Optimizing the plan first decorrelates those two into joins, but not the uncorrelated scalar subquery.
Fix
- Make the walk reach subqueries, by searching each node's expressions for subquery variants. This will become the public
tables_readproposed on DataFusion Accelerated needs: a release on DataFusion 55, and public hooks for the MLX package #314. - Count views: add
CREATE OR REPLACE VIEWandDROP VIEWto_TABLE_DDL, and list a view's own name among the names a query reads. - Make the check conservative: a prepared query plans again after any table or view is replaced or dropped. Then a form the walk misses costs a replan, not a stale answer. Replacing tables is rare next to running a prepared query, so the cost is small. The walk still decides which tables a call may write; a table it misses there gets an error, not a wrong answer.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
Release 0.7.1 (#321)
Plan a prepared query again after a table in a subquery, or a view, c…