Skip to content

Commit 50c0046

Browse files
committed
Add ident format for JOIN
1 parent 2588297 commit 50c0046

9 files changed

Lines changed: 51 additions & 18 deletions

parser/format.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,15 +1770,18 @@ func writeJoinSQL(formatter *Formatter, expr Expr) {
17701770

17711771
if len(joinExpr.Modifiers) == 0 {
17721772
formatter.WriteByte(',')
1773+
formatter.WriteExpr(joinExpr.Left)
17731774
} else {
1774-
formatter.WriteByte(whitespace)
1775+
formatter.Break()
17751776
formatter.WriteString(strings.Join(joinExpr.Modifiers, " "))
1776-
formatter.WriteByte(whitespace)
1777-
}
1778-
formatter.WriteExpr(joinExpr.Left)
1779-
if joinExpr.Constraints != nil {
1780-
formatter.WriteByte(whitespace)
1781-
formatter.WriteExpr(joinExpr.Constraints)
1777+
formatter.Indent()
1778+
formatter.Break()
1779+
formatter.WriteExpr(joinExpr.Left)
1780+
if joinExpr.Constraints != nil {
1781+
formatter.WriteByte(whitespace)
1782+
formatter.WriteExpr(joinExpr.Constraints)
1783+
}
1784+
formatter.Dedent()
17821785
}
17831786
if joinExpr.Right != nil {
17841787
writeJoinSQL(formatter, joinExpr.Right)

parser/testdata/query/format/beautify/select_table_alias_without_keyword.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ SELECT t1.Timestamp FROM my_table t1 INNER JOIN my_other_table t2 ON t1.a=t2.b
44
-- Beautify SQL:
55
SELECT
66
t1.Timestamp
7-
FROM my_table AS t1 INNER JOIN my_other_table AS t2 ON t1.a = t2.b;
7+
FROM my_table AS t1
8+
INNER JOIN
9+
my_other_table AS t2 ON t1.a = t2.b;

parser/testdata/query/format/beautify/select_with_join_only.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ SELECT * FROM "t1" JOIN "t2" ON true
55
-- Beautify SQL:
66
SELECT
77
*
8-
FROM "t1" JOIN "t2" ON true;
8+
FROM "t1"
9+
JOIN
10+
"t2" ON true;

parser/testdata/query/format/beautify/select_with_left_join.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ WITH
2020
2 AS value)
2121
SELECT
2222
*
23-
FROM t1 LEFT JOIN t2 ON true;
23+
FROM t1
24+
LEFT JOIN
25+
t2 ON true;

parser/testdata/query/format/beautify/select_with_multi_array_and_inner_join.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ WHERE value != '';
1111
-- Beautify SQL:
1212
SELECT
1313
JSONExtractString(t3.props, 'value') AS value
14-
FROM t1 ARRAY JOIN JSONExtractArrayRaw(t1.props, 'arr1') AS a1 INNER JOIN t2 ON t2.id = JSONExtractString(a1, 'id') ARRAY JOIN JSONExtractArrayRaw(t2.props, 'arr2') AS a2 INNER JOIN t3 ON t3.id = JSONExtractString(a2, 'id')
14+
FROM t1
15+
ARRAY JOIN
16+
JSONExtractArrayRaw(t1.props, 'arr1') AS a1
17+
INNER JOIN
18+
t2 ON t2.id = JSONExtractString(a1, 'id')
19+
ARRAY JOIN
20+
JSONExtractArrayRaw(t2.props, 'arr2') AS a2
21+
INNER JOIN
22+
t3 ON t3.id = JSONExtractString(a2, 'id')
1523
WHERE
1624
value != '';

parser/testdata/query/format/beautify/select_with_multi_array_join.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ FROM t1
1313
SELECT
1414
v,
1515
j
16-
FROM t1 ARRAY JOIN JSONExtractArrayRaw(a) AS j ARRAY JOIN array(JSONExtractString(j, 'x'), JSONExtractString(j, 'y')) AS v;
16+
FROM t1
17+
ARRAY JOIN
18+
JSONExtractArrayRaw(a) AS j
19+
ARRAY JOIN
20+
array(JSONExtractString(j, 'x'), JSONExtractString(j, 'y')) AS v;

parser/testdata/query/format/beautify/select_with_multi_join.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ SELECT
3030
t1.value AS value1,
3131
t2.value AS value2,
3232
t3.value AS value3
33-
FROM t1 JOIN t2 ON true JOIN t3 JOIN t4 ON true JOIN t5;
33+
FROM t1
34+
JOIN
35+
t2 ON true
36+
JOIN
37+
t3
38+
JOIN
39+
t4 ON true
40+
JOIN
41+
t5;

parser/testdata/query/format/beautify/select_with_number_field.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ SELECT
66
foo,
77
bar.1,
88
foo.2
9-
FROM foo ARRAY JOIN m AS bar;
9+
FROM foo
10+
ARRAY JOIN
11+
m AS bar;

parser/testdata/query/format/beautify/select_with_settings_additional_table_filters.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ SELECT
3939
FROM (SELECT
4040
number
4141
FROM system.numbers
42-
LIMIT 5) AS f ANY LEFT JOIN (SELECT
43-
x,
44-
y
45-
FROM table_1) AS s ON f.number = s.x
42+
LIMIT 5) AS f
43+
ANY LEFT JOIN
44+
(SELECT
45+
x,
46+
y
47+
FROM table_1) AS s ON f.number = s.x
4648
SETTINGS additional_table_filters={'system.numbers': 'number != 3', 'table_1': 'x != 2'};

0 commit comments

Comments
 (0)