Skip to content

Commit 2d130e5

Browse files
rustyconoverclaude
andcommitted
table_function: expose transaction_storage in BindParams
Lets cardinality() and statistics() share the same per-transaction storage that on_init reads/writes, so workers can cache expensive bind-time lookups (e.g. remote watermarks) once per SQL transaction instead of once per bind/cardinality/statistics/init phase. _make_bind_params constructs the TransactionBoundStorage from bind_call.transaction_id and cls.storage; None when transaction_id is unset (the framework normally provides one under autocommit). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b02a467 commit 2d130e5

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎vgi/table_function.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
_accepts_none,
3737
_extract_setting_secret_params,
3838
)
39-
from vgi.function_storage import BoundStorage
39+
from vgi.function_storage import BoundStorage, TransactionBoundStorage
4040
from vgi.invocation import (
4141
BaseInitResponse,
4242
BindResponse,
@@ -294,6 +294,13 @@ class BindParams[TArgs]:
294294
# Convenient access to settings and secrets, extracted from the bind_call.
295295
settings: dict[str, pa.Scalar[Any]]
296296
secrets: SecretsAccessor
297+
# Transaction-scoped storage view. Lets ``cardinality()`` and
298+
# ``statistics()`` cache expensive lookups (e.g. Kafka watermarks)
299+
# in the same store ``on_init`` reads/writes for snapshot isolation
300+
# — so a topic's row count is fetched once per SQL transaction
301+
# rather than once per bind/cardinality/statistics/init phase.
302+
# ``None`` when ``bind_call.transaction_id`` is unset.
303+
transaction_storage: TransactionBoundStorage | None = None
297304
auth_context: AuthContext = AuthContext.anonymous()
298305

299306

@@ -574,11 +581,13 @@ def _make_bind_params(
574581
Shared by bind() and table_function_cardinality() to avoid
575582
duplicating BindParams construction logic.
576583
"""
584+
txn_id = input.transaction_id
577585
return BindParams[TArgs](
578586
args=cls._parse_arguments(cls.FunctionArguments, input.arguments),
579587
bind_call=input,
580588
settings=_batch_to_scalar_dict(input.settings),
581589
secrets=SecretsAccessor(input.secrets, is_retry=input.resolved_secrets_provided),
590+
transaction_storage=TransactionBoundStorage(cls.storage, txn_id) if txn_id else None,
582591
auth_context=auth_context if auth_context is not None else AuthContext.anonymous(),
583592
)
584593

0 commit comments

Comments
 (0)