Expand description
Query language for AST-aware code search Query language for AST-aware code search
This module provides a powerful query language for searching code based on semantic properties like symbol type, name patterns, language, and more.
§Query Parsing
All queries are parsed through the boolean AST parser:
- Boolean queries: Parsed directly via
QueryParser - Execution: Use
QueryExecutor::execute_on_graphfor string queries
§Recommended Entry Points
| Use Case | Function | Notes |
|---|---|---|
| Query execution | QueryExecutor::execute_on_graph | Most common - handles everything |
| AST access | QueryParser::parse_query | Boolean queries only |
§Query Language
The boolean query language supports:
- Boolean operators: AND, OR, NOT
- Comparison operators:
:(equal),~=(regex),>,<,>=,<= - Field types: kind, name, path, lang, text, and plugin-specific fields
- Values: strings, regex patterns, numbers, booleans
- Grouping: parentheses for precedence
§Core Types
Query- Complete query with root expressionExpr- Expression nodes (Or, And, Not, Condition)Condition- Field-operator-value conditionsOperator- Comparison operatorsValue- Values in conditions
§Error Handling
All errors use QueryError which wraps:
LexError- Tokenization errorsParseError- Parsing errorsValidationError- Semantic validation errorsExecutionError- Runtime execution errors
§Performance
The query lexer uses a thread-local buffer pool to reduce allocations:
- Allocation efficiency: 80% fewer heap allocations vs creating fresh lexers
- Memory efficiency: Reuses token buffers across multiple tokenize calls
- Thread-safe: Each thread has its own isolated pool
- Configurable: Can be tuned or disabled via environment variables
§Lexer Pool Configuration
The lexer pool can be configured via environment variables:
# Pool size (default: 4 lexers per thread)
export SQRY_LEXER_POOL_MAX=8
# Buffer capacity limit (default: 256 tokens)
export SQRY_LEXER_POOL_MAX_CAP=512
# Shrink ratio (default: 8x)
export SQRY_LEXER_POOL_SHRINK_RATIO=4
# Disable pooling (for latency-critical workloads)
export SQRY_LEXER_POOL_MAX=0Performance characteristics:
- Simple queries (<10 tokens): ~380ns (pooled) vs ~223ns (fresh) – pooling adds overhead
- Long queries (>100 tokens): ~18.2µs (pooled) vs ~19.1µs (fresh) – pooling saves ~4%
- High-throughput workloads: Pool reduces GC pressure and memory fragmentation
Recommendation: Keep pooling enabled (default) for most workloads. The overhead (~150ns per query) is negligible compared to typical query execution time (>10ms). Disable only for latency-critical single-query scenarios.
§Examples
ⓘ
use sqry_core::query::QueryParser;
// Find all async functions
let query = QueryParser::parse_query("kind:function AND async:true").unwrap();
// Find test functions (using regex)
let query = QueryParser::parse_query("kind:function AND name~=/^test_/").unwrap();
// Find functions OR methods in Rust files
let query = QueryParser::parse_query("(kind:function OR kind:method) AND lang:rust").unwrap();
// Find non-test functions
let query = QueryParser::parse_query("kind:function AND NOT name~=/test/").unwrap();Re-exports§
pub use error::ExecutionError;pub use error::LexError;pub use error::ParseError;pub use error::QueryError;pub use error::RichQueryError;pub use error::ValidationError;pub use lexer::Lexer;pub use lexer::Token;pub use lexer::TokenType;pub use optimizer::Optimizer;pub use parsed_query::ParsedQuery;pub use parser_new::Parser as QueryParser;pub use plan::CacheStatus;pub use plan::ExecutionStep;pub use plan::QueryPlan;pub use registry::FieldRegistry;pub use registry::core_fields;pub use types::Condition;pub use types::Expr;pub use types::Field;pub use types::FieldDescriptor;pub use types::FieldType;pub use types::JoinEdgeKind;pub use types::JoinExpr;pub use types::Operator;pub use types::PipelineQuery;pub use types::PipelineStage;pub use types::Query as QueryAST;pub use types::RegexFlags;pub use types::RegexValue;pub use types::Span;pub use types::Value;pub use validator::ContradictionWarning;pub use validator::Validator;pub use pipeline::AggregationResult;pub use pipeline::CountResult;pub use pipeline::GroupByResult;pub use pipeline::StatsResult;pub use pipeline::TopResult;pub use builder::BuildError;pub use builder::ConditionBuilder;pub use builder::QueryBuilder;pub use builder::RegexBuilder;pub use results::JoinMatch;pub use results::JoinResults;pub use results::QueryMatch;pub use results::QueryOutput;pub use results::QueryResults;
Modules§
- builder
- Query Builder API for type-safe programmatic query construction
- cache
- Query caching module
- error
- Error types for the query language
- lexer
- Lexer for the query language
- optimizer
- Query optimizer for efficient execution
- parsed_
query ParsedQuery- canonical AST representation for indexed and non-indexed flows- parser_
new - Parser for the query language
- pipeline
- Pipeline/aggregation result types for post-query analysis.
- plan
- Query execution plan types for –explain output
- regex_
cache - Regex compilation caching for query performance
- registry
- Field registry for query validation and optimization
- results
- Query results with Arc-based ownership for zero-copy access.
- security
- Security controls for query execution
- types
- Core types for the query language
- validator
- Semantic validation for query ASTs
Structs§
- Circular
Config - Configuration for circular dependency detection
- Duplicate
Config - Configuration for duplicate detection
- Duplicate
Group - A group of duplicate symbols
- Query
Executor - Executes queries against code using
CodeGraph. - Repo
Filter - Filter helper for repository scoping predicates (
repo:).
Enums§
- Circular
Type - Type of circular dependency to detect
- Duplicate
Type - Type of duplicate detection
- Unused
Scope - Scope for unused analysis
Functions§
- build_
duplicate_ groups_ graph - Find all duplicate groups in the graph.
- compute_
reachable_ set_ graph - Compute the set of reachable nodes from entry points via BFS traversal.
- execute_
pipeline_ stage - Execute a single pipeline stage on query results.
- find_
all_ cycles_ graph - Find all cycles in the graph using call or import edges.
- find_
unused_ nodes - Find all unused nodes in the graph that match the given scope.
- is_
node_ in_ cycle - Check if a specific node is part of a cycle.
- is_
node_ unused - Check if a specific node is unused based on the scope filter.