MCPcopy
hub / github.com/grafana/tempo / splitReqConditions

Function splitReqConditions

pkg/traceql/lenient_extract.go:133–188  ·  view source on GitHub ↗

splitReqConditions converts a field expression into condition groups by expanding OR clauses into separate groups. Each group represents one AND-connected set of conditions that must all be satisfied together. Example: { (.a="1" || .b="2") && .c="3" } produces two groups: - [.a="1", .c="3"] - [.b="

(expr FieldExpression, maxGroups int)

Source from the content-addressed store, hash-verified

131// - [.a="1", .c="3"]
132// - [.b="2", .c="3"]
133func splitReqConditions(expr FieldExpression, maxGroups int) ([][]Condition, bool) {
134 var reachedMaxGroups bool
135 ops := flattenExprToOperations(expr, OpNone)
136
137 totalGroups := 1
138 for _, op := range ops {
139 if op.opType == OpOr {
140 totalGroups *= len(op.conditions)
141 if totalGroups > maxGroups {
142 totalGroups = maxGroups
143 break
144 }
145 }
146 }
147
148 conditionGroups := make([][]Condition, totalGroups)
149 repeats := 1
150 for _, op := range ops {
151 opCondIdx := 0
152 repeated := 0
153 for i := 0; i < totalGroups; i++ {
154 if op.opType == OpOr {
155 conditionGroups[i] = append(conditionGroups[i], op.conditions[opCondIdx]...)
156 repeated++
157 if repeated == repeats {
158 repeated = 0
159 opCondIdx++
160 }
161 if opCondIdx >= len(op.conditions) {
162 opCondIdx = 0
163 }
164 } else {
165 for _, conds := range op.conditions {
166 conditionGroups[i] = append(conditionGroups[i], conds...)
167 }
168 }
169 }
170 if op.opType == OpOr {
171 repeats *= len(op.conditions)
172 if repeats > maxGroups {
173 repeats = maxGroups
174 reachedMaxGroups = true
175 }
176 }
177 }
178
179 // Drop empty groups (e.g. from an empty filter `{}`).
180 result := conditionGroups[:0]
181 for _, g := range conditionGroups {
182 if len(g) > 0 {
183 result = append(result, g)
184 }
185 }
186
187 return result, reachedMaxGroups
188}
189
190// deduplicateConditionBranches removes duplicate branches from an OR group.

Callers 1

ExtractConditionGroupsFunction · 0.85

Calls 1

flattenExprToOperationsFunction · 0.85

Tested by

no test coverage detected