SQL over the code index
sqlRun a read-only SELECT or WITH over indexed repository chunks to search, rank, filter, group, and count code in a single statement instead of many file reads.
Instructions
Read-only SQL, one SELECT or WITH, over chunks(path, start_line, end_line, lang, symbol, content[, embedding]) - lang is the file extension, e.g. 'rs' - for counts, rankings, and GROUP BY across the whole repo. The search functions are table-valued: a ranked search is a relation, so WHERE, GROUP BY, ORDER BY and joins compose with it in one pass, and one query replaces the several round trips of searching, then filtering, then counting. Rank through a search relation rather than scanning the whole table - with ILIKE, LIKE, regexp_like, or a bare filter on path or lang: a scan has no relevance ranking, reads every chunk, and answers 'contains this substring' when the question asked which code is about something. Rank with hybrid_search('chunks','content','terms','embedding', {{q}}, k) - 'terms' and {{q}} are yours to fill in, not literals to copy - unless you have a reason not to: it fuses exact terms with meaning, so it reaches the code whether or not the question's words are the code's words, and they rarely are. That covers a concept, a subsystem, 'code about X', 'files that do Y' - the shape of almost every ranking question. A question asking what is related to something is a join or a question of meaning: include hybrid_search or vector_search in the statement or the search. Keep every condition the question names in the statement: a question about some of the rows (the village articles, one project's files) is answered over those rows alone, never over the whole table. Put as much of the question as you can into one statement: WITH, UNION ALL, joins, CASE, GROUP BY and window functions such as ROW_NUMBER() OVER (PARTITION BY ...) compose in one, so a question in several parts - several things each counted, the top rows of each group, a count beside the rows it counts - is one statement, never a query per part. Finding rows is a hybrid_search, whatever the question names - a figure, a topic, a kind of thing, a word it says the rows mention: the rows rarely use the question's words, scanned and typed text misspells them, and the keyword half of the search still ranks an exact hit first. The keyword functions - token_match, bm25_search, LIKE on an indexed column - are for one thing only, counting or listing every exact occurrence of a literal, where a ranked top k would be incomplete. A question with no text in it - how many rows, the largest values, a total - is plain SQL with no search function. bm25_search('chunks','content','terms', k) is keyword only: reach for it when the topic is itself a literal string you know appears in the source and you want counts a reader can check as occurrences. vector_search('chunks','embedding', {{q}}, k) is meaning alone - for a question whose words will not be the code's words: SELECT path, start_line, content FROM vector_search('chunks','embedding', {{q}}, 20). token_match('chunks','content','the terms','and') is every row holding every WORD given, unranked and complete - a count a reader can check: SELECT count() FROM token_match('chunks','content','','and'). Its unit is the word: in_bulk is the two words in and bulk, and 122 Django files hold both where 30 hold the identifier. An identifier, an error text, an exact phrase is find's, never token_match's. Several terms counted at once are one token_match per term, side by side in a UNION ALL - SELECT 'a' AS term, COUNT() AS n FROM token_match('chunks','content','a','and') UNION ALL SELECT 'b', COUNT() FROM token_match('chunks','content','b','and') - so a row holding two of them counts for both. exact_match('chunks','content','value') is the rows whose whole value equals the string - a lookup on a short indexed column, never on content. The {{name}} placeholders are filled server-side from the embed map, so they cost you nothing but the name. The whole question as one statement, each clause one job - filter, keyword and meaning search, group, count - with your own words in place of the example's: SELECT path, SUM(end_line - start_line + 1) AS ranked_lines, COUNT() AS chunks FROM hybrid_search('chunks','content','merge small superfiles','embedding', {{q}}, 300) WHERE path LIKE 'src/%' AND lang = 'rs' GROUP BY path ORDER BY ranked_lines DESC LIMIT 15, with embed {"q":"how small superfiles are merged into larger ones"}: the terms are the keyword job and the {{q}} text the meaning job, the WHERE the filter, the GROUP BY with its aggregates the which and the how many; a second table joins into the same statement (FROM ... AS chunks JOIN ON ) where the database has one - one call, not a search, then a filter, then a count. Where the topic is a literal string you want counted as occurrences, the same shape over bm25_search('chunks','content','compaction', 300) instead - every row then holds the word, so a reader can check it. What such a total means: a search relation holds only the top k chunks of that query, so a SUM or COUNT over it is the lines or chunks that ranked within the top k - a share of the file about the topic - and never the file's length or the repository's count; report it as 'lines ranked in the top 300 for ', and expect files outside the top k, including large ones, to be missing from it. A question with no topic in it - a file's length, the largest files, a count over the whole repository - comes from chunks with no search function: SELECT path, MAX(end_line) AS lines FROM chunks WHERE lang IN ('rs','ts','py') GROUP BY path ORDER BY lines DESC. Name the languages you mean, as that example does, on any question about code: an unfiltered ranking over a real repository comes back topped by generated data - benchmark result JSON, fixtures, vendored blobs - which genuinely are the longest files and are never the answer. lang is the file extension, so the filter is the cheapest way to say 'code, not data', and it works the same inside a ranked search's aggregate. A path prefix is not a topic: filtering on WHERE path LIKE 'src/thing/%' and measuring lengths answers how big those files are, not which code is about the thing, and it guesses the answer from a directory name instead of retrieving it. sql is also how you READ here, and never the shell, Grep or Read - with the search functions doing the finding: the lines of a file, SELECT start_line, content FROM chunks WHERE path = 'src/x.rs' ORDER BY start_line (a stretch: AND start_line BETWEEN 380 AND 440), and whole files the index named with read, several in one call; every place a phrase occurs, complete and unranked, token_match('chunks', 'content', 'the terms', 'and') - how often, SELECT count() FROM token_match('chunks', 'content', 'the terms', 'and') WHERE path LIKE 'x/%'; the lines about something, ranked, bm25_search or hybrid_search as above; what a long file or log is made of, the shell's cut | sort | uniq -c, SELECT substr(line, 1, 50) AS head, count() AS n FROM (SELECT unnest(string_to_array(content, chr(10))) AS line FROM chunks WHERE path = '...') GROUP BY head ORDER BY n DESC LIMIT 20; the kinds of error in a log, the same unnest over bm25_search('chunks', 'content', 'Error Exception', 200) WHERE path = '...', then regexp_replace(line, '^.?([A-Za-z.]+(Error|Exception)).$', '\1') AS kind, count(*) ... GROUP BY kind - the search function finds the windows, string functions only shape their lines, and a LIKE or regexp_like scan over the whole table is the one form to avoid. Rows are windows that overlap by a few lines, so a per-line count runs a little high and the ranking is right; log lines may carry colour codes, so match inside an unnested line (LIKE '%FAILED%'), never at its start. The dialect is Apache DataFusion: regexp_like, regexp_replace, substr, position, split_part, string_to_array with unnest, CASE WHEN; regexp_match returns a list this side cannot show, so shape with regexp_replace instead, and there is no regexp_extract. A result past the tool's budget keeps every row's keys and places and cuts the text, so select the lines you mean rather than whole windows. Select start_line beside content whenever you mean to read or cite the code: a row's text comes back with each line's own number in the file when the row carries its start line, and unnumbered when it does not, since nothing then places the text. The result includes a 'usage' field, a one-line receipt of tokens returned and rows.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "sql"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | No | Absolute path to the repository root to query. Defaults to the server's configured root; set it to target a specific repo when a session spans more than one. | |
| embed | No | Map of placeholder name → query text, embedded server-side. E.g. {"q":"vector indexing"} fills {{q}}. | |
| query | No | A single read-only SELECT or WITH statement. May use search table functions and {{name}} vector placeholders. | |
| table | No | With `database`: the table the statement is about, for the verdict on its rows; absent, the first table of the database the statement names. | |
| queries | No | Several statements in one call, run at the same time and sharing embed and question; the results come back in the same order, each under its statement. Use it in place of one call per statement. | |
| database | No | A database of the Infino account to run over instead of a repository: the names are what `tables` lists. With it, `path` is not read. | |
| question | No | The question these rows are meant to answer, in the words it was asked. Used only to check the rows against it - the result's 'validation' then says whether they answer it and what is missing if not. |