Skip to content

A prepared query returns stale results after a table read only in a subquery, or a view, is replaced #319

Closed

Description

@alxmrs

🌀 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:

  1. _tables_read follows LogicalPlan.inputs(), which doesn't reach a subquery. A subquery lives inside an expression (ScalarSubquery, InSubquery, Exists), not among a node's inputs.
  2. _TABLE_DDL matches only CREATE OR REPLACE TABLE and DROP TABLE, so replacing or dropping a view isn't counted. And _tables_read sees 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], stale

IN (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_read proposed on DataFusion Accelerated needs: a release on DataFusion 55, and public hooks for the MLX package #314.
  • Count views: add CREATE OR REPLACE VIEW and DROP VIEW to _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.
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions