Skip to main content

ctx sql — Public Schema (v1)

ctx sql runs read-only SQL against the code-intelligence index through DuckDB. The query surface is the versioned v1 schema: a set of stable views over the physical index. Query v1.* — not the underlying tables.

Stability contract

  • v1.* is the contract. Columns and views may be added within schema_version 1. Renaming or removing anything increments v1.meta.schema_version and is noted in the changelog.
  • Everything outside v1.* is internal and unstable. The raw index is reachable as code.*, but its shape can change at any time and it is excluded from all compatibility guarantees. Do not depend on it.
  • Access is read-only and engine-hardened. Filesystem access, extension loading, and attaching other databases are disabled; the index cannot be modified.

Views

v1.symbols — one row per symbol

ColumnTypeDescription
idVARCHARStable symbol identifier
nameVARCHARSymbol name
qualified_nameVARCHARFully-qualified name, when known
kindVARCHARfunction, method, struct, enum, trait, …
fileVARCHARPath of the file that defines the symbol
line_startBIGINTFirst line of the symbol
line_endBIGINTLast line of the symbol
is_publicBOOLEANWhether the symbol is publicly visible
complexityBIGINTfan_out * 2 + fan_in heuristic complexity score
fan_inBIGINTNumber of resolved incoming calls edges
fan_outBIGINTNumber of outgoing calls edges
docVARCHARDocstring or brief, when present

v1.edges — one row per relationship

ColumnTypeDescription
source_idVARCHARid of the source symbol
source_nameVARCHARName of the source symbol
source_fileVARCHARFile of the source symbol
target_idVARCHARid of the target symbol; NULL when unresolved
target_nameVARCHARName of the target; retained even when unresolved
target_fileVARCHARFile of the target symbol; NULL when unresolved
kindVARCHARcalls, extends, implements, or imports
lineBIGINTLine of the reference in the source file

v1.files — one row per indexed file

ColumnTypeDescription
pathVARCHARFile path
languageVARCHARDetected language
symbol_countBIGINTNumber of symbols defined in the file
total_complexityBIGINTSum of v1.symbols.complexity for the file
indexed_atBIGINTUnix time the file was last indexed

v1.meta — single row of index metadata

ColumnTypeDescription
schema_versionINTEGERPublic schema version (starts at 1)
ctx_versionVARCHARVersion of ctx that produced this output
index_created_atBIGINTEarliest file index time (Unix seconds)
index_rootVARCHARAbsolute root path of the indexed project

Examples

-- Ten most complex symbols
SELECT name, file, complexity
FROM v1.symbols
ORDER BY complexity DESC
LIMIT 10;
-- Symbol counts by kind
SELECT kind, COUNT(*) AS n
FROM v1.symbols
GROUP BY kind
ORDER BY n DESC;
-- Public functions that nothing calls (dead-code candidates)
SELECT name, file
FROM v1.symbols
WHERE kind IN ('function', 'method') AND is_public AND fan_in = 0
ORDER BY file, name;

Snapshot tables (snap.*) — only with --snapshots

ctx sql --snapshots[=DIR] (default DIR is .ctx/snapshots) additionally loads the Parquet snapshot partitions written by ctx snapshot as in-memory tables in the snap schema. These tables exist only when --snapshots is passed; without it, any snap.* reference is an error. Every row is denormalized with the partition stamp:

ColumnTypeDescription
commit_shaVARCHARFull sha of the snapshotted commit
committed_atTIMESTAMPCommitter date of that commit, normalized to UTC

snap.files — one row per file per commit

Stamp columns plus:

ColumnTypeDescription
pathVARCHARFile path
languageVARCHARDetected language
symbol_countBIGINTSymbols defined in the file
total_complexityDOUBLESum of symbol complexity for the file
max_complexityBIGINTHighest single-symbol complexity in the file
churn_commitsINTEGERCommits touching the file in the churn window
violation_countINTEGERArchitecture-rule violations in the file

snap.symbols — one row per symbol per commit

Stamp columns plus the v1.symbols columns id, name, qualified_name, kind, file, line_start, line_end, is_public, complexity, fan_in, and fan_out (same types as in v1.symbols; no doc).

snap.dup_pairs — one row per near-duplicate pair per commit

Stamp columns plus:

ColumnTypeDescription
file_aVARCHARFile of the first symbol
symbol_aVARCHARName of the first symbol
file_bVARCHARFile of the second symbol
symbol_bVARCHARName of the second symbol
similarityDOUBLEVerified token similarity (0–1)
token_count_aBIGINTNormalized token count of the first
token_count_bBIGINTNormalized token count of the second

snap.meta — one row per partition

Stamp columns plus:

ColumnTypeDescription
captured_atVARCHARRFC 3339 time the snapshot was captured
ctx_versionVARCHARctx version that wrote the partition
snapshot_schema_versionINTEGERSnapshot Parquet schema version
capture_modeVARCHARlive or backfill

Trend queries

-- Duplication trend
SELECT commit_sha, min(committed_at) AS committed_at, count(*) AS dup_pairs FROM snap.dup_pairs GROUP BY commit_sha ORDER BY committed_at;
-- Violation trend
SELECT commit_sha, min(committed_at) AS committed_at, sum(violation_count) AS violations FROM snap.files GROUP BY commit_sha ORDER BY committed_at;
-- Hotspot mass (top-decile complexity by commit)
WITH ranked AS (SELECT commit_sha, committed_at, churn_commits * total_complexity AS mass, percent_rank() OVER (PARTITION BY commit_sha ORDER BY total_complexity) AS pr FROM snap.files) SELECT commit_sha, min(committed_at) AS committed_at, sum(mass) AS hotspot_mass FROM ranked WHERE pr >= 0.9 GROUP BY commit_sha ORDER BY committed_at;