Dllb.MetaAST.Query (Dllb v0.4.0)

Copy Markdown View Source

Domain-aware query builders for MetaAST data stored in dllb.

Every function returns a plain query string (or list of query strings) ready to be sent through Dllb.query/1 or Dllb.batch/1. Higher-level exec_* variants accept a query function and return parsed results via Dllb.MetaAST.from_dllb_row/1.

Query categories

  • Node queries -- find/list AST nodes by file, kind, module, or composite key
  • Graph traversals -- callers, callees, importers, imports, call chains
  • Search -- HNSW vector similarity, full-text BM25, hybrid
  • Lifecycle -- delete by file/project, stats
  • Tree reconstruction -- rebuild MetaAST 3-tuples from stored data

Summary

Functions

Multi-hop call chain traversal from a starting node.

Graph traversal: find all callees of a node (outgoing calls edges).

Graph traversal: find all callers of a node (incoming calls edges).

Returns a COUNT query for ast_node rows that have a source_embedding set -- i.e. the number of stored embedding vectors.

Deletes all AST nodes belonging to a file.

Executes a query through query_fn and returns parsed result maps.

Executes a COUNT query and returns {:ok, count} or {:error, reason}.

Counts stored embedding vectors (ast_node rows with source_embedding).

Executes a two-step delete-by-file: SELECT ids, then batch DELETE.

Executes a two-step delete-by-project: SELECT ids, then batch DELETE.

Loads all nodes for a file and returns them as parsed maps.

Executes stats query and returns aggregated counts.

SELECT a function node by module, name, and arity.

SELECT a module (container) node by name.

SELECT function nodes belonging to a module.

Combined vector + full-text hybrid search.

Graph traversal: find all modules that import a given node.

Graph traversal: find all imports of a node.

SELECT all AST nodes belonging to a file.

SELECT all AST nodes of a given kind (e.g. "function_def", "container").

SELECT nodes filtered by project path.

Full-text BM25 search on docstring.

Full-text BM25 search on source_text.

Builds an UPDATE ast_node SET source_embedding = [...] WHERE <attrs> statement that attaches an embedding to the row(s) identified by attrs.

HNSW vector similarity search on source_embedding.

Returns a SELECT for overall statistics (node count).

Functions

call_chain(record_id, depth)

@spec call_chain(String.t(), pos_integer()) :: String.t()

Multi-hop call chain traversal from a starting node.

Builds a chained ->calls->ast_node traversal repeated depth times.

callees_of(record_id)

@spec callees_of(String.t()) :: String.t()

Graph traversal: find all callees of a node (outgoing calls edges).

callers_of(record_id)

@spec callers_of(String.t()) :: String.t()

Graph traversal: find all callers of a node (incoming calls edges).

Returns a SELECT with <-calls<-ast_node traversal syntax.

count_embeddings_query()

@spec count_embeddings_query() :: String.t()

Returns a COUNT query for ast_node rows that have a source_embedding set -- i.e. the number of stored embedding vectors.

delete_by_file_select(file_path)

@spec delete_by_file_select(String.t()) :: String.t()

Deletes all AST nodes belonging to a file.

Since dllb currently supports DELETE table:id (point deletes) but not DELETE ... WHERE, this returns a two-step approach: a SELECT to find the ids, then point DELETEs for each. Use with exec_delete_by_file/2.

exec(query_string, query_fn)

@spec exec(String.t(), Dllb.MetaAST.query_fn()) :: {:ok, [map()]} | {:error, term()}

Executes a query through query_fn and returns parsed result maps.

exec_count(query_string, query_fn)

@spec exec_count(String.t(), Dllb.MetaAST.query_fn()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Executes a COUNT query and returns {:ok, count} or {:error, reason}.

exec_count_embeddings(query_fn)

@spec exec_count_embeddings(Dllb.MetaAST.query_fn()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Counts stored embedding vectors (ast_node rows with source_embedding).

exec_delete_by_file(file_path, query_fn)

@spec exec_delete_by_file(String.t(), Dllb.MetaAST.query_fn()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Executes a two-step delete-by-file: SELECT ids, then batch DELETE.

Returns {:ok, count} or {:error, reason}.

exec_delete_by_project(project_path, query_fn)

@spec exec_delete_by_project(String.t(), Dllb.MetaAST.query_fn()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Executes a two-step delete-by-project: SELECT ids, then batch DELETE.

exec_load_file_nodes(file_path, query_fn)

@spec exec_load_file_nodes(String.t(), Dllb.MetaAST.query_fn()) ::
  {:ok, [map()]} | {:error, term()}

Loads all nodes for a file and returns them as parsed maps.

exec_stats(query_fn)

@spec exec_stats(Dllb.MetaAST.query_fn()) :: {:ok, map()} | {:error, term()}

Executes stats query and returns aggregated counts.

find_function(module_name, func_name, arity)

@spec find_function(String.t(), String.t(), non_neg_integer()) :: String.t()

SELECT a function node by module, name, and arity.

find_module(name)

@spec find_module(String.t()) :: String.t()

SELECT a module (container) node by name.

functions_of_module(module_name, opts \\ [])

@spec functions_of_module(
  String.t(),
  keyword()
) :: String.t()

SELECT function nodes belonging to a module.

Requires the module field to be populated during ingestion.

hybrid_search(text, embedding, opts \\ [])

@spec hybrid_search(String.t(), [float()], keyword()) :: String.t()

Combined vector + full-text hybrid search.

Scores are combined as vec_weight * vec_score + ft_weight * ft_score.

Options

  • :limit - max results (default 10)
  • :ef - HNSW exploration factor (default 100)
  • :vec_weight - weight for vector score (default 0.6)
  • :ft_weight - weight for full-text score (default 0.4)

importers_of(record_id)

@spec importers_of(String.t()) :: String.t()

Graph traversal: find all modules that import a given node.

imports_of(record_id)

@spec imports_of(String.t()) :: String.t()

Graph traversal: find all imports of a node.

nodes_by_file(file_path, opts \\ [])

@spec nodes_by_file(
  String.t(),
  keyword()
) :: String.t()

SELECT all AST nodes belonging to a file.

nodes_by_kind(kind, opts \\ [])

@spec nodes_by_kind(
  String.t(),
  keyword()
) :: String.t()

SELECT all AST nodes of a given kind (e.g. "function_def", "container").

nodes_by_project(project_path, opts \\ [])

@spec nodes_by_project(
  String.t(),
  keyword()
) :: String.t()

SELECT nodes filtered by project path.

search_docs(text, opts \\ [])

@spec search_docs(
  String.t(),
  keyword()
) :: String.t()

Full-text BM25 search on docstring.

search_source(text, opts \\ [])

@spec search_source(
  String.t(),
  keyword()
) :: String.t()

Full-text BM25 search on source_text.

Options

  • :limit - max results (default 20)
  • :kind - optional kind filter

set_source_embedding(attrs, embedding)

@spec set_source_embedding(map(), [number()]) :: String.t()

Builds an UPDATE ast_node SET source_embedding = [...] WHERE <attrs> statement that attaches an embedding to the row(s) identified by attrs.

attrs is a map of column => value (e.g. %{kind: "function_def", module: "Foo", name: "bar", arity: 2}). nil values are dropped and the remaining columns are ANDed together (sorted for determinism). Targeting by stable attributes avoids reconstructing synthetic record IDs.

similar_to(embedding, opts \\ [])

@spec similar_to(
  [float()],
  keyword()
) :: String.t()

HNSW vector similarity search on source_embedding.

Options

  • :limit - max results (default 10)
  • :ef - HNSW exploration factor (default 100)
  • :kind - optional kind filter
  • :file_path - optional file filter
  • :language - optional language filter

stats_query()

@spec stats_query() :: String.t()

Returns a SELECT for overall statistics (node count).

Note: dllb does not yet support COUNT/GROUP BY, so this returns all ids and the caller aggregates client-side.