2021-03-06 10:51:39 +00:00
|
|
|
#include <functional>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <iostream>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <string_view>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2023-12-27 17:13:56 +00:00
|
|
|
#include <IO/copyData.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
2024-01-09 06:33:48 +00:00
|
|
|
#include <Interpreters/registerInterpreters.h>
|
2021-02-28 10:59:10 +00:00
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <Parsers/ParserQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <Parsers/obfuscateQueries.h>
|
2021-02-28 10:59:10 +00:00
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Common/ErrorCodes.h>
|
2023-12-27 17:13:56 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.h>
|
2019-08-23 15:47:27 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <Core/BaseSettingsProgramOptions.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2023-12-30 03:35:00 +00:00
|
|
|
#include <Databases/registerDatabases.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <Functions/registerFunctions.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/registerAggregateFunctions.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <TableFunctions/registerTableFunctions.h>
|
|
|
|
#include <Storages/StorageFactory.h>
|
|
|
|
#include <Storages/registerStorages.h>
|
2023-12-23 03:55:55 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeSettings.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2021-10-03 02:56:32 +00:00
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <Formats/registerFormats.h>
|
2023-12-27 14:00:52 +00:00
|
|
|
#include <Processors/Transforms/getSourceFromASTInsertQuery.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
|
2023-12-27 14:00:52 +00:00
|
|
|
namespace DB::ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
}
|
2023-12-23 03:55:55 +00:00
|
|
|
|
2023-12-27 14:00:52 +00:00
|
|
|
namespace
|
2021-02-28 10:59:10 +00:00
|
|
|
{
|
2023-12-27 14:00:52 +00:00
|
|
|
|
|
|
|
void skipSpacesAndComments(const char*& pos, const char* end, bool print_comments)
|
2021-02-28 10:59:10 +00:00
|
|
|
{
|
2023-12-27 14:00:52 +00:00
|
|
|
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);
|
2021-02-28 10:59:10 +00:00
|
|
|
}
|
2023-12-27 14:00:52 +00:00
|
|
|
|
2021-02-28 10:59:10 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 12:54:34 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wunused-function"
|
|
|
|
#pragma clang diagnostic ignored "-Wmissing-declarations"
|
2023-12-27 14:00:52 +00:00
|
|
|
|
|
|
|
extern const char * auto_time_zones[];
|
|
|
|
|
2017-11-11 01:04:14 +00:00
|
|
|
int mainEntryClickHouseFormat(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
using namespace DB;
|
|
|
|
|
2022-04-14 14:49:49 +00:00
|
|
|
try
|
2022-02-07 05:34:13 +00:00
|
|
|
{
|
2022-04-14 14:49:49 +00:00
|
|
|
boost::program_options::options_description desc = createOptionsDescription("Allowed options", getTerminalWidth());
|
|
|
|
desc.add_options()
|
|
|
|
("query", po::value<std::string>(), "query to format")
|
|
|
|
("help,h", "produce help message")
|
2023-12-27 14:00:52 +00:00
|
|
|
("comments", "keep comments in the output")
|
2022-04-14 14:49:49 +00:00
|
|
|
("hilite", "add syntax highlight with ANSI terminal escape sequences")
|
|
|
|
("oneline", "format in single line")
|
2023-12-27 14:00:52 +00:00
|
|
|
("max_line_length", po::value<size_t>()->default_value(0), "format in single line queries with length less than specified")
|
2022-04-14 14:49:49 +00:00
|
|
|
("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")
|
|
|
|
("allow_settings_after_format_in_insert", "Allow SETTINGS after FORMAT, but note, that this is not always safe")
|
|
|
|
("seed", po::value<std::string>(), "seed (arbitrary string) that determines the result of obfuscation")
|
|
|
|
;
|
2022-02-06 03:22:05 +00:00
|
|
|
|
2022-04-14 14:49:49 +00:00
|
|
|
Settings cmd_settings;
|
|
|
|
for (const auto & field : cmd_settings.all())
|
|
|
|
{
|
2023-01-27 09:39:10 +00:00
|
|
|
std::string_view name = field.getName();
|
|
|
|
if (name == "max_parser_depth" || name == "max_query_size")
|
2024-03-19 16:04:29 +00:00
|
|
|
addProgramOption(cmd_settings, desc, name, field);
|
2022-04-14 14:49:49 +00:00
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2022-04-14 14:49:49 +00:00
|
|
|
boost::program_options::variables_map options;
|
|
|
|
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), options);
|
|
|
|
po::notify(options);
|
|
|
|
|
|
|
|
if (options.count("help"))
|
|
|
|
{
|
2023-04-20 22:54:34 +00:00
|
|
|
std::cout << "Usage: " << argv[0] << " [options] < query" << std::endl;
|
2022-04-14 14:49:49 +00:00
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
|
|
|
|
bool hilite = options.count("hilite");
|
|
|
|
bool oneline = options.count("oneline");
|
|
|
|
bool quiet = options.count("quiet");
|
2020-05-12 14:56:52 +00:00
|
|
|
bool multiple = options.count("multiquery");
|
2023-12-27 14:00:52 +00:00
|
|
|
bool print_comments = options.count("comments");
|
|
|
|
size_t max_line_length = options["max_line_length"].as<size_t>();
|
2020-09-26 00:38:59 +00:00
|
|
|
bool obfuscate = options.count("obfuscate");
|
2021-03-06 10:51:39 +00:00
|
|
|
bool backslash = options.count("backslash");
|
2022-04-04 08:09:22 +00:00
|
|
|
bool allow_settings_after_format_in_insert = options.count("allow_settings_after_format_in_insert");
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
if (quiet && (hilite || oneline || obfuscate))
|
|
|
|
{
|
|
|
|
std::cerr << "Options 'hilite' or 'oneline' or 'obfuscate' have no sense in 'quiet' mode." << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
if (obfuscate && (hilite || oneline || quiet))
|
2017-11-11 01:04:14 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
std::cerr << "Options 'hilite' or 'oneline' or 'quiet' have no sense in 'obfuscate' mode." << std::endl;
|
2017-11-11 01:04:14 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2023-12-27 14:00:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-11 01:04:14 +00:00
|
|
|
String query;
|
2021-09-24 10:16:46 +00:00
|
|
|
|
|
|
|
if (options.count("query"))
|
|
|
|
{
|
|
|
|
query = options["query"].as<std::string>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReadBufferFromFileDescriptor in(STDIN_FILENO);
|
|
|
|
readStringUntilEOF(query, in);
|
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
if (obfuscate)
|
2020-05-12 16:13:34 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
WordMap obfuscated_words_map;
|
|
|
|
WordSet used_nouns;
|
|
|
|
SipHash hash_func;
|
|
|
|
|
|
|
|
if (options.count("seed"))
|
2020-05-12 14:56:52 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
hash_func.update(options["seed"].as<std::string>());
|
2020-05-12 14:56:52 +00:00
|
|
|
}
|
2020-09-26 00:38:59 +00:00
|
|
|
|
2024-01-09 06:33:48 +00:00
|
|
|
registerInterpreters();
|
2020-09-26 00:38:59 +00:00
|
|
|
registerFunctions();
|
|
|
|
registerAggregateFunctions();
|
|
|
|
registerTableFunctions();
|
2023-12-30 03:35:00 +00:00
|
|
|
registerDatabases();
|
2020-09-26 00:38:59 +00:00
|
|
|
registerStorages();
|
2021-10-03 02:56:32 +00:00
|
|
|
registerFormats();
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
std::unordered_set<std::string> additional_names;
|
|
|
|
|
|
|
|
auto all_known_storage_names = StorageFactory::instance().getAllRegisteredNames();
|
|
|
|
auto all_known_data_type_names = DataTypeFactory::instance().getAllRegisteredNames();
|
2023-12-23 03:55:55 +00:00
|
|
|
auto all_known_settings = Settings().getAllRegisteredNames();
|
|
|
|
auto all_known_merge_tree_settings = MergeTreeSettings().getAllRegisteredNames();
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
additional_names.insert(all_known_storage_names.begin(), all_known_storage_names.end());
|
|
|
|
additional_names.insert(all_known_data_type_names.begin(), all_known_data_type_names.end());
|
2023-12-23 03:55:55 +00:00
|
|
|
additional_names.insert(all_known_settings.begin(), all_known_settings.end());
|
|
|
|
additional_names.insert(all_known_merge_tree_settings.begin(), all_known_merge_tree_settings.end());
|
|
|
|
|
|
|
|
for (auto * it = auto_time_zones; *it; ++it)
|
|
|
|
{
|
|
|
|
String time_zone_name = *it;
|
|
|
|
|
|
|
|
/// Example: Europe/Amsterdam
|
|
|
|
Strings split;
|
|
|
|
boost::split(split, time_zone_name, [](char c){ return c == '/'; });
|
|
|
|
for (const auto & word : split)
|
|
|
|
if (!word.empty())
|
|
|
|
additional_names.insert(word);
|
|
|
|
}
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
KnownIdentifierFunc is_known_identifier = [&](std::string_view name)
|
|
|
|
{
|
|
|
|
std::string what(name);
|
|
|
|
|
2021-10-02 21:46:51 +00:00
|
|
|
return FunctionFactory::instance().has(what)
|
2020-09-26 00:38:59 +00:00
|
|
|
|| AggregateFunctionFactory::instance().isAggregateFunctionName(what)
|
|
|
|
|| TableFunctionFactory::instance().isTableFunctionName(what)
|
2021-10-03 02:56:32 +00:00
|
|
|
|| FormatFactory::instance().isOutputFormat(what)
|
|
|
|
|| FormatFactory::instance().isInputFormat(what)
|
2022-04-18 10:18:43 +00:00
|
|
|
|| additional_names.contains(what);
|
2020-09-26 00:38:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
WriteBufferFromFileDescriptor out(STDOUT_FILENO);
|
|
|
|
obfuscateQueries(query, out, obfuscated_words_map, used_nouns, hash_func, is_known_identifier);
|
2023-06-28 08:51:15 +00:00
|
|
|
out.finalize();
|
2020-09-26 00:38:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * pos = query.data();
|
|
|
|
const char * end = pos + query.size();
|
2023-12-27 14:00:52 +00:00
|
|
|
skipSpacesAndComments(pos, end, print_comments);
|
2020-09-26 00:38:59 +00:00
|
|
|
|
2022-04-04 08:09:22 +00:00
|
|
|
ParserQuery parser(end, allow_settings_after_format_in_insert);
|
2023-12-27 14:00:52 +00:00
|
|
|
while (pos != end)
|
2020-09-26 00:38:59 +00:00
|
|
|
{
|
2023-12-27 14:00:52 +00:00
|
|
|
size_t approx_query_length = multiple ? find_first_symbols<';'>(pos, end) - pos : end - pos;
|
|
|
|
|
2022-02-06 03:22:05 +00:00
|
|
|
ASTPtr res = parseQueryAndMovePosition(
|
2024-03-17 18:53:58 +00:00
|
|
|
parser, pos, end, "query", multiple, cmd_settings.max_query_size, cmd_settings.max_parser_depth, cmd_settings.max_parser_backtracks);
|
2023-08-04 23:43:54 +00:00
|
|
|
|
2024-04-03 18:50:33 +00:00
|
|
|
std::unique_ptr<ReadBuffer> insert_query_payload;
|
2023-12-27 14:00:52 +00:00
|
|
|
/// 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)
|
2021-02-28 10:59:10 +00:00
|
|
|
{
|
2023-12-27 14:00:52 +00:00
|
|
|
if ("Values" != insert_query->format)
|
2023-12-27 17:13:56 +00:00
|
|
|
throw Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "Can't format INSERT query with data format '{}'", insert_query->format);
|
|
|
|
|
|
|
|
/// Reset format to default to have `INSERT INTO table VALUES` instead of `INSERT INTO table VALUES FORMAT Values`
|
|
|
|
insert_query->format = {};
|
2023-12-27 14:00:52 +00:00
|
|
|
|
|
|
|
/// 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);
|
2021-02-28 10:59:10 +00:00
|
|
|
}
|
2023-08-04 23:43:54 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
if (!quiet)
|
|
|
|
{
|
2021-03-06 10:51:39 +00:00
|
|
|
if (!backslash)
|
|
|
|
{
|
2023-12-27 14:00:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
String res_string = str_buf.str();
|
|
|
|
const char * s_pos = res_string.data();
|
|
|
|
const char * s_end = s_pos + res_string.size();
|
2023-12-27 17:13:56 +00:00
|
|
|
/// remove trailing spaces
|
|
|
|
while (s_end > s_pos && isWhitespaceASCIIOneLine(*(s_end - 1)))
|
|
|
|
--s_end;
|
2023-12-27 14:00:52 +00:00
|
|
|
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";
|
|
|
|
}
|
2024-01-08 12:16:53 +00:00
|
|
|
else if (multiple && insert_query_payload)
|
|
|
|
/// Do not need to add ; because it's already in the insert_query_payload
|
|
|
|
std::cout << "\n";
|
|
|
|
|
2021-03-06 10:51:39 +00:00
|
|
|
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++);
|
|
|
|
}
|
|
|
|
|
2023-06-28 08:51:15 +00:00
|
|
|
res_cout.finalize();
|
2021-03-06 10:51:39 +00:00
|
|
|
if (multiple)
|
|
|
|
std::cout << " \\\n;\n";
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2020-09-26 00:38:59 +00:00
|
|
|
}
|
2023-12-27 14:00:52 +00:00
|
|
|
skipSpacesAndComments(pos, end, print_comments);
|
|
|
|
if (!multiple)
|
|
|
|
break;
|
|
|
|
}
|
2020-09-26 00:38:59 +00:00
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-12-23 21:18:08 +00:00
|
|
|
std::cerr << getCurrentExceptionMessage(true) << '\n';
|
2017-11-11 01:04:14 +00:00
|
|
|
return getCurrentExceptionCode();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|