Skip to content

Cookbook

Quick-reference table for common code analysis tasks. Each links to a focused how-to guide with full examples and variations.

Task Index

What you want to do Guide Key pattern
Find all functions Common Queries WHERE semantic_type = 'DEFINITION_FUNCTION'
Find all classes Common Queries WHERE semantic_type = 'DEFINITION_CLASS'
Find function calls Common Queries WHERE semantic_type = 'COMPUTATION_CALL'
Find imports Common Queries WHERE semantic_type = 'EXTERNAL_IMPORT'
Find by name Common Queries WHERE name LIKE '%pattern%'
Build a call graph Call Graphs JOIN ... ON scope.function = node_id
Find unused functions Find Dead Code ast_dead_code('table') or .func:not(:is-called)
Measure complexity Complexity Analysis ast_function_metrics('table')
Find long functions Complexity Analysis WHERE end_line - start_line > 50
Analyze nesting depth Complexity Analysis ast_nesting_analysis('table')
Find security issues Security Audit ast_security_audit('table')
Find hardcoded secrets Security Audit string_contains_any_i(peek, [...])
Parse once, query many Parse Once, Query Many CREATE TABLE ... AS SELECT * FROM read_ast(...)
Export to Parquet Parse Once, Query Many COPY (...) TO 'file.parquet'
Process multiple files Multi-File Processing read_ast(['glob1', 'glob2'])
Compare across languages Cross-Language Analysis GROUP BY language
Use CSS selectors Selector Examples ast_select('src/*.py', '.func')
Extract context Context Extraction context := 'native'
Search by structure Structural Search :has(), :match(), combinators

Quick recipes

Language distribution

SELECT language, COUNT(DISTINCT file_path) AS files, COUNT(*) AS nodes
FROM read_ast('**/*.*', ignore_errors := true)
GROUP BY language
ORDER BY files DESC;

Definition inventory

SELECT
    language,
    COUNT(CASE WHEN semantic_type = 'DEFINITION_FUNCTION' THEN 1 END) AS functions,
    COUNT(CASE WHEN semantic_type = 'DEFINITION_CLASS' THEN 1 END) AS classes,
    COUNT(CASE WHEN semantic_type = 'DEFINITION_VARIABLE' THEN 1 END) AS variables
FROM read_ast('src/**/*.*', ignore_errors := true)
GROUP BY language
ORDER BY functions DESC;

Most-called functions

SELECT name, COUNT(*) AS call_count
FROM read_ast('src/**/*.*', ignore_errors := true)
WHERE semantic_type = 'COMPUTATION_CALL'
  AND name IS NOT NULL
GROUP BY name
ORDER BY call_count DESC
LIMIT 20;

TODO/FIXME comments

SELECT file_path, start_line, peek
FROM read_ast('src/**/*.*', ignore_errors := true)
WHERE semantic_type = 'METADATA_COMMENT'
  AND (peek LIKE '%TODO%' OR peek LIKE '%FIXME%' OR peek LIKE '%HACK%')
ORDER BY file_path, start_line;

Check parsing errors

SELECT file_path, type, peek
FROM read_ast('src/**/*.*', ignore_errors := true)
WHERE type = 'ERROR'
LIMIT 10;

See also