|
| 1 | +package queryopt |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/authzed/spicedb/pkg/query" |
| 5 | +) |
| 6 | + |
| 7 | +func init() { |
| 8 | + MustRegisterOptimization(Optimizer{ |
| 9 | + Name: "reachability-pruning", |
| 10 | + Description: ` |
| 11 | + Replaces subtrees with NullIteratorType nodes when they can never |
| 12 | + produce the target subject type of the request. |
| 13 | + `, |
| 14 | + NewTransform: func(params RequestParams) OutlineTransform { |
| 15 | + return reachabilityPruning(params) |
| 16 | + }, |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +func reachabilityPruning(params RequestParams) func(outline query.Outline) query.Outline { |
| 21 | + return func(outline query.Outline) query.Outline { |
| 22 | + if params.SubjectType == "" || (params.SubjectRelation != "" && params.SubjectRelation != "...") { |
| 23 | + // do not mutate if subjectType is empty or if subjectRelation is non-empty |
| 24 | + return outline |
| 25 | + } |
| 26 | + // Pre-pass: find all node IDs inside arrow left subtrees. |
| 27 | + // These are intermediate hops whose subject type does not need |
| 28 | + // to match the target, so they must be excluded from pruning. |
| 29 | + immune := collectArrowLeftSubtreeIDs(outline) |
| 30 | + return query.MutateOutline(outline, []query.OutlineMutation{ |
| 31 | + leafSubjectTypePruner(params.SubjectType, immune), |
| 32 | + query.NullPropagation, |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// leafSubjectTypePruner returns an OutlineMutation that replaces leaf outline |
| 38 | +// nodes (DatastoreIteratorType, SelfIteratorType) with NullIteratorType when |
| 39 | +// their subject type does not match the target. Nodes whose IDs appear in the |
| 40 | +// immune set are skipped — these are nodes inside arrow left subtrees whose |
| 41 | +// subject types are intermediate hops, not final outputs. |
| 42 | +// |
| 43 | +// Null propagation through compound nodes (unions, intersections, arrows, etc.) |
| 44 | +// is handled by query.NullPropagation, which should run after this mutation in |
| 45 | +// the same MutateOutline call. |
| 46 | +func leafSubjectTypePruner(targetSubjectType string, immune map[query.OutlineNodeID]bool) query.OutlineMutation { |
| 47 | + return func(outline query.Outline) query.Outline { |
| 48 | + if immune[outline.ID] { |
| 49 | + return outline |
| 50 | + } |
| 51 | + |
| 52 | + switch outline.Type { |
| 53 | + case query.DatastoreIteratorType: |
| 54 | + if outline.Args != nil && outline.Args.Relation != nil && outline.Args.Relation.Type() == targetSubjectType { |
| 55 | + return outline |
| 56 | + } |
| 57 | + return query.Outline{Type: query.NullIteratorType, ID: outline.ID} |
| 58 | + |
| 59 | + case query.SelfIteratorType: |
| 60 | + if outline.Args != nil && outline.Args.DefinitionName == targetSubjectType { |
| 61 | + return outline |
| 62 | + } |
| 63 | + return query.Outline{Type: query.NullIteratorType, ID: outline.ID} |
| 64 | + |
| 65 | + default: |
| 66 | + return outline |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// collectArrowLeftSubtreeIDs walks the outline tree and collects the IDs of all |
| 72 | +// nodes that appear inside the left subtree of an arrow (or intersection arrow). |
| 73 | +// These nodes have intermediate subject types that should not be pruned. |
| 74 | +func collectArrowLeftSubtreeIDs(outline query.Outline) map[query.OutlineNodeID]bool { |
| 75 | + ids := make(map[query.OutlineNodeID]bool) |
| 76 | + _ = query.WalkOutlinePreOrder(outline, func(node query.Outline) error { |
| 77 | + if (node.Type == query.ArrowIteratorType || node.Type == query.IntersectionArrowIteratorType) && len(node.SubOutlines) == 2 { |
| 78 | + markAllIDs(node.SubOutlines[0], ids) |
| 79 | + } |
| 80 | + return nil |
| 81 | + }) |
| 82 | + return ids |
| 83 | +} |
| 84 | + |
| 85 | +// markAllIDs recursively adds the ID of every node in the subtree to the set. |
| 86 | +func markAllIDs(outline query.Outline, ids map[query.OutlineNodeID]bool) { |
| 87 | + ids[outline.ID] = true |
| 88 | + for _, sub := range outline.SubOutlines { |
| 89 | + markAllIDs(sub, ids) |
| 90 | + } |
| 91 | +} |
0 commit comments