Skip to content

Commit 89772c9

Browse files
rustyconoverclaude
andcommitted
catalog: partition functions by type in estimated_object_count
Schema.to_schema_info reported every function under the scalar_function key with the others left absent, which the C++ client interprets as "count=1, eager-load". DuckDB's name resolution probes scalar → aggregate → table separately, so a worker with only table functions still paid three catalog_schema_contents_functions RPCs per cold miss (one per category) — two of which returned empty. Walk self.functions, partition by func.get_metadata().function_type, emit explicit 0 for the categories the schema doesn't populate. The C++ client treats count=0 as a hard guarantee and skips both the bulk RPC and per-name lookups for that category. For the Ducklake worker (28 declared functions, all TableFunctionGenerator) this collapses 3 schema_contents_functions RPCs to 1. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 57a9113 commit 89772c9

2 files changed

Lines changed: 68 additions & 6 deletions

File tree

‎tests/catalog/test_declarative.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,58 @@ def test_schema_to_schema_info(self) -> None:
901901
assert info.comment == "Main schema"
902902
assert info.tags == {"type": "core"}
903903

904+
def test_estimated_object_count_partitions_functions_by_type(self) -> None:
905+
"""Functions are split into scalar/aggregate/table by metadata type.
906+
907+
DuckDB's name resolution probes each function category separately
908+
(scalar → aggregate → table). Each category triggers its own
909+
``catalog_schema_contents_functions`` RPC the first time the C++
910+
client hits a cold miss. Reporting an explicit ``0`` for empty
911+
categories lets the client skip the bulk RPC entirely.
912+
"""
913+
from typing import Annotated
914+
915+
from vgi.arguments import Param, Returns
916+
from vgi.scalar_function import ScalarFunction
917+
918+
class _Upper(ScalarFunction):
919+
class Meta:
920+
name = "upper"
921+
922+
@classmethod
923+
def compute(
924+
cls, x: Annotated[pa.StringArray, Param(doc="x")]
925+
) -> Annotated[pa.StringArray, Returns()]:
926+
return pa.compute.utf8_upper(x)
927+
928+
schema = Schema(
929+
name="main",
930+
functions=[UsersFunction, EventsFunction, _Upper], # 2 table + 1 scalar
931+
)
932+
info = schema.to_schema_info(AttachId(b"x"))
933+
assert info.estimated_object_count == {
934+
"table": 0,
935+
"view": 0,
936+
"scalar_function": 1,
937+
"aggregate_function": 0,
938+
"table_function": 2,
939+
"macro": 0,
940+
"index": 0,
941+
}
942+
943+
def test_estimated_object_count_zeros_empty_categories(self) -> None:
944+
"""Empty function categories emit 0 — load-bearing for the client.
945+
946+
The C++ extension treats explicit 0 as a hard guarantee and skips
947+
both the bulk listing and per-name lookups; absence reads as
948+
"unknown / eager-load" instead.
949+
"""
950+
schema = Schema(name="main", functions=[UsersFunction]) # only table fn
951+
info = schema.to_schema_info(AttachId(b"x"))
952+
assert info.estimated_object_count["scalar_function"] == 0
953+
assert info.estimated_object_count["aggregate_function"] == 0
954+
assert info.estimated_object_count["table_function"] == 1
955+
904956

905957
# =============================================================================
906958
# Catalog Descriptor Tests

‎vgi/catalog/descriptors.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
ViewInfo,
3232
)
3333
from vgi.invocation import BindResponse, FunctionType
34+
from vgi.metadata import CatalogFunctionType
3435

3536
if TYPE_CHECKING:
3637
from vgi.function import Function
@@ -660,18 +661,25 @@ def to_schema_info(self, attach_id: AttachId) -> SchemaInfo:
660661
Populates ``estimated_object_count`` from the declared population so
661662
the C++ extension's eager-load gate can choose between bulk
662663
``LoadEntries`` and per-name single-entry RPCs without an extra round
663-
trip. Splitting functions into scalar / aggregate / table requires a
664-
type discriminator we don't have here, so we report the total under
665-
the ``scalar_function`` key and leave the others at the client-side
666-
default of 1 (i.e. eager-load) — workers wanting finer control can
667-
return their own SchemaInfo rather than calling this helper.
664+
trip. Functions are partitioned by ``get_metadata().function_type``
665+
into the three keys (``scalar_function``, ``aggregate_function``,
666+
``table_function``) so DuckDB's per-type catalog probes (a name
667+
lookup walks scalar → aggregate → table) skip the bulk RPC for any
668+
category the schema doesn't populate.
668669
669670
**Zero counts are load-bearing.** Empty declarative collections
670671
(e.g. ``views=()``) emit ``0`` here, which the C++ client treats as
671672
a hard guarantee and uses to skip the corresponding bulk + per-name
672673
RPCs entirely. Do not "optimize" this into omitting empty keys —
673674
absence reads as count=1 (unknown), suppressing the RPC bypass.
674675
"""
676+
function_counts = {
677+
CatalogFunctionType.SCALAR: 0,
678+
CatalogFunctionType.AGGREGATE: 0,
679+
CatalogFunctionType.TABLE: 0,
680+
}
681+
for func in self.functions:
682+
function_counts[func.get_metadata().function_type] += 1
675683
return SchemaInfo(
676684
attach_id=attach_id,
677685
name=self.name,
@@ -680,7 +688,9 @@ def to_schema_info(self, attach_id: AttachId) -> SchemaInfo:
680688
estimated_object_count={
681689
"table": len(self.tables),
682690
"view": len(self.views),
683-
"scalar_function": len(self.functions),
691+
"scalar_function": function_counts[CatalogFunctionType.SCALAR],
692+
"aggregate_function": function_counts[CatalogFunctionType.AGGREGATE],
693+
"table_function": function_counts[CatalogFunctionType.TABLE],
684694
"macro": len(self.macros),
685695
"index": len(self.indexes),
686696
},

0 commit comments

Comments
 (0)