Dllb.MetaAST.Query (Dllb v0.7.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.

Builds a single-statement DELETE ast_node WHERE file_path = ... that removes every node belonging to a file in one server-side operation (secondary, full-text, and vector indexes are maintained by the engine). Pair with exec_delete_by_file/2.

Builds a single-statement DELETE ast_node WHERE project_path = ... that removes every node of a project in one server-side operation. Pair with exec_delete_by_project/2.

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 native delete-by-file (DELETE ... WHERE) in a single round-trip, returning {:ok, count} (rows removed) or {:error, reason}.

Executes a native delete-by-project (DELETE ... WHERE) in a single round-trip, returning {:ok, count} or {:error, reason}.

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

Executes the grouped stats query and returns {:ok, %{total: n, by_kind: %{kind => count}}}.

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

SELECT a module (container) node by name.

SELECT function nodes belonging to a module.

Combined full-text + vector hybrid search via the engine's native HYBRID SEARCH verb: BM25 on source_text fused with HNSW on source_embedding. Each row carries score, text_score, and vector_score.

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, built with the engine's native SEARCH verb. Each row carries a score.

Full-text BM25 search on source_text, built with the engine's native SEARCH verb. Each row carries a score. Requires a full-text index 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.

Vector (HNSW) similarity search over source_embedding, built with the engine's native VECTOR SEARCH verb. Results come back nearest-first, each row carrying a distance field. Requires a vector index on source_embedding (see Dllb.Schema.ast_node_search_indexes/0).

Returns a COUNT ast_node GROUP BY kind query: one row per kind, each carrying a count, aggregated server-side.

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(file_path)

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

Builds a single-statement DELETE ast_node WHERE file_path = ... that removes every node belonging to a file in one server-side operation (secondary, full-text, and vector indexes are maintained by the engine). Pair with exec_delete_by_file/2.

delete_by_project(project_path)

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

Builds a single-statement DELETE ast_node WHERE project_path = ... that removes every node of a project in one server-side operation. Pair with exec_delete_by_project/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 native delete-by-file (DELETE ... WHERE) in a single round-trip, returning {:ok, count} (rows removed) 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 native delete-by-project (DELETE ... WHERE) in a single round-trip, returning {:ok, count} or {:error, reason}.

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 the grouped stats query and returns {:ok, %{total: n, by_kind: %{kind => count}}}.

The grouped rows carry serde-tagged values (e.g. %{"String" => "..."}), which are unwrapped here into plain kinds and integer 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(), [number()], keyword()) :: String.t()

Combined full-text + vector hybrid search via the engine's native HYBRID SEARCH verb: BM25 on source_text fused with HNSW on source_embedding. Each row carries score, text_score, and vector_score.

Options

  • :limit - max results (default 10)
  • :alpha - weight on the text score in 0.0..1.0 (default 0.5); the vector score gets 1 - alpha
  • :kind / :file_path / :language / :project_path - optional scope filters

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, built with the engine's native SEARCH verb. Each row carries a score.

Options

  • :limit - max results (default 20)

search_source(text, opts \\ [])

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

Full-text BM25 search on source_text, built with the engine's native SEARCH verb. Each row carries a score. Requires a full-text index on source_text.

Options

  • :limit - max results (default 20)
  • :kind / :file_path / :language / :project_path - optional scope filters

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(
  [number()],
  keyword()
) :: String.t()

Vector (HNSW) similarity search over source_embedding, built with the engine's native VECTOR SEARCH verb. Results come back nearest-first, each row carrying a distance field. Requires a vector index on source_embedding (see Dllb.Schema.ast_node_search_indexes/0).

Options

  • :limit - max results (default 10)
  • :kind - optional kind filter (scopes results server-side)
  • :file_path - optional file filter
  • :language - optional language filter
  • :project_path - optional project filter (multi-project isolation)

stats_query()

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

Returns a COUNT ast_node GROUP BY kind query: one row per kind, each carrying a count, aggregated server-side.