Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions packages/server/duckdb-server/pkg/query.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
from hashlib import sha256
import duckdb

import pyarrow as pa
import pyarrow
import pyarrow.lib

logger = logging.getLogger(__name__)


def get_key(sql, command):
def get_key(sql: str, command: str) -> str:
return f"{sha256(sql.encode('utf-8')).hexdigest()}.{command}"


Expand All @@ -26,21 +28,24 @@ def retrieve(cache, query, get):
return result


def get_arrow(con, sql):
def get_arrow(
con: duckdb.DuckDBPyConnection, sql: str
) -> pyarrow.lib.RecordBatchReader:
return con.query(sql).arrow()


def arrow_to_bytes(arrow):
sink = pa.BufferOutputStream()
with pa.ipc.new_stream(sink, arrow.schema) as writer:
writer.write(arrow)
def arrow_to_bytes(reader: pyarrow.lib.RecordBatchReader):
sink = pyarrow.BufferOutputStream()
with pyarrow.ipc.new_stream(sink, reader.schema) as writer:
for batch in reader:
writer.write(batch)
return sink.getvalue().to_pybytes()


def get_arrow_bytes(con, sql):
def get_arrow_bytes(con: duckdb.DuckDBPyConnection, sql: str):
return arrow_to_bytes(get_arrow(con, sql))


def get_json(con, sql):
def get_json(con: duckdb.DuckDBPyConnection, sql: str):
result = con.query(sql).df()
return result.to_json(orient="records")
3 changes: 2 additions & 1 deletion packages/server/duckdb-server/pkg/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import ujson
from socketify import App, CompressOptions, OpCode
import duckdb

from pkg.query import get_arrow_bytes, get_json, retrieve

Expand Down Expand Up @@ -72,7 +73,7 @@ def error(self, error):
self.res.end(str(error))


def handle_query(handler: Handler, con, cache, query):
def handle_query(handler: Handler, con: duckdb.DuckDBPyConnection, cache, query):
logger.debug(f"{query=}")

start = time.time()
Expand Down
2 changes: 1 addition & 1 deletion packages/server/duckdb-server/pkg/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ def test_query_arrow():
my_schema = pa.schema([pa.field("a", pa.int32())])
table = pa.Table.from_pylist([{"a": 1}], schema=my_schema)

assert partial(get_arrow, con)("SELECT 1 AS a") == table
assert partial(get_arrow, con)("SELECT 1 AS a").read_all() == table
2 changes: 1 addition & 1 deletion packages/server/duckdb-server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"diskcache",
"duckdb>=1.3.1",
"duckdb>=1.4.1",
"pandas",
"pyarrow",
"socketify",
Expand Down
2 changes: 1 addition & 1 deletion packages/vgplot/widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Learn how to install and use the widget in the [Mosaic documentation](https://uw

We use [uv](https://docs.astral.sh/uv/) to manage our development setup.

You can start Jupyter with `ANYWIDGET_HMR=1 uv run jupyter lab --notebook-dir=../../dev/notebooks`.
You can start Jupyter with `ANYWIDGET_HMR=1 uv run jupyter lab --notebook-dir=../../../dev/notebooks`.

Run `npm run build` to build the widget JavaScript code. If you want to live edit the widget code, run `npm run dev` in a separate terminal.

Expand Down
3 changes: 2 additions & 1 deletion packages/vgplot/widget/mosaic_widget/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def _handle_custom_msg(self, data: dict, buffers: list):
result = self.con.query(sql).arrow()
sink = pa.BufferOutputStream()
with pa.ipc.new_stream(sink, result.schema) as writer:
writer.write(result)
for batch in result:
writer.write(batch)
buf = sink.getvalue()

self.send({"type": "arrow", "uuid": uuid}, buffers=[buf.to_pybytes()])
Expand Down
6 changes: 3 additions & 3 deletions packages/vgplot/widget/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ dynamic = ["version"]
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"anywidget>=0.9.0",
"duckdb>=1.3.1",
"narwhals>=1.46.0",
"anywidget>=0.9.8",
"duckdb>=1.4.1",
"narwhals>=2.8.0",
"pyarrow",
]

Expand Down
2,588 changes: 1,517 additions & 1,071 deletions uv.lock

Large diffs are not rendered by default.