mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-10-28 07:03:20 +08:00
Performance & quality: - Transaction wrapping (Querier interface, single tx per index run) - Composite indexes on edges table for faster traversals - Cypher WHERE push-down (=, CONTAINS, STARTS WITH → SQL) - golangci-lint with 25 linters, zero violations - 11 benchmarks for pipeline, search, and BFS Incremental indexing: - Content-hash based file change detection - Dependency-aware two-level invalidation (changed + dependents) - Stale file cleanup for deleted files Async boundary detection: - Project-wide env var URL scanner (Dockerfile, YAML, .tf, .sh, .env, TOML, properties files) with 3-layer secret exclusion - Env var default extraction in constant propagation (getEnv, os.environ.get, process.env with ||/?? fallback) - ASYNC_CALLS edge type for Cloud Tasks, Pub/Sub, SQS/SNS, RabbitMQ, Kafka dispatch patterns, separate from synchronous HTTP_CALLS - Tool descriptions updated with all edge types Other improvements: - Type inference for method dispatch resolution - Interface implementation detection (IMPLEMENTS edges) - Improved tool descriptions for discoverability
964 lines
23 KiB
Go
964 lines
23 KiB
Go
package cypher
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/DeusData/codebase-memory-mcp/internal/store"
|
|
)
|
|
|
|
const maxResultRows = 200
|
|
|
|
// Executor runs Cypher execution plans against a store.
|
|
type Executor struct {
|
|
Store *store.Store
|
|
}
|
|
|
|
// Result holds the tabular output of a query.
|
|
type Result struct {
|
|
Columns []string `json:"columns"`
|
|
Rows []map[string]any `json:"rows"`
|
|
}
|
|
|
|
// binding maps variable names to matched nodes and edges.
|
|
type binding struct {
|
|
nodes map[string]*store.Node
|
|
edges map[string]*store.Edge
|
|
}
|
|
|
|
func newBinding() binding {
|
|
return binding{
|
|
nodes: make(map[string]*store.Node),
|
|
edges: make(map[string]*store.Edge),
|
|
}
|
|
}
|
|
|
|
// adjacentResult pairs a matched node with the edge that reached it.
|
|
type adjacentResult struct {
|
|
Node *store.Node
|
|
Edge *store.Edge
|
|
}
|
|
|
|
// Execute parses, plans, and executes a Cypher query across all projects.
|
|
func (e *Executor) Execute(query string) (*Result, error) {
|
|
q, err := Parse(query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse: %w", err)
|
|
}
|
|
plan, err := BuildPlan(q)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("plan: %w", err)
|
|
}
|
|
return e.executePlan(plan)
|
|
}
|
|
|
|
func (e *Executor) executePlan(plan *Plan) (*Result, error) {
|
|
projects, err := e.Store.ListProjects()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list projects: %w", err)
|
|
}
|
|
|
|
var allBindings []binding
|
|
for _, proj := range projects {
|
|
bindings, err := e.executeStepsForProject(proj.Name, plan.Steps)
|
|
if err != nil {
|
|
continue // skip projects that error
|
|
}
|
|
allBindings = append(allBindings, bindings...)
|
|
if len(allBindings) > maxResultRows*2 {
|
|
allBindings = allBindings[:maxResultRows*2]
|
|
break
|
|
}
|
|
}
|
|
|
|
return e.projectResults(allBindings, plan.ReturnSpec)
|
|
}
|
|
|
|
func (e *Executor) executeStepsForProject(project string, steps []PlanStep) ([]binding, error) {
|
|
var bindings []binding
|
|
|
|
for i, step := range steps {
|
|
var err error
|
|
switch s := step.(type) {
|
|
case *ScanNodes:
|
|
// Check if the next step is a FilterWhere that can be pushed down
|
|
var pushDown *FilterWhere
|
|
if i+1 < len(steps) {
|
|
if fw, ok := steps[i+1].(*FilterWhere); ok {
|
|
pushDown = fw
|
|
}
|
|
}
|
|
bindings, err = e.execScan(project, s, pushDown)
|
|
case *ExpandRelationship:
|
|
bindings, err = e.execExpand(s, bindings)
|
|
case *FilterWhere:
|
|
// Skip if this was already consumed by push-down
|
|
if i > 0 {
|
|
if _, wasScan := steps[i-1].(*ScanNodes); wasScan {
|
|
continue // already handled in execScan
|
|
}
|
|
}
|
|
bindings, err = e.execFilter(s, bindings)
|
|
default:
|
|
return nil, fmt.Errorf("unknown step type: %T", step)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
isLastStep := i == len(steps)-1
|
|
_, isExpand := step.(*ExpandRelationship)
|
|
if isLastStep || isExpand {
|
|
if len(bindings) > maxResultRows*2 {
|
|
bindings = bindings[:maxResultRows*2]
|
|
}
|
|
}
|
|
}
|
|
|
|
return bindings, nil
|
|
}
|
|
|
|
// sqlPushableColumns are node properties that map directly to SQL columns.
|
|
var sqlPushableColumns = map[string]string{
|
|
"name": "name",
|
|
"qualified_name": "qualified_name",
|
|
"label": "label",
|
|
"file_path": "file_path",
|
|
}
|
|
|
|
func (e *Executor) execScan(project string, s *ScanNodes, pushDown *FilterWhere) ([]binding, error) {
|
|
// Build dynamic SQL query with optional push-down conditions
|
|
query := `SELECT id, project, label, name, qualified_name, file_path, start_line, end_line, properties FROM nodes WHERE project=?`
|
|
args := []any{project}
|
|
|
|
if s.Label != "" {
|
|
query += " AND label=?"
|
|
args = append(args, s.Label)
|
|
}
|
|
|
|
// Push down WHERE conditions into SQL where possible
|
|
var unpushedConditions []Condition
|
|
if pushDown != nil {
|
|
for _, c := range pushDown.Conditions {
|
|
col, canPush := sqlPushableColumns[c.Property]
|
|
if !canPush {
|
|
unpushedConditions = append(unpushedConditions, c)
|
|
continue
|
|
}
|
|
switch c.Operator {
|
|
case "=":
|
|
query += " AND " + col + "=?"
|
|
args = append(args, c.Value)
|
|
case "CONTAINS":
|
|
query += " AND " + col + " LIKE ?"
|
|
args = append(args, "%"+c.Value+"%")
|
|
case "STARTS WITH":
|
|
query += " AND " + col + " LIKE ?"
|
|
args = append(args, c.Value+"%")
|
|
default:
|
|
// =~, numeric comparisons: can't push to SQL easily
|
|
unpushedConditions = append(unpushedConditions, c)
|
|
}
|
|
}
|
|
}
|
|
|
|
rows, err := e.Store.DB().Query(query, args...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan nodes: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var nodes []*store.Node
|
|
for rows.Next() {
|
|
n, scanErr := scanNodeFromRows(rows)
|
|
if scanErr != nil {
|
|
return nil, scanErr
|
|
}
|
|
nodes = append(nodes, n)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Apply inline property filters
|
|
if len(s.Props) > 0 {
|
|
nodes = filterNodesByProps(nodes, s.Props)
|
|
}
|
|
|
|
bindings := make([]binding, 0, len(nodes))
|
|
for _, n := range nodes {
|
|
// Apply unpushed conditions in Go
|
|
if len(unpushedConditions) > 0 {
|
|
b := newBinding()
|
|
b.nodes[s.Variable] = n
|
|
match, evalErr := evaluateConditions(b, unpushedConditions, "AND")
|
|
if evalErr != nil {
|
|
return nil, evalErr
|
|
}
|
|
if !match {
|
|
continue
|
|
}
|
|
}
|
|
b := newBinding()
|
|
if s.Variable != "" {
|
|
b.nodes[s.Variable] = n
|
|
}
|
|
bindings = append(bindings, b)
|
|
}
|
|
return bindings, nil
|
|
}
|
|
|
|
// scanNodeFromRows scans a single node from sql.Rows.
|
|
func scanNodeFromRows(rows *sql.Rows) (*store.Node, error) {
|
|
var n store.Node
|
|
var props string
|
|
if err := rows.Scan(&n.ID, &n.Project, &n.Label, &n.Name, &n.QualifiedName, &n.FilePath, &n.StartLine, &n.EndLine, &props); err != nil {
|
|
return nil, err
|
|
}
|
|
n.Properties = store.UnmarshalProps(props)
|
|
return &n, nil
|
|
}
|
|
|
|
func (e *Executor) execExpand(s *ExpandRelationship, bindings []binding) ([]binding, error) {
|
|
if len(bindings) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
isVariableLength := s.MinHops != 1 || s.MaxHops != 1
|
|
|
|
result := make([]binding, 0, len(bindings))
|
|
for _, b := range bindings {
|
|
fromNode, ok := b.nodes[s.FromVar]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if isVariableLength {
|
|
expanded, err := e.expandVariableLength(b, fromNode, s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, expanded...)
|
|
} else {
|
|
expanded, err := e.expandFixedLength(b, fromNode, s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, expanded...)
|
|
}
|
|
|
|
if len(result) > maxResultRows*2 {
|
|
result = result[:maxResultRows*2]
|
|
break
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (e *Executor) expandFixedLength(b binding, fromNode *store.Node, s *ExpandRelationship) ([]binding, error) {
|
|
adjacents, err := e.findAdjacentNodes(fromNode.ID, s.EdgeTypes, s.Direction)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]binding, 0, len(adjacents))
|
|
for _, adj := range adjacents {
|
|
if s.ToLabel != "" && adj.Node.Label != s.ToLabel {
|
|
continue
|
|
}
|
|
if len(s.ToProps) > 0 && !nodeMatchesProps(adj.Node, s.ToProps) {
|
|
continue
|
|
}
|
|
newB := copyBinding(b)
|
|
if s.ToVar != "" {
|
|
newB.nodes[s.ToVar] = adj.Node
|
|
}
|
|
if s.RelVar != "" && adj.Edge != nil {
|
|
newB.edges[s.RelVar] = adj.Edge
|
|
}
|
|
result = append(result, newB)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (e *Executor) expandVariableLength(b binding, fromNode *store.Node, s *ExpandRelationship) ([]binding, error) {
|
|
maxDepth := s.MaxHops
|
|
if maxDepth == 0 {
|
|
maxDepth = 10 // cap unbounded at 10
|
|
}
|
|
|
|
direction := s.Direction
|
|
if direction == "" {
|
|
direction = "outbound"
|
|
}
|
|
|
|
edgeTypes := s.EdgeTypes
|
|
if len(edgeTypes) == 0 {
|
|
edgeTypes = []string{"CALLS"} // default
|
|
}
|
|
|
|
bfsResult, err := e.Store.BFS(fromNode.ID, direction, edgeTypes, maxDepth, maxResultRows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("bfs: %w", err)
|
|
}
|
|
|
|
result := make([]binding, 0, len(bfsResult.Visited))
|
|
for _, nh := range bfsResult.Visited {
|
|
if nh.Hop < s.MinHops {
|
|
continue
|
|
}
|
|
if s.MaxHops > 0 && nh.Hop > s.MaxHops {
|
|
continue
|
|
}
|
|
if s.ToLabel != "" && nh.Node.Label != s.ToLabel {
|
|
continue
|
|
}
|
|
if len(s.ToProps) > 0 && !nodeMatchesProps(nh.Node, s.ToProps) {
|
|
continue
|
|
}
|
|
newB := copyBinding(b)
|
|
if s.ToVar != "" {
|
|
newB.nodes[s.ToVar] = nh.Node
|
|
}
|
|
// Note: variable-length BFS doesn't bind individual edges
|
|
result = append(result, newB)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// findAdjacentNodes returns nodes connected to nodeID via the given edge types and direction.
|
|
func (e *Executor) findAdjacentNodes(nodeID int64, edgeTypes []string, direction string) ([]adjacentResult, error) {
|
|
allEdges, err := e.collectEdges(nodeID, edgeTypes, direction)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return e.resolveEdgeTargets(allEdges, nodeID, direction)
|
|
}
|
|
|
|
// collectEdges gathers edges from the store based on direction and type filters.
|
|
func (e *Executor) collectEdges(nodeID int64, edgeTypes []string, direction string) ([]*store.Edge, error) {
|
|
switch direction {
|
|
case "outbound":
|
|
return e.collectDirectionalEdges(nodeID, edgeTypes, true)
|
|
case "inbound":
|
|
return e.collectDirectionalEdges(nodeID, edgeTypes, false)
|
|
case "any":
|
|
return e.collectBidirectionalEdges(nodeID, edgeTypes)
|
|
default:
|
|
return e.collectDirectionalEdges(nodeID, edgeTypes, true)
|
|
}
|
|
}
|
|
|
|
// collectDirectionalEdges collects edges in one direction (outbound if isSource, inbound otherwise).
|
|
func (e *Executor) collectDirectionalEdges(nodeID int64, edgeTypes []string, isSource bool) ([]*store.Edge, error) {
|
|
if len(edgeTypes) > 0 {
|
|
var allEdges []*store.Edge
|
|
for _, et := range edgeTypes {
|
|
var edges []*store.Edge
|
|
var err error
|
|
if isSource {
|
|
edges, err = e.Store.FindEdgesBySourceAndType(nodeID, et)
|
|
} else {
|
|
edges, err = e.Store.FindEdgesByTargetAndType(nodeID, et)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
allEdges = append(allEdges, edges...)
|
|
}
|
|
return allEdges, nil
|
|
}
|
|
if isSource {
|
|
return e.Store.FindEdgesBySource(nodeID)
|
|
}
|
|
return e.Store.FindEdgesByTarget(nodeID)
|
|
}
|
|
|
|
// collectBidirectionalEdges collects edges in both directions, optionally filtered by type.
|
|
func (e *Executor) collectBidirectionalEdges(nodeID int64, edgeTypes []string) ([]*store.Edge, error) {
|
|
outEdges, err := e.Store.FindEdgesBySource(nodeID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
inEdges, err := e.Store.FindEdgesByTarget(nodeID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(edgeTypes) == 0 {
|
|
result := make([]*store.Edge, 0, len(outEdges)+len(inEdges))
|
|
result = append(result, outEdges...)
|
|
result = append(result, inEdges...)
|
|
return result, nil
|
|
}
|
|
typeSet := make(map[string]bool, len(edgeTypes))
|
|
for _, et := range edgeTypes {
|
|
typeSet[et] = true
|
|
}
|
|
var allEdges []*store.Edge
|
|
for _, edge := range outEdges {
|
|
if typeSet[edge.Type] {
|
|
allEdges = append(allEdges, edge)
|
|
}
|
|
}
|
|
for _, edge := range inEdges {
|
|
if typeSet[edge.Type] {
|
|
allEdges = append(allEdges, edge)
|
|
}
|
|
}
|
|
return allEdges, nil
|
|
}
|
|
|
|
// resolveEdgeTargets resolves edges to adjacentResult by looking up the target/source node.
|
|
func (e *Executor) resolveEdgeTargets(allEdges []*store.Edge, nodeID int64, direction string) ([]adjacentResult, error) {
|
|
seen := make(map[int64]bool)
|
|
results := make([]adjacentResult, 0, len(allEdges))
|
|
for _, edge := range allEdges {
|
|
targetID := edgeTargetID(edge, nodeID, direction)
|
|
if seen[targetID] {
|
|
continue
|
|
}
|
|
seen[targetID] = true
|
|
|
|
node, err := e.Store.FindNodeByID(targetID)
|
|
if err != nil || node == nil {
|
|
continue
|
|
}
|
|
results = append(results, adjacentResult{Node: node, Edge: edge})
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// edgeTargetID returns the ID of the node on the "other end" of the edge relative to nodeID.
|
|
func edgeTargetID(edge *store.Edge, nodeID int64, direction string) int64 {
|
|
switch direction {
|
|
case "inbound":
|
|
return edge.SourceID
|
|
case "any":
|
|
if edge.SourceID == nodeID {
|
|
return edge.TargetID
|
|
}
|
|
return edge.SourceID
|
|
default:
|
|
return edge.TargetID
|
|
}
|
|
}
|
|
|
|
func (e *Executor) execFilter(s *FilterWhere, bindings []binding) ([]binding, error) {
|
|
var result []binding
|
|
for _, b := range bindings {
|
|
match, err := evaluateConditions(b, s.Conditions, s.Operator)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if match {
|
|
result = append(result, b)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func evaluateConditions(b binding, conditions []Condition, op string) (bool, error) {
|
|
if op == "OR" {
|
|
for _, c := range conditions {
|
|
ok, err := evaluateCondition(b, c)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if ok {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
// AND (default)
|
|
for _, c := range conditions {
|
|
ok, err := evaluateCondition(b, c)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func evaluateCondition(b binding, c Condition) (bool, error) {
|
|
// Try node first, then edge
|
|
var actual any
|
|
if node, ok := b.nodes[c.Variable]; ok {
|
|
actual = getNodeProperty(node, c.Property)
|
|
} else if edge, ok := b.edges[c.Variable]; ok {
|
|
actual = getEdgeProperty(edge, c.Property)
|
|
} else {
|
|
return false, nil
|
|
}
|
|
|
|
switch c.Operator {
|
|
case "=":
|
|
return fmt.Sprintf("%v", actual) == c.Value, nil
|
|
case "=~":
|
|
s, ok := actual.(string)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
matched, err := regexp.MatchString(c.Value, s)
|
|
if err != nil {
|
|
return false, fmt.Errorf("regex %q: %w", c.Value, err)
|
|
}
|
|
return matched, nil
|
|
case "CONTAINS":
|
|
s, ok := actual.(string)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
return strings.Contains(s, c.Value), nil
|
|
case "STARTS WITH":
|
|
s, ok := actual.(string)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
return strings.HasPrefix(s, c.Value), nil
|
|
case ">", "<", ">=", "<=":
|
|
return compareNumeric(actual, c.Value, c.Operator)
|
|
default:
|
|
return false, fmt.Errorf("unsupported operator: %s", c.Operator)
|
|
}
|
|
}
|
|
|
|
func compareNumeric(actual any, expected, op string) (bool, error) {
|
|
expectedNum, err := strconv.ParseFloat(expected, 64)
|
|
if err != nil {
|
|
return false, fmt.Errorf("invalid numeric value %q: %w", expected, err)
|
|
}
|
|
var actualNum float64
|
|
switch v := actual.(type) {
|
|
case int:
|
|
actualNum = float64(v)
|
|
case int64:
|
|
actualNum = float64(v)
|
|
case float64:
|
|
actualNum = v
|
|
case string:
|
|
n, parseErr := strconv.ParseFloat(v, 64)
|
|
if parseErr != nil {
|
|
return false, fmt.Errorf("cannot compare %q as number: %w", v, parseErr)
|
|
}
|
|
actualNum = n
|
|
default:
|
|
return false, nil
|
|
}
|
|
|
|
switch op {
|
|
case ">":
|
|
return actualNum > expectedNum, nil
|
|
case "<":
|
|
return actualNum < expectedNum, nil
|
|
case ">=":
|
|
return actualNum >= expectedNum, nil
|
|
case "<=":
|
|
return actualNum <= expectedNum, nil
|
|
default:
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
func getNodeProperty(n *store.Node, prop string) any {
|
|
switch prop {
|
|
case "name":
|
|
return n.Name
|
|
case "qualified_name":
|
|
return n.QualifiedName
|
|
case "label":
|
|
return n.Label
|
|
case "file_path":
|
|
return n.FilePath
|
|
case "start_line":
|
|
return n.StartLine
|
|
case "end_line":
|
|
return n.EndLine
|
|
case "id":
|
|
return n.ID
|
|
case "project":
|
|
return n.Project
|
|
default:
|
|
if n.Properties != nil {
|
|
if v, ok := n.Properties[prop]; ok {
|
|
return v
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// getEdgeProperty returns a property value from an edge.
|
|
func getEdgeProperty(edge *store.Edge, prop string) any {
|
|
switch prop {
|
|
case "type":
|
|
return edge.Type
|
|
case "id":
|
|
return edge.ID
|
|
case "source_id":
|
|
return edge.SourceID
|
|
case "target_id":
|
|
return edge.TargetID
|
|
default:
|
|
if edge.Properties != nil {
|
|
if v, ok := edge.Properties[prop]; ok {
|
|
return v
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (e *Executor) projectResults(bindings []binding, ret *ReturnClause) (*Result, error) {
|
|
if ret == nil {
|
|
return e.defaultProjection(bindings)
|
|
}
|
|
|
|
// Check if we have a COUNT aggregation
|
|
hasCount := false
|
|
for _, item := range ret.Items {
|
|
if item.Func == "COUNT" {
|
|
hasCount = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if hasCount {
|
|
return e.aggregateResults(bindings, ret)
|
|
}
|
|
|
|
return e.simpleProjection(bindings, ret)
|
|
}
|
|
|
|
func (e *Executor) defaultProjection(bindings []binding) (*Result, error) {
|
|
if len(bindings) == 0 {
|
|
return &Result{Columns: []string{}, Rows: []map[string]any{}}, nil
|
|
}
|
|
|
|
// Collect all variable names from nodes and edges
|
|
varSet := make(map[string]bool)
|
|
edgeVarSet := make(map[string]bool)
|
|
for _, b := range bindings {
|
|
for k := range b.nodes {
|
|
varSet[k] = true
|
|
}
|
|
for k := range b.edges {
|
|
edgeVarSet[k] = true
|
|
}
|
|
}
|
|
cols := make([]string, 0, len(varSet)*3+len(edgeVarSet))
|
|
for k := range varSet {
|
|
cols = append(cols, k+".name", k+".qualified_name", k+".label")
|
|
}
|
|
for k := range edgeVarSet {
|
|
cols = append(cols, k+".type")
|
|
}
|
|
sort.Strings(cols)
|
|
|
|
rows := make([]map[string]any, 0, len(bindings))
|
|
for _, b := range bindings {
|
|
row := make(map[string]any)
|
|
for varName, node := range b.nodes {
|
|
row[varName+".name"] = node.Name
|
|
row[varName+".qualified_name"] = node.QualifiedName
|
|
row[varName+".label"] = node.Label
|
|
}
|
|
for varName, edge := range b.edges {
|
|
row[varName+".type"] = edge.Type
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
|
|
if len(rows) > maxResultRows {
|
|
rows = rows[:maxResultRows]
|
|
}
|
|
|
|
return &Result{Columns: cols, Rows: rows}, nil
|
|
}
|
|
|
|
func (e *Executor) simpleProjection(bindings []binding, ret *ReturnClause) (*Result, error) {
|
|
cols := buildColumnNames(ret.Items)
|
|
|
|
seen := make(map[string]bool)
|
|
rows := make([]map[string]any, 0, len(bindings))
|
|
for _, b := range bindings {
|
|
row := buildProjectionRow(b, ret.Items, cols)
|
|
|
|
// DISTINCT check
|
|
if ret.Distinct {
|
|
key := fmt.Sprintf("%v", row)
|
|
if seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
}
|
|
|
|
rows = append(rows, row)
|
|
}
|
|
|
|
// ORDER BY
|
|
if ret.OrderBy != "" {
|
|
orderCol := resolveOrderColumn(ret.OrderBy, ret.Items, cols)
|
|
sortRows(rows, orderCol, ret.OrderDir)
|
|
}
|
|
|
|
// LIMIT
|
|
rows = applyLimit(rows, ret.Limit)
|
|
|
|
return &Result{Columns: cols, Rows: rows}, nil
|
|
}
|
|
|
|
// buildColumnNames builds column names from return items.
|
|
func buildColumnNames(items []ReturnItem) []string {
|
|
cols := make([]string, 0, len(items))
|
|
for _, item := range items {
|
|
col := item.Variable
|
|
if item.Property != "" {
|
|
col = item.Variable + "." + item.Property
|
|
}
|
|
if item.Alias != "" {
|
|
col = item.Alias
|
|
}
|
|
cols = append(cols, col)
|
|
}
|
|
return cols
|
|
}
|
|
|
|
// buildProjectionRow builds a single result row from a binding.
|
|
func buildProjectionRow(b binding, items []ReturnItem, cols []string) map[string]any {
|
|
row := make(map[string]any)
|
|
for i, item := range items {
|
|
row[cols[i]] = resolveItemValue(b, item)
|
|
}
|
|
return row
|
|
}
|
|
|
|
// resolveItemValue resolves a return item value from a binding (node or edge).
|
|
func resolveItemValue(b binding, item ReturnItem) any {
|
|
if node, ok := b.nodes[item.Variable]; ok {
|
|
return resolveNodeItemValue(node, item.Property)
|
|
}
|
|
if edge, ok := b.edges[item.Variable]; ok {
|
|
return resolveEdgeItemValue(edge, item.Property)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// resolveNodeItemValue resolves a node return item: full node map or single property.
|
|
func resolveNodeItemValue(node *store.Node, property string) any {
|
|
if property == "" {
|
|
return map[string]any{
|
|
"name": node.Name,
|
|
"qualified_name": node.QualifiedName,
|
|
"label": node.Label,
|
|
"file_path": node.FilePath,
|
|
"start_line": node.StartLine,
|
|
"end_line": node.EndLine,
|
|
}
|
|
}
|
|
return getNodeProperty(node, property)
|
|
}
|
|
|
|
// resolveEdgeItemValue resolves an edge return item: full edge map or single property.
|
|
func resolveEdgeItemValue(edge *store.Edge, property string) any {
|
|
if property == "" {
|
|
return map[string]any{
|
|
"type": edge.Type,
|
|
"source_id": edge.SourceID,
|
|
"target_id": edge.TargetID,
|
|
}
|
|
}
|
|
return getEdgeProperty(edge, property)
|
|
}
|
|
|
|
// resolveOrderColumn maps an ORDER BY field to the actual column name.
|
|
func resolveOrderColumn(orderBy string, items []ReturnItem, cols []string) string {
|
|
for i, item := range items {
|
|
if item.Alias == orderBy {
|
|
return cols[i]
|
|
}
|
|
}
|
|
return orderBy
|
|
}
|
|
|
|
// applyLimit enforces the LIMIT clause on result rows.
|
|
func applyLimit(rows []map[string]any, limit int) []map[string]any {
|
|
if limit <= 0 || limit > maxResultRows {
|
|
limit = maxResultRows
|
|
}
|
|
if len(rows) > limit {
|
|
return rows[:limit]
|
|
}
|
|
return rows
|
|
}
|
|
|
|
func (e *Executor) aggregateResults(bindings []binding, ret *ReturnClause) (*Result, error) {
|
|
groupItems, countItem := splitAggregateItems(ret.Items)
|
|
groups, order := buildGroups(bindings, groupItems)
|
|
|
|
// Build columns
|
|
cols := buildColumnNames(ret.Items)
|
|
|
|
// Build result rows
|
|
countCol := countItem.Alias
|
|
if countCol == "" {
|
|
countCol = "COUNT(" + countItem.Variable + ")"
|
|
}
|
|
|
|
rows := make([]map[string]any, 0, len(order))
|
|
for _, key := range order {
|
|
g := groups[key]
|
|
row := g.row
|
|
row[countCol] = g.count
|
|
rows = append(rows, row)
|
|
}
|
|
|
|
// ORDER BY
|
|
if ret.OrderBy != "" {
|
|
sortRows(rows, ret.OrderBy, ret.OrderDir)
|
|
}
|
|
|
|
// LIMIT
|
|
rows = applyLimit(rows, ret.Limit)
|
|
|
|
return &Result{Columns: cols, Rows: rows}, nil
|
|
}
|
|
|
|
// splitAggregateItems separates return items into group-by items and the COUNT item.
|
|
func splitAggregateItems(items []ReturnItem) (groupItems []ReturnItem, countItem ReturnItem) {
|
|
for _, item := range items {
|
|
if item.Func == "COUNT" {
|
|
countItem = item
|
|
} else {
|
|
groupItems = append(groupItems, item)
|
|
}
|
|
}
|
|
return groupItems, countItem
|
|
}
|
|
|
|
type groupEntry struct {
|
|
key string
|
|
row map[string]any
|
|
count int
|
|
}
|
|
|
|
// buildGroups groups bindings by non-COUNT items and counts occurrences.
|
|
func buildGroups(bindings []binding, groupItems []ReturnItem) (groups map[string]*groupEntry, order []string) {
|
|
groups = make(map[string]*groupEntry)
|
|
|
|
for _, b := range bindings {
|
|
row := make(map[string]any)
|
|
var keyParts []string
|
|
for _, item := range groupItems {
|
|
col := item.Variable
|
|
if item.Property != "" {
|
|
col = item.Variable + "." + item.Property
|
|
}
|
|
if item.Alias != "" {
|
|
col = item.Alias
|
|
}
|
|
val := resolveItemValue(b, item)
|
|
row[col] = val
|
|
keyParts = append(keyParts, fmt.Sprintf("%v", val))
|
|
}
|
|
key := strings.Join(keyParts, "\x00")
|
|
if g, ok := groups[key]; ok {
|
|
g.count++
|
|
} else {
|
|
groups[key] = &groupEntry{key: key, row: row, count: 1}
|
|
order = append(order, key)
|
|
}
|
|
}
|
|
return groups, order
|
|
}
|
|
|
|
// sortRows sorts rows by the given column.
|
|
func sortRows(rows []map[string]any, col, dir string) {
|
|
sort.SliceStable(rows, func(i, j int) bool {
|
|
a, b := rows[i][col], rows[j][col]
|
|
cmp := compareValues(a, b)
|
|
if dir == "DESC" {
|
|
return cmp > 0
|
|
}
|
|
return cmp < 0
|
|
})
|
|
}
|
|
|
|
func compareValues(a, b any) int {
|
|
// Try numeric
|
|
aNum, aOK := toFloat(a)
|
|
bNum, bOK := toFloat(b)
|
|
if aOK && bOK {
|
|
if aNum < bNum {
|
|
return -1
|
|
}
|
|
if aNum > bNum {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
// Fall back to string
|
|
aStr := fmt.Sprintf("%v", a)
|
|
bStr := fmt.Sprintf("%v", b)
|
|
if aStr < bStr {
|
|
return -1
|
|
}
|
|
if aStr > bStr {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func toFloat(v any) (float64, bool) {
|
|
switch n := v.(type) {
|
|
case int:
|
|
return float64(n), true
|
|
case int64:
|
|
return float64(n), true
|
|
case float64:
|
|
return n, true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
|
|
// copyBinding makes a shallow copy of a binding.
|
|
func copyBinding(b binding) binding {
|
|
c := newBinding()
|
|
for k, v := range b.nodes {
|
|
c.nodes[k] = v
|
|
}
|
|
for k, v := range b.edges {
|
|
c.edges[k] = v
|
|
}
|
|
return c
|
|
}
|
|
|
|
// filterNodesByProps filters nodes by inline property key-value pairs.
|
|
func filterNodesByProps(nodes []*store.Node, props map[string]string) []*store.Node {
|
|
var filtered []*store.Node
|
|
for _, n := range nodes {
|
|
if nodeMatchesProps(n, props) {
|
|
filtered = append(filtered, n)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// nodeMatchesProps checks if a node matches all given property filters.
|
|
func nodeMatchesProps(n *store.Node, props map[string]string) bool {
|
|
for key, val := range props {
|
|
actual := getNodeProperty(n, key)
|
|
if fmt.Sprintf("%v", actual) != val {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|