use WriteBuffer in formatAST(...)

This commit is contained in:
Alexander Tokmakov 2020-11-09 19:05:40 +03:00
parent a06be511df
commit 62ff00ee8b
95 changed files with 241 additions and 193 deletions

View File

@ -54,6 +54,7 @@
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h> #include <IO/Operators.h>
#include <IO/UseSSL.h> #include <IO/UseSSL.h>
#include <IO/WriteBufferFromOStream.h>
#include <DataStreams/AsynchronousBlockInputStream.h> #include <DataStreams/AsynchronousBlockInputStream.h>
#include <DataStreams/AddingDefaultsBlockInputStream.h> #include <DataStreams/AddingDefaultsBlockInputStream.h>
#include <DataStreams/InternalTextLogsRowOutputStream.h> #include <DataStreams/InternalTextLogsRowOutputStream.h>
@ -1529,7 +1530,8 @@ private:
if (is_interactive) if (is_interactive)
{ {
std::cout << std::endl; std::cout << std::endl;
formatAST(*res, std::cout); WriteBufferFromOStream res_buf(std::cout, 4096);
formatAST(*res, res_buf);
std::cout << std::endl << std::endl; std::cout << std::endl << std::endl;
} }

View File

@ -8,6 +8,7 @@
#include <Core/Types.h> #include <Core/Types.h>
#include <IO/Operators.h> #include <IO/Operators.h>
#include <IO/UseSSL.h> #include <IO/UseSSL.h>
#include <IO/WriteBufferFromOStream.h>
#include <Parsers/ASTExpressionList.h> #include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h> #include <Parsers/ASTFunction.h>
#include <Parsers/ASTIdentifier.h> #include <Parsers/ASTIdentifier.h>
@ -419,7 +420,8 @@ void QueryFuzzer::fuzzMain(ASTPtr & ast)
fuzz(ast); fuzz(ast);
std::cout << std::endl; std::cout << std::endl;
formatAST(*ast, std::cout, false /*highlight*/); WriteBufferFromOStream ast_buf(std::cout, 4096);
formatAST(*ast, ast_buf, false /*highlight*/);
std::cout << std::endl << std::endl; std::cout << std::endl << std::endl;
} }

View File

@ -6,6 +6,7 @@
#include <IO/ReadBufferFromFileDescriptor.h> #include <IO/ReadBufferFromFileDescriptor.h>
#include <IO/ReadHelpers.h> #include <IO/ReadHelpers.h>
#include <IO/WriteBufferFromFileDescriptor.h> #include <IO/WriteBufferFromFileDescriptor.h>
#include <IO/WriteBufferFromOStream.h>
#include <Parsers/ParserQuery.h> #include <Parsers/ParserQuery.h>
#include <Parsers/parseQuery.h> #include <Parsers/parseQuery.h>
#include <Parsers/formatAST.h> #include <Parsers/formatAST.h>
@ -129,7 +130,8 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH); ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
if (!quiet) if (!quiet)
{ {
formatAST(*res, std::cout, hilite, oneline); WriteBufferFromOStream res_buf(std::cout, 4096);
formatAST(*res, res_buf, hilite, oneline);
if (multiple) if (multiple)
std::cout << "\n;\n"; std::cout << "\n;\n";
std::cout << std::endl; std::cout << std::endl;

View File

@ -113,16 +113,16 @@ void ODBCColumnsInfoHandler::handleRequest(Poco::Net::HTTPServerRequest & reques
/// TODO Why not do SQLColumns instead? /// TODO Why not do SQLColumns instead?
std::string name = schema_name.empty() ? backQuoteIfNeed(table_name) : backQuoteIfNeed(schema_name) + "." + backQuoteIfNeed(table_name); std::string name = schema_name.empty() ? backQuoteIfNeed(table_name) : backQuoteIfNeed(schema_name) + "." + backQuoteIfNeed(table_name);
std::stringstream ss; WriteBufferFromOwnString buf;
std::string input = "SELECT * FROM " + name + " WHERE 1 = 0"; std::string input = "SELECT * FROM " + name + " WHERE 1 = 0";
ParserQueryWithOutput parser; ParserQueryWithOutput parser;
ASTPtr select = parseQuery(parser, input.data(), input.data() + input.size(), "", context_settings.max_query_size, context_settings.max_parser_depth); ASTPtr select = parseQuery(parser, input.data(), input.data() + input.size(), "", context_settings.max_query_size, context_settings.max_parser_depth);
IAST::FormatSettings settings(ss, true); IAST::FormatSettings settings(buf, true);
settings.always_quote_identifiers = true; settings.always_quote_identifiers = true;
settings.identifier_quoting_style = getQuotingStyle(hdbc); settings.identifier_quoting_style = getQuotingStyle(hdbc);
select->format(settings); select->format(settings);
std::string query = ss.str(); std::string query = buf.str();
LOG_TRACE(log, "Inferring structure with query '{}'", query); LOG_TRACE(log, "Inferring structure with query '{}'", query);

View File

@ -32,12 +32,12 @@ namespace
for (const auto & column : columns) for (const auto & column : columns)
query.columns->children.emplace_back(std::make_shared<ASTIdentifier>(column.name)); query.columns->children.emplace_back(std::make_shared<ASTIdentifier>(column.name));
std::stringstream ss; WriteBufferFromOwnString buf;
IAST::FormatSettings settings(ss, true); IAST::FormatSettings settings(buf, true);
settings.always_quote_identifiers = true; settings.always_quote_identifiers = true;
settings.identifier_quoting_style = quoting; settings.identifier_quoting_style = quoting;
query.IAST::format(settings); query.IAST::format(settings);
return ss.str(); return buf.str();
} }
std::string getQuestionMarks(size_t n) std::string getQuestionMarks(size_t n)

View File

@ -197,11 +197,13 @@ namespace
boost::range::push_back(queries, InterpreterShowGrantsQuery::getAttachGrantQueries(entity)); boost::range::push_back(queries, InterpreterShowGrantsQuery::getAttachGrantQueries(entity));
/// Serialize the list of ATTACH queries to a string. /// Serialize the list of ATTACH queries to a string.
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit);
for (const ASTPtr & query : queries) for (const ASTPtr & query : queries)
ss << *query << ";\n"; {
String file_contents = std::move(ss).str(); formatAST(*query, buf, false, true);
buf.write(";\n", 2);
}
String file_contents = buf.str();
/// First we save *.tmp file and then we rename if everything's ok. /// First we save *.tmp file and then we rename if everything's ok.
auto tmp_file_path = std::filesystem::path{file_path}.replace_extension(".tmp"); auto tmp_file_path = std::filesystem::path{file_path}.replace_extension(".tmp");

View File

@ -94,10 +94,9 @@ String getObjectDefinitionFromCreateQuery(const ASTPtr & query)
if (!create) if (!create)
{ {
std::ostringstream query_stream; WriteBufferFromOwnString query_buf;
query_stream.exceptions(std::ios::failbit); formatAST(*query, query_buf, true);
formatAST(*query, query_stream, true); throw Exception(ErrorCodes::LOGICAL_ERROR, "Query '{}' is not CREATE query", query_buf.str());
throw Exception("Query '" + query_stream.str() + "' is not CREATE query", ErrorCodes::LOGICAL_ERROR);
} }
if (!create->is_dictionary) if (!create->is_dictionary)
@ -121,11 +120,10 @@ String getObjectDefinitionFromCreateQuery(const ASTPtr & query)
if (create->uuid != UUIDHelpers::Nil) if (create->uuid != UUIDHelpers::Nil)
create->table = TABLE_WITH_UUID_NAME_PLACEHOLDER; create->table = TABLE_WITH_UUID_NAME_PLACEHOLDER;
std::ostringstream statement_stream; WriteBufferFromOwnString statement_buf;
statement_stream.exceptions(std::ios::failbit); formatAST(*create, statement_buf, false);
formatAST(*create, statement_stream, false); writeChar('\n', statement_buf);
statement_stream << '\n'; return statement_buf.str();
return statement_stream.str();
} }
DatabaseOnDisk::DatabaseOnDisk( DatabaseOnDisk::DatabaseOnDisk(

View File

@ -45,6 +45,7 @@ struct BinaryManipReadBuffer : std::reference_wrapper<ReadBuffer> { usin
template <typename T> WriteBuffer & operator<< (WriteBuffer & buf, const T & x) { writeText(x, buf); return buf; } template <typename T> WriteBuffer & operator<< (WriteBuffer & buf, const T & x) { writeText(x, buf); return buf; }
/// If you do not use the manipulators, the string is displayed without an escape, as is. /// If you do not use the manipulators, the string is displayed without an escape, as is.
template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const String & x) { writeString(x, buf); return buf; } template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const String & x) { writeString(x, buf); return buf; }
template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const std::string_view & x) { writeString(StringRef(x), buf); return buf; }
template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const char & x) { writeChar(x, buf); return buf; } template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const char & x) { writeChar(x, buf); return buf; }
template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const pcg32_fast & x) { PcgSerializer::serializePcg32(x, buf); return buf; } template <> inline WriteBuffer & operator<< (WriteBuffer & buf, const pcg32_fast & x) { PcgSerializer::serializePcg32(x, buf); return buf; }

View File

@ -271,6 +271,11 @@ inline void writeString(const StringRef & ref, WriteBuffer & buf)
writeString(ref.data, ref.size, buf); writeString(ref.data, ref.size, buf);
} }
//inline void writeString(const std::string_view & view, WriteBuffer & buf)
//{
// writeString(view.data(), view.size(), buf);
//}
/** Writes a C-string without creating a temporary object. If the string is a literal, then `strlen` is executed at the compilation stage. /** Writes a C-string without creating a temporary object. If the string is a literal, then `strlen` is executed at the compilation stage.
* Use when the string is a literal. * Use when the string is a literal.

View File

@ -25,7 +25,7 @@ class AddDefaultDatabaseVisitor
{ {
public: public:
explicit AddDefaultDatabaseVisitor( explicit AddDefaultDatabaseVisitor(
const String & database_name_, bool only_replace_current_database_function_ = false, std::ostream * ostr_ = nullptr) const String & database_name_, bool only_replace_current_database_function_ = false, WriteBuffer * ostr_ = nullptr)
: database_name(database_name_) : database_name(database_name_)
, only_replace_current_database_function(only_replace_current_database_function_) , only_replace_current_database_function(only_replace_current_database_function_)
, visit_depth(0) , visit_depth(0)
@ -66,7 +66,7 @@ private:
const String database_name; const String database_name;
bool only_replace_current_database_function = false; bool only_replace_current_database_function = false;
mutable size_t visit_depth; mutable size_t visit_depth;
std::ostream * ostr; WriteBuffer * ostr;
void visit(ASTSelectWithUnionQuery & select, ASTPtr &) const void visit(ASTSelectWithUnionQuery & select, ASTPtr &) const
{ {

View File

@ -106,10 +106,9 @@ String formattedAST(const ASTPtr & ast)
{ {
if (!ast) if (!ast)
return {}; return {};
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); formatAST(*ast, buf, false, true);
formatAST(*ast, ss, false, true); return buf.str();
return ss.str();
} }
} }

View File

@ -16,7 +16,7 @@ class InDepthNodeVisitor
public: public:
using Data = typename Matcher::Data; using Data = typename Matcher::Data;
InDepthNodeVisitor(Data & data_, std::ostream * ostr_ = nullptr) InDepthNodeVisitor(Data & data_, WriteBuffer * ostr_ = nullptr)
: data(data_), : data(data_),
visit_depth(0), visit_depth(0),
ostr(ostr_) ostr(ostr_)
@ -46,7 +46,7 @@ public:
private: private:
Data & data; Data & data;
size_t visit_depth; size_t visit_depth;
std::ostream * ostr; WriteBuffer * ostr;
void visitChildren(T & ast) void visitChildren(T & ast)
{ {

View File

@ -135,10 +135,7 @@ BlockIO InterpreterCreateQuery::createDatabase(ASTCreateQuery & create)
else if ((create.columns_list && create.columns_list->indices && !create.columns_list->indices->children.empty())) else if ((create.columns_list && create.columns_list->indices && !create.columns_list->indices->children.empty()))
{ {
/// Currently, there are no database engines, that support any arguments. /// Currently, there are no database engines, that support any arguments.
std::stringstream ostr; throw Exception(ErrorCodes::UNKNOWN_DATABASE_ENGINE, "Unknown database engine: {}", serializeAST(*create.storage));
ostr.exceptions(std::ios::failbit);
formatAST(*create.storage, ostr, false, false);
throw Exception("Unknown database engine: " + ostr.str(), ErrorCodes::UNKNOWN_DATABASE_ENGINE);
} }
if (create.storage->engine->name == "Atomic") if (create.storage->engine->name == "Atomic")
@ -182,11 +179,10 @@ BlockIO InterpreterCreateQuery::createDatabase(ASTCreateQuery & create)
create.attach = true; create.attach = true;
create.if_not_exists = false; create.if_not_exists = false;
std::ostringstream statement_stream; WriteBufferFromOwnString statement_buf;
statement_stream.exceptions(std::ios::failbit); formatAST(create, statement_buf, false);
formatAST(create, statement_stream, false); writeChar('\n', statement_buf);
statement_stream << '\n'; String statement = statement_buf.str();
String statement = statement_stream.str();
/// Exclusive flag guarantees, that database is not created right now in another thread. /// Exclusive flag guarantees, that database is not created right now in another thread.
WriteBufferFromFile out(metadata_file_tmp_path, statement.size(), O_WRONLY | O_CREAT | O_EXCL); WriteBufferFromFile out(metadata_file_tmp_path, statement.size(), O_WRONLY | O_CREAT | O_EXCL);

View File

@ -222,15 +222,14 @@ BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
Block sample_block = getSampleBlock(); Block sample_block = getSampleBlock();
MutableColumns res_columns = sample_block.cloneEmptyColumns(); MutableColumns res_columns = sample_block.cloneEmptyColumns();
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit);
if (ast.getKind() == ASTExplainQuery::ParsedAST) if (ast.getKind() == ASTExplainQuery::ParsedAST)
{ {
if (ast.getSettings()) if (ast.getSettings())
throw Exception("Settings are not supported for EXPLAIN AST query.", ErrorCodes::UNKNOWN_SETTING); throw Exception("Settings are not supported for EXPLAIN AST query.", ErrorCodes::UNKNOWN_SETTING);
dumpAST(*ast.getExplainedQuery(), ss); dumpAST(*ast.getExplainedQuery(), buf);
} }
else if (ast.getKind() == ASTExplainQuery::AnalyzedSyntax) else if (ast.getKind() == ASTExplainQuery::AnalyzedSyntax)
{ {
@ -240,7 +239,7 @@ BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
ExplainAnalyzedSyntaxVisitor::Data data{.context = context}; ExplainAnalyzedSyntaxVisitor::Data data{.context = context};
ExplainAnalyzedSyntaxVisitor(data).visit(query); ExplainAnalyzedSyntaxVisitor(data).visit(query);
ast.getExplainedQuery()->format(IAST::FormatSettings(ss, false)); ast.getExplainedQuery()->format(IAST::FormatSettings(buf, false));
} }
else if (ast.getKind() == ASTExplainQuery::QueryPlan) else if (ast.getKind() == ASTExplainQuery::QueryPlan)
{ {
@ -256,8 +255,7 @@ BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
if (settings.optimize) if (settings.optimize)
plan.optimize(); plan.optimize();
WriteBufferFromOStream buffer(ss); plan.explainPlan(buf, settings.query_plan_options);
plan.explainPlan(buffer, settings.query_plan_options);
} }
else if (ast.getKind() == ASTExplainQuery::QueryPipeline) else if (ast.getKind() == ASTExplainQuery::QueryPipeline)
{ {
@ -271,8 +269,6 @@ BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
interpreter.buildQueryPlan(plan); interpreter.buildQueryPlan(plan);
auto pipeline = plan.buildQueryPipeline(); auto pipeline = plan.buildQueryPipeline();
WriteBufferFromOStream buffer(ss);
if (settings.graph) if (settings.graph)
{ {
/// Pipe holds QueryPlan, should not go out-of-scope /// Pipe holds QueryPlan, should not go out-of-scope
@ -280,17 +276,17 @@ BlockInputStreamPtr InterpreterExplainQuery::executeImpl()
const auto & processors = pipe.getProcessors(); const auto & processors = pipe.getProcessors();
if (settings.compact) if (settings.compact)
printPipelineCompact(processors, buffer, settings.query_pipeline_options.header); printPipelineCompact(processors, buf, settings.query_pipeline_options.header);
else else
printPipeline(processors, buffer); printPipeline(processors, buf);
} }
else else
{ {
plan.explainPipeline(buffer, settings.query_pipeline_options); plan.explainPipeline(buf, settings.query_pipeline_options);
} }
} }
fillColumn(*res_columns[0], ss.str()); fillColumn(*res_columns[0], buf.str());
return std::make_shared<OneBlockInputStream>(sample_block.cloneWithColumns(std::move(res_columns))); return std::make_shared<OneBlockInputStream>(sample_block.cloneWithColumns(std::move(res_columns)));
} }

View File

@ -34,13 +34,12 @@ BlockInputStreamPtr InterpreterShowAccessQuery::executeImpl() const
/// Build the result column. /// Build the result column.
MutableColumnPtr column = ColumnString::create(); MutableColumnPtr column = ColumnString::create();
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit);
for (const auto & query : queries) for (const auto & query : queries)
{ {
ss.str(""); buf.restart();
formatAST(*query, ss, false, true); formatAST(*query, buf, false, true);
column->insert(ss.str()); column->insert(buf.str());
} }
String desc = "ACCESS"; String desc = "ACCESS";

View File

@ -238,21 +238,19 @@ BlockInputStreamPtr InterpreterShowCreateAccessEntityQuery::executeImpl()
/// Build the result column. /// Build the result column.
MutableColumnPtr column = ColumnString::create(); MutableColumnPtr column = ColumnString::create();
std::stringstream create_query_ss; WriteBufferFromOwnString create_query_buf;
create_query_ss.exceptions(std::ios::failbit);
for (const auto & create_query : create_queries) for (const auto & create_query : create_queries)
{ {
formatAST(*create_query, create_query_ss, false, true); formatAST(*create_query, create_query_buf, false, true);
column->insert(create_query_ss.str()); column->insert(create_query_buf.str());
create_query_ss.str(""); create_query_buf.restart();
} }
/// Prepare description of the result column. /// Prepare description of the result column.
std::stringstream desc_ss; WriteBufferFromOwnString desc_buf;
desc_ss.exceptions(std::ios::failbit);
const auto & show_query = query_ptr->as<const ASTShowCreateAccessEntityQuery &>(); const auto & show_query = query_ptr->as<const ASTShowCreateAccessEntityQuery &>();
formatAST(show_query, desc_ss, false, true); formatAST(show_query, desc_buf, false, true);
String desc = desc_ss.str(); String desc = desc_buf.str();
String prefix = "SHOW "; String prefix = "SHOW ";
if (startsWith(desc, prefix)) if (startsWith(desc, prefix))
desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix. desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix.

View File

@ -78,10 +78,9 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl()
create.uuid = UUIDHelpers::Nil; create.uuid = UUIDHelpers::Nil;
} }
std::stringstream stream; WriteBufferFromOwnString buf;
stream.exceptions(std::ios::failbit); formatAST(*create_query, buf, false, false);
formatAST(*create_query, stream, false, false); String res = buf.str();
String res = stream.str();
MutableColumnPtr column = ColumnString::create(); MutableColumnPtr column = ColumnString::create();
column->insert(res); column->insert(res);

View File

@ -118,21 +118,19 @@ BlockInputStreamPtr InterpreterShowGrantsQuery::executeImpl()
/// Build the result column. /// Build the result column.
MutableColumnPtr column = ColumnString::create(); MutableColumnPtr column = ColumnString::create();
std::stringstream grant_ss; WriteBufferFromOwnString grant_buf;
grant_ss.exceptions(std::ios::failbit);
for (const auto & grant_query : grant_queries) for (const auto & grant_query : grant_queries)
{ {
grant_ss.str(""); grant_buf.restart();
formatAST(*grant_query, grant_ss, false, true); formatAST(*grant_query, grant_buf, false, true);
column->insert(grant_ss.str()); column->insert(grant_buf.str());
} }
/// Prepare description of the result column. /// Prepare description of the result column.
std::stringstream desc_ss; WriteBufferFromOwnString desc_buf;
desc_ss.exceptions(std::ios::failbit);
const auto & show_query = query_ptr->as<const ASTShowGrantsQuery &>(); const auto & show_query = query_ptr->as<const ASTShowGrantsQuery &>();
formatAST(show_query, desc_ss, false, true); formatAST(show_query, desc_buf, false, true);
String desc = desc_ss.str(); String desc = desc_buf.str();
String prefix = "SHOW "; String prefix = "SHOW ";
if (desc.starts_with(prefix)) if (desc.starts_with(prefix))
desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix. desc = desc.substr(prefix.length()); /// `desc` always starts with "SHOW ", so we can trim this prefix.

View File

@ -20,13 +20,12 @@ namespace ErrorCodes
static String wrongAliasMessage(const ASTPtr & ast, const ASTPtr & prev_ast, const String & alias) static String wrongAliasMessage(const ASTPtr & ast, const ASTPtr & prev_ast, const String & alias)
{ {
std::stringstream message; WriteBufferFromOwnString message;
message.exceptions(std::ios::failbit); message << "Different expressions with the same alias " << backQuoteIfNeed(alias) << ":\n";
message << "Different expressions with the same alias " << backQuoteIfNeed(alias) << ":" << std::endl;
formatAST(*ast, message, false, true); formatAST(*ast, message, false, true);
message << std::endl << "and" << std::endl; message << "\nand\n";
formatAST(*prev_ast, message, false, true); formatAST(*prev_ast, message, false, true);
message << std::endl; message << '\n';
return message.str(); return message.str();
} }

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTAlterQuery.h> #include <Parsers/ASTAlterQuery.h>
#include <IO/Operators.h>
#include <iomanip> #include <iomanip>
#include <Common/quoteString.h> #include <Common/quoteString.h>
@ -246,7 +247,7 @@ void ASTAlterCommand::formatImpl(
<< "PARTITION " << (settings.hilite ? hilite_none : ""); << "PARTITION " << (settings.hilite ? hilite_none : "");
partition->formatImpl(settings, state, frame); partition->formatImpl(settings, state, frame);
settings.ostr << (settings.hilite ? hilite_keyword : "") settings.ostr << (settings.hilite ? hilite_keyword : "")
<< " FROM " << (settings.hilite ? hilite_none : "") << std::quoted(from, '\''); << " FROM " << (settings.hilite ? hilite_none : "") << DB::quote << from;
} }
else if (type == ASTAlterCommand::FREEZE_PARTITION) else if (type == ASTAlterCommand::FREEZE_PARTITION)
{ {
@ -256,7 +257,7 @@ void ASTAlterCommand::formatImpl(
if (!with_name.empty()) if (!with_name.empty())
{ {
settings.ostr << " " << (settings.hilite ? hilite_keyword : "") << "WITH NAME" << (settings.hilite ? hilite_none : "") settings.ostr << " " << (settings.hilite ? hilite_keyword : "") << "WITH NAME" << (settings.hilite ? hilite_none : "")
<< " " << std::quoted(with_name, '\''); << " " << DB::quote << with_name;
} }
} }
else if (type == ASTAlterCommand::FREEZE_ALL) else if (type == ASTAlterCommand::FREEZE_ALL)
@ -266,7 +267,7 @@ void ASTAlterCommand::formatImpl(
if (!with_name.empty()) if (!with_name.empty())
{ {
settings.ostr << " " << (settings.hilite ? hilite_keyword : "") << "WITH NAME" << (settings.hilite ? hilite_none : "") settings.ostr << " " << (settings.hilite ? hilite_keyword : "") << "WITH NAME" << (settings.hilite ? hilite_none : "")
<< " " << std::quoted(with_name, '\''); << " " << DB::quote << with_name;
} }
} }
else if (type == ASTAlterCommand::DELETE) else if (type == ASTAlterCommand::DELETE)

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTAsterisk.h> #include <Parsers/ASTAsterisk.h>
#include <IO/WriteBuffer.h> #include <IO/WriteBuffer.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTColumnDeclaration.h> #include <Parsers/ASTColumnDeclaration.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -3,6 +3,7 @@
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <re2/re2.h> #include <re2/re2.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -5,6 +5,7 @@
#include <Parsers/ASTIdentifier.h> #include <Parsers/ASTIdentifier.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTConstraintDeclaration.h> #include <Parsers/ASTConstraintDeclaration.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -5,6 +5,7 @@
#include <Parsers/ASTSetQuery.h> #include <Parsers/ASTSetQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <Interpreters/StorageID.h> #include <Interpreters/StorageID.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -3,6 +3,7 @@
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <Common/IntervalKind.h> #include <Common/IntervalKind.h>
#include <ext/range.h> #include <ext/range.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTCreateRoleQuery.h> #include <Parsers/ASTCreateRoleQuery.h>
#include <Parsers/ASTSettingsProfileElement.h> #include <Parsers/ASTSettingsProfileElement.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -6,6 +6,7 @@
#include <ext/range.h> #include <ext/range.h>
#include <boost/range/algorithm/transform.hpp> #include <boost/range/algorithm/transform.hpp>
#include <sstream> #include <sstream>
#include <IO/Operators.h>
namespace DB namespace DB
@ -62,14 +63,13 @@ namespace
void formatForClauses(const std::vector<std::pair<ConditionType, ASTPtr>> & conditions, bool alter, const IAST::FormatSettings & settings) void formatForClauses(const std::vector<std::pair<ConditionType, ASTPtr>> & conditions, bool alter, const IAST::FormatSettings & settings)
{ {
std::vector<std::pair<ConditionType, String>> conditions_as_strings; std::vector<std::pair<ConditionType, String>> conditions_as_strings;
std::stringstream temp_sstream; WriteBufferFromOwnString temp_buf;
temp_sstream.exceptions(std::ios::failbit); IAST::FormatSettings temp_settings(temp_buf, settings);
IAST::FormatSettings temp_settings(temp_sstream, settings);
for (const auto & [condition_type, condition] : conditions) for (const auto & [condition_type, condition] : conditions)
{ {
formatConditionalExpression(condition, temp_settings); formatConditionalExpression(condition, temp_settings);
conditions_as_strings.emplace_back(condition_type, temp_sstream.str()); conditions_as_strings.emplace_back(condition_type, temp_buf.str());
temp_sstream.str(""); temp_buf.restart();
} }
boost::container::flat_set<std::string_view> commands; boost::container::flat_set<std::string_view> commands;

View File

@ -2,6 +2,7 @@
#include <Parsers/ASTSettingsProfileElement.h> #include <Parsers/ASTSettingsProfileElement.h>
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -3,6 +3,7 @@
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Parsers/ASTSettingsProfileElement.h> #include <Parsers/ASTSettingsProfileElement.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTDictionary.h> #include <Parsers/ASTDictionary.h>
#include <Poco/String.h> #include <Poco/String.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTDictionaryAttributeDeclaration.h> #include <Parsers/ASTDictionaryAttributeDeclaration.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTDropAccessEntityQuery.h> #include <Parsers/ASTDropAccessEntityQuery.h>
#include <Parsers/ASTRowPolicyName.h> #include <Parsers/ASTRowPolicyName.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTDropQuery.h> #include <Parsers/ASTDropQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTExpressionList.h> #include <Parsers/ASTExpressionList.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -6,6 +6,7 @@
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/WriteBufferFromString.h> #include <IO/WriteBufferFromString.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -2,6 +2,7 @@
#include <Poco/String.h> #include <Poco/String.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTGrantQuery.h> #include <Parsers/ASTGrantQuery.h>
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -5,6 +5,7 @@
#include <Interpreters/IdentifierSemantic.h> #include <Interpreters/IdentifierSemantic.h>
#include <Interpreters/StorageID.h> #include <Interpreters/StorageID.h>
#include <Parsers/queryToString.h> #include <Parsers/queryToString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTIndexDeclaration.h> #include <Parsers/ASTIndexDeclaration.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -3,6 +3,7 @@
#include <Parsers/ASTFunction.h> #include <Parsers/ASTFunction.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTKillQueryQuery.h> #include <Parsers/ASTKillQueryQuery.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -73,4 +73,9 @@ void ASTLiteral::appendColumnNameImpl(WriteBuffer & ostr) const
} }
} }
void ASTLiteral::formatImplWithoutAlias(const FormatSettings & settings, IAST::FormatState &, IAST::FormatStateStacked) const
{
settings.ostr << applyVisitor(FieldVisitorToString(), value);
}
} }

View File

@ -43,10 +43,7 @@ public:
void updateTreeHashImpl(SipHash & hash_state) const override; void updateTreeHashImpl(SipHash & hash_state) const override;
protected: protected:
void formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const override void formatImplWithoutAlias(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
{
settings.ostr << applyVisitor(FieldVisitorToString(), value);
}
void appendColumnNameImpl(WriteBuffer & ostr) const override; void appendColumnNameImpl(WriteBuffer & ostr) const override;
}; };

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTNameTypePair.h> #include <Parsers/ASTNameTypePair.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTOptimizeQuery.h> #include <Parsers/ASTOptimizeQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,6 +1,7 @@
#include <Columns/Collator.h> #include <Columns/Collator.h>
#include <Parsers/ASTOrderByElement.h> #include <Parsers/ASTOrderByElement.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTPartition.h> #include <Parsers/ASTPartition.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTQualifiedAsterisk.h> #include <Parsers/ASTQualifiedAsterisk.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTQueryParameter.h> #include <Parsers/ASTQueryParameter.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -5,6 +5,7 @@
#include <Parsers/parseIdentifierOrStringLiteral.h> #include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <Parsers/IAST.h> #include <Parsers/IAST.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTQueryWithTableAndOutput.h> #include <Parsers/ASTQueryWithTableAndOutput.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -4,6 +4,7 @@
#include <Parsers/ASTQueryWithOutput.h> #include <Parsers/ASTQueryWithOutput.h>
#include <Parsers/ASTQueryWithOnCluster.h> #include <Parsers/ASTQueryWithOnCluster.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTRowPolicyName.h> #include <Parsers/ASTRowPolicyName.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTSampleRatio.h> #include <Parsers/ASTSampleRatio.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {
@ -34,5 +35,9 @@ String ASTSampleRatio::toString(Rational ratio)
return toString(ratio.numerator) + " / " + toString(ratio.denominator); return toString(ratio.numerator) + " / " + toString(ratio.denominator);
} }
void ASTSampleRatio::formatImpl(const IAST::FormatSettings & settings, IAST::FormatState &, IAST::FormatStateStacked) const
{
settings.ostr << toString(ratio);
}
} }

View File

@ -35,10 +35,7 @@ public:
static String toString(BigNum num); static String toString(BigNum num);
static String toString(Rational ratio); static String toString(Rational ratio);
void formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override void formatImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
{
settings.ostr << toString(ratio);
}
}; };
} }

View File

@ -6,6 +6,7 @@
#include <Parsers/ASTOrderByElement.h> #include <Parsers/ASTOrderByElement.h>
#include <Parsers/ASTTablesInSelectQuery.h> #include <Parsers/ASTTablesInSelectQuery.h>
#include <Interpreters/StorageID.h> #include <Interpreters/StorageID.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTSelectWithUnionQuery.h> #include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSelectQuery.h> #include <Parsers/ASTSelectQuery.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTSetQuery.h> #include <Parsers/ASTSetQuery.h>
#include <Parsers/formatSettingName.h> #include <Parsers/formatSettingName.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTSetRoleQuery.h> #include <Parsers/ASTSetRoleQuery.h>
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -2,6 +2,7 @@
#include <Parsers/formatSettingName.h> #include <Parsers/formatSettingName.h>
#include <Common/FieldVisitors.h> #include <Common/FieldVisitors.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTShowAccessEntitiesQuery.h> #include <Parsers/ASTShowAccessEntitiesQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTShowCreateAccessEntityQuery.h> #include <Parsers/ASTShowCreateAccessEntityQuery.h>
#include <Parsers/ASTRowPolicyName.h> #include <Parsers/ASTRowPolicyName.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTShowGrantsQuery.h> #include <Parsers/ASTShowGrantsQuery.h>
#include <Parsers/ASTRolesOrUsersSet.h> #include <Parsers/ASTRolesOrUsersSet.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <iomanip> #include <iomanip>
#include <Parsers/ASTShowTablesQuery.h> #include <Parsers/ASTShowTablesQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {
@ -21,7 +22,7 @@ void ASTShowTablesQuery::formatLike(const FormatSettings & settings) const
<< (not_like ? " NOT" : "") << (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE ") << (case_insensitive_like ? " ILIKE " : " LIKE ")
<< (settings.hilite ? hilite_none : "") << (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\''); << DB::quote << like;
} }
void ASTShowTablesQuery::formatLimit(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const void ASTShowTablesQuery::formatLimit(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTSubquery.h> #include <Parsers/ASTSubquery.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,6 +1,7 @@
#include <Parsers/IAST.h> #include <Parsers/IAST.h>
#include <Parsers/ASTSystemQuery.h> #include <Parsers/ASTSystemQuery.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -2,6 +2,7 @@
#include <Columns/Collator.h> #include <Columns/Collator.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <Parsers/ASTTTLElement.h> #include <Parsers/ASTTTLElement.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -2,6 +2,7 @@
#include <Parsers/ASTExpressionList.h> #include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTTablesInSelectQuery.h> #include <Parsers/ASTTablesInSelectQuery.h>
#include <Common/SipHash.h> #include <Common/SipHash.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -2,6 +2,7 @@
#include <Parsers/IAST.h> #include <Parsers/IAST.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,5 +1,6 @@
#include <Parsers/ASTUserNameWithHost.h> #include <Parsers/ASTUserNameWithHost.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,6 +1,7 @@
#include <Parsers/ASTWithAlias.h> #include <Parsers/ASTWithAlias.h>
#include <IO/WriteBufferFromOStream.h> #include <IO/WriteBufferFromOStream.h>
#include <IO/WriteHelpers.h> #include <IO/WriteHelpers.h>
#include <IO/Operators.h>
namespace DB namespace DB

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTWithElement.h> #include <Parsers/ASTWithElement.h>
#include <IO/Operators.h>
namespace DB namespace DB
{ {

View File

@ -1,6 +1,7 @@
#include <Common/StringUtils/StringUtils.h> #include <Common/StringUtils/StringUtils.h>
#include <Parsers/CommonParsers.h> #include <Parsers/CommonParsers.h>
#include <common/find_symbols.h> #include <common/find_symbols.h>
#include <IO/Operators.h>
#include <string.h> /// strncmp, strncasecmp #include <string.h> /// strncmp, strncasecmp

View File

@ -3,6 +3,7 @@
#include <sstream> #include <sstream>
#include <common/logger_useful.h> #include <common/logger_useful.h>
#include <Poco/Util/Application.h> #include <Poco/Util/Application.h>
#include <IO/Operators.h>
#include <Parsers/IAST.h> #include <Parsers/IAST.h>
@ -14,7 +15,7 @@ namespace DB
class DumpASTNode class DumpASTNode
{ {
public: public:
DumpASTNode(const IAST & ast_, std::ostream * ostr_, size_t & depth, const char * label_ = nullptr) DumpASTNode(const IAST & ast_, WriteBuffer * ostr_, size_t & depth, const char * label_ = nullptr)
: ast(ast_), : ast(ast_),
ostr(ostr_), ostr(ostr_),
indent(depth), indent(depth),
@ -24,12 +25,12 @@ public:
if (!ostr) if (!ostr)
return; return;
if (label && visit_depth == 0) if (label && visit_depth == 0)
(*ostr) << "-- " << label << std::endl; (*ostr) << "-- " << label << '\n';
++visit_depth; ++visit_depth;
(*ostr) << String(indent, ' '); (*ostr) << String(indent, ' ');
printNode(); printNode();
(*ostr) << std::endl; (*ostr) << '\n';
} }
~DumpASTNode() ~DumpASTNode()
@ -38,7 +39,7 @@ public:
return; return;
--visit_depth; --visit_depth;
if (label && visit_depth == 0) if (label && visit_depth == 0)
(*ostr) << "--" << std::endl; (*ostr) << "--\n";
} }
template <typename T, typename U> template <typename T, typename U>
@ -50,14 +51,14 @@ public:
(*ostr) << (str_indent ? String(str_indent) : String(indent, ' ')); (*ostr) << (str_indent ? String(str_indent) : String(indent, ' '));
(*ostr) << '(' << name << ' ' << value << ')'; (*ostr) << '(' << name << ' ' << value << ')';
if (!str_indent) if (!str_indent)
(*ostr) << std::endl; (*ostr) << '\n';
} }
size_t & getDepth() { return visit_depth; } size_t & getDepth() { return visit_depth; }
private: private:
const IAST & ast; const IAST & ast;
std::ostream * ostr; WriteBuffer * ostr;
size_t indent; size_t indent;
size_t & visit_depth; /// shared with children size_t & visit_depth; /// shared with children
const char * label; const char * label;
@ -77,7 +78,7 @@ private:
} }
}; };
inline void dumpAST(const IAST & ast, std::ostream & ostr, DumpASTNode * parent = nullptr) inline void dumpAST(const IAST & ast, WriteBuffer & ostr, DumpASTNode * parent = nullptr)
{ {
size_t depth = 0; size_t depth = 0;
DumpASTNode dump(ast, &ostr, (parent ? parent->getDepth() : depth)); DumpASTNode dump(ast, &ostr, (parent ? parent->getDepth() : depth));
@ -95,7 +96,6 @@ public:
DebugASTLog() DebugASTLog()
: log(nullptr) : log(nullptr)
{ {
ss.exceptions(std::ios::failbit);
if constexpr (_enable) if constexpr (_enable)
log = &Poco::Logger::get("AST"); log = &Poco::Logger::get("AST");
} }
@ -103,14 +103,14 @@ public:
~DebugASTLog() ~DebugASTLog()
{ {
if constexpr (_enable) if constexpr (_enable)
LOG_DEBUG(log, ss.str()); LOG_DEBUG(log, buf.str());
} }
std::ostream * stream() { return (_enable ? &ss : nullptr); } WriteBuffer * stream() { return (_enable ? &buf : nullptr); }
private: private:
Poco::Logger * log; Poco::Logger * log;
std::stringstream ss; WriteBufferFromOwnString buf;
}; };

View File

@ -89,10 +89,9 @@ size_t IAST::checkDepthImpl(size_t max_depth, size_t level) const
std::string IAST::formatForErrorMessage() const std::string IAST::formatForErrorMessage() const
{ {
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); format(FormatSettings(buf, true /* one line */));
format(FormatSettings(ss, true /* one line */)); return buf.str();
return ss.str();
} }
void IAST::cloneChildren() void IAST::cloneChildren()
@ -112,8 +111,6 @@ String IAST::getColumnName() const
void IAST::FormatSettings::writeIdentifier(const String & name) const void IAST::FormatSettings::writeIdentifier(const String & name) const
{ {
WriteBufferFromOStream out(ostr, 32);
switch (identifier_quoting_style) switch (identifier_quoting_style)
{ {
case IdentifierQuotingStyle::None: case IdentifierQuotingStyle::None:
@ -121,36 +118,34 @@ void IAST::FormatSettings::writeIdentifier(const String & name) const
if (always_quote_identifiers) if (always_quote_identifiers)
throw Exception("Incompatible arguments: always_quote_identifiers = true && identifier_quoting_style == IdentifierQuotingStyle::None", throw Exception("Incompatible arguments: always_quote_identifiers = true && identifier_quoting_style == IdentifierQuotingStyle::None",
ErrorCodes::BAD_ARGUMENTS); ErrorCodes::BAD_ARGUMENTS);
writeString(name, out); writeString(name, ostr);
break; break;
} }
case IdentifierQuotingStyle::Backticks: case IdentifierQuotingStyle::Backticks:
{ {
if (always_quote_identifiers) if (always_quote_identifiers)
writeBackQuotedString(name, out); writeBackQuotedString(name, ostr);
else else
writeProbablyBackQuotedString(name, out); writeProbablyBackQuotedString(name, ostr);
break; break;
} }
case IdentifierQuotingStyle::DoubleQuotes: case IdentifierQuotingStyle::DoubleQuotes:
{ {
if (always_quote_identifiers) if (always_quote_identifiers)
writeDoubleQuotedString(name, out); writeDoubleQuotedString(name, ostr);
else else
writeProbablyDoubleQuotedString(name, out); writeProbablyDoubleQuotedString(name, ostr);
break; break;
} }
case IdentifierQuotingStyle::BackticksMySQL: case IdentifierQuotingStyle::BackticksMySQL:
{ {
if (always_quote_identifiers) if (always_quote_identifiers)
writeBackQuotedStringMySQL(name, out); writeBackQuotedStringMySQL(name, ostr);
else else
writeProbablyBackQuotedStringMySQL(name, out); writeProbablyBackQuotedStringMySQL(name, ostr);
break; break;
} }
} }
out.next();
} }
} }

View File

@ -5,6 +5,7 @@
#include <Parsers/IdentifierQuotingStyle.h> #include <Parsers/IdentifierQuotingStyle.h>
#include <Common/Exception.h> #include <Common/Exception.h>
#include <Common/TypePromotion.h> #include <Common/TypePromotion.h>
#include <IO/WriteBufferFromString.h>
#include <algorithm> #include <algorithm>
#include <ostream> #include <ostream>
@ -161,7 +162,7 @@ public:
/// Format settings. /// Format settings.
struct FormatSettings struct FormatSettings
{ {
std::ostream & ostr; WriteBuffer & ostr;
bool hilite = false; bool hilite = false;
bool one_line; bool one_line;
bool always_quote_identifiers = false; bool always_quote_identifiers = false;
@ -169,13 +170,13 @@ public:
char nl_or_ws; char nl_or_ws;
FormatSettings(std::ostream & ostr_, bool one_line_) FormatSettings(WriteBuffer & ostr_, bool one_line_)
: ostr(ostr_), one_line(one_line_) : ostr(ostr_), one_line(one_line_)
{ {
nl_or_ws = one_line ? ' ' : '\n'; nl_or_ws = one_line ? ' ' : '\n';
} }
FormatSettings(std::ostream & ostr_, const FormatSettings & other) FormatSettings(WriteBuffer & ostr_, const FormatSettings & other)
: ostr(ostr_), hilite(other.hilite), one_line(other.one_line), : ostr(ostr_), hilite(other.hilite), one_line(other.one_line),
always_quote_identifiers(other.always_quote_identifiers), identifier_quoting_style(other.identifier_quoting_style) always_quote_identifiers(other.always_quote_identifiers), identifier_quoting_style(other.identifier_quoting_style)
{ {
@ -242,17 +243,17 @@ private:
template <typename AstArray> template <typename AstArray>
std::string IAST::formatForErrorMessage(const AstArray & array) std::string IAST::formatForErrorMessage(const AstArray & array)
{ {
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit);
for (size_t i = 0; i < array.size(); ++i) for (size_t i = 0; i < array.size(); ++i)
{ {
if (i > 0) if (i > 0)
{ {
ss << ", "; const char * delim = ", ";
buf.write(delim, strlen(delim));
} }
array[i]->format(IAST::FormatSettings(ss, true /* one line */)); array[i]->format(IAST::FormatSettings(buf, true /* one line */));
} }
return ss.str(); return buf.str();
} }
} }

View File

@ -5,9 +5,9 @@
namespace DB namespace DB
{ {
void formatAST(const IAST & ast, std::ostream & s, bool hilite, bool one_line) void formatAST(const IAST & ast, WriteBuffer & buf, bool hilite, bool one_line)
{ {
IAST::FormatSettings settings(s, one_line); IAST::FormatSettings settings(buf, one_line);
settings.hilite = hilite; settings.hilite = hilite;
ast.format(settings); ast.format(settings);
@ -15,10 +15,9 @@ void formatAST(const IAST & ast, std::ostream & s, bool hilite, bool one_line)
String serializeAST(const IAST & ast, bool one_line) String serializeAST(const IAST & ast, bool one_line)
{ {
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); formatAST(ast, buf, false, one_line);
formatAST(ast, ss, false, one_line); return buf.str();
return ss.str();
} }
} }

View File

@ -7,23 +7,25 @@
namespace DB namespace DB
{ {
class WriteBuffer;
/** Takes a syntax tree and turns it back into text. /** Takes a syntax tree and turns it back into text.
* In case of INSERT query, the data will be missing. * In case of INSERT query, the data will be missing.
*/ */
void formatAST(const IAST & ast, std::ostream & s, bool hilite = true, bool one_line = false); void formatAST(const IAST & ast, WriteBuffer & buf, bool hilite = true, bool one_line = false);
String serializeAST(const IAST & ast, bool one_line = true); String serializeAST(const IAST & ast, bool one_line = true);
inline std::ostream & operator<<(std::ostream & os, const IAST & ast) //inline std::ostream & operator<<(std::ostream & os, const IAST & ast)
{ //{
formatAST(ast, os, false, true); // formatAST(ast, os, false, true);
return os; // return os;
} //}
//
inline std::ostream & operator<<(std::ostream & os, const ASTPtr & ast) //inline std::ostream & operator<<(std::ostream & os, const ASTPtr & ast)
{ //{
formatAST(*ast, os, false, true); // formatAST(*ast, os, false, true);
return os; // return os;
} //}
} }

View File

@ -2,13 +2,13 @@
#include <Common/StringUtils/StringUtils.h> #include <Common/StringUtils/StringUtils.h>
#include <Common/quoteString.h> #include <Common/quoteString.h>
#include <common/find_symbols.h> #include <common/find_symbols.h>
#include <ostream> #include <IO/Operators.h>
namespace DB namespace DB
{ {
void formatSettingName(const String & setting_name, std::ostream & out) void formatSettingName(const String & setting_name, WriteBuffer & out)
{ {
if (isValidIdentifier(setting_name)) if (isValidIdentifier(setting_name))
{ {

View File

@ -7,9 +7,11 @@
namespace DB namespace DB
{ {
class WriteBuffer;
/// Outputs built-in or custom setting's name. /// Outputs built-in or custom setting's name.
/// The function is like backQuoteIfNeed() but didn't quote with backticks /// The function is like backQuoteIfNeed() but didn't quote with backticks
/// if the name consists of identifiers joined with dots. /// if the name consists of identifiers joined with dots.
void formatSettingName(const String & setting_name, std::ostream & out); void formatSettingName(const String & setting_name, WriteBuffer & out);
} }

View File

@ -1,6 +1,5 @@
#include <Parsers/queryToString.h> #include <Parsers/queryToString.h>
#include <Parsers/formatAST.h> #include <Parsers/formatAST.h>
#include <sstream>
namespace DB namespace DB
{ {
@ -11,9 +10,6 @@ namespace DB
String queryToString(const IAST & query) String queryToString(const IAST & query)
{ {
std::ostringstream out; return serializeAST(query);
out.exceptions(std::ios::failbit);
formatAST(query, out, false, true);
return out.str();
} }
} }

View File

@ -4,6 +4,7 @@
#include <Parsers/ParserCreateQuery.h> #include <Parsers/ParserCreateQuery.h>
#include <Parsers/formatAST.h> #include <Parsers/formatAST.h>
#include <Parsers/parseQuery.h> #include <Parsers/parseQuery.h>
#include <IO/WriteBufferFromOStream.h>
int main(int, char **) int main(int, char **)
@ -14,7 +15,8 @@ int main(int, char **)
ParserCreateQuery parser; ParserCreateQuery parser;
ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0, 0); ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0, 0);
formatAST(*ast, std::cerr); WriteBufferFromOStream out(std::cerr, 4096);
formatAST(*ast, out);
std::cerr << std::endl; std::cerr << std::endl;
return 0; return 0;

View File

@ -8,6 +8,7 @@
#include <Parsers/DumpASTNode.h> #include <Parsers/DumpASTNode.h>
#include <Parsers/TablePropertiesQueriesASTs.h> #include <Parsers/TablePropertiesQueriesASTs.h>
#include <Parsers/ParserTablePropertiesQuery.h> #include <Parsers/ParserTablePropertiesQuery.h>
#include <IO/WriteBufferFromString.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -17,10 +18,9 @@ using namespace DB;
static String astToString(IAST * ast) static String astToString(IAST * ast)
{ {
std::ostringstream oss; WriteBufferFromOwnString buf;
oss.exceptions(std::ios::failbit); dumpAST(*ast, buf);
dumpAST(*ast, oss); return buf.str();
return oss.str();
} }
/// Tests for external dictionaries DDL parser /// Tests for external dictionaries DDL parser

View File

@ -3,6 +3,7 @@
#include <Parsers/ParserQueryWithOutput.h> #include <Parsers/ParserQueryWithOutput.h>
#include <Parsers/parseQuery.h> #include <Parsers/parseQuery.h>
#include <Parsers/formatAST.h> #include <Parsers/formatAST.h>
#include <IO/WriteBufferFromOStream.h>
int main(int, char **) int main(int, char **)
@ -25,7 +26,8 @@ try
ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0, 0); ASTPtr ast = parseQuery(parser, input.data(), input.data() + input.size(), "", 0, 0);
std::cout << "Success." << std::endl; std::cout << "Success." << std::endl;
formatAST(*ast, std::cerr); WriteBufferFromOStream out(std::cerr, 4096);
formatAST(*ast, out);
std::cout << std::endl; std::cout << std::endl;
return 0; return 0;

View File

@ -1693,13 +1693,12 @@ std::vector<MergeTreeMutationStatus> ReplicatedMergeTreeQueue::getMutationsStatu
for (const MutationCommand & command : entry.commands) for (const MutationCommand & command : entry.commands)
{ {
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); formatAST(*command.ast, buf, false, true);
formatAST(*command.ast, ss, false, true);
result.push_back(MergeTreeMutationStatus result.push_back(MergeTreeMutationStatus
{ {
entry.znode_name, entry.znode_name,
ss.str(), buf.str(),
entry.create_time, entry.create_time,
entry.block_numbers, entry.block_numbers,
parts_to_mutate, parts_to_mutate,

View File

@ -18,10 +18,9 @@ static String formattedAST(const ASTPtr & ast)
{ {
if (!ast) if (!ast)
return ""; return "";
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); formatAST(*ast, buf, false, true);
formatAST(*ast, ss, false, true); return buf.str();
return ss.str();
} }
ReplicatedMergeTreeTableMetadata::ReplicatedMergeTreeTableMetadata(const MergeTreeData & data, const StorageMetadataPtr & metadata_snapshot) ReplicatedMergeTreeTableMetadata::ReplicatedMergeTreeTableMetadata(const MergeTreeData & data, const StorageMetadataPtr & metadata_snapshot)

View File

@ -126,10 +126,9 @@ std::shared_ptr<ASTAlterCommandList> MutationCommands::ast() const
void MutationCommands::writeText(WriteBuffer & out) const void MutationCommands::writeText(WriteBuffer & out) const
{ {
std::stringstream commands_ss; WriteBufferFromOwnString commands_buf;
commands_ss.exceptions(std::ios::failbit); formatAST(*ast(), commands_buf, /* hilite = */ false, /* one_line = */ true);
formatAST(*ast(), commands_ss, /* hilite = */ false, /* one_line = */ true); out << escape << commands_buf.str();
out << escape << commands_ss.str();
} }
void MutationCommands::readText(ReadBuffer & in) void MutationCommands::readText(ReadBuffer & in)

View File

@ -538,13 +538,12 @@ std::vector<MergeTreeMutationStatus> StorageMergeTree::getMutationsStatus() cons
for (const MutationCommand & command : entry.commands) for (const MutationCommand & command : entry.commands)
{ {
std::stringstream ss; WriteBufferFromOwnString buf;
ss.exceptions(std::ios::failbit); formatAST(*command.ast, buf, false, true);
formatAST(*command.ast, ss, false, true);
result.push_back(MergeTreeMutationStatus result.push_back(MergeTreeMutationStatus
{ {
entry.file_name, entry.file_name,
ss.str(), buf.str(),
entry.create_time, entry.create_time,
block_numbers_map, block_numbers_map,
parts_to_do_names, parts_to_do_names,

View File

@ -220,8 +220,7 @@ String transformQueryForExternalDatabase(
ASTPtr select_ptr = select; ASTPtr select_ptr = select;
dropAliases(select_ptr); dropAliases(select_ptr);
std::stringstream out; WriteBufferFromOwnString out;
out.exceptions(std::ios::failbit);
IAST::FormatSettings settings(out, true); IAST::FormatSettings settings(out, true);
settings.identifier_quoting_style = identifier_quoting_style; settings.identifier_quoting_style = identifier_quoting_style;
settings.always_quote_identifiers = identifier_quoting_style != IdentifierQuotingStyle::None; settings.always_quote_identifiers = identifier_quoting_style != IdentifierQuotingStyle::None;

View File

@ -16,6 +16,7 @@
#include <Parsers/ParserQueryWithOutput.h> #include <Parsers/ParserQueryWithOutput.h>
#include <Parsers/formatAST.h> #include <Parsers/formatAST.h>
#include <Parsers/parseQuery.h> #include <Parsers/parseQuery.h>
#include <IO/WriteBufferFromString.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
@ -828,9 +829,9 @@ FuncRet arithmeticFunc(DB::ASTPtr ch, std::map<std::string, Column> & columns)
FuncRet r(ret_type, ""); FuncRet r(ret_type, "");
if (no_indent) if (no_indent)
{ {
std::ostringstream ss; DB::WriteBufferFromOwnString buf;
formatAST(*ch, ss); formatAST(*ch, buf);
r.value = ss.str(); r.value = buf.str();
} }
return r; return r;
} }
@ -990,10 +991,10 @@ FuncRet simpleFunc(DB::ASTPtr ch, std::map<std::string, Column> & columns)
{ {
if (no_indent) if (no_indent)
{ {
std::ostringstream ss; DB::WriteBufferFromOwnString buf;
formatAST(*ch, ss); formatAST(*ch, buf);
auto r = func_to_return_type[boost::algorithm::to_lower_copy(x->name)]; auto r = func_to_return_type[boost::algorithm::to_lower_copy(x->name)];
r.value = ss.str(); r.value = buf.str();
return r; return r;
} }
return func_to_return_type[boost::algorithm::to_lower_copy(x->name)]; return func_to_return_type[boost::algorithm::to_lower_copy(x->name)];
@ -1003,11 +1004,11 @@ FuncRet simpleFunc(DB::ASTPtr ch, std::map<std::string, Column> & columns)
{ {
if (no_indent) if (no_indent)
{ {
std::ostringstream ss; DB::WriteBufferFromOwnString buf;
formatAST(*ch, ss); formatAST(*ch, buf);
return FuncRet( return FuncRet(
func_to_param_type[boost::algorithm::to_lower_copy(x->name)], func_to_param_type[boost::algorithm::to_lower_copy(x->name)],
ss.str()); buf.str());
} }
return FuncRet( return FuncRet(
func_to_param_type[boost::algorithm::to_lower_copy(x->name)], func_to_param_type[boost::algorithm::to_lower_copy(x->name)],