Merge pull request #10852 from dgrr/format/multiple-queries

Added multiple query formatting on clickhouse-format
This commit is contained in:
alexey-milovidov 2020-05-14 01:06:44 +03:00 committed by GitHub
commit b3d8c09bfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 6 deletions

View File

@ -21,6 +21,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
("hilite", "add syntax highlight with ANSI terminal escape sequences")
("oneline", "format in single line")
("quiet,q", "just check syntax, no output on success")
("multiquery,n", "allow multiple queries in the same file")
;
boost::program_options::variables_map options;
@ -38,6 +39,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
bool hilite = options.count("hilite");
bool oneline = options.count("oneline");
bool quiet = options.count("quiet");
bool multiple = options.count("multiquery");
if (quiet && (hilite || oneline))
{
@ -53,13 +55,17 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
const char * end = pos + query.size();
ParserQuery parser(end);
ASTPtr res = parseQuery(parser, pos, end, "query", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
if (!quiet)
do
{
formatAST(*res, std::cout, hilite, oneline);
std::cout << std::endl;
}
ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
if (!quiet)
{
formatAST(*res, std::cout, hilite, oneline);
if (multiple)
std::cout << "\n;\n";
std::cout << std::endl;
}
} while (multiple && pos != end);
}
catch (...)
{

View File

@ -0,0 +1,16 @@
SELECT
a,
b AS x
FROM table AS t
INNER JOIN table2 AS t2 ON t.id = t2.t_id
WHERE 1 = 1
;
SELECT
a,
b AS x,
if(x = 0, a, b)
FROM table2 AS t
WHERE t.id != 0
;

View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $CURDIR/../shell_config.sh
set -e
format="$CLICKHOUSE_FORMAT -n"
$format <<EOF
SELECT a, b AS x FROM table AS t
JOIN table2 AS t2 ON (t.id = t2.t_id)
WHERE 1 = 1
;
SELECT a, b AS x,
x == 0 ? a : b
FROM table2 AS t WHERE t.id != 0
EOF