Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

feat(fenic): fenic catalogs as a data source — multi-schema DuckDB introspection + lite dataframe bridge

FABIOTESSaff50bc592a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c3a30ef4..63d6add3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,21 @@ All notable changes to Open Ontologies are documented here.
## [Unreleased]
+### Added
+- **fenic support.** [fenic](https://github.com/typedef-ai/fenic) (typedef-ai's
+ semantic DataFrame framework) keeps its local catalog in a plain DuckDB file
+ with user tables under the `typedef_default` schema. The DuckDB schema
+ introspector now scans all user schemas instead of only `main` (excluding
+ `information_schema`, `pg_catalog`, `__`-prefixed internals, and fenic's
+ `fenic_system` telemetry schema), so `import-schema` / `onto_import_schema`
+ work directly against fenic catalogs; cross-schema table-name collisions are
+ disambiguated as `<schema>_<table>`. `open-ontologies-lite` gains a
+ duck-typed dataframe bridge — `rows_from_dataframe`, `rows_to_turtle`, and
+ `OntologyEngine.load_rows` accept fenic, polars, pandas, and pyarrow objects
+ with no new dependencies. New end-to-end example
+ `python/examples/fenic_pipeline.py` and a "fenic" section in
+ `docs/data-pipeline.md`.
+
## [1.2.0] - 2026-08-15
### Added
diff --git a/docs/data-pipeline.md b/docs/data-pipeline.md
index 8f4d0802..0d6d2f23 100644
--- a/docs/data-pipeline.md
+++ b/docs/data-pipeline.md
@@ -297,6 +297,42 @@ The same query runs identically as MCP from Claude:
> from Open Ontologies. Credentials are passed via DuckDB's standard
> `CREATE SECRET` / environment-variable mechanism, never written to RDF.
+### 4. Pull from a fenic catalog (semantic DataFrames)
+
+[fenic](https://github.com/typedef-ai/fenic) is typedef-ai's PySpark-inspired
+DataFrame framework whose `semantic.*` operators (extract, classify, map, join)
+turn unstructured text into typed rows with an LLM. Its local catalog is a
+**plain DuckDB file**: a session with `app_name="my_app"` writes
+`my_app.duckdb` into the working directory, and `df.write.save_as_table("t")`
+lands the table at `typedef_default.t`. That makes every fenic pipeline output
+directly ingestable — fenic does the LLM extraction, Open Ontologies does the
+ontology governance.
+
+```bash
+# Import the fenic tables as an OWL ontology (fenic's internal
+# __fenic_system / fenic_system schemas are excluded automatically)
+open-ontologies import-schema duckdb:///path/to/my_app.duckdb
+
+# Ingest rows from a fenic table — note the typedef_default schema qualifier
+open-ontologies sql-ingest \
+ duckdb:///path/to/my_app.duckdb \
+ "SELECT id, name, parent FROM typedef_default.product_category"
+```
+
+Caveats and alternatives:
+
+- **Close the fenic session first** (`session.stop()`): DuckDB files are
+ single-writer, so ingest after the pipeline finishes — or have fenic
+ `df.write.parquet("out.parquet")` and feed that to `onto_ingest` instead.
+- **Python-side handoff:** `open-ontologies-lite` accepts a fenic DataFrame
+ directly — `engine.load_rows(df, base_iri=…, class_iri=…, id_column=…)`
+ (duck-typed via `to_pylist()`; polars, pandas, and pyarrow objects work
+ too). See [python/examples/fenic_pipeline.py](../python/examples/fenic_pipeline.py).
+- **MCP composition:** fenic can serve its tables as MCP tools
+ (`fenic-serve`), and Open Ontologies is an MCP server — connect both to the
+ same agent and it can query fenic tables and govern the resulting graph in
+ one conversation.
+
## Schema → ontology in 3 commands
`onto_import_schema` introspects a database, generates OWL, and loads it into
diff --git a/docs/superpowers/specs/2026-08-16-fenic-support-design.md b/docs/superpowers/specs/2026-08-16-fenic-support-design.md
new file mode 100644
index 00000000..48c1444c
--- /dev/null
+++ b/docs/superpowers/specs/2026-08-16-fenic-support-design.md
@@ -0,0 +1,80 @@
+# Fenic Support — Design
+
+**Date:** 2026-08-16
+**Status:** Implemented in the same autonomous session; awaiting Fabio's review
+**Scope:** Make [fenic](https://github.com/typedef-ai/fenic) (typedef-ai's semantic
+DataFrame framework, PySpark-inspired, LLM operators over DuckDB/Polars/Arrow) a
+supported data source for the Open Ontologies pipeline.
+
+## Verified facts (fenic 0.12.0, probed live)
+
+- fenic's local catalog is a **plain DuckDB file** named `<app_name>.duckdb` in
+ the session's working directory. `df.write.save_as_table("t")` lands the table
+ at `typedef_default.t`. Internal schemas: `__fenic_system` (tool/schema
+ metadata), `fenic_system` (query metrics).
+- DataFrame exports: `to_polars()`, `to_pandas()`, `to_arrow()`, `to_pydict()`,
+ `to_pylist()`; `df.write.parquet/csv` for files.
+- Python ≥3.10; DuckDB 1.1.3–1.4.x writes the catalog. Our bundled `duckdb`
+ crate (1.4-line) reads those files.
+- `sql-ingest "duckdb://…/app.duckdb" "SELECT … FROM typedef_default.t"`
+ **already works** (verified: 2 rows → 7 triples).
+- `import-schema` against a fenic catalog returns **0 tables**: the DuckDB
+ introspector hardcodes `table_schema = 'main'`.
+
+## Design
+
+Three lanes, smallest change that makes each true:
+
+### 1. Rust engine — fix `introspect_duckdb` to scan user schemas
+
+`SchemaIntrospector::introspect_duckdb` iterates `(table_schema, table_name)`
+pairs across all schemas except system ones (`information_schema`,
+`pg_catalog`), engine-internal ones (any schema starting with `__`), and
+fenic's telemetry schema (`fenic_system`). Column/PK/FK queries are
+parameterized by schema instead of hardcoding `'main'`. `TableInfo.name` stays
+the bare table name (feeds `table_to_class`); if the same bare name appears in
+two schemas, later occurrences are disambiguated as `<schema>_<table>`.
+No signature changes; Postgres path untouched.
+
+### 2. Python lite — duck-typed dataframe bridge
+
+New `open_ontologies_lite/dataframe.py`:
+
+- `rows_from_dataframe(obj)` — accepts fenic DataFrame / pyarrow Table
+ (`to_pylist`), polars (`to_dicts`), pandas (`to_dict("records")`), or an
+ iterable of dicts. No hard dependency on any of them.
+- `rows_to_turtle(rows, base_iri, class_iri, id_column=None)` — deterministic
+ row→RDF: int→`xsd:integer`, float→`xsd:double`, bool→`xsd:boolean`,
+ str→plain literal, `None` skipped. Subject IRIs from `id_column` or row index.
+- `OntologyEngine.load_rows(rows_or_df, ...)` — convenience: convert + load,
+ returns triple count.
+
+MCP-native: fenic does the LLM work (semantic.extract etc.); lite only
+validates, loads, lints, SHACL-checks. No model calls in the bridge.
+
+### 3. Docs + example
+
+- `docs/data-pipeline.md`: "Fenic (semantic DataFrames)" section — the
+ verified `sql-ingest` / `import-schema` commands against `<app_name>.duckdb`,
+ the `typedef_default` qualification, the file-lock caveat (close the fenic
+ session first), the Parquet handoff alternative, and the MCP composition note
+ (fenic serves tables as MCP tools; connect both servers to one agent).
+- `python/examples/fenic_pipeline.py`: runnable end-to-end — fenic session →
+ DataFrame → `save_as_table` + `rows_from_dataframe` → lite load → SHACL
+ validate → lint. Exits with a clear message if fenic is not installed; no
+ API keys required (semantic operators shown as comments).
+
+## Testing
+
+- Rust: extend `tests/duckdb_test.rs` with a fenic-shaped catalog fixture
+ (tables in `typedef_default`, decoy tables in `__fenic_system` +
+ `fenic_system`) asserting user tables are found and internals excluded.
+- Python: `python/tests/test_dataframe.py` — protocol fakes for each export
+ method, turtle determinism, typed literals, `load_rows` round-trip via
+ SPARQL. Fenic itself optional (`pytest.importorskip`).
+
+## Out of scope
+
+- No fenic dependency anywhere (not even optional extra).
+- No RDF writer inside fenic (their side, not ours).
+- No changes to `onto_sql_ingest` / mapping layer — already sufficient.
diff --git a/python/README.md b/python/README.md
index 24919ad2..7d5c9683 100644
--- a/python/README.md
+++ b/python/README.md
@@ -48,6 +48,23 @@ print(cs.to_kgcl()) # KGCL change records, one per line
(node created/deleted, renamed, annotation changed, edge created/deleted). Pure
structural comparison, no model. Also exposed as the `onto_kgcl_diff` MCP tool.
+### Dataframe ingestion (fenic, polars, pandas, pyarrow)
+
+```python
+engine.load_rows(df, base_iri="http://x.org/", class_iri="http://x.org/Thing", id_column="id")
+```
+
+`load_rows` duck-types against the common export methods — `to_pylist()`
+(fenic DataFrame, pyarrow Table), `to_dicts()` (polars), `to_dict("records")`
+(pandas) — or takes a plain list of dicts. Values become typed literals
+(int/float/bool → XSD), `None` is skipped, and the output is deterministic.
+The primary consumer is [fenic](https://github.com/typedef-ai/fenic): its
+semantic operators do the LLM extraction, this bridge just loads and lets
+SHACL/lint/SPARQL govern the result. See
+[examples/fenic_pipeline.py](examples/fenic_pipeline.py) for the end-to-end
+shape, and `docs/data-pipeline.md` for ingesting a fenic DuckDB catalog with
+the full Rust engine.
+
### Alignment candidate generation with HNSW (optional `[align]` extra)
```bash
diff --git a/python/examples/fenic_pipeline.py b/python/examples/fenic_pipeline.py
new file mode 100644
index 00000000..afcdc08c
--- /dev/null
+++ b/python/examples/fenic_pipeline.py
@@ -0,0 +1,78 @@
+"""fenic → Open Ontologies Lite: semantic DataFrames, governed.
+
+fenic (https://github.com/typedef-ai/fenic) is typedef-ai's PySpark-inspired
+DataFrame framework: its ``semantic.*`` operators (extract, classify, map,
+join) turn unstructured text into typed rows with an LLM. This example shows
+the handoff — fenic produces the rows, Open Ontologies turns them into RDF and
+governs them (SHACL validation + lint), with no model calls on this side.
+
+Run: pip install open-ontologies-lite fenic && python fenic_pipeline.py
+
+The same rows can instead flow through the full Rust engine: a fenic session's
+local catalog is a plain DuckDB file (``<app_name>.duckdb``, tables under the
+``typedef_default`` schema), so after ``df.write.save_as_table(...)``:
+
+ open-ontologies sql-ingest "duckdb:///path/to/oo_fenic_demo.duckdb" \
+ "SELECT id, name, parent FROM typedef_default.product_category"
+ open-ontologies import-schema "duckdb:///path/to/oo_fenic_demo.duckdb"
+
+See docs/data-pipeline.md ("Fenic" section) for that route.
+"""
+
+import sys
+
+from open_ontologies_lite import OntologyEngine
+
+try:
+ import fenic as fc
+except ImportError:
+ sys.exit("fenic is not installed — pip install fenic")
+
+BASE = "http://example.org/shop/"
+
+# 1. A fenic pipeline. In real use the rows would come out of semantic
+# operators over unstructured text, e.g.:
+# df.select(fc.semantic.extract(fc.col("description"), ProductSchema))
+# Those need provider API keys; the DataFrame mechanics are identical, so
+# this example sticks to structured input.
+session = fc.Session.get_or_create(fc.SessionConfig(app_name="oo_fenic_demo"))
+df = session.create_dataframe(
+ {
+ "id": [1, 2, 3],
+ "name": ["Bakery", "Dairy", "Sourdough Loaf"],
+ "parent": [None, None, "Bakery"],
+ "price": [None, None, 4.20],
+ }
+)
+df = df.filter(fc.col("name").is_not_null())
+df.write.save_as_table("product_category", mode="overwrite") # → typedef_default.product_category
+
+# 2. Hand the rows to Open Ontologies Lite. load_rows() accepts the fenic
+# DataFrame directly (duck-typed via to_pylist()).
+engine = OntologyEngine()
+triples = engine.load_rows(df, base_iri=BASE, class_iri=f"{BASE}Category", id_column="id")
+print(f"loaded {triples} triples")
+
+# 3. Govern: SHACL — every Category must have exactly one name.
+shapes = f"""
+@prefix sh: <http://www.w3.org/ns/shacl#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+<{BASE}CategoryShape> a sh:NodeShape ;
+ sh:targetClass <{BASE}Category> ;
+ sh:property [ sh:path <{BASE}name> ; sh:minCount 1 ; sh:maxCount 1 ] .
+"""
+try:
+ from open_ontologies_lite.shacl import shacl_validate
+
+ report = shacl_validate(engine.dump("turtle"), shapes)
+ print("SHACL conforms:", report["conforms"])
+except ImportError:
+ print("pyshacl not installed — skipping SHACL step (pip install pyshacl)")
+
+# 4. Ask questions back.
+rows = engine.query(
+ f"SELECT ?name WHERE {{ ?s <{BASE}parent> \"Bakery\" ; <{BASE}name> ?name }}"
+)["rows"]
+print("children of Bakery:", [r["name"] for r in rows])
+
+session.stop()
diff --git a/python/src/open_ontologies_lite/__init__.py b/python/src/open_ontologies_lite/__init__.py
index e247eedb..c0fcb0ac 100644
--- a/python/src/open_ontologies_lite/__init__.py
+++ b/python/src/open_ontologies_lite/__init__.py
@@ -2,6 +2,7 @@
Oxigraph RDF/OWL engine. No Rust toolchain, no compilation, prebuilt wheels only.
"""
+from .dataframe import rows_from_dataframe, rows_to_turtle
from .engine import OntologyEngine, ValidationResult, resolve_format
from .kgcl import ChangeSet, kgcl_diff
@@ -12,6 +13,8 @@ __all__ = [
"resolve_format",
"ChangeSet",
"kgcl_diff",
+ "rows_from_dataframe",
+ "rows_to_turtle",
"__version__",
]
diff --git a/python/src/open_ontologies_lite/dataframe.py b/python/src/open_ontologies_lite/dataframe.py
new file mode 100644
index 00000000..e99cbc87
--- /dev/null
+++ b/python/src/open_ontologies_lite/dataframe.py
@@ -0,0 +1,88 @@
+"""Dataframe bridge: turn tabular rows into RDF and load them.
+
+Duck-typed against the export methods of the common dataframe stacks, so none
+of them is a dependency:
+
+- fenic ``DataFrame`` and pyarrow ``Table`` — ``to_pylist()``
+- polars ``DataFrame`` — ``to_dicts()``
+- pandas ``DataFrame`` — ``to_dict("records")``
+- any iterable of dicts — used as-is
+
+The primary consumer is fenic (typedef-ai): its semantic operators do the LLM
+extraction, this bridge does none of it. Rows come in, deterministic typed
+triples come out, and the existing engine surface (SHACL, lint, SPARQL) governs
+the result. See ``python/examples/fenic_pipeline.py`` for the end-to-end shape.
+"""
+
+from __future__ import annotations
+
+import re
+from typing import Any, Iterable
+
+XSD = "http://www.w3.org/2001/XMLSchema#"
+RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
+
+_LOCAL_SAFE = re.compile(r"[^A-Za-z0-9_.-]+")
+
+
+def rows_from_dataframe(obj: Any) -> list[dict[str, Any]]:
+ """Extract rows as a list of dicts from any dataframe-like object."""
+ if hasattr(obj, "to_pylist"):
+ return list(obj.to_pylist())
+ if hasattr(obj, "to_dicts"):
+ return list(obj.to_dicts())
+ if hasattr(obj, "to_dict"):
+ return list(obj.to_dict("records"))
+ if isinstance(obj, Iterable):
+ rows = list(obj)
+ if all(isinstance(r, dict) for r in rows):
+ return rows
+ raise TypeError(
+ f"cannot extract rows from {type(obj).__name__}: expected an object "
+ "with to_pylist()/to_dicts()/to_dict('records') or an iterable of dicts"
+ )
+
+
+def _local_name(value: Any) -> str:
+ return _LOCAL_SAFE.sub("_", str(value))
+
+
+def _literal(value: Any) -> str | None:
+ if value is None:
+ return None
+ if isinstance(value, bool):
+ return f'"{str(value).lower()}"^^<{XSD}boolean>'
+ if isinstance(value, int):
+ return f'"{value}"^^<{XSD}integer>'
+ if isinstance(value, float):
+ return f'"{value!r}"^^<{XSD}double>'
+ escaped = str(value).replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n")
+ return f'"{escaped}"'
+
+
+def rows_to_turtle(
+ rows: list[dict[str, Any]],
+ base_iri: str = "http://example.org/data/",
+ class_iri: str | None = None,
+ id_column: str | None = None,
+) -> str:
+ """Serialize rows to Turtle, one subject per row.
+
+ Subjects are ``<base_iri><id>`` where ``id`` comes from ``id_column`` when
+ given (rows missing it fall back to their index), else the row index.
+ Predicates are ``<base_iri><column>``. Values map to typed literals
+ (bool → xsd:boolean, int → xsd:integer, float → xsd:double, anything else
+ → plain string literal); ``None`` values are skipped. Deterministic:
+ same rows in, same document out.
+ """
+ lines: list[str] = []
+ for i, row in enumerate(rows):
+ key = row.get(id_column, i) if id_column else i
+ subject = f"<{base_iri}{_local_name(key)}>"
+ if class_iri:
+ lines.append(f"{subject} <{RDF_TYPE}> <{class_iri}> .")
+ for column, value in row.items():
+ lit = _literal(value)
+ if lit is not None:
+ lines.append(f"{subject} <{base_iri}{_local_name(column)}> {lit} .")
+ return "\n".join(lines) + ("\n" if lines else "")
diff --git a/python/src/open_ontologies_lite/engine.py b/python/src/open_ontologies_lite/engine.py
index 6a09a98c..15bcb6d3 100644
--- a/python/src/open_ontologies_lite/engine.py
+++ b/python/src/open_ontologies_lite/engine.py
@@ -81,6 +81,21 @@ class OntologyEngine:
self.store.load(path=str(p), format=rdf_format)
return len(self.store)
+ def load_rows(
+ self,
+ rows_or_df,
+ base_iri: str = "http://example.org/data/",
+ class_iri: str | None = None,
+ id_column: str | None = None,
+ ) -> int:
+ """Load tabular rows (or any dataframe-like object — fenic, polars,
+ pandas, pyarrow) into the store as RDF; return total triple count."""
+ from .dataframe import rows_from_dataframe, rows_to_turtle
+
+ rows = rows_from_dataframe(rows_or_df)
+ ntriples = rows_to_turtle(rows, base_iri=base_iri, class_iri=class_iri, id_column=id_column)
+ return self.load(ntriples, "ntriples")
+
def clear(self) -> None:
self.store.clear()
diff --git a/python/tests/test_dataframe.py b/python/tests/test_dataframe.py
new file mode 100644
index 00000000..fe7faf6b
--- /dev/null
+++ b/python/tests/test_dataframe.py
@@ -0,0 +1,94 @@
+"""Tests for the dataframe bridge (fenic / polars / pandas / pyarrow duck-typing)."""
+
+import pytest
+
+from open_ontologies_lite import OntologyEngine, rows_from_dataframe, rows_to_turtle
+
+ROWS = [
+ {"id": 1, "name": "Bakery", "parent": None},
+ {"id": 2, "name": "Dairy", "parent": "Food"},
+]
+
+
+class FakeFenicDataFrame:
+ """fenic DataFrame / pyarrow Table protocol: to_pylist()."""
+
+ def to_pylist(self):
+ return list(ROWS)
+
+
+class FakePolarsDataFrame:
+ def to_dicts(self):
+ return list(ROWS)
+
+
+class FakePandasDataFrame:
+ def to_dict(self, orient):
+ assert orient == "records"
+ return list(ROWS)
+
+
+ "df", [FakeFenicDataFrame(), FakePolarsDataFrame(), FakePandasDataFrame(), ROWS]
+)
+def test_rows_from_dataframe_protocols(df):
+ assert rows_from_dataframe(df) == ROWS
+
+
+def test_rows_from_dataframe_rejects_unknown():
+ with pytest.raises(TypeError):
+ rows_from_dataframe(object())
+
+
+def test_rows_to_turtle_typed_literals_and_none_skipped():
+ ttl = rows_to_turtle(ROWS, base_iri="http://x.org/", id_column="id")
+ assert '<http://x.org/1> <http://x.org/name> "Bakery" .' in ttl
+ assert '"1"^^<http://www.w3.org/2001/XMLSchema#integer>' in ttl
+ assert "parent> ." not in ttl # None dropped, no dangling triple
+ assert ttl == rows_to_turtle(ROWS, base_iri="http://x.org/", id_column="id") # deterministic
+
+
+def test_rows_to_turtle_class_and_index_subjects():
+ ttl = rows_to_turtle(
+ [{"v": True, "w": 1.5}],
+ base_iri="http://x.org/",
+ class_iri="http://x.org/Thing",
+ )
+ assert "<http://x.org/0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://x.org/Thing> ." in ttl
+ assert '"true"^^<http://www.w3.org/2001/XMLSchema#boolean>' in ttl
+ assert '"1.5"^^<http://www.w3.org/2001/XMLSchema#double>' in ttl
+
+
+def test_rows_to_turtle_escapes_strings_and_sanitizes_ids():
+ ttl = rows_to_turtle(
+ [{"id": "a b/c", "note": 'say "hi"\nbye'}],
+ base_iri="http://x.org/",
+ id_column="id",
+ )
+ assert "<http://x.org/a_b_c>" in ttl
+ assert '"say \\"hi\\"\\nbye"' in ttl
+
+
+def test_engine_load_rows_roundtrip():
+ engine = OntologyEngine()
+ n = engine.load_rows(
+ FakeFenicDataFrame(),
+ base_iri="http://x.org/",
+ class_iri="http://x.org/Category",
+ id_column="id",
+ )
+ assert n == engine.stats()["triples"]
+ res = engine.query(
+ "SELECT ?name WHERE { ?s a <http://x.org/Category> ; <http://x.org/name> ?name } ORDER BY ?name"
+ )
+ assert [r["name"] for r in res["rows"]] == ["Bakery", "Dairy"]
+
+
+def test_real_fenic_dataframe_if_installed():
+ fc = pytest.importorskip("fenic")
+ session = fc.Session.get_or_create(fc.SessionConfig(app_name="oo_lite_test"))
+ df = session.create_dataframe({"id": [1, 2], "name": ["Bakery", "Dairy"]})
+ engine = OntologyEngine()
+ n = engine.load_rows(df, base_iri="http://x.org/", id_column="id")
+ assert n == 4
+ session.stop()
diff --git a/src/schema.rs b/src/schema.rs
index a2d29dac..231b6991 100644
--- a/src/schema.rs
+++ b/src/schema.rs
@@ -208,27 +208,32 @@ impl SchemaIntrospector {
Connection::open(target)?
};
- // Tables in the default schema (`main`). User tables only — exclude
- // system schemas.
+ // User tables across ALL user schemas — not just `main`. Frameworks
+ // that keep their local catalog in a DuckDB file put tables in their
+ // own schema (fenic writes to `typedef_default`). Excluded: SQL system
+ // schemas, engine-internal schemas (`__`-prefixed, e.g. fenic's
+ // `__fenic_system`), and fenic's telemetry schema `fenic_system`.
let mut stmt = conn.prepare(
- "SELECT table_name FROM information_schema.tables \
- WHERE table_schema = 'main' AND table_type = 'BASE TABLE' \
- ORDER BY table_name",
+ "SELECT table_schema, table_name FROM information_schema.tables \
+ WHERE table_type = 'BASE TABLE' \
+ AND table_schema NOT IN ('information_schema', 'pg_catalog', 'fenic_system') \
+ AND table_schema NOT LIKE '\\_\\_%' ESCAPE '\\' \
+ ORDER BY table_schema, table_name",
)?;
- let table_names: Vec<String> = stmt
- .query_map([], |row| row.get::<_, String>(0))?
+ let schema_tables: Vec<(String, String)> = stmt
+ .query_map([], |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)))?
.collect::<Result<Vec<_>, _>>()?;
drop(stmt);
- let mut tables = Vec::new();
- for table_name in &table_names {
+ let mut tables: Vec<TableInfo> = Vec::new();
+ for (schema_name, table_name) in &schema_tables {
// Columns
let mut col_stmt = conn.prepare(
"SELECT column_name, data_type, is_nullable FROM information_schema.columns \
- WHERE table_schema = 'main' AND table_name = ? ORDER BY ordinal_position",
+ WHERE table_schema = ? AND table_name = ? ORDER BY ordinal_position",
)?;
let col_rows: Vec<(String, String, String)> = col_stmt
- .query_map([table_name], |row| {
+ .query_map([schema_name, table_name], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?, row.get::<_, String>(2)?))
})?
.collect::<Result<Vec<_>, _>>()?;
@@ -241,10 +246,10 @@ impl SchemaIntrospector {
let mut pk_stmt = conn.prepare(
"SELECT array_to_string(constraint_column_names, ',') AS cols \
FROM duckdb_constraints() \
- WHERE schema_name = 'main' AND table_name = ? AND constraint_type = 'PRIMARY KEY'",
+ WHERE schema_name = ? AND table_name = ? AND constraint_type = 'PRIMARY KEY'",
)?;
let pk_strings: Vec<String> = pk_stmt
- .query_map([table_name], |row| row.get::<_, String>(0))?
+ .query_map([schema_name, table_name], |row| row.get::<_, String>(0))?
.collect::<Result<Vec<_>, _>>()?;
drop(pk_stmt);
let pk_cols: Vec<String> = pk_strings
@@ -260,10 +265,10 @@ impl SchemaIntrospector {
referenced_table, \
array_to_string(referenced_column_names, ',') AS parent_cols \
FROM duckdb_constraints() \
- WHERE schema_name = 'main' AND table_name = ? AND constraint_type = 'FOREIGN KEY'",
+ WHERE schema_name = ? AND table_name = ? AND constraint_type = 'FOREIGN KEY'",
)?;
let fk_rows: Vec<(String, String, String)> = fk_stmt
- .query_map([table_name], |row| {
+ .query_map([schema_name, table_name], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
@@ -300,8 +305,15 @@ impl SchemaIntrospector {
})
.collect();
+ // Bare table names feed `table_to_class`; qualify only on a
+ // cross-schema collision so single-schema catalogs are unchanged.
+ let name = if tables.iter().any(|t: &TableInfo| t.name == *table_name) {
+ format!("{}_{}", schema_name, table_name)
+ } else {
+ table_name.clone()
+ };
tables.push(TableInfo {
- name: table_name.clone(),
+ name,
columns,
foreign_keys,
});
diff --git a/tests/duckdb_test.rs b/tests/duckdb_test.rs
index 8704a17b..9a08ab97 100644
--- a/tests/duckdb_test.rs
+++ b/tests/duckdb_test.rs
@@ -79,6 +79,70 @@ fn introspect_duckdb_extracts_tables_and_constraints() {
assert!(turtle.contains("rdfs:range db:Customer"));
}
+#[test]
+fn introspect_duckdb_reads_fenic_catalogs() {
+ // fenic (typedef-ai) keeps its local catalog in `<app_name>.duckdb`:
+ // user tables in the `typedef_default` schema, internals in
+ // `__fenic_system` (metadata) and `fenic_system` (query metrics).
+ // Introspection must surface the user tables and skip the internals.
+ let tmp = tempfile::tempdir().unwrap();
+ let path = tmp.path().join("fenic_app.duckdb");
+ let path_str = path.to_string_lossy().to_string();
+
+ {
+ let conn = duckdb::Connection::open(&path).unwrap();
+ conn.execute_batch(
+ r#"
+ CREATE SCHEMA typedef_default;
+ CREATE TABLE typedef_default.product_category (
+ id INTEGER PRIMARY KEY,
+ name VARCHAR NOT NULL,
+ parent VARCHAR
+ );
+ CREATE SCHEMA "__fenic_system";
+ CREATE TABLE "__fenic_system".table_schemas (schema_name VARCHAR, blob VARCHAR);
+ CREATE SCHEMA fenic_system;
+ CREATE TABLE fenic_system.query_metrics (execution_id VARCHAR, cost DOUBLE);
+ "#,
+ )
+ .unwrap();
+ }
+
+ let tables = SchemaIntrospector::introspect_duckdb(&path_str).unwrap();
+ let names: Vec<&str> = tables.iter().map(|t| t.name.as_str()).collect();
+ assert_eq!(names, vec!["product_category"], "user table only, internals excluded");
+
+ let cat = &tables[0];
+ assert!(cat.columns.iter().any(|c| c.name == "id" && c.is_primary_key));
+
+ let turtle = SchemaIntrospector::generate_turtle(&tables, "http://example.org/db/");
+ assert!(turtle.contains("db:ProductCategory a owl:Class"));
+}
+
+#[test]
+fn introspect_duckdb_qualifies_cross_schema_name_collisions() {
+ let tmp = tempfile::tempdir().unwrap();
+ let path = tmp.path().join("collision.duckdb");
+ let path_str = path.to_string_lossy().to_string();
+
+ {
+ let conn = duckdb::Connection::open(&path).unwrap();
+ conn.execute_batch(
+ r#"
+ CREATE TABLE item (id INTEGER PRIMARY KEY);
+ CREATE SCHEMA staging;
+ CREATE TABLE staging.item (id INTEGER PRIMARY KEY, note VARCHAR);
+ "#,
+ )
+ .unwrap();
+ }
+
+ let mut names: Vec<String> =
+ SchemaIntrospector::introspect_duckdb(&path_str).unwrap().into_iter().map(|t| t.name).collect();
+ names.sort();
+ assert_eq!(names, vec!["item".to_string(), "staging_item".to_string()]);
+}
+
#[test]
fn query_rows_returns_tabular_data_from_duckdb() {
let tmp = tempfile::tempdir().unwrap();