|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""Three small catalogs for the ``catalog_contents`` RPC. |
| 4 | +
|
| 5 | +The same two-schema catalog is served under three names, differing only in how |
| 6 | +they answer ``catalog_contents`` (see ``vgi/docs/catalog_contents.md``): |
| 7 | +
|
| 8 | +``contents_probe`` |
| 9 | + Advertises ``supports_catalog_contents`` and serves it normally (the |
| 10 | + ``ReadOnlyCatalogInterface`` default). A client loads it in one RPC. |
| 11 | +``contents_broken`` |
| 12 | + Advertises ``supports_catalog_contents`` but its ``catalog_contents`` |
| 13 | + raises. A client must fall back to ``catalog_schemas`` plus the per-schema |
| 14 | + ``catalog_schema_contents_*`` RPCs and still see the whole catalog. |
| 15 | +``contents_legacy`` |
| 16 | + Does not advertise it, like an older worker. A client must never call |
| 17 | + ``catalog_contents`` and must use the per-schema RPCs. |
| 18 | +``contents_memory`` |
| 19 | + A DDL-capable ``InMemoryCatalog`` (non-frozen version, bumped by every |
| 20 | + DDL) that advertises ``catalog_contents``, for invalidation after DDL. Each |
| 21 | + ATTACH gets its own empty catalog (one ``main`` schema), so tests sharing a |
| 22 | + warm worker never see each other's objects. |
| 23 | +
|
| 24 | +Every kind the client seeds from ``catalog_contents`` is present at least once |
| 25 | +(tables, a view, scalar / aggregate / table functions, scalar and table |
| 26 | +macros), split over two schemas (``main`` and ``extra``), so RPC accounting in |
| 27 | +``vgi/test/sql/integration/catalog/catalog_contents*.test`` is deterministic. |
| 28 | +Served by ``vgi-fixture-worker`` and ``vgi-fixture-http`` (MetaWorker), so the |
| 29 | +same tests run over every transport. |
| 30 | +""" |
| 31 | + |
| 32 | +from __future__ import annotations |
| 33 | + |
| 34 | +import uuid |
| 35 | +from dataclasses import replace |
| 36 | +from typing import Any |
| 37 | + |
| 38 | +import pyarrow as pa |
| 39 | + |
| 40 | +from vgi._test_fixtures.aggregate.basic import SumFunction |
| 41 | +from vgi._test_fixtures.catalog import CatalogData, InMemoryCatalog, SchemaData |
| 42 | +from vgi._test_fixtures.scalar.arithmetic import DoubleFunction |
| 43 | +from vgi._test_fixtures.table.sequence import SequenceFunction |
| 44 | +from vgi.arguments import Arguments |
| 45 | +from vgi.catalog import ( |
| 46 | + AttachOpaqueData, |
| 47 | + Catalog, |
| 48 | + CatalogAttachResult, |
| 49 | + CatalogInfo, |
| 50 | + Macro, |
| 51 | + MacroType, |
| 52 | + ReadOnlyCatalogInterface, |
| 53 | + Schema, |
| 54 | + SchemaContentsInfo, |
| 55 | + SchemaInfo, |
| 56 | + Table, |
| 57 | + View, |
| 58 | +) |
| 59 | +from vgi.worker import Worker |
| 60 | + |
| 61 | +CATALOG_PROBE = "contents_probe" |
| 62 | +CATALOG_BROKEN = "contents_broken" |
| 63 | +CATALOG_LEGACY = "contents_legacy" |
| 64 | +CATALOG_MEMORY = "contents_memory" |
| 65 | + |
| 66 | +BROKEN_MESSAGE = "contents_broken: catalog_contents deliberately fails" |
| 67 | + |
| 68 | + |
| 69 | +def _catalog(name: str) -> Catalog: |
| 70 | + return Catalog( |
| 71 | + name=name, |
| 72 | + default_schema="main", |
| 73 | + comment=f"catalog_contents test catalog ({name})", |
| 74 | + schemas=[ |
| 75 | + Schema( |
| 76 | + path=["main"], |
| 77 | + comment="Every object kind", |
| 78 | + tables=[ |
| 79 | + Table( |
| 80 | + name="ten", |
| 81 | + function=SequenceFunction, |
| 82 | + arguments=Arguments(positional=(pa.scalar(10),)), |
| 83 | + comment="Integers 0..9", |
| 84 | + ), |
| 85 | + ], |
| 86 | + views=[View(name="answer", definition="SELECT 42 AS answer", comment="One row")], |
| 87 | + functions=[DoubleFunction, SumFunction, SequenceFunction], |
| 88 | + macros=[ |
| 89 | + Macro( |
| 90 | + name="contents_triple", |
| 91 | + macro_type=MacroType.SCALAR, |
| 92 | + parameters=["x"], |
| 93 | + definition="x * 3", |
| 94 | + comment="Triple a value", |
| 95 | + ), |
| 96 | + Macro( |
| 97 | + name="contents_range", |
| 98 | + macro_type=MacroType.TABLE, |
| 99 | + parameters=["n"], |
| 100 | + definition="SELECT * FROM range(n)", |
| 101 | + comment="Table macro over range(n)", |
| 102 | + ), |
| 103 | + ], |
| 104 | + ), |
| 105 | + Schema( |
| 106 | + path=["extra"], |
| 107 | + comment="A second schema, tables only", |
| 108 | + tables=[ |
| 109 | + Table( |
| 110 | + name="five", |
| 111 | + function=SequenceFunction, |
| 112 | + arguments=Arguments(positional=(pa.scalar(5),)), |
| 113 | + comment="Integers 0..4", |
| 114 | + ), |
| 115 | + ], |
| 116 | + ), |
| 117 | + ], |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +_CATALOG_PROBE = _catalog(CATALOG_PROBE) |
| 122 | +_CATALOG_BROKEN = _catalog(CATALOG_BROKEN) |
| 123 | +_CATALOG_LEGACY = _catalog(CATALOG_LEGACY) |
| 124 | + |
| 125 | + |
| 126 | +class ContentsProbeCatalog(ReadOnlyCatalogInterface): |
| 127 | + """Advertises and serves ``catalog_contents``.""" |
| 128 | + |
| 129 | + catalog = _CATALOG_PROBE |
| 130 | + catalog_name = CATALOG_PROBE |
| 131 | + |
| 132 | + |
| 133 | +class ContentsBrokenCatalog(ReadOnlyCatalogInterface): |
| 134 | + """Advertises ``catalog_contents`` but fails to serve it.""" |
| 135 | + |
| 136 | + catalog = _CATALOG_BROKEN |
| 137 | + catalog_name = CATALOG_BROKEN |
| 138 | + |
| 139 | + def catalog_contents(self, *, attach_opaque_data: Any) -> list[SchemaContentsInfo]: |
| 140 | + """Always fail, to drive the client's per-schema fallback.""" |
| 141 | + raise RuntimeError(BROKEN_MESSAGE) |
| 142 | + |
| 143 | + |
| 144 | +class ContentsLegacyCatalog(ReadOnlyCatalogInterface): |
| 145 | + """Does not advertise ``catalog_contents`` (an older worker).""" |
| 146 | + |
| 147 | + catalog = _CATALOG_LEGACY |
| 148 | + catalog_name = CATALOG_LEGACY |
| 149 | + |
| 150 | + def catalog_attach(self, **kwargs: Any) -> CatalogAttachResult: |
| 151 | + """Attach as the base class does, without the capability flag.""" |
| 152 | + return replace(super().catalog_attach(**kwargs), supports_catalog_contents=False) |
| 153 | + |
| 154 | + |
| 155 | +class ContentsProbeWorker(Worker): |
| 156 | + """Serves ``contents_probe``.""" |
| 157 | + |
| 158 | + catalog_interface = ContentsProbeCatalog |
| 159 | + catalog_name = CATALOG_PROBE |
| 160 | + catalog = _CATALOG_PROBE |
| 161 | + |
| 162 | + |
| 163 | +class ContentsBrokenWorker(Worker): |
| 164 | + """Serves ``contents_broken``.""" |
| 165 | + |
| 166 | + catalog_interface = ContentsBrokenCatalog |
| 167 | + catalog_name = CATALOG_BROKEN |
| 168 | + catalog = _CATALOG_BROKEN |
| 169 | + |
| 170 | + |
| 171 | +class ContentsLegacyWorker(Worker): |
| 172 | + """Serves ``contents_legacy``.""" |
| 173 | + |
| 174 | + catalog_interface = ContentsLegacyCatalog |
| 175 | + catalog_name = CATALOG_LEGACY |
| 176 | + catalog = _CATALOG_LEGACY |
| 177 | + |
| 178 | + |
| 179 | +class ContentsMemoryCatalog(InMemoryCatalog): |
| 180 | + """DDL-capable in-memory catalog that advertises ``catalog_contents``. |
| 181 | +
|
| 182 | + Every ATTACH of ``contents_memory`` gets a private, empty catalog (one |
| 183 | + ``main`` schema) registered under an internal name, so state never leaks |
| 184 | + between attaches that share one worker process. |
| 185 | + """ |
| 186 | + |
| 187 | + def __init__(self) -> None: |
| 188 | + """Start with no catalogs; each attach creates its own.""" |
| 189 | + super().__init__() |
| 190 | + self._catalogs.clear() |
| 191 | + |
| 192 | + def catalogs(self) -> list[CatalogInfo]: |
| 193 | + """Advertise only the public name (MetaWorker routes on it).""" |
| 194 | + return [CatalogInfo(name=CATALOG_MEMORY, implementation_version=None, data_version_spec=None)] |
| 195 | + |
| 196 | + def catalog_attach( |
| 197 | + self, |
| 198 | + *, |
| 199 | + name: str, |
| 200 | + options: dict[str, Any], |
| 201 | + data_version_spec: str | None, |
| 202 | + implementation_version: str | None, |
| 203 | + ctx: Any = None, |
| 204 | + ) -> CatalogAttachResult: |
| 205 | + """Attach a fresh private catalog, advertising ``catalog_contents``.""" |
| 206 | + if name != CATALOG_MEMORY: |
| 207 | + raise ValueError(f"Unknown catalog: {name!r}. Available: {CATALOG_MEMORY}") |
| 208 | + private = f"{CATALOG_MEMORY}/{uuid.uuid4().hex}" |
| 209 | + catalog = CatalogData(name=private) |
| 210 | + catalog.schemas[("main",)] = SchemaData( |
| 211 | + info=SchemaInfo(attach_opaque_data=AttachOpaqueData(b"\x00" * 16), path=["main"], comment=None, tags={}) |
| 212 | + ) |
| 213 | + self._catalogs[private] = catalog |
| 214 | + result = super().catalog_attach( |
| 215 | + name=private, |
| 216 | + options=options, |
| 217 | + data_version_spec=data_version_spec, |
| 218 | + implementation_version=implementation_version, |
| 219 | + ctx=ctx, |
| 220 | + ) |
| 221 | + return replace(result, supports_catalog_contents=True) |
| 222 | + |
| 223 | + def catalog_detach(self, *, attach_opaque_data: AttachOpaqueData) -> None: |
| 224 | + """Detach and drop the private catalog.""" |
| 225 | + private = self._attachments.pop(attach_opaque_data, None) |
| 226 | + if private is not None: |
| 227 | + self._catalogs.pop(private, None) |
| 228 | + |
| 229 | + |
| 230 | +class ContentsMemoryWorker(Worker): |
| 231 | + """Serves ``contents_memory``.""" |
| 232 | + |
| 233 | + catalog_interface = ContentsMemoryCatalog |
| 234 | + catalog_name = CATALOG_MEMORY |
| 235 | + |
| 236 | + |
| 237 | +CONTENTS_WORKERS: list[type[Worker]] = [ |
| 238 | + ContentsProbeWorker, |
| 239 | + ContentsBrokenWorker, |
| 240 | + ContentsLegacyWorker, |
| 241 | + ContentsMemoryWorker, |
| 242 | +] |
0 commit comments