-
Notifications
You must be signed in to change notification settings - Fork 1
lazy catalog integration #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| """Lazy (callback-driven) catalog example. | ||
|
|
||
| Riffq can answer PostgreSQL catalog queries (``pg_catalog`` / ``information_schema``) | ||
| from a *source object* that is consulted on every scan, instead of a snapshot | ||
| registered up front. This example backs that source with a plain in-memory dict | ||
| -- no external engine -- so you can see the whole contract in one file. | ||
|
|
||
| Run it:: | ||
|
|
||
| python example/lazy_catalog.py | ||
|
|
||
| then connect with psql and watch the catalog stay in sync as you create tables:: | ||
|
|
||
| psql -h 127.0.0.1 -p 5444 -U user -d appdb | ||
|
|
||
| appdb=> SELECT relname FROM pg_class WHERE relname='orders'; -- 0 rows | ||
| appdb=> CREATE TABLE orders(id INT, total INT); -- handled below | ||
| appdb=> SELECT relname FROM pg_class WHERE relname='orders'; -- now 1 row | ||
| appdb=> \\d orders | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import pyarrow as pa | ||
| import riffq | ||
|
|
||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| # The "engine": a live, in-memory catalog. In a real app this would be DuckDB, | ||
| # PostgreSQL, a config file, a remote service -- anything. The lazy source below | ||
| # reads whatever is here *at query time*, so mutating it is reflected instantly. | ||
| CATALOG = { | ||
| "appdb": { | ||
| "public": { | ||
| # table_name -> list of (column_name, pg_type_oid, nullable) | ||
| "users": [("id", 23, False), ("name", 25, True)], # 23=int4, 25=text | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # pg_type OIDs the example understands, keyed by a coarse type name. | ||
| TYPE_OIDS = {"int": 23, "bigint": 20, "text": 25, "bool": 16, "float": 701} | ||
|
|
||
|
|
||
| def stable_oid(salt: str, *parts: str) -> int: | ||
| """Derive a stable, built-in-clear OID from a name. | ||
|
|
||
| The same inputs always return the same OID, so ``pg_class.oid`` and | ||
| ``pg_attribute.attrelid`` agree across scans and catalog joins resolve. | ||
| Distinct object classes use distinct salts to avoid collisions, and the | ||
| result is kept well above the built-in OID range. | ||
| """ | ||
| h = 5381 | ||
| for ch in (salt + "\x00" + "\x00".join(parts)): | ||
| h = (h * 33 + ord(ch)) & 0x7FFFFFFF | ||
| return 16384 + (h % 2_000_000_000) | ||
|
|
||
|
|
||
| class DictCatalogSource: | ||
| """A lazy catalog source over the in-memory ``CATALOG`` dict. | ||
|
|
||
| Each method receives a ``callback`` and invokes it with a list of row dicts, | ||
| mirroring Riffq's Rust ``LazyCatalogSource`` trait one method per level. | ||
| """ | ||
|
|
||
| def databases(self, callback): | ||
| callback( | ||
| [{"oid": stable_oid("db", name), "name": name} for name in CATALOG] | ||
| ) | ||
|
|
||
| def schemas(self, database, callback): | ||
| callback( | ||
| [ | ||
| {"oid": stable_oid("ns", database, schema), "name": schema} | ||
| for schema in CATALOG.get(database, {}) | ||
| ] | ||
| ) | ||
|
|
||
| def relations(self, database, schema, callback): | ||
| tables = CATALOG.get(database, {}).get(schema, {}) | ||
| callback( | ||
| [ | ||
| { | ||
| "oid": stable_oid("rel", database, schema, name), | ||
| "reltype_oid": stable_oid("type", database, schema, name), | ||
| "name": name, | ||
| "kind": "table", | ||
| } | ||
| for name in tables | ||
| ] | ||
| ) | ||
|
|
||
| def columns(self, database, schema, relation, callback): | ||
| cols = CATALOG.get(database, {}).get(schema, {}).get(relation, []) | ||
| callback( | ||
| [ | ||
| {"name": col, "type_oid": type_oid, "nullable": nullable} | ||
| for (col, type_oid, nullable) in cols | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| class Connection(riffq.BaseConnection): | ||
| """Data path. Catalog queries are served by the lazy source above; everything | ||
| else lands here. We implement a toy ``CREATE TABLE`` so you can watch the | ||
| catalog update live, and echo a single row for any other query.""" | ||
|
|
||
| def handle_auth(self, user, password, host, database=None, callback=callable): | ||
| # Accept any credentials in this example. | ||
| return callback(True) | ||
|
|
||
| def handle_query(self, sql, callback=callable, **kwargs): | ||
| text = sql.strip().rstrip(";") | ||
| low = text.lower() | ||
|
|
||
| if low.startswith("create table "): | ||
| # "create table public.orders(id int, total int)" (schema optional) | ||
| head, _, body = text[len("create table "):].partition("(") | ||
| qualified = head.strip() | ||
| schema, _, name = qualified.rpartition(".") | ||
| schema = schema or "public" | ||
| cols = [] | ||
| for part in body.rstrip(")").split(","): | ||
| bits = part.split() | ||
| if len(bits) >= 2: | ||
| cols.append((bits[0].strip('"'), TYPE_OIDS.get(bits[1].lower(), 25), True)) | ||
| CATALOG.setdefault("appdb", {}).setdefault(schema, {})[name] = cols | ||
| return callback("CREATE TABLE", is_tag=True) | ||
|
|
||
| # Any other statement: return a single dummy row so clients are happy. | ||
| batch = self.arrow_batch([pa.array([1])], ["?column?"]) | ||
| return self.send_reader(batch, callback) | ||
|
|
||
|
|
||
| def main(): | ||
| server = riffq.RiffqServer("127.0.0.1:5444", connection_cls=Connection) | ||
| # Install the lazy source instead of register_database/schema/table. | ||
| server.set_lazy_catalog(DictCatalogSource()) | ||
| server.start(catalog_emulation=True) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded database name breaks multi-database scenarios.
Line 127 always mutates
CATALOGunder the key"appdb", regardless of which database the client connected to. If a client connects with-d other_dband issuesCREATE TABLE, the table will be incorrectly added toappdbin the catalog structure, breaking catalog queries forother_db.Extract the database name from the connection context (likely available in
**kwargsor viaself) and use it instead of the hardcoded"appdb"literal.🔧 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents