diff options
| author | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-13 18:33:34 -0400 |
|---|---|---|
| committer | JP Appel <jeanpierre.appel01@gmail.com> | 2025-06-13 18:33:34 -0400 |
| commit | 06d091cc609e90974f8da7e7ae153f3c2a83ee46 (patch) | |
| tree | a619963db899570ff8631383fff94cecd28ab8ac /pkg/query/parser.go | |
| parent | 6b0151257a76b5f2f6aa4bbcdc027fce57cc6170 (diff) | |
Add only child optimization to query parser
Diffstat (limited to 'pkg/query/parser.go')
| -rw-r--r-- | pkg/query/parser.go | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/pkg/query/parser.go b/pkg/query/parser.go index 5de803c..d7f4fdd 100644 --- a/pkg/query/parser.go +++ b/pkg/query/parser.go @@ -53,8 +53,11 @@ type Statement struct { Operator opType Value Valuer } + +type Statements []Statement + type Clause struct { - Statements []Statement + Statements Statements Clauses []*Clause Operator clauseOperator } @@ -101,7 +104,7 @@ func (v StringValue) Compare(other Valuer) int { } type DatetimeValue struct { - d time.Time + D time.Time } func (v DatetimeValue) Type() valuerType { @@ -114,7 +117,7 @@ func (v DatetimeValue) Compare(other Valuer) int { return 0 } - return v.d.Compare(o.d) + return v.D.Compare(o.D) } var _ Valuer = StringValue{} @@ -230,6 +233,16 @@ func (root *Clause) Flatten() { stack = stack[:top] hasMerged := false + + // merge if only child clause + if len(node.Statements) == 0 && len(node.Clauses) == 1 { + child := node.Clauses[0] + + node.Operator = child.Operator + node.Statements = child.Statements + node.Clauses = child.Clauses + } + // cannot be "modernized", node.Clauses is modified in loop for i := 0; i < len(node.Clauses); i++ { child := node.Clauses[i] @@ -293,6 +306,15 @@ func (root Clause) Depth() int { return maxHeight } +// Order of clause tree +func (root Clause) Order() int { + count := 0 + for range root.DFS() { + count++ + } + return count +} + func (root *Clause) DFS() iter.Seq[*Clause] { return func(yield func(*Clause) bool) { stack := make([]*Clause, 0, len(root.Clauses)) |
