Skip to content

Commit

Permalink
Better clickhouse-format: support VALUES, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vdimir committed Dec 27, 2023
1 parent bd43660 commit 440b7bf
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 41 deletions.
2 changes: 2 additions & 0 deletions docs/en/operations/utilities/clickhouse-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Keys:
- `--query` — Format queries of any length and complexity.
- `--hilite` — Add syntax highlight with ANSI terminal escape sequences.
- `--oneline` — Format in single line.
- `--max_line_length` — Format in single line queries with length less than specified.
- `--comments` — Keep comments in the output.
- `--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.
Expand Down
147 changes: 106 additions & 41 deletions programs/format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,49 @@
#include <Formats/FormatFactory.h>
#include <Formats/registerFormats.h>

#include <Processors/Transforms/getSourceFromASTInsertQuery.h>
#include <IO/copyData.h>

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wmissing-declarations"

extern const char * auto_time_zones[];

namespace DB::ErrorCodes
{
extern const int NOT_IMPLEMENTED;
}

namespace DB
namespace
{
namespace ErrorCodes

void skipSpacesAndComments(const char*& pos, const char* end, bool print_comments)
{
extern const int INVALID_FORMAT_INSERT_QUERY_WITH_DATA;
do
{
/// skip spaces to avoid throw exception after last query
while (pos != end && std::isspace(*pos))
++pos;

const char * comment_begin = pos;
/// for skip comment after the last query and to not throw exception
if (end - pos > 2 && *pos == '-' && *(pos + 1) == '-')
{
pos += 2;
/// skip until the end of the line
while (pos != end && *pos != '\n')
++pos;
if (print_comments)
std::cout << std::string_view(comment_begin, pos - comment_begin) << "\n";
}
/// need to parse next sql
else
break;
} while (pos != end);
}

}

#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wmissing-declarations"

extern const char * auto_time_zones[];

int mainEntryClickHouseFormat(int argc, char ** argv)
{
using namespace DB;
Expand All @@ -54,8 +82,10 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
desc.add_options()
("query", po::value<std::string>(), "query to format")
("help,h", "produce help message")
("comments", "keep comments in the output")
("hilite", "add syntax highlight with ANSI terminal escape sequences")
("oneline", "format in single line")
("max_line_length", po::value<size_t>()->default_value(0), "format in single line queries with length less than specified")
("quiet,q", "just check syntax, no output on success")
("multiquery,n", "allow multiple queries in the same file")
("obfuscate", "obfuscate instead of formatting")
Expand Down Expand Up @@ -87,6 +117,8 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
bool oneline = options.count("oneline");
bool quiet = options.count("quiet");
bool multiple = options.count("multiquery");
bool print_comments = options.count("comments");
size_t max_line_length = options["max_line_length"].as<size_t>();
bool obfuscate = options.count("obfuscate");
bool backslash = options.count("backslash");
bool allow_settings_after_format_in_insert = options.count("allow_settings_after_format_in_insert");
Expand All @@ -103,6 +135,19 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
return 2;
}

if (oneline && max_line_length)
{
std::cerr << "Options 'oneline' and 'max_line_length' are mutually exclusive." << std::endl;
return 2;
}

if (max_line_length > 255)
{
std::cerr << "Option 'max_line_length' must be less than 256." << std::endl;
return 2;
}


String query;

if (options.count("query"))
Expand All @@ -123,7 +168,6 @@ int mainEntryClickHouseFormat(int argc, char ** argv)

if (options.count("seed"))
{
std::string seed;
hash_func.update(options["seed"].as<std::string>());
}

Expand Down Expand Up @@ -177,30 +221,68 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
{
const char * pos = query.data();
const char * end = pos + query.size();
skipSpacesAndComments(pos, end, print_comments);

ParserQuery parser(end, allow_settings_after_format_in_insert);
do
while (pos != end)
{
size_t approx_query_length = multiple ? find_first_symbols<';'>(pos, end) - pos : end - pos;

ASTPtr res = parseQueryAndMovePosition(
parser, pos, end, "query", multiple, cmd_settings.max_query_size, cmd_settings.max_parser_depth);

/// For insert query with data(INSERT INTO ... VALUES ...), that will lead to the formatting failure,
/// we should throw an exception early, and make exception message more readable.
if (const auto * insert_query = res->as<ASTInsertQuery>(); insert_query && insert_query->data)
std::unique_ptr<ReadBuffer> insert_query_payload = nullptr;
/// If the query is INSERT ... VALUES, then we will try to parse the data.
if (auto * insert_query = res->as<ASTInsertQuery>(); insert_query && insert_query->data)
{
throw Exception(DB::ErrorCodes::INVALID_FORMAT_INSERT_QUERY_WITH_DATA,
"Can't format ASTInsertQuery with data, since data will be lost");
if ("Values" != insert_query->format)
throw Exception(DB::ErrorCodes::NOT_IMPLEMENTED,
"Can't format INSERT query with data format '{}'", insert_query->format);

/// We assume that data ends with a newline character (same as client does)
const char * this_query_end = find_first_symbols<'\n'>(insert_query->data, end);
insert_query->end = this_query_end;
pos = this_query_end;
insert_query_payload = getReadBufferFromASTInsertQuery(res);
}

if (!quiet)
{
if (!backslash)
{
WriteBufferFromOStream res_buf(std::cout, 4096);
formatAST(*res, res_buf, hilite, oneline);
res_buf.finalize();
if (multiple)
std::cout << "\n;\n";
WriteBufferFromOwnString str_buf;
formatAST(*res, str_buf, hilite, oneline || approx_query_length < max_line_length);

if (insert_query_payload)
{
str_buf.write(' ');
copyData(*insert_query_payload, str_buf);
if (multiple)
str_buf.write('\n');
}

String res_string = str_buf.str();
const char * s_pos = res_string.data();
const char * s_end = s_pos + res_string.size();
WriteBufferFromOStream res_cout(std::cout, 4096);
/// For multiline queries we print ';' at new line,
/// but for single line queries we print ';' at the same line
bool has_multiple_lines = false;
while (s_pos != s_end)
{
if (*s_pos == '\n')
has_multiple_lines = true;
res_cout.write(*s_pos++);
}
res_cout.finalize();

if (multiple && !insert_query_payload)
{
if (oneline || !has_multiple_lines)
std::cout << ";\n";
else
std::cout << "\n;\n";
}
std::cout << std::endl;
}
/// add additional '\' at the end of each line;
Expand Down Expand Up @@ -228,27 +310,10 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
std::cout << std::endl;
}
}

do
{
/// skip spaces to avoid throw exception after last query
while (pos != end && std::isspace(*pos))
++pos;

/// for skip comment after the last query and to not throw exception
if (end - pos > 2 && *pos == '-' && *(pos + 1) == '-')
{
pos += 2;
/// skip until the end of the line
while (pos != end && *pos != '\n')
++pos;
}
/// need to parse next sql
else
break;
} while (pos != end);

} while (multiple && pos != end);
skipSpacesAndComments(pos, end, print_comments);
if (!multiple)
break;
}
}
}
catch (...)
Expand Down
139 changes: 139 additions & 0 deletions tests/queries/0_stateless/02946_format_values.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td')
======================================
SELECT a
FROM table1
;

INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');

SELECT b
FROM table1
;

======================================
-- begin
SELECT a
FROM table1
;

-- some insert query
INSERT INTO table1 FORMAT Values (1, [1,3], 'fd'), (2, [2,4], 'sd'), (3, [3,5], 'td');

-- more comments
-- in a row
SELECT b
FROM table1
;

-- end
======================================
SELECT b FROM table1;

SELECT b, c FROM table1;

SELECT
b,
c,
d
FROM table1
;

SELECT
b,
c,
d,
e
FROM table1
;

SELECT
b,
c,
d,
e,
f
FROM table1
;

SELECT
b,
c
FROM
(
SELECT
b,
c
FROM table1
)
;

SELECT
b,
c,
d,
e,
f
FROM
(
SELECT
b,
c,
d,
e,
f
FROM table1
)
;

======================================
SELECT b FROM table1;

SELECT b, c FROM table1;

SELECT b, c, d FROM table1;

SELECT b, c, d, e FROM table1;

SELECT b, c, d, e, f FROM table1;

SELECT b, c FROM (SELECT b, c FROM table1);

SELECT
b,
c,
d,
e,
f
FROM
(
SELECT
b,
c,
d,
e,
f
FROM table1
)
;

======================================
SELECT
b,
c,
d,
e,
f
FROM
(
SELECT
b,
c,
d,
e,
f
FROM table1
)
SELECT b, c, d, e, f FROM (SELECT b, c, d, e, f FROM table1)
======================================
BAD_ARGUMENTS
BAD_ARGUMENTS

0 comments on commit 440b7bf

Please sign in to comment.