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 f024e39 commit 4311e0b
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 38 deletions.
138 changes: 100 additions & 38 deletions programs/format/Format.cpp
Expand Up @@ -28,18 +28,43 @@
#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"

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"

int mainEntryClickHouseFormat(int argc, char ** argv)
{
using namespace DB;
Expand All @@ -50,8 +75,10 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
desc.add_options()
("query", po::value<std::string>(), "query to format")
("help,h", "produce help message")
("comments", "leave 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 @@ -83,6 +110,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 @@ -99,6 +128,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 @@ -119,7 +161,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 @@ -157,30 +198,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 @@ -208,27 +287,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
@@ -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 4311e0b

Please sign in to comment.