From ba16896162b0b91729a19c386d61b10a6073d073 Mon Sep 17 00:00:00 2001 From: feng lv Date: Sat, 6 Mar 2021 10:51:39 +0000 Subject: [PATCH] add --backslash option for clickhouse-format fix fix fix --- programs/format/Format.cpp | 43 ++++++++++++++++--- ...1754_clickhouse_format_backslash.reference | 16 +++++++ .../01754_clickhouse_format_backslash.sh | 9 ++++ 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 tests/queries/0_stateless/01754_clickhouse_format_backslash.reference create mode 100755 tests/queries/0_stateless/01754_clickhouse_format_backslash.sh diff --git a/programs/format/Format.cpp b/programs/format/Format.cpp index 86a85d1d4a5..ac6352c4033 100644 --- a/programs/format/Format.cpp +++ b/programs/format/Format.cpp @@ -1,6 +1,6 @@ +#include #include #include -#include #include #include @@ -40,6 +40,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv) ("quiet,q", "just check syntax, no output on success") ("multiquery,n", "allow multiple queries in the same file") ("obfuscate", "obfuscate instead of formatting") + ("backslash", "add a backslash at the end of each line of the formatted query") ("seed", po::value(), "seed (arbitrary string) that determines the result of obfuscation") ; @@ -60,6 +61,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv) bool quiet = options.count("quiet"); bool multiple = options.count("multiquery"); bool obfuscate = options.count("obfuscate"); + bool backslash = options.count("backslash"); if (quiet && (hilite || oneline || obfuscate)) { @@ -130,12 +132,39 @@ int mainEntryClickHouseFormat(int argc, char ** argv) ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH); if (!quiet) { - WriteBufferFromOStream res_buf(std::cout, 4096); - formatAST(*res, res_buf, hilite, oneline); - res_buf.next(); - if (multiple) - std::cout << "\n;\n"; - std::cout << std::endl; + if (!backslash) + { + WriteBufferFromOStream res_buf(std::cout, 4096); + formatAST(*res, res_buf, hilite, oneline); + res_buf.next(); + if (multiple) + std::cout << "\n;\n"; + std::cout << std::endl; + } + /// add additional '\' at the end of each line; + else + { + WriteBufferFromOwnString str_buf; + formatAST(*res, str_buf, hilite, oneline); + + auto res_string = str_buf.str(); + WriteBufferFromOStream res_cout(std::cout, 4096); + + const char * s_pos= res_string.data(); + const char * s_end = s_pos + res_string.size(); + + while (s_pos != s_end) + { + if (*s_pos == '\n') + res_cout.write(" \\", 2); + res_cout.write(*s_pos++); + } + + res_cout.next(); + if (multiple) + std::cout << " \\\n;\n"; + std::cout << std::endl; + } } } while (multiple && pos != end); } diff --git a/tests/queries/0_stateless/01754_clickhouse_format_backslash.reference b/tests/queries/0_stateless/01754_clickhouse_format_backslash.reference new file mode 100644 index 00000000000..328483d9867 --- /dev/null +++ b/tests/queries/0_stateless/01754_clickhouse_format_backslash.reference @@ -0,0 +1,16 @@ +SELECT * \ +FROM \ +( \ + SELECT 1 AS x \ + UNION ALL \ + SELECT 1 \ + UNION DISTINCT \ + SELECT 3 \ +) +SELECT 1 \ +UNION ALL \ +( \ + SELECT 1 \ + UNION DISTINCT \ + SELECT 1 \ +) diff --git a/tests/queries/0_stateless/01754_clickhouse_format_backslash.sh b/tests/queries/0_stateless/01754_clickhouse_format_backslash.sh new file mode 100755 index 00000000000..6a76dc9c5c8 --- /dev/null +++ b/tests/queries/0_stateless/01754_clickhouse_format_backslash.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +echo "select * from (select 1 as x union all select 1 union distinct select 3)" | $CLICKHOUSE_FORMAT --backslash; + +echo "select 1 union all (select 1 union distinct select 1)" | $CLICKHOUSE_FORMAT --backslash;