query

package
v0.52.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package query implements the TDQ (td query) language parser, lexer, AST, and evaluator for filtering issues.

Index

Constants

View Source
const (
	OpEq          = "="
	OpNeq         = "!="
	OpLt          = "<"
	OpGt          = ">"
	OpLte         = "<="
	OpGte         = ">="
	OpContains    = "~"
	OpNotContains = "!~"
	OpIn          = "IN"
	OpNotIn       = "NOT IN"
)

Operator constants

View Source
const (
	OpAnd = "AND"
	OpOr  = "OR"
	OpNot = "NOT"
)

Boolean operator constants

View Source
const (
	// DefaultMaxResults limits in-memory filtering to prevent OOM
	DefaultMaxResults = 10000
	// MaxDescendantDepth prevents infinite recursion in descendant_of
	MaxDescendantDepth = 100
)
View Source
const MaxQueryDepth = 50

MaxQueryDepth limits nesting to prevent stack overflow

Variables

View Source
var CrossEntityFields = map[string]map[string]string{
	"log": {
		"message":   "string",
		"type":      "enum",
		"timestamp": "date",
		"session":   "string",
	},
	"comment": {
		"text":    "string",
		"created": "date",
		"session": "string",
	},
	"handoff": {
		"done":      "string",
		"remaining": "string",
		"decisions": "string",
		"uncertain": "string",
		"timestamp": "date",
	},
	"file": {
		"path": "string",
		"role": "enum",
	},
	"dep": {
		"blocks":     "string",
		"depends_on": "string",
	},
	"note": {
		"title":    "string",
		"content":  "string",
		"created":  "date",
		"updated":  "date",
		"pinned":   "bool",
		"archived": "bool",
	},
}

Cross-entity field mappings

View Source
var EnumValues = map[string][]string{
	"status":    {"open", "in_progress", "blocked", "in_review", "closed"},
	"type":      {"bug", "feature", "task", "epic", "chore"},
	"priority":  {"P0", "P1", "P2", "P3", "P4"},
	"log.type":  {"progress", "blocker", "decision", "hypothesis", "tried", "result", "orchestration"},
	"file.role": {"implementation", "test", "reference", "config"},
}

Enum values for validation

View Source
var KnownFields = map[string]string{

	"id":          "string",
	"title":       "string",
	"description": "string",
	"status":      "enum",
	"type":        "enum",
	"priority":    "ordinal",
	"points":      "number",
	"labels":      "string",
	"parent":      "string",
	"epic":        "string",
	"implementer": "string",
	"reviewer":    "string",
	"minor":       "bool",
	"branch":      "string",
	"sprint":      "string",
	"created":     "date",
	"updated":     "date",
	"closed":      "date",

	"log":     "prefix",
	"comment": "prefix",
	"handoff": "prefix",
	"file":    "prefix",
	"dep":     "prefix",
	"note":    "prefix",
}

Known field names for validation

View Source
var KnownFunctions = map[string]struct {
	MinArgs int
	MaxArgs int
	Help    string
}{
	"has":           {1, 1, "has(field) - field is not empty"},
	"is":            {1, 1, "is(status) - shorthand for status check"},
	"any":           {2, -1, "any(field, v1, v2, ...) - field matches any value"},
	"all":           {2, -1, "all(field, v1, v2, ...) - field matches all values"},
	"none":          {2, -1, "none(field, v1, v2, ...) - field matches none"},
	"blocks":        {1, 1, "blocks(id) - issues that block the given id"},
	"blocked_by":    {1, 1, "blocked_by(id) - issues blocked by the given id"},
	"child_of":      {1, 1, "child_of(id) - direct children of issue"},
	"descendant_of": {1, 1, "descendant_of(id) - all descendants (recursive)"},
	"linked_to":     {1, 1, "linked_to(path) - issues linked to file path"},
	"rework":        {0, 0, "rework() - issues rejected and awaiting rework"},
	"is_ready":      {0, 0, "is_ready() - issues with no open dependencies"},
	"has_open_deps": {0, 0, "has_open_deps() - issues with open dependencies"},
	"label":         {1, 1, "label(name) - issues with the given label"},
	"labels":        {1, 1, "labels(name) - alias for label()"},
}

Known functions

View Source
var NoteSortFieldToColumn = map[string]string{
	"created":  "created_at",
	"updated":  "updated_at",
	"title":    "title",
	"pinned":   "pinned",
	"archived": "archived",
}

NoteSortFieldToColumn maps user-facing sort field names to DB columns for notes

View Source
var SortFieldToColumn = map[string]string{
	"created":  "created_at",
	"updated":  "updated_at",
	"closed":   "closed_at",
	"deleted":  "deleted_at",
	"priority": "priority",
	"id":       "id",
	"title":    "title",
	"status":   "status",
	"points":   "points",
	"sprint":   "sprint",
}

SortFieldToColumn maps user-facing sort field names to DB columns

Functions

func Execute

func Execute(database QuerySource, queryStr string, sessionID string, opts ExecuteOptions) ([]models.Issue, error)

Execute parses and executes a TDQ query

func ExecuteNotes added in v0.33.0

func ExecuteNotes(database NoteQuerySource, queryStr string, opts NoteExecuteOptions) ([]models.Note, error)

ExecuteNotes parses and executes a TDQ query against the notes table. The query must use note. prefixed fields (e.g., note.title ~ "meeting"). The note. prefix is stripped before matching against Note struct fields.

func HasNoteFields added in v0.33.0

func HasNoteFields(q *Query) bool

HasNoteFields checks if the query AST contains any note. prefix fields. Used to determine whether to route the query to ExecuteNotes instead of Execute.

func QuickSearch

func QuickSearch(database *db.DB, text string, sessionID string, limit int) ([]models.Issue, error)

QuickSearch performs a simple text search (backward compatible with existing search)

Types

type BinaryExpr

type BinaryExpr struct {
	Op    string // "AND" or "OR"
	Left  Node
	Right Node
}

BinaryExpr represents a binary expression (AND, OR)

func (*BinaryExpr) String

func (b *BinaryExpr) String() string

type CommentEntry

type CommentEntry struct {
	Text    string
	Created time.Time
	Session string
}

CommentEntry represents a comment for cross-entity filtering

type DateValue

type DateValue struct {
	Raw      string // original value: "2024-01-15", "-7d", "today", etc.
	Relative bool   // true if relative date
}

DateValue represents a date (absolute or relative)

func (*DateValue) String

func (d *DateValue) String() string

type EvalContext

type EvalContext struct {
	CurrentSession string    // for @me resolution
	Now            time.Time // for relative date calculation
}

EvalContext provides context for query evaluation

func NewEvalContext

func NewEvalContext(sessionID string) *EvalContext

NewEvalContext creates a new evaluation context

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator converts a Query AST to SQL conditions and in-memory filters

func NewEvaluator

func NewEvaluator(ctx *EvalContext, query *Query) *Evaluator

NewEvaluator creates a new query evaluator

func (*Evaluator) HasCrossEntityConditions

func (e *Evaluator) HasCrossEntityConditions() bool

HasCrossEntityConditions checks if the query has cross-entity conditions

func (*Evaluator) ToMatcher

func (e *Evaluator) ToMatcher() (func(models.Issue) bool, error)

ToMatcher returns a function that matches issues in memory Used for complex conditions that can't be expressed in SQL

func (*Evaluator) ToSQLConditions

func (e *Evaluator) ToSQLConditions() ([]SQLCondition, error)

ToSQLConditions converts the query to SQL WHERE clauses Returns conditions that can be pushed to the database

type ExecuteOptions

type ExecuteOptions struct {
	Limit      int
	SortBy     string
	SortDesc   bool
	MaxResults int // Max issues to process in-memory (0 = DefaultMaxResults)
}

ExecuteOptions contains options for query execution

type FieldExpr

type FieldExpr struct {
	Field    string      // e.g., "status", "log.message"
	Operator string      // =, !=, ~, !~, <, >, <=, >=
	Value    interface{} // string, int, date value, or special value
}

FieldExpr represents a field comparison (field op value)

func (*FieldExpr) String

func (f *FieldExpr) String() string

type FileEntry

type FileEntry struct {
	Path string
	Role string
}

FileEntry represents a linked file for cross-entity filtering

type FunctionCall

type FunctionCall struct {
	Name string
	Args []interface{}
}

FunctionCall represents a function call like has(labels), is(open)

func (*FunctionCall) String

func (fn *FunctionCall) String() string

type HandoffEntry

type HandoffEntry struct {
	Done      string
	Remaining string
	Decisions string
	Uncertain string
	Timestamp time.Time
}

HandoffEntry represents a handoff for cross-entity filtering

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer tokenizes TDQ query strings

func NewLexer

func NewLexer(input string) *Lexer

NewLexer creates a new lexer for the given input

func (*Lexer) Tokenize

func (l *Lexer) Tokenize() ([]Token, error)

Tokenize returns all tokens from the input

type ListValue

type ListValue struct {
	Values []interface{}
}

ListValue represents a list of values for IN operator

func (*ListValue) String

func (l *ListValue) String() string

type LogEntry

type LogEntry struct {
	Message   string
	Type      string
	Timestamp time.Time
	Session   string
}

LogEntry represents a log entry for cross-entity filtering

type Node

type Node interface {
	String() string
	// contains filtered or unexported methods
}

Node is the interface for all AST nodes

type NoteExecuteOptions added in v0.33.0

type NoteExecuteOptions struct {
	Limit      int
	SortBy     string
	SortDesc   bool
	MaxResults int
}

NoteExecuteOptions contains options for note query execution

type NoteQuerySource added in v0.33.0

type NoteQuerySource interface {
	ListNotes(opts db.ListNotesOptions) ([]models.Note, error)
}

NoteQuerySource abstracts note-related database operations for TDQ note queries. Notes are standalone entities (not linked to issues), so they use a separate interface.

type ParseError

type ParseError struct {
	Message  string
	Pos      int
	Line     int
	Column   int
	Token    Token
	Expected string
}

ParseError represents a parsing error with position information

func (*ParseError) Error

func (e *ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses TDQ query strings into an AST

type Query

type Query struct {
	Root Node
	Raw  string      // original query string
	Sort *SortClause // optional sort clause
}

Query represents a parsed TDQ query

func Parse

func Parse(input string) (*Query, error)

Parse parses a TDQ query string and returns a Query AST

func (*Query) String

func (q *Query) String() string

func (*Query) Validate

func (q *Query) Validate() []error

Validate checks the query AST for semantic errors

type QueryResult

type QueryResult struct {
	Issues []models.Issue

	// For cross-entity queries, we may need to filter after fetching
	CrossEntityFilter func(issue models.Issue, logs []LogEntry, comments []CommentEntry, handoffs []HandoffEntry, files []FileEntry) bool
}

QueryResult contains the result of query evaluation

type QuerySource added in v0.32.0

type QuerySource interface {
	ListIssues(opts db.ListIssuesOptions) ([]models.Issue, error)
	GetIssue(id string) (*models.Issue, error)
	GetLogs(issueID string, limit int) ([]models.Log, error)
	GetComments(issueID string) ([]models.Comment, error)
	GetLatestHandoff(issueID string) (*models.Handoff, error)
	GetLinkedFiles(issueID string) ([]models.IssueFile, error)
	GetDependencies(issueID string) ([]string, error)
	GetRejectedInProgressIssueIDs() (map[string]bool, error)
	GetIssuesWithOpenDeps() (map[string]bool, error)
}

QuerySource abstracts the database operations needed by the TDQ query engine. Both db.DB (client-side) and snapshot adapters (server-side) implement this.

type SQLCondition

type SQLCondition struct {
	Clause string
	Args   []interface{}
}

SQLCondition represents a SQL WHERE clause fragment

type SortClause added in v0.4.15

type SortClause struct {
	Field      string // DB column name (e.g., "created_at", "priority")
	Descending bool   // true for descending order
}

SortClause represents a sort specification

func (*SortClause) String added in v0.4.15

func (s *SortClause) String() string

type SpecialValue

type SpecialValue struct {
	Type string // "me", "empty", "null"
}

SpecialValue represents special values like @me, EMPTY, NULL

func (*SpecialValue) String

func (s *SpecialValue) String() string

type TextSearch

type TextSearch struct {
	Text string
}

TextSearch represents a bare text search (searches title, description, id)

func (*TextSearch) String

func (t *TextSearch) String() string

type Token

type Token struct {
	Type   TokenType
	Value  string
	Pos    int // position in input
	Line   int
	Column int
}

Token represents a lexer token

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int

TokenType represents the type of a lexer token

const (
	// Special tokens
	TokenEOF TokenType = iota
	TokenError

	// Literals
	TokenIdent  // field names, function names
	TokenString // "quoted" or 'quoted' strings
	TokenNumber // integers
	TokenDate   // 2024-01-15

	// Operators
	TokenEq          // =
	TokenNeq         // !=
	TokenLt          // <
	TokenGt          // >
	TokenLte         // <=
	TokenGte         // >=
	TokenContains    // ~
	TokenNotContains // !~

	// Boolean operators
	TokenAnd // AND, &&
	TokenOr  // OR, ||
	TokenNot // NOT, !, -

	// Delimiters
	TokenLParen // (
	TokenRParen // )
	TokenComma  // ,
	TokenDot    // .

	// Special values
	TokenAtMe  // @me
	TokenEmpty // EMPTY
	TokenNull  // NULL

	// Sort clause
	TokenSort // sort:field or sort:-field
)

func (TokenType) String

func (t TokenType) String() string

type UnaryExpr

type UnaryExpr struct {
	Op   string // "NOT"
	Expr Node
}

UnaryExpr represents a unary expression (NOT)

func (*UnaryExpr) String

func (u *UnaryExpr) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL