ClickHouse/docs/en/operations/utilities/clickhouse-format.md

99 lines
2.1 KiB
Markdown
Raw Normal View History

---
toc_priority: 65
toc_title: clickhouse-format
---
# clickhouse-format {#clickhouse-format}
Allows formatting input queries.
Keys:
- `--help` or`-h` — Produce help message.
- `--hilite` — Add syntax highlight with ANSI terminal escape sequences.
- `--oneline` — Format in single line.
- `--quiet` or `-q` — Just check syntax, no output on success.
- `--multiquery` or `-n` — Allow multiple queries in the same file.
- `--obfuscate` — Obfuscate instead of formatting.
- `--seed <string>` — Seed arbitrary string that determines the result of obfuscation.
- `--backslash` — Add a backslash at the end of each line of the formatted query. Can be useful when you copy a query from web or somewhere else with multiple lines, and want to execute it in command line.
## Examples {#examples}
1. Highlighting and single line:
2021-04-25 07:21:46 +00:00
```bash
2021-04-25 08:47:05 +00:00
$ clickhouse-format --oneline --hilite <<< "SELECT sum(number) FROM numbers(5);"
2021-04-25 07:21:46 +00:00
```
2021-04-25 07:21:46 +00:00
Result:
2021-04-25 07:21:46 +00:00
```sql
SELECT sum(number) FROM numbers(5)
```
2. Multiqueries:
2021-04-25 07:21:46 +00:00
```bash
2021-04-25 08:47:05 +00:00
$ clickhouse-format -n <<< "SELECT * FROM (SELECT 1 AS x UNION ALL SELECT 1 UNION DISTINCT SELECT 3);"
2021-04-25 07:21:46 +00:00
```
2021-04-25 07:21:46 +00:00
Result:
2021-04-25 07:21:46 +00:00
```text
2021-05-14 16:22:24 +00:00
SELECT *
FROM
2021-04-25 07:21:46 +00:00
(
2021-05-14 16:22:24 +00:00
SELECT 1 AS x
UNION ALL
SELECT 1
2021-04-25 07:21:46 +00:00
UNION DISTINCT
SELECT 3
)
;
```
3. Obfuscating:
2021-04-25 07:21:46 +00:00
```bash
2021-04-25 08:47:05 +00:00
$ clickhouse-format --seed Hello --obfuscate <<< "SELECT cost_first_screen BETWEEN a AND b, CASE WHEN x >= 123 THEN y ELSE NULL END;"
2021-04-25 07:21:46 +00:00
```
2021-04-25 07:09:43 +00:00
2021-04-25 07:21:46 +00:00
Result:
2021-04-25 07:21:46 +00:00
```text
SELECT treasury_mammoth_hazelnut BETWEEN nutmeg AND span, CASE WHEN chive >= 116 THEN switching ELSE ANYTHING END;
```
Same query and another seed string:
2021-04-25 07:21:46 +00:00
```bash
$ clickhouse-format --seed World --obfuscate <<< "SELECT cost_first_screen BETWEEN a AND b, CASE WHEN x >= 123 THEN y ELSE NULL END;"
```
2021-04-25 07:09:43 +00:00
2021-04-25 07:21:46 +00:00
Result:
2021-04-25 07:09:43 +00:00
2021-04-25 07:21:46 +00:00
```text
SELECT horse_tape_summer BETWEEN folklore AND moccasins, CASE WHEN intestine >= 116 THEN nonconformist ELSE FORESTRY END;
```
4. Adding backslash:
2021-04-25 07:21:46 +00:00
```bash
2021-04-25 08:47:05 +00:00
$ clickhouse-format --backslash <<< "SELECT * FROM (SELECT 1 AS x UNION ALL SELECT 1 UNION DISTINCT SELECT 3);"
2021-04-25 07:21:46 +00:00
```
Result:
```text
SELECT * \
FROM \
( \
SELECT 1 AS x \
UNION ALL \
SELECT 1 \
UNION DISTINCT \
SELECT 3 \
)
```