coalesceConditions reduces the amount of data pulled from the backend when the same column is used in multiple conditions todo: There is a lot that can be done with this method. for example we can coalesce conditions if they have equivalent operands. consider a query like: { span.foo >= 1 } && {
(f *traceql.FetchSpansRequest)
| 9 | // the >= condition. it also is currently ignoring situations where allconditions is true, but some of these |
| 10 | // improvements apply to that case as well. |
| 11 | func coalesceConditions(f *traceql.FetchSpansRequest) { |
| 12 | // only do this if all conditions is false for now. it's safer and all conditions queries tend to be quite quick |
| 13 | if !f.AllConditions { |
| 14 | prevLen := 0 |
| 15 | for len(f.Conditions) != prevLen { // check combinations until we can't coalesce any more |
| 16 | prevLen = len(f.Conditions) |
| 17 | |
| 18 | // search for conditions with the same attribute name and consider coalescing them |
| 19 | for i := 0; i < len(f.Conditions); i++ { |
| 20 | for j := i + 1; j < len(f.Conditions); j++ { |
| 21 | if c, ok := coalesce(f.Conditions[i], f.Conditions[j]); ok { |
| 22 | f.Conditions[i] = c |
| 23 | f.Conditions = append(f.Conditions[:j], f.Conditions[j+1:]...) |
| 24 | j-- |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // coalesce takes two conditions and turns them into one. it returns a bool to indicate |
| 33 | // if the returned condition is valid or if it should just continue using the original 2 conditions |