Skip to content

Commit 7ebbe19

Browse files
rustyconoverclaude
andcommitted
catalog: inline column_statistics on TableInfo
Add a `column_statistics: bytes | None` field at the end of TableInfo carrying the IPC payload of `serialize_column_statistics`. When populated, the C++ extension uses the cached value and skips both the per-scan `table_function_statistics` RPC and the per-table `catalog_table_column_statistics_get` RPC. `Table.to_table_info` auto-inlines from `Table.statistics` via the existing `resolve_column_statistics` helper. The trade-off — stats are frozen for the lifetime of the catalog cache (until catalog_version bumps) — is documented on the field's docstring, mirroring the warning on `cardinality_estimate`. Workers whose stats change faster (live counters) must override `to_table_info` and leave column_statistics null. Audited every existing `Table(statistics=...)` caller in vgi/_test_fixtures and tests; all use static literal values, no live counters, safe to flip the contract. Tests cover: round-trip the IPC blob through TableInfo serialize/deserialize, descriptor populates from `Table.statistics`, descriptor leaves it null when statistics is empty, schema-shape asserts the field is positionally last for old-extension compat. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent dcb9339 commit 7ebbe19

4 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎tests/catalog/test_declarative.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,44 @@ def test_explicit_columns_table_omits_scan_function(self) -> None:
270270
assert info.update_function is None
271271
assert info.delete_function is None
272272

273+
def test_table_inlines_resolved_column_statistics(self) -> None:
274+
"""``Table.statistics`` flows into ``TableInfo.column_statistics`` as IPC bytes.
275+
276+
The C++ extension reads the inlined blob and skips the per-scan
277+
``table_function_statistics`` and per-table
278+
``catalog_table_column_statistics_get`` RPCs.
279+
"""
280+
from vgi_rpc.utils import deserialize_record_batch
281+
282+
from vgi.catalog.descriptors import ColumnStatisticsInput
283+
284+
table = Table(
285+
name="users",
286+
function=UsersFunction,
287+
statistics={
288+
"id": ColumnStatisticsInput(min=0, max=999_999, has_null=False, distinct_count=1_000_000),
289+
},
290+
)
291+
info = table.to_table_info("main")
292+
assert info.supports_column_statistics is True
293+
assert info.column_statistics is not None
294+
# The blob is an Arrow IPC RecordBatch; confirm it deserializes and
295+
# carries the column we declared.
296+
batch, _ = deserialize_record_batch(info.column_statistics)
297+
column_names = batch.column("column_name").to_pylist()
298+
assert column_names == ["id"]
299+
300+
def test_table_without_statistics_omits_column_statistics(self) -> None:
301+
"""Tables without ``statistics`` set leave column_statistics null.
302+
303+
Workers without static stats keep the existing on-demand RPC path —
304+
no regression.
305+
"""
306+
table = Table(name="users", function=UsersFunction)
307+
info = table.to_table_info("main")
308+
assert info.supports_column_statistics is False
309+
assert info.column_statistics is None
310+
273311

274312
class TestTableValidation:
275313
"""Tests for Table validation errors."""

‎tests/catalog/test_serialization.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,51 @@ def test_omitted_inline_fields_default_to_none(self) -> None:
378378
assert restored.delete_function is None
379379
assert restored.cardinality_estimate is None
380380
assert restored.cardinality_max is None
381+
assert restored.column_statistics is None
382+
383+
def test_inlined_column_statistics_round_trip(self) -> None:
384+
"""``column_statistics`` IPC bytes round-trip through TableInfo.
385+
386+
The C++ extension reads this field (when populated) and skips the
387+
per-scan ``table_function_statistics`` and per-table
388+
``catalog_table_column_statistics_get`` RPCs.
389+
"""
390+
from vgi.catalog.catalog_interface import ColumnStatistics, serialize_column_statistics
391+
392+
columns_schema = schema(id=pa.int64())
393+
columns_bytes = SerializedSchema(columns_schema.serialize().to_pybytes())
394+
stats_blob = serialize_column_statistics(
395+
[
396+
ColumnStatistics(
397+
column_name="id",
398+
min=pa.scalar(0, pa.int64()),
399+
max=pa.scalar(999, pa.int64()),
400+
has_null=False,
401+
distinct_count=1000,
402+
),
403+
],
404+
cache_max_age_seconds=3600,
405+
)
406+
original = TableInfo(
407+
name="t",
408+
schema_name="s",
409+
columns=columns_bytes,
410+
not_null_constraints=[],
411+
unique_constraints=[],
412+
check_constraints=[],
413+
comment=None,
414+
tags={},
415+
supports_column_statistics=True,
416+
column_statistics=stats_blob,
417+
)
418+
serialized = original.serialize_to_bytes()
419+
batch, _ = deserialize_record_batch(serialized)
420+
restored = TableInfo.deserialize_from_batch(batch)
421+
assert restored.column_statistics is not None
422+
assert restored.column_statistics == stats_blob
423+
# Other inline fields default to None.
424+
assert restored.scan_function is None
425+
assert restored.cardinality_estimate is None
381426

382427

383428
class TestViewInfoSerialization:
@@ -926,6 +971,13 @@ def test_table_info_schema(self) -> None:
926971
assert schema.field("not_null_constraints").type == pa.list_(pa.int32())
927972
assert schema.field("unique_constraints").type == pa.list_(pa.list_(pa.int32()))
928973
assert schema.field("check_constraints").type == pa.list_(pa.string())
974+
# Inlined optional fields — populated to skip per-bind RPCs.
975+
assert schema.field("scan_function").type == pa.binary()
976+
assert schema.field("cardinality_estimate").type == pa.int64()
977+
assert schema.field("column_statistics").type == pa.binary()
978+
# column_statistics is the LAST field (positional schema compatibility:
979+
# old C++ extensions ignore-by-position trailing fields).
980+
assert schema.field(len(schema) - 1).name == "column_statistics"
929981

930982
def test_view_info_schema(self) -> None:
931983
"""Verify ViewInfo Arrow schema."""

‎vgi/catalog/catalog_interface.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ class TableInfo(CatalogSchemaObject, ArrowSerializableDataclass):
268268
cardinality_estimate: Annotated[int | None, ArrowType(pa.int64())] = None
269269
cardinality_max: Annotated[int | None, ArrowType(pa.int64())] = None
270270

271+
# Optional inlined column statistics. When populated, the C++ extension
272+
# uses the cached value and skips the per-bind / per-table
273+
# ``catalog_table_column_statistics_get`` RPC and the per-scan
274+
# ``table_function_statistics`` RPC. Bytes are the IPC payload from
275+
# ``serialize_column_statistics(stats, cache_max_age_seconds)``.
276+
#
277+
# Populating this field freezes the resolved stats for the lifetime of
278+
# the catalog cache (until ``catalog_version`` bumps). Workers whose
279+
# statistics change faster than ``catalog_version`` (e.g. live counters,
280+
# rapidly-mutating dimensions) MUST leave this null so the on-demand
281+
# RPC continues to fire.
282+
column_statistics: Annotated[bytes | None, ArrowType(pa.binary())] = None
283+
271284

272285
@dataclass(frozen=True)
273286
class ViewInfo(CatalogSchemaObject, ArrowSerializableDataclass):

‎vgi/catalog/descriptors.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
TableColumnStatisticsResult,
3131
TableInfo,
3232
ViewInfo,
33+
serialize_column_statistics,
3334
)
3435
from vgi.invocation import BindResponse, FunctionType
3536
from vgi.metadata import CatalogFunctionType
@@ -485,6 +486,21 @@ def to_table_info(self, schema_name: str) -> TableInfo:
485486
cols = self._apply_defaults_to_schema(self.resolved_columns)
486487
cols = self._apply_generated_columns_to_schema(cols)
487488
cols = self._apply_column_comments_to_schema(cols)
489+
# Inline the resolved stats blob so the C++ extension can short-circuit
490+
# the per-scan ``table_function_statistics`` and per-table
491+
# ``catalog_table_column_statistics_get`` RPCs entirely. This freezes
492+
# the resolved stats for the lifetime of the catalog cache; workers
493+
# whose stats change faster than catalog_version must override
494+
# ``to_table_info`` and leave column_statistics null.
495+
resolved_stats = self.resolve_column_statistics()
496+
column_statistics_blob = (
497+
serialize_column_statistics(
498+
resolved_stats.statistics,
499+
resolved_stats.cache_max_age_seconds,
500+
)
501+
if resolved_stats is not None
502+
else None
503+
)
488504
return TableInfo(
489505
name=self.name,
490506
schema_name=schema_name,
@@ -506,6 +522,7 @@ def to_table_info(self, schema_name: str) -> TableInfo:
506522
delete_function=_inline_function_result(self.delete_function),
507523
cardinality_estimate=self.cardinality_estimate,
508524
cardinality_max=self.cardinality_max,
525+
column_statistics=column_statistics_blob,
509526
)
510527

511528
def resolve_column_statistics(self) -> TableColumnStatisticsResult | None:

0 commit comments

Comments
 (0)