Skip to content

Commit 16a408e

Browse files
authored
fix sql format (#385)
1 parent 8066de7 commit 16a408e

File tree

76 files changed

+1415
-1549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1415
-1549
lines changed

src/Common/tests/gtest_ident.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ String convertQueryToFormat(String query,bool one_line)
2121

2222
TEST(indent, stream_name)
2323
{
24-
//test format 'one_line'=false;
24+
/// test format 'one_line'=false;
2525
String query("with it1 as (with it2 as (select * from numbers(10)) select * from it2) select user.id from user,it1 where it1.number=user.id");
26-
String expected_ans("WITH it1 AS\n (\n WITH it2 AS\n (\n SELECT \n *\n FROM \n numbers(10)\n )\n SELECT \n *\n FROM \n it2\n )\nSELECT \n user.id\nFROM \n user,it1\nWHERE \n it1.number = user.id");
26+
String expected_ans("WITH it1 AS\n (\n WITH it2 AS\n (\n SELECT\n *\n FROM\n numbers(10)\n )\n SELECT\n *\n FROM\n it2\n )\nSELECT\n user.id\nFROM\n user, it1\nWHERE\n it1.number = user.id");
2727
String ans = convertQueryToFormat(query,false);
2828
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
2929

30-
//test DISTINCT JOIN WHERE PARTITION BY GROUP BY HAVING ORDER BY LIMIT EMIT SETTINGS
30+
/// test DISTINCT JOIN WHERE PARTITION BY GROUP BY HAVING ORDER BY LIMIT EMIT SETTINGS
3131
query = "with it1 as (select * from numbers(10)) select distinct user.id from user join it1 on user.id=it1.number group by user.id having id>0 order by id limit 10 emit last 1s settings query_mode='stream'";
32-
expected_ans = "WITH it1 AS\n (\n SELECT \n *\n FROM \n numbers(10)\n )\nSELECT DISTINCT \n user.id\nFROM \n user\nINNER JOIN it1 ON user.id = it1.number\nGROUP BY \n user.id\nHAVING \n id > 0\nORDER BY \n id ASC\nLIMIT 10\nEMIT STREAM LAST 1s\nSETTINGS \n query_mode = 'stream'";
32+
expected_ans = "WITH it1 AS\n (\n SELECT\n *\n FROM\n numbers(10)\n )\nSELECT DISTINCT\n user.id\nFROM\n user\nINNER JOIN it1 ON user.id = it1.number\nGROUP BY\n user.id\nHAVING\n id > 0\nORDER BY\n id ASC\nLIMIT 10\nEMIT STREAM LAST 1s\nSETTINGS\n query_mode = 'stream'";
3333
ans = convertQueryToFormat(query,false);
3434
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
3535

36-
//test format 'one_line'=true;
36+
/// test format 'one_line'=true;
3737
query = "with it1 as (with it2 as (select * from numbers(10)) select * from it2) select user.id from user,it1 where it1.number=user.id";
38-
expected_ans = "WITH it1 AS (WITH it2 AS (SELECT * FROM numbers(10)) SELECT * FROM it2) SELECT user.id FROM user,it1 WHERE it1.number = user.id";
38+
expected_ans = "WITH it1 AS (WITH it2 AS (SELECT * FROM numbers(10)) SELECT * FROM it2) SELECT user.id FROM user, it1 WHERE it1.number = user.id";
3939
ans = convertQueryToFormat(query,true);
4040
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
4141

@@ -45,3 +45,33 @@ TEST(indent, stream_name)
4545
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
4646

4747
}
48+
49+
TEST(indent, complex_query)
50+
{
51+
/// Covering: WITH SELECT DISTINCT FROM JOIN WHERE PARTITION BY SHUFFLE BY GROUP BY HAVING ORDER BY LIMIT EMIT SETTINGS UNION
52+
/// test format 'one_line'=false;
53+
String query = "with it1 as (select * from numbers(10)), it2 as (select * from it1) select distinct user.id from user join it1 on user.id=it1.number array join it2.number as number where number > 1 or number < 5 partition by user.id group by user.id having id>0 order by id limit 10 emit last 1s settings query_mode='stream' union select * from it2";
54+
String expected_ans = """WITH it1 AS\n (\n SELECT\n *\n FROM\n numbers(10)\n ), it2 AS\n (\n SELECT\n *\n FROM\n it1\n )\nSELECT DISTINCT\n user.id\nFROM\n user\nINNER JOIN it1 ON user.id = it1.number\nARRAY JOIN it2.number AS number\nWHERE\n (number > 1) OR (number < 5)\nPARTITION BY\n user.id\nGROUP BY\n user.id\nHAVING\n id > 0\nORDER BY\n id ASC\nLIMIT 10\nEMIT STREAM LAST 1s\nSETTINGS\n query_mode = 'stream'\nUNION\nSELECT\n *\nFROM\n it2";
55+
String ans = convertQueryToFormat(query,false);
56+
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
57+
58+
/// test format 'one_line'=true;
59+
expected_ans = "WITH it1 AS (SELECT * FROM numbers(10)), it2 AS (SELECT * FROM it1) SELECT DISTINCT user.id FROM user INNER JOIN it1 ON user.id = it1.number ARRAY JOIN it2.number AS number WHERE (number > 1) OR (number < 5) PARTITION BY user.id GROUP BY user.id HAVING id > 0 ORDER BY id ASC LIMIT 10 EMIT STREAM LAST 1s SETTINGS query_mode = 'stream' UNION SELECT * FROM it2";
60+
ans = convertQueryToFormat(query,true);
61+
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
62+
}
63+
64+
TEST(indent, join_query)
65+
{
66+
/// Covering: JOIN, INNER JOIN, LEFT JOIN, ASOF JOIN, LEFT ASOF JOIN, INNER ASOF JOIN, LATEST JOIN, LEFT LATEST JOIN, INNER LATEST JOIN, ARRAY JOIN
67+
/// test format 'one_line'=false;
68+
String query = "with tmp as (select * from t1 join t2 using(id) inner join t3 on t1.id=t3.id left join t4 on t1.id = t4.id asof join t as t5 on t1.id=t5.id and t1.time < t5.time array join t1.time as time, t2.time as time2 inner asof join (select * from t6) as t6 on t1.id=t2.id and t1.time < t6.time left asof join t7 on t1.id=t7.id and t1.time > t7.time latest join t8 using(id) inner latest join t9 on t1.id=t9.id left latest join cte as t10 on t1.id=t10.id) select * from tmp";
69+
String expected_ans = "WITH tmp AS\n (\n SELECT\n *\n FROM\n t1\n INNER JOIN t2 USING (id)\n INNER JOIN t3 ON t1.id = t3.id\n LEFT JOIN t4 ON t1.id = t4.id\n ASOF INNER JOIN t AS t5 ON (t1.id = t5.id) AND (t1.time < t5.time)\n ARRAY JOIN t1.time AS time, t2.time AS time2\n ASOF INNER JOIN (\n SELECT\n *\n FROM\n t6\n ) AS t6 ON (t1.id = t2.id) AND (t1.time < t6.time)\n ASOF LEFT JOIN t7 ON (t1.id = t7.id) AND (t1.time > t7.time)\n LATEST INNER JOIN t8 USING (id)\n LATEST INNER JOIN t9 ON t1.id = t9.id\n LATEST LEFT JOIN cte AS t10 ON t1.id = t10.id\n )\nSELECT\n *\nFROM\n tmp";
70+
String ans = convertQueryToFormat(query,false);
71+
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
72+
73+
/// test format 'one_line'=true;
74+
expected_ans = "WITH tmp AS (SELECT * FROM t1 INNER JOIN t2 USING (id) INNER JOIN t3 ON t1.id = t3.id LEFT JOIN t4 ON t1.id = t4.id ASOF INNER JOIN t AS t5 ON (t1.id = t5.id) AND (t1.time < t5.time) ARRAY JOIN t1.time AS time, t2.time AS time2 ASOF INNER JOIN (SELECT * FROM t6) AS t6 ON (t1.id = t2.id) AND (t1.time < t6.time) ASOF LEFT JOIN t7 ON (t1.id = t7.id) AND (t1.time > t7.time) LATEST INNER JOIN t8 USING (id) LATEST INNER JOIN t9 ON t1.id = t9.id LATEST LEFT JOIN cte AS t10 ON t1.id = t10.id) SELECT * FROM tmp";
75+
ans = convertQueryToFormat(query,true);
76+
EXPECT_STREQ(expected_ans.c_str(), ans.c_str());
77+
}

src/Interpreters/Streaming/tests/gtest_eliminate_subquery.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TEST(EliminateSubquery, OptimizedQuery)
6565
EXPECT_EQ(
6666
optimizeSubquery("SELECT count(a.productid) FROM (SELECT a.*, b.* FROM access1 as a, db2.product_info as b WHERE (a.productid = "
6767
"b.productid))"),
68-
"SELECT count(a.productid) FROM access1 AS a,db2.product_info AS b WHERE a.productid = b.productid");
68+
"SELECT count(a.productid) FROM access1 AS a, db2.product_info AS b WHERE a.productid = b.productid");
6969
EXPECT_EQ(
7070
optimizeSubquery("SELECT count(*) FROM (SELECT SIGN FROM (SELECT SIGN, query FROM tablea WHERE query_id='mock-1') WHERE "
7171
"query = "
@@ -76,7 +76,7 @@ TEST(EliminateSubquery, OptimizedQuery)
7676
optimizeSubquery("SELECT sum(a.price), sum(b.sale) FROM (SELECT a.*, b.* FROM access1 as a, product_info as b WHERE (a.productid "
7777
"= "
7878
"b.productid)) GROUP BY a.name, b.sign"),
79-
"SELECT sum(a.price), sum(b.sale) FROM access1 AS a,product_info AS b WHERE a.productid = b.productid GROUP BY a.name, b.sign");
79+
"SELECT sum(a.price), sum(b.sale) FROM access1 AS a, product_info AS b WHERE a.productid = b.productid GROUP BY a.name, b.sign");
8080
EXPECT_EQ(
8181
optimizeSubquery("SELECT sum(a.price), sum(b.sale) FROM (SELECT a.*, b.* FROM access1 as a JOIN product_info as b ON a.productid "
8282
"= "
@@ -97,11 +97,11 @@ TEST(EliminateSubquery, OptimizedQuery)
9797
EXPECT_EQ(
9898
optimizeSubquery("SELECT count(b.code) FROM (SELECT a.code, b.code FROM product_info as a, product_info as b WHERE (a.productid = "
9999
"b.productid))"),
100-
"SELECT count(b.code) FROM product_info AS a,product_info AS b WHERE a.productid = b.productid");
100+
"SELECT count(b.code) FROM product_info AS a, product_info AS b WHERE a.productid = b.productid");
101101
EXPECT_EQ(
102102
optimizeSubquery("SELECT count(a.code) FROM (SELECT a.code, b.code FROM product_info as a, product_info as b WHERE (a.productid = "
103103
"b.productid))"),
104-
"SELECT count(a.code) FROM product_info AS a,product_info AS b WHERE a.productid = b.productid");
104+
"SELECT count(a.code) FROM product_info AS a, product_info AS b WHERE a.productid = b.productid");
105105
EXPECT_EQ(
106106
optimizeSubquery("SELECT * FROM (SELECT _raw AS c, t._raw FROM default.frontier_integration_test AS t)"),
107107
"SELECT _raw AS c, t._raw FROM default.frontier_integration_test AS t");
@@ -187,19 +187,19 @@ TEST(EliminateSubquery, FailedOptimizedQuery)
187187
EXPECT_EQ(
188188
optimizeSubquery("SELECT count(t.code) FROM (SELECT a.code, b.code FROM product_info as a, product_info as b WHERE a.productid = "
189189
"b.productid) AS t"),
190-
"SELECT count(t.code) FROM (SELECT a.code, b.code FROM product_info AS a,product_info AS b WHERE a.productid = b.productid) AS t");
190+
"SELECT count(t.code) FROM (SELECT a.code, b.code FROM product_info AS a, product_info AS b WHERE a.productid = b.productid) AS t");
191191
EXPECT_EQ(
192192
optimizeSubquery("SELECT count(t.b.code) FROM (SELECT a.code, b.code FROM product_info as a, product_info as b WHERE a.productid = "
193193
"b.productid) AS t"),
194-
"SELECT count(t.b.code) FROM (SELECT a.code, b.code FROM product_info AS a,product_info AS b WHERE a.productid = b.productid) AS t");
194+
"SELECT count(t.b.code) FROM (SELECT a.code, b.code FROM product_info AS a, product_info AS b WHERE a.productid = b.productid) AS t");
195195
EXPECT_EQ(
196196
optimizeSubquery(
197197
"SELECT count(t.code) FROM (SELECT code, code FROM product_info as a, product_info as b WHERE a.productid = b.productid) AS t"),
198-
"SELECT count(t.code) FROM (SELECT code, code FROM product_info AS a,product_info AS b WHERE a.productid = b.productid) AS t");
198+
"SELECT count(t.code) FROM (SELECT code, code FROM product_info AS a, product_info AS b WHERE a.productid = b.productid) AS t");
199199
EXPECT_EQ(optimizeSubquery("SELECT _time FROM (SELECT name FROM price)"), "SELECT _time FROM (SELECT name FROM price)");
200200
EXPECT_EQ(
201201
optimizeSubquery(
202202
"SELECT count(code) FROM (SELECT a.code, b.code FROM product_info as a, product_info as b WHERE a.productid = b.productid)"),
203-
"SELECT count(code) FROM (SELECT a.code, b.code FROM product_info AS a,product_info AS b WHERE a.productid = b.productid)");
203+
"SELECT count(code) FROM (SELECT a.code, b.code FROM product_info AS a, product_info AS b WHERE a.productid = b.productid)");
204204
EXPECT_EQ(optimizeSubquery("SELECT b FROM (SELECT b AS a FROM product)"), "SELECT b FROM (SELECT b AS a FROM product)");
205205
}

src/Parsers/ASTAlterQuery.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,9 @@ void ASTAlterQuery::formatQueryImpl(const FormatSettings & settings, FormatState
550550
{
551551
frame.need_parens = false;
552552

553-
std::string indent_str = settings.one_line ? "" : std::string(4u * frame.indent, ' ');
553+
/// proton: starts
554+
std::string indent_str = settings.one_line ? "" : std::string(settings.indent_size * frame.indent, ' ');
555+
/// proton: ends
554556
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str;
555557

556558
switch (alter_object)
@@ -576,7 +578,7 @@ void ASTAlterQuery::formatQueryImpl(const FormatSettings & settings, FormatState
576578
settings.ostr << indent_str << backQuoteIfNeed(getDatabase());
577579
settings.ostr << ".";
578580
}
579-
settings.ostr << indent_str << backQuoteIfNeed(getTable()) << " ";
581+
settings.ostr << indent_str << backQuoteIfNeed(getTable());
580582
}
581583
else if (alter_object == AlterObjectType::DATABASE && database)
582584
{

src/Parsers/ASTCheckQuery.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ struct ASTCheckQuery : public ASTQueryWithTableAndOutput
2828
{
2929
std::string nl_or_nothing = settings.one_line ? "" : "\n";
3030

31-
std::string indent_str = settings.one_line ? "" : std::string(4 * frame.indent, ' ');
31+
/// proton: starts
32+
std::string indent_str = settings.one_line ? "" : std::string(settings.indent_size * frame.indent, ' ');
33+
/// proton: ends
3234
std::string nl_or_ws = settings.one_line ? " " : "\n";
3335

3436
settings.ostr << (settings.hilite ? hilite_keyword : "") << indent_str << "CHECK TABLE " << (settings.hilite ? hilite_none : "");

src/Parsers/ASTExpressionList.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ASTPtr ASTExpressionList::clone() const
1414

1515
void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
1616
{
17-
if (frame.expression_list_prepend_whitespace && !settings.one_line )
17+
if (frame.expression_list_prepend_whitespace)
1818
settings.ostr << ' ';
1919

2020
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
@@ -40,18 +40,19 @@ void ASTExpressionList::formatImpl(const FormatSettings & settings, FormatState
4040

4141
void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
4242
{
43-
/// proton: starts. expression list in one line
44-
std::string indent_str = " ";
43+
/// proton: starts.
44+
std::string indent_str = "\n" + std::string(settings.indent_size * (frame.indent + 1), ' ');
45+
if (frame.expression_list_always_start_on_new_line)
46+
settings.ostr << indent_str;
47+
else if (frame.expression_list_prepend_whitespace)
48+
settings.ostr << " ";
49+
50+
std::string element_indent_str
51+
= (children.size() > 1 || frame.expression_list_always_start_on_new_line) && !frame.expression_list_elements_are_always_on_one_line
52+
? indent_str
53+
: " ";
4554
/// proton: ends.
4655

47-
if (frame.expression_list_prepend_whitespace)
48-
{
49-
if (!(children.size() > 1 || frame.expression_list_always_start_on_new_line))
50-
/// proton: starts
51-
settings.ostr << "";
52-
/// proton: ends
53-
}
54-
5556
++frame.indent;
5657

5758
for (ASTs::const_iterator it = children.begin(); it != children.end(); ++it)
@@ -60,12 +61,9 @@ void ASTExpressionList::formatImplMultiline(const FormatSettings & settings, For
6061
{
6162
if (separator)
6263
settings.ostr << separator;
63-
}
6464

65-
/// proton: starts
66-
if (it != children.begin() && (children.size() > 1 || frame.expression_list_always_start_on_new_line))
67-
/// proton: ends
68-
settings.ostr << indent_str;
65+
settings.ostr << element_indent_str;
66+
}
6967

7068
FormatStateStacked frame_nested = frame;
7169
frame_nested.expression_list_always_start_on_new_line = false;

src/Parsers/ASTFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void ASTFunction::formatImplWithoutAlias(const FormatSettings & settings, Format
239239
{
240240
std::string nl_or_nothing = settings.one_line ? "" : "\n";
241241
/// proton: starts
242-
std::string indent_str = settings.one_line ? "" : std::string(2u * frame.indent, ' ');
242+
std::string indent_str = settings.one_line ? "" : std::string(settings.indent_size * frame.indent, ' ');
243243
/// proton: ends
244244
settings.ostr << (settings.hilite ? hilite_function : "") << name << "(" << nl_or_nothing;
245245
FormatStateStacked frame_nested = frame;

src/Parsers/ASTNameTypePair.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ ASTPtr ASTNameTypePair::clone() const
2424
void ASTNameTypePair::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
2525
{
2626
/// proton: starts
27-
std::string indent_str = settings.one_line ? "" : std::string(2 * frame.indent, ' ');
27+
std::string indent_str = settings.one_line ? "" : std::string(settings.indent_size * frame.indent, ' ');
2828
/// proton: ends
29-
3029
settings.ostr << indent_str << backQuoteIfNeed(name) << ' ';
3130
type->formatImpl(settings, state, frame);
3231
}

src/Parsers/ASTProjectionDeclaration.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ ASTPtr ASTProjectionDeclaration::clone() const
1818
void ASTProjectionDeclaration::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
1919
{
2020
settings.ostr << backQuoteIfNeed(name);
21-
std::string indent_str = settings.one_line ? "" : std::string(4u * frame.indent, ' ');
21+
/// proton: starts
22+
std::string indent_str = settings.one_line ? "" : std::string(settings.indent_size * frame.indent, ' ');
23+
/// proton: ends
2224
std::string nl_or_nothing = settings.one_line ? "" : "\n";
2325
settings.ostr << nl_or_nothing << indent_str << "(" << nl_or_nothing;
2426
FormatStateStacked frame_nested = frame;

src/Parsers/ASTProjectionSelectQuery.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ void ASTProjectionSelectQuery::formatImpl(const FormatSettings & s, FormatState
5151
{
5252
frame.current_select = this;
5353
frame.need_parens = false;
54-
std::string indent_str = s.one_line ? "" : std::string(4 * frame.indent, ' ');
54+
/// proton: starts
55+
std::string indent_str = s.one_line ? "" : std::string(s.indent_size * frame.indent, ' ');
56+
/// proton: ends
5557

5658
if (with())
5759
{

src/Parsers/ASTQueryWithOutput.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ void ASTQueryWithOutput::cloneOutputOptions(ASTQueryWithOutput & cloned) const
2525
void ASTQueryWithOutput::formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const
2626
{
2727
formatQueryImpl(s, state, frame);
28-
2928
/// proton: starts
30-
std::string indent_str = s.one_line ? "" : std::string(2u * frame.indent, ' ');
29+
std::string indent_str = s.one_line ? "" : std::string(s.indent_size * frame.indent, ' ');
3130
/// proton: ends
3231

3332
if (out_file)

0 commit comments

Comments
 (0)