Avoid printing SETTINGS twice when formatting a query

This commit is contained in:
Raúl Marín 2022-07-06 13:39:27 +02:00
parent 82f6d18249
commit dfc20266c7
6 changed files with 40 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#include <Parsers/ASTQueryWithOutput.h>
#include <Parsers/ASTSetQuery.h>
namespace DB
{
@ -40,7 +41,7 @@ void ASTQueryWithOutput::formatImpl(const FormatSettings & s, FormatState & stat
format->formatImpl(s, state, frame);
}
if (settings_ast)
if (settings_ast && !assert_cast<ASTSetQuery *>(settings_ast.get())->is_clone)
{
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "SETTINGS " << (s.hilite ? hilite_none : "");
settings_ast->formatImpl(s, state, frame);

View File

@ -192,7 +192,7 @@ void ASTSelectQuery::formatImpl(const FormatSettings & s, FormatState & state, F
limitOffset()->formatImpl(s, state, frame);
}
if (settings())
if (settings() && !assert_cast<ASTSetQuery *>(settings().get())->is_clone)
{
s.ostr << (s.hilite ? hilite_keyword : "") << s.nl_or_ws << indent_str << "SETTINGS " << (s.hilite ? hilite_none : "");
settings()->formatImpl(s, state, frame);

View File

@ -13,6 +13,7 @@ class ASTSetQuery : public IAST
{
public:
bool is_standalone = true; /// If false, this AST is a part of another query, such as SELECT.
bool is_clone = false; /// If true, this AST is a clone from other part of the query and should not be printed in format()
SettingsChanges changes;

View File

@ -127,7 +127,9 @@ bool ParserQueryWithOutput::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
// Pass them manually, to apply in InterpreterSelectQuery::initSettings()
if (query->as<ASTSelectWithUnionQuery>())
{
QueryWithOutputSettingsPushDownVisitor::Data data{query_with_output.settings_ast};
auto settings = query_with_output.settings_ast->clone();
assert_cast<ASTSetQuery *>(settings.get())->is_clone = true;
QueryWithOutputSettingsPushDownVisitor::Data data{settings};
QueryWithOutputSettingsPushDownVisitor(data).visit(query);
}
}

View File

@ -0,0 +1,17 @@
SELECT 1
FORMAT CSV
SETTINGS max_execution_time = 0.001
SELECT 1
SETTINGS max_execution_time = 0.001
FORMAT CSV
SELECT 1
UNION ALL
SELECT 2
FORMAT CSV
SETTINGS max_execution_time = 0.001
SELECT 1
SETTINGS max_threads = 1
UNION ALL
SELECT 2
SETTINGS max_execution_time = 2
FORMAT `Null`

View File

@ -0,0 +1,16 @@
#!/usr/bin/env bash
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CUR_DIR"/../shell_config.sh
set -e
format="$CLICKHOUSE_FORMAT"
echo "select 1 format CSV settings max_execution_time = 0.001" | $format
echo "select 1 settings max_execution_time = 0.001 format CSV" | $format
echo "select 1 UNION ALL Select 2 format CSV settings max_execution_time = 0.001" | $format
# I don't think having multiple settings makes sense, but it's supported so test that it still works
echo "select 1 settings max_threads=1 UNION ALL select 2 settings max_execution_time=2 format Null" | $format