Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] AST pretty-printing #182257

Closed
drewdaemon opened this issue May 1, 2024 · 6 comments
Closed

[ES|QL] AST pretty-printing #182257

drewdaemon opened this issue May 1, 2024 · 6 comments
Assignees
Labels
DX Issues related to Developer Experience enhancement New value added to drive a business result impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. Team:ESQL ES|QL related features in Kibana

Comments

@drewdaemon
Copy link
Contributor

drewdaemon commented May 1, 2024

There are use cases for making programmatic edits to an ES|QL query. For example, adding or changing a where clause when a user filters by a field (ref).

We have code to parse an ES|QL string to an AST (a reader), but no code for the inverse (a generator).

This leaves us no choice but to use string manipulation.

Instead, we should provide a generator so that edits can be made to the AST and then serialized into a new query. This is a more robust and less error-prone method of manipulating queries.

@drewdaemon drewdaemon added enhancement New value added to drive a business result DX Issues related to Developer Experience Team:ESQL ES|QL related features in Kibana labels May 1, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-esql (Team:ESQL)

@stratoula stratoula added the impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. label May 1, 2024
@vadimkibana vadimkibana changed the title [ES|QL] provide AST -> query generator [ES|QL] AST pretty-printing Jul 26, 2024
@vadimkibana
Copy link
Contributor

Depends on #189259

@vadimkibana
Copy link
Contributor

Depends on #182393 (comment)

@vadimkibana
Copy link
Contributor

vadimkibana commented Jul 26, 2024

Optionally depends on #182393 (comment) and #182393 (comment)

@vadimkibana
Copy link
Contributor

Blocked by #189491

@vadimkibana
Copy link
Contributor

Blocked by #189913

vadimkibana added a commit that referenced this issue Aug 6, 2024
## Summary

Partially addresses #182257

Add a helper function, which given an ES|QL AST formats it to a one-line
string.


### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios


### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
vadimkibana added a commit that referenced this issue Aug 20, 2024
## Summary

Partially addresses #182257

- Improves the basic "one-line" printer `BasicPrettyPrinter`, notable
changes:
- It is now possible better specify if query keywords should be
uppercased
- Better formatting columns names, adds backquotes when escaping needed:
`` `name👍` ``
- Wraps cast expressions into brackets, where needed: `(1 + 2)::string`
instead of `1 + 2::string`
- Adds initial implementations of the more complex
`WrappingPrettyPrinter`.
- "Initial implementation" because it probably covers 80-90% of the
cases, some follow up will be needed.
- The `WrappingPrettyPrinter` formats the query like `Prettier`, it
tries to format AST nodes horizontally as lists, but based on various
conditions breaks the lines and indents them.


#### Cases handled by the `WrappingPrettyPrinter`

Below are examples of some of the cases handled by the
`WrappingPrettyPrinter`. (See test files for many more cases.)

##### Short queries

Queries with less than 4 commands and if they do not require wrapping
are formatted to a single line.

Source:

```
FROM index | WHERE a == 123
```

Result:

```
FROM index | WHERE a == 123
```


##### Argument wrapping

Command arguments are wrapped (at wrapping threshold, defaults to 80).

Source:

```
FROM index, another_index, yet_another_index, on-more-index, last_index, very_last_index, ok_this_is_the_last_index
```

Result:

```
FROM index, another_index, yet_another_index, on-more-index, last_index,
     very_last_index, ok_this_is_the_last_index
```


##### Argument breaking

Command argument combinations which result into a single argument
occupying a whole line (due to that argument being long, or because the
surrounding argument combination results into such a case), except the
last argument, results into the argument list being broken by line.

Source:

```
FROM xxxxxxxxxx, yyyyyyyyyyy, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,  // <------------ this one
  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ccccccc, ggggggggg
```

Result:

```
FROM
  xxxxxxxxxx,
  yyyyyyyyyyy,
  zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz,
  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
  ccccccc,
  ggggggggg
```

##### Binary expression chain vertical flattening

Binary expressions of the same precedence are vertically flattened, if
wrapping is required. Same as it is done by `Prettier`, where there is
an indentation after the first line to allow for different precedence
expressions.

###### All expressions have the same precedence

Source:

```
FROM index
| STATS super_function_name(11111111111111.111 + 11111111111111.111 + 11111111111111.111 + 11111111111111.111 + 11111111111111.111))
```

Result:

```
FROM index
  | STATS
      SUPER_FUNCTION_NAME(
        11111111111111.111 +
          11111111111111.111 +
          11111111111111.111 +
          11111111111111.111 +
          11111111111111.111)
```

###### Expressions with `additive` and `multiplicative` precedence mixed

Source:

```
FROM index
| STATS super_function_name(11111111111111.111 + 3333333333333.3333 * 3333333333333.3333 * 3333333333333.3333 * 3333333333333.3333 + 11111111111111.111 + 11111111111111.111 + 11111111111111.111 + 11111111111111.111))
```

Result:

```
FROM index
  | STATS
      SUPER_FUNCTION_NAME(
        11111111111111.111 +
          3333333333333.3335 *
            3333333333333.3335 *
            3333333333333.3335 *
            3333333333333.3335 +
          11111111111111.111 +
          11111111111111.111 +
          11111111111111.111 +
          11111111111111.111)
```


### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Issues related to Developer Experience enhancement New value added to drive a business result impact:medium Addressing this issue will have a medium level of impact on the quality/strength of our product. Team:ESQL ES|QL related features in Kibana
Projects
None yet
Development

No branches or pull requests

4 participants