summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/format
diff options
context:
space:
mode:
authorvitya-smirnov <[email protected]>2025-07-09 14:16:01 +0300
committervitya-smirnov <[email protected]>2025-07-09 14:38:43 +0300
commit59c3167c208900bb714ff5cc477476e3ab14f4f0 (patch)
tree043a4d70e21ede0da9df08806fe5a8c0d656e201 /yql/essentials/sql/v1/format
parent40a6e6e096b3533857b70c79a8ac9820655c7bf3 (diff)
YQL-17269: Support UNION/INTERSECT/EXCEPT combinations
Introduce `UNION` and `INTERSECT/EXCEPT` grammar rules for precedence. Rewrote `Build` procedure into `BuildStmt`, `BuildUnion`, `BuildIntersection`. Added tests, modify format. It took a lot of time trying to adapt the existing `Build` procedure. The I noticed that the logic for `union` and `intersection` is different, since `union` groups arguments into bundles, but `intersection` is a strictly binary operation. commit_hash:70008ae3c2603364b6dfbeeb189fdc7f5237433d
Diffstat (limited to 'yql/essentials/sql/v1/format')
-rw-r--r--yql/essentials/sql/v1/format/sql_format.cpp28
-rw-r--r--yql/essentials/sql/v1/format/sql_format_ut.h12
2 files changed, 38 insertions, 2 deletions
diff --git a/yql/essentials/sql/v1/format/sql_format.cpp b/yql/essentials/sql/v1/format/sql_format.cpp
index 4979a75d4e8..e45adc280d8 100644
--- a/yql/essentials/sql/v1/format/sql_format.cpp
+++ b/yql/essentials/sql/v1/format/sql_format.cpp
@@ -833,10 +833,21 @@ private:
void VisitSelect(const TRule_select_stmt& msg) {
NewLine();
+ Visit(msg.GetRule_select_stmt_intersect1());
+ for (const auto& block : msg.GetBlock2()) {
+ NewLine();
+ Visit(block.GetRule_union_op1());
+ NewLine();
+ Visit(block.GetRule_select_stmt_intersect2());
+ }
+ }
+
+ void VisitSelectIntersect(const TRule_select_stmt_intersect& msg) {
+ NewLine();
Visit(msg.GetRule_select_kind_parenthesis1());
for (const auto& block : msg.GetBlock2()) {
NewLine();
- Visit(block.GetRule_select_op1());
+ Visit(block.GetRule_intersect_op1());
NewLine();
Visit(block.GetRule_select_kind_parenthesis2());
}
@@ -844,10 +855,21 @@ private:
void VisitSelectUnparenthesized(const TRule_select_unparenthesized_stmt& msg) {
NewLine();
+ Visit(msg.GetRule_select_unparenthesized_stmt_intersect1());
+ for (const auto& block : msg.GetBlock2()) {
+ NewLine();
+ Visit(block.GetRule_union_op1());
+ NewLine();
+ Visit(block.GetRule_select_stmt_intersect2());
+ }
+ }
+
+ void VisitSelectUnparenthesizedIntersect(const TRule_select_unparenthesized_stmt_intersect& msg) {
+ NewLine();
Visit(msg.GetRule_select_kind_partial1());
for (const auto& block : msg.GetBlock2()) {
NewLine();
- Visit(block.GetRule_select_op1());
+ Visit(block.GetRule_intersect_op1());
NewLine();
Visit(block.GetRule_select_kind_parenthesis2());
}
@@ -3012,7 +3034,9 @@ TStaticData::TStaticData()
{TRule_pragma_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitPragma)},
{TRule_select_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelect)},
+ {TRule_select_stmt_intersect::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectIntersect)},
{TRule_select_unparenthesized_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectUnparenthesized)},
+ {TRule_select_unparenthesized_stmt_intersect::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitSelectUnparenthesizedIntersect)},
{TRule_named_nodes_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitNamedNodes)},
{TRule_create_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitCreateTable)},
{TRule_drop_table_stmt::GetDescriptor(), MakePrettyFunctor(&TPrettyVisitor::VisitDropTable)},
diff --git a/yql/essentials/sql/v1/format/sql_format_ut.h b/yql/essentials/sql/v1/format/sql_format_ut.h
index 35e95634e63..7f77d456a15 100644
--- a/yql/essentials/sql/v1/format/sql_format_ut.h
+++ b/yql/essentials/sql/v1/format/sql_format_ut.h
@@ -1576,6 +1576,18 @@ Y_UNIT_TEST(Except) {
setup.Run(cases);
}
+Y_UNIT_TEST(UnionIntersectExcept) {
+ TCases cases = {
+ {"select 1 union select 2 intersect select 3 except select 4",
+ "SELECT\n\t1\nUNION\nSELECT\n\t2\nINTERSECT\nSELECT\n\t3\nEXCEPT\nSELECT\n\t4\n;\n"},
+ {"select 1 intersect select 2 union select 3 except select 4",
+ "SELECT\n\t1\nINTERSECT\nSELECT\n\t2\nUNION\nSELECT\n\t3\nEXCEPT\nSELECT\n\t4\n;\n"},
+ };
+
+ TSetup setup;
+ setup.Run(cases);
+}
+
Y_UNIT_TEST(Comment) {
TCases cases = {
{"/*\nmulti\nline\ncomment\n*/\npragma foo = \"true\";\npragma bar = \"1\"",