Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zinic committed Mar 4, 2025
1 parent 828a1e5 commit 3e1a04a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 79 deletions.
11 changes: 0 additions & 11 deletions packages/go/cypher/models/pgsql/pgtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ const (
)

var (
Columns = []Identifier{
ColumnID,
ColumnPath,
ColumnProperties,
ColumnKindIDs,
ColumnKindID,
ColumnGraphID,
ColumnStartID,
ColumnEndID,
}

NodeTableColumns = []Identifier{
ColumnID,
ColumnKindIDs,
Expand Down
16 changes: 3 additions & 13 deletions packages/go/cypher/models/pgsql/translate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ type Expansion struct {

type PatternSegment struct {
Frame *Frame
PreviousFrame *Frame
Direction graph.Direction
Expansion models.Optional[Expansion]
LeftNode *BoundIdentifier
Expand Down Expand Up @@ -209,7 +208,7 @@ func (s *Query) PreparePart(numReadingClauses, numUpdatingClauses int, allocateF
}

if allocateFrame {
if frame, err := s.Scope.PushContainerFrame(); err != nil {
if frame, err := s.Scope.PushFrame(); err != nil {
return err
} else {
newPart.Frame = frame
Expand Down Expand Up @@ -243,10 +242,6 @@ type QueryPart struct {
fromClauses []pgsql.FromClause
}

func (s *QueryPart) CurrentPattern() *Pattern {
return s.currentPattern
}

func (s *QueryPart) AddFromClause(clause pgsql.FromClause) {
s.fromClauses = append(s.fromClauses, clause)
}
Expand Down Expand Up @@ -535,7 +530,8 @@ func extractIdentifierFromCypherExpression(expression cypher.Expression) (pgsql.
}
}

// TODO: Better name
// Symbols is a symbol table that has some generic functions for negotiating unique symbols from identifiers,
// compound identifiers and other PgSQL AST elements.
type Symbols struct {
table map[string]any
}
Expand All @@ -562,12 +558,6 @@ func SymbolsFor(node pgsql.SyntaxNode) (*Symbols, error) {
}))
}

func (s *Symbols) Filter() {
for _, filtered := range pgsql.Columns {
delete(s.table, filtered.String())
}
}

func (s *Symbols) IsEmpty() bool {
return len(s.table) == 0
}
Expand Down
69 changes: 14 additions & 55 deletions packages/go/cypher/models/pgsql/translate/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/specterops/bloodhound/cypher/models/pgsql"
)

// IdentifierGenerator is a map that creates a unique identifier for each call with a given
// data type. This ensures that renamed identifiers in queries do not conflict with each other.
type IdentifierGenerator map[pgsql.DataType]int

func (s IdentifierGenerator) NewIdentifier(dataType pgsql.DataType) (pgsql.Identifier, error) {
Expand Down Expand Up @@ -82,14 +84,13 @@ func previousValidFrame(query *Query, partFrame *Frame) (*Frame, bool) {

// Frame represents a snapshot of all identifiers defined and visible in a given scope
type Frame struct {
id int
Previous *Frame
IsContainerFrame bool
Binding *BoundIdentifier
Visible *pgsql.IdentifierSet
stashedVisible *pgsql.IdentifierSet
Exported *pgsql.IdentifierSet
stashedExported *pgsql.IdentifierSet
id int
Previous *Frame
Binding *BoundIdentifier
Visible *pgsql.IdentifierSet
stashedVisible *pgsql.IdentifierSet
Exported *pgsql.IdentifierSet
stashedExported *pgsql.IdentifierSet
}

func (s *Frame) RestoreStashed() {
Expand Down Expand Up @@ -151,38 +152,6 @@ func NewScope() *Scope {
}
}

func (s *Scope) LastProjectedFrameFrom(from *Frame) *Frame {
// Declare idx here to track where this walk is in the stack
idx := len(s.stack) - 1

// Search backwards for the target frame
for ; idx >= 0; idx-- {
if nextFrame := s.stack[idx]; nextFrame == from {
// Don't revisit this frame
idx -= 1
break
}
}

// Unable to find the frame passed in
if idx < 0 {
return nil
}

// Unwind further if the nearest frame is a multipart container and take the most
// recent ancestor
if s.stack[idx].IsContainerFrame {
idx -= 1
}

// Unable to find a multipart container frame with an ancestor
if idx <= 0 {
return nil
}

return s.stack[idx-1]
}

func (s *Scope) PruneDefinitions(protectedIdentifiers *pgsql.IdentifierSet) error {
var (
prunedAliases = make(map[pgsql.Identifier]pgsql.Identifier, len(s.aliases))
Expand Down Expand Up @@ -289,23 +258,13 @@ func (s *Scope) UnwindToFrame(frame *Frame) error {
return nil
}

func (s *Scope) PushContainerFrame() (*Frame, error) {
if frame, err := s.PushFrame(); err != nil {
return nil, err
} else {
frame.IsContainerFrame = true
return frame, nil
}
}

func (s *Scope) PushFrame() (*Frame, error) {
newFrame := &Frame{
id: s.nextFrameID,
IsContainerFrame: false,
Visible: pgsql.NewIdentifierSet(),
stashedVisible: pgsql.NewIdentifierSet(),
Exported: pgsql.NewIdentifierSet(),
stashedExported: pgsql.NewIdentifierSet(),
id: s.nextFrameID,
Visible: pgsql.NewIdentifierSet(),
stashedVisible: pgsql.NewIdentifierSet(),
Exported: pgsql.NewIdentifierSet(),
stashedExported: pgsql.NewIdentifierSet(),
}

s.nextFrameID += 1
Expand Down

0 comments on commit 3e1a04a

Please sign in to comment.