mirror of
https://github.com/openobserve/openobserve.git
synced 2026-11-05 16:10:43 +00:00
## What changed Fixed `addFieldTerm` in `SearchBar.vue` so that include/exclude search terms are inserted at the correct position in SQL aggregation queries. Previously the code checked for `ORDER BY` before `GROUP BY`, causing the injected `WHERE` clause to land after `GROUP BY` (invalid SQL). The fix scans all terminating clauses (`GROUP BY`, `HAVING`, `ORDER BY`, `LIMIT`) and inserts `WHERE` before whichever appears earliest. The remaining diff is Prettier-driven reformatting with no logic changes. ## Why The old sequential `if/else if` checked `ORDER BY` first. On a query containing both `GROUP BY` and `ORDER BY`, it would split at `ORDER BY` and produce `… WHERE <filter> ORDER BY … GROUP BY …` — structurally invalid SQL. The fix enforces correct SQL clause ordering (`FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT`). ## Approach The new algorithm iterates over all four terminating clauses, records the index of each that is present, picks the one with the smallest index (`firstClause`), then uses the existing `queryIndexSplit` utility to splice the `WHERE <filter>` token in before it. If no terminating clause exists the filter is appended. The spec file was extended with 14 new unit tests covering the core bug scenario (GROUP BY + ORDER BY together), each individual clause in isolation, all-clauses-present, AND-append paths, and edge cases (case-insensitive matching, column names containing clause keywords, special characters in filter values). ## Risk The logic change is isolated to the `else` branch of `addFieldTerm` that handles queries with no existing `WHERE` clause. Queries that already have a `WHERE` clause follow a separate code path and are unaffected. Reviewers should confirm the `queryIndexSplit` utility is case-insensitive-safe (the fix lowercases only for the index scan, then splits the original string) and verify the preserved trailing `afterClause` string includes the space/newline that was previously part of the `" order by"` / `" group by"` literals.