Result of sql(..).execute() still refers to local context variables.
What happens?
The relation returned by duckdb.sql(..).execute() still refers to local context variables, such that if it is used in a different context (e.g., after being returned by a function) it can either fail or return unwanted results.
(see https://github.com/duckdb/duckdb/discussions/10962 for context)
To Reproduce
import duckdb
def f():
a = duckdb.sql('SELECT * from range(1)').execute()
b = duckdb.sql("SELECT * FROM a").execute()
return b
print(f())
InvalidInputException: Invalid Input Error: Attempting to execute an unsuccessful or closed pending query result
Error: Catalog Error: Table with name a does not exist!
Did you mean "pg_am"?
Even worse
import duckdb
def f():
a = duckdb.sql('SELECT * from range(1)').execute()
b = duckdb.sql("SELECT * FROM a").execute()
return b
a = duckdb.sql('SELECT * FROM range(2)')
print(f())
┌───────┐
│ range │
│ int64 │
├───────┤
│ 0 │
│ 1 │
└───────┘
OS:
Linux
DuckDB Version:
0.10.1-dev126
DuckDB Client:
Python
Full Name:
Soeren Wolfers
Affiliation:
G-Research
Have you tried this on the latest nightly build?
I have tested with a nightly build
Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?
- [X] Yes, I have
Thanks for the report, I believe I discovered this issue myself a couple days ago, if it's the same issue I am aware of the cause and am working on a fix 👍
This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.
Both queries now return a single row (with value 0)