ExtractConditionGroups parses a query string using the lenient parser and returns groups of conditions. Conditions with OpNone (from incomplete matchers) are filtered out. Returns nil if the query is empty or parsing fails completely. Returns nil, ErrMaxConditionGroupsPerTagQueryReached if the numb
(query string, maxGroups int)
| 22 | // Returns nil if the query is empty or parsing fails completely. |
| 23 | // Returns nil, ErrMaxConditionGroupsPerTagQueryReached if the number of groups exceeds maxGroups. |
| 24 | func ExtractConditionGroups(query string, maxGroups int) ([][]Condition, error) { |
| 25 | if maxGroups <= 0 { |
| 26 | maxGroups = DefaultMaxConditionGroupsPerTagQuery |
| 27 | } |
| 28 | query = strings.TrimSpace(query) |
| 29 | if len(query) == 0 { |
| 30 | return nil, nil |
| 31 | } |
| 32 | |
| 33 | expr, err := ParseLenient(query) |
| 34 | if err != nil { |
| 35 | return nil, nil |
| 36 | } |
| 37 | if err := expr.validate(); err != nil { |
| 38 | return nil, nil |
| 39 | } |
| 40 | |
| 41 | // Find the first SpansetFilter in the pipeline. |
| 42 | // Returns nil for structural operators (SpansetOperation) indicating multiple spansets. |
| 43 | filter := findSpansetFilter(expr.Pipeline) |
| 44 | if filter == nil { |
| 45 | return nil, nil |
| 46 | } |
| 47 | |
| 48 | groups, reachedMaxGroupsInSplitConditions := splitReqConditions(filter.Expression, maxGroups) |
| 49 | for i := range groups { |
| 50 | // Filter out OpNone conditions — these are column-fetch hints (bare attributes, |
| 51 | // structural intrinsics), not filterable conditions. |
| 52 | conditions := make([]Condition, 0, len(groups[i])) |
| 53 | for _, cond := range groups[i] { |
| 54 | if cond.Op != OpNone { |
| 55 | conditions = append(conditions, cond) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // if even one group has zero conditions after filtering, treat the whole query as empty (e.g. `{.attr || .foo}`) |
| 60 | if len(conditions) == 0 { |
| 61 | return nil, nil |
| 62 | } |
| 63 | groups[i] = conditions |
| 64 | } |
| 65 | |
| 66 | if reachedMaxGroupsInSplitConditions { |
| 67 | return nil, fmt.Errorf("%w (limit: %d). Reduce the number of OR conditions in the query", ErrMaxConditionGroupsPerTagQueryReached, maxGroups) |
| 68 | } |
| 69 | |
| 70 | return groups, nil |
| 71 | } |
| 72 | |
| 73 | // IsEmptyQuery returns true if the query is empty or a match-all (e.g. "{}", "{ }", "{ true }"). |
| 74 | func IsEmptyQuery(query string) bool { |