Skip to content

Commit 7f97280

Browse files
author
zhvv117
committed
remove redundant semicolons, always add semicolon after last statement in lambdas and actions
commit_hash:d2b19f44d3c6f6e89f03c2c27e897d2b55e6174e
1 parent 73e8da4 commit 7f97280

File tree

136 files changed

+457
-386
lines changed

Some content is hidden

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

136 files changed

+457
-386
lines changed

yql/essentials/sql/v1/format/sql_format.cpp

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,39 @@ TTokenIterator SkipWSOrComment(TTokenIterator curr, TTokenIterator end) {
4343
return curr;
4444
}
4545

46+
TTokenIterator SkipWSOrCommentBackward(TTokenIterator curr, TTokenIterator begin) {
47+
while (curr != begin && (curr->Name == "WS" || curr->Name == "COMMENT")) {
48+
--curr;
49+
}
50+
return curr;
51+
}
52+
53+
void SkipForValidate(
54+
TTokenIterator& in,
55+
TTokenIterator& out,
56+
const TParsedTokenList& query,
57+
const TParsedTokenList& formattedQuery
58+
) {
59+
in = SkipWS(in, query.end());
60+
out = SkipWS(out, formattedQuery.end());
61+
62+
while (
63+
in != query.end() && in->Name == "SEMICOLON" &&
64+
(out == formattedQuery.end() || out->Name != "SEMICOLON") &&
65+
in != query.begin() && IsIn({"SEMICOLON", "LBRACE_CURLY", "AS"}, SkipWSOrCommentBackward(in - 1, query.begin())->Name)
66+
) {
67+
in = SkipWS(++in, query.end());
68+
}
69+
70+
auto inSkippedComments = SkipWSOrComment(in, query.end());
71+
if (
72+
out != formattedQuery.end() && out->Name == "SEMICOLON" &&
73+
inSkippedComments != query.end() && IsIn({"RBRACE_CURLY", "END"}, inSkippedComments->Name)
74+
) {
75+
out = SkipWS(++out, formattedQuery.end());
76+
}
77+
}
78+
4679
TParsedToken TransformTokenForValidate(TParsedToken token) {
4780
if (token.Name == "EQUALS2") {
4881
token.Name = "EQUALS";
@@ -61,8 +94,7 @@ bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQu
6194
auto outEnd = formattedQuery.end();
6295

6396
while (in != inEnd && out != outEnd) {
64-
in = SkipWS(in, inEnd);
65-
out = SkipWS(out, outEnd);
97+
SkipForValidate(in, out, query, formattedQuery);
6698
if (in != inEnd && out != outEnd) {
6799
auto inToken = TransformTokenForValidate(*in);
68100
auto outToken = TransformTokenForValidate(*out);
@@ -82,8 +114,7 @@ bool Validate(const TParsedTokenList& query, const TParsedTokenList& formattedQu
82114
++out;
83115
}
84116
}
85-
in = SkipWS(in, inEnd);
86-
out = SkipWS(out, outEnd);
117+
SkipForValidate(in, out, query, formattedQuery);
87118
return in == inEnd && out == outEnd;
88119
}
89120

@@ -777,20 +808,34 @@ friend struct TStaticData;
777808
}
778809
}
779810

811+
template <typename T>
812+
void SkipSemicolons(const ::google::protobuf::RepeatedPtrField<T>& field, bool printOne = false) {
813+
for (const auto& m : field) {
814+
if (printOne) {
815+
Visit(m);
816+
printOne = false;
817+
} else {
818+
++TokenIndex;
819+
}
820+
}
821+
if (printOne) {
822+
Out(';');
823+
}
824+
}
825+
780826
void VisitDefineActionOrSubqueryBody(const TRule_define_action_or_subquery_body& msg) {
781-
VisitRepeated(msg.GetBlock1());
827+
SkipSemicolons(msg.GetBlock1());
782828
if (msg.HasBlock2()) {
783829
const auto& b = msg.GetBlock2();
784830
Visit(b.GetRule_sql_stmt_core1());
785831
for (auto block : b.GetBlock2()) {
786-
VisitRepeated(block.GetBlock1());
832+
SkipSemicolons(block.GetBlock1(), /* printOne = */ true);
787833
if (!IsSimpleStatement(block.GetRule_sql_stmt_core2()).GetOrElse(false)) {
788834
Out('\n');
789835
}
790836
Visit(block.GetRule_sql_stmt_core2());
791837
}
792-
793-
VisitRepeated(b.GetBlock3());
838+
SkipSemicolons(b.GetBlock3(), /* printOne = */ true);
794839
}
795840
}
796841

@@ -2344,18 +2389,19 @@ friend struct TStaticData;
23442389
void VisitLambdaBody(const TRule_lambda_body& msg) {
23452390
PushCurrentIndent();
23462391
NewLine();
2347-
VisitRepeated(msg.GetBlock1());
2392+
SkipSemicolons(msg.GetBlock1());
23482393
for (const auto& block : msg.GetBlock2()) {
2349-
Visit(block);
2394+
Visit(block.GetRule_lambda_stmt1());
2395+
SkipSemicolons(block.GetBlock2(), /* printOne = */ true);
23502396
NewLine();
23512397
}
23522398

23532399
Visit(msg.GetToken3());
23542400
ExprLineIndent = CurrentIndent;
23552401

23562402
Visit(msg.GetRule_expr4());
2357-
VisitRepeated(msg.GetBlock5());
23582403

2404+
SkipSemicolons(msg.GetBlock5(), /* printOne = */ true);
23592405
ExprLineIndent = 0;
23602406

23612407
PopCurrentIndent();
@@ -3068,7 +3114,7 @@ class TSqlFormatter : public NSQLFormat::ISqlFormatter {
30683114
return false;
30693115
}
30703116

3071-
if (!Validate(stmtFormattedTokens, stmtTokens)) {
3117+
if (!Validate(stmtTokens, stmtFormattedTokens)) {
30723118
issues.AddIssue(NYql::TIssue({}, TStringBuilder() << "Validation failed: " << currentQuery.Quote() << " != " << currentFormattedQuery.Quote()));
30733119
return false;
30743120
}

yql/essentials/sql/v1/format/sql_format_ut.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,25 @@ Y_UNIT_TEST(DefineActionOrSubquery) {
557557
"VALUES\n\t\t\t(1)\n\t\t;\n\tEND DEFINE;\n\n\t"
558558
"DEFINE SUBQUERY $c() AS\n\t\tSELECT\n\t\t\t1\n\t\t;\n\t"
559559
"END DEFINE;\n\tDO\n\t\t$b()\n\t;\n\n\tPROCESS $c();\nEND DEFINE;\n"},
560+
{"define action $foo($bar) as;"
561+
"$a = 10;; "
562+
"$b = 20;;; "
563+
"$c = $a + $b "
564+
"end define",
565+
"DEFINE ACTION $foo($bar) AS\n\t"
566+
"$a = 10;\n\t"
567+
"$b = 20;\n\t"
568+
"$c = $a + $b;\n"
569+
"END DEFINE;\n"},
570+
{"define subquery $s() as;"
571+
"select * from $t1 "
572+
"union all select * from $t2 "
573+
"end define",
574+
"DEFINE SUBQUERY $s() AS\n\t"
575+
"SELECT\n\t\t*\n\tFROM\n\t\t$t1\n\t"
576+
"UNION ALL\n\t"
577+
"SELECT\n\t\t*\n\tFROM\n\t\t$t2\n\t;\n"
578+
"END DEFINE;\n"},
560579
};
561580

562581
TSetup setup;
@@ -872,9 +891,11 @@ Y_UNIT_TEST(CompositeTypesAndQuestions) {
872891
Y_UNIT_TEST(Lambda) {
873892
TCases cases = {
874893
{"$f=($a,$b)->{$x=$a+$b;return $a*$x};$g=($a,$b?)->($a+$b??0);select $f(10,4),$g(1,2);",
875-
"$f = ($a, $b) -> {\n\t$x = $a + $b;\n\tRETURN $a * $x\n};\n\n"
894+
"$f = ($a, $b) -> {\n\t$x = $a + $b;\n\tRETURN $a * $x;\n};\n\n"
876895
"$g = ($a, $b?) -> ($a + $b ?? 0);\n\n"
877896
"SELECT\n\t$f(10, 4),\n\t$g(1, 2)\n;\n"},
897+
{"$f=($arg)->{;$a=10;;$b=20;;;RETURN $a+$b}",
898+
"$f = ($arg) -> {\n\t$a = 10;\n\t$b = 20;\n\tRETURN $a + $b;\n};\n"},
878899
};
879900

880901
TSetup setup;
@@ -1444,7 +1465,7 @@ Y_UNIT_TEST(ExistsExpr) {
14441465
Y_UNIT_TEST(LambdaInsideExpr) {
14451466
TCases cases = {
14461467
{"SELECT ListMap(AsList(1,2),($x)->{return $x+1});",
1447-
"SELECT\n\tListMap(\n\t\tAsList(1, 2), ($x) -> {\n\t\t\tRETURN $x + 1\n\t\t}\n\t)\n;\n"},
1468+
"SELECT\n\tListMap(\n\t\tAsList(1, 2), ($x) -> {\n\t\t\tRETURN $x + 1;\n\t\t}\n\t)\n;\n"},
14481469
};
14491470

14501471
TSetup setup;

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-combine_subqueries_with_table_param_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $combineQueries = ($query, $list) -> {
88
($world) -> {
99
$queries = ListMap(
1010
$list, ($arg) -> {
11-
RETURN FuncCode("Apply", QuoteCode($query), $world, ReprCode($arg))
11+
RETURN FuncCode("Apply", QuoteCode($query), $world, ReprCode($arg));
1212
}
1313
);
1414
RETURN FuncCode("UnionAll", $queries);

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-dep_world_quote_code_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $combineQueries = ($query, $list) -> {
88
($world) -> {
99
$queries = ListMap(
1010
$list, ($arg) -> {
11-
RETURN FuncCode("Apply", QuoteCode($query), $world, ReprCode($arg))
11+
RETURN FuncCode("Apply", QuoteCode($query), $world, ReprCode($arg));
1212
}
1313
);
1414
RETURN FuncCode("Extend", $queries);

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-eval_code_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SELECT
77
$inc = EvaluateCode(
88
LambdaCode(
99
($x) -> {
10-
RETURN FuncCode("+", $x, FuncCode("Int32", AtomCode("1")))
10+
RETURN FuncCode("+", $x, FuncCode("Int32", AtomCode("1")));
1111
}
1212
)
1313
);

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-eval_filter_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ USE plato;
55
$arg1 = "" || "";
66

77
$arg2 = ($_item) -> {
8-
RETURN TRUE
8+
RETURN TRUE;
99
};
1010

1111
$arg3 = "" || "";

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-eval_for_/formatted.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ EVALUATE FOR $i IN ListMap(ListFromRange(0, 0), $f) DO
2828

2929
EVALUATE FOR $i IN Yql::Map(
3030
1 / 1, ($x) -> {
31-
RETURN AsList($x)
31+
RETURN AsList($x);
3232
}
3333
) DO
3434
$action1($i)
3535
;
3636

3737
EVALUATE FOR $i IN Yql::Map(
3838
1 / 0, ($x) -> {
39-
RETURN AsList($x)
39+
RETURN AsList($x);
4040
}
4141
) DO
4242
$action1($i)

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-eval_unresolved_type_arg_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ $myAddSuffix = ($row, $value) -> {
2121
FuncCode("Member", $r, AtomCode($i.Name)),
2222
ReprCode($value)
2323
)
24-
)
24+
);
2525
}
2626
)
2727
);

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-evaluate_match_type_/formatted.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ USE plato;
44

55
$keep_only_last = ($row) -> {
66
$members = ListFilter(StructMembers($row), ($x) -> (FIND($x, "key") IS NOT NULL));
7-
RETURN ChooseMembers($row, $members)
7+
RETURN ChooseMembers($row, $members);
88
};
99

1010
SELECT

yql/essentials/tests/sql/sql2yql/canondata/test_sql_format.test_action-mixed_eval_typeof_world1_/formatted.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ $force_remove_members = ($struct, $to_remove) -> {
99
($st) -> {
1010
$to_keep = ListFlatMap(
1111
StructTypeComponents(TypeHandle(TypeOf($struct))), ($x) -> {
12-
RETURN IF($x.Name NOT IN $to_remove, $x.Name)
12+
RETURN IF($x.Name NOT IN $to_remove, $x.Name);
1313
}
1414
);
1515
RETURN FuncCode(
1616
"AsStruct",
1717
ListMap(
1818
$to_keep,
1919
($x) -> {
20-
RETURN ListCode(AtomCode($x), FuncCode("Member", $st, AtomCode($x)))
20+
RETURN ListCode(AtomCode($x), FuncCode("Member", $st, AtomCode($x)));
2121
}
2222
)
23-
)
23+
);
2424
}
2525
)
2626
);
27-
RETURN $remover($struct)
27+
RETURN $remover($struct);
2828
};
2929

3030
DEFINE ACTION $func($input, $output) AS

0 commit comments

Comments
 (0)