Skip to content

Commit

Permalink
fix: support duckdb 1.1.0, update globals() for scanning (#2279)
Browse files Browse the repository at this point in the history
* fix: support duckdb 1.1.0, update globals() for scanning

* clear when adding back originals

* unused

* minor edits

* don't modify globals; just exec with the correct globals

---------

Co-authored-by: Akshay Agrawal <akshay@marimo.io>
  • Loading branch information
mscolnick and akshayka authored Sep 10, 2024
1 parent baeb3bc commit 9b00451
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion marimo/_sql/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from marimo._output.rich_help import mddoc
from marimo._plugins.ui._impl import table
from marimo._runtime import output
from marimo._runtime.context.types import (
ContextNotInitializedError,
get_context,
)


def get_default_result_limit() -> Optional[int]:
Expand Down Expand Up @@ -37,7 +41,22 @@ def sql(

import duckdb # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501

relation = duckdb.sql(query=query)
# In Python globals() are scoped to modules; since this function
# is in a different module than user code, globals() doesn't return
# the kernel globals, it just returns this module's global namespace.
#
# However, duckdb needs access to the kernel's globals. For this reason,
# we manually exec duckdb and provide it with the kernel's globals.
try:
ctx = get_context()
except ContextNotInitializedError:
relation = duckdb.sql(query=query)
else:
relation = eval(
"duckdb.sql(query=query)",
ctx.globals,
{"query": query, "duckdb": duckdb},
)

if not relation:
return None
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ dev = [
# For server testing
"httpx~=0.27.0",
# For SQL
"duckdb~=1.0.0",
"duckdb~=1.1.0",
# For testing mo.ui.chart, dataframes, tables
"pandas>=1.5.3",
"pandas-stubs>=1.5.3.230321",
Expand Down

0 comments on commit 9b00451

Please sign in to comment.