diff options
author | thelex <thelex@yandex-team.com> | 2024-05-15 16:36:07 +0300 |
---|---|---|
committer | thelex <thelex@yandex-team.com> | 2024-05-15 16:56:36 +0300 |
commit | ef02042bb8fc43776cf79941029af1e4181db0f9 (patch) | |
tree | 393d9b5427ad8ef722e234f134f7b75114adbbec | |
parent | 1c3cea8ba893a97b04ddc33ad381f17d2bf05ef8 (diff) | |
download | ydb-ef02042bb8fc43776cf79941029af1e4181db0f9.tar.gz |
YT: Add join expression to query builder
ef0cf75a2639c295e45bcb66c3af27554dc3586c
-rw-r--r-- | yt/yt/client/query_client/query_builder.cpp | 18 | ||||
-rw-r--r-- | yt/yt/client/query_client/query_builder.h | 16 | ||||
-rw-r--r-- | yt/yt/client/unittests/query_builder_ut.cpp | 5 |
3 files changed, 39 insertions, 0 deletions
diff --git a/yt/yt/client/query_client/query_builder.cpp b/yt/yt/client/query_client/query_builder.cpp index 1608cd32c0..f13e787361 100644 --- a/yt/yt/client/query_client/query_builder.cpp +++ b/yt/yt/client/query_client/query_builder.cpp @@ -97,6 +97,19 @@ void TQueryBuilder::SetLimit(i64 limit) Limit_ = limit; } +void TQueryBuilder::AddJoinExpression( + TString table, + TString alias, + TString onExpression, + ETableJoinType type) +{ + JoinEntries_.push_back(TJoinEntry{ + std::move(table), + std::move(alias), + std::move(onExpression), + type}); +} + TString TQueryBuilder::Build() { std::vector<TString> parts; @@ -112,6 +125,11 @@ TString TQueryBuilder::Build() } parts.push_back(Format("FROM [%v]", *Source_)); + for (const auto& join : JoinEntries_) { + TStringBuf joinType = join.Type == ETableJoinType::Inner ? "JOIN" : "LEFT JOIN"; + parts.push_back(Format("%v [%v] AS [%v] ON %v", joinType, join.Table, join.Alias, join.OnExpression)); + } + if (!WhereConjuncts_.empty()) { parts.push_back("WHERE"); parts.push_back(JoinSeq(" AND ", Parenthesize(WhereConjuncts_))); diff --git a/yt/yt/client/query_client/query_builder.h b/yt/yt/client/query_client/query_builder.h index c965337576..95b87d1766 100644 --- a/yt/yt/client/query_client/query_builder.h +++ b/yt/yt/client/query_client/query_builder.h @@ -13,6 +13,11 @@ DEFINE_ENUM(EOrderByDirection, (Descending) ); +DEFINE_ENUM(ETableJoinType, + (Inner) + (Left) +); + //////////////////////////////////////////////////////////////////////////////// class TQueryBuilder @@ -36,6 +41,8 @@ public: void AddOrderByAscendingExpression(TString expression); void AddOrderByDescendingExpression(TString expression); + void AddJoinExpression(TString table, TString alias, TString onExpression, ETableJoinType type); + void SetLimit(i64 limit); TString Build(); @@ -53,6 +60,14 @@ private: std::optional<EOrderByDirection> Direction; }; + struct TJoinEntry + { + TString Table; + TString Alias; + TString OnExpression; + ETableJoinType Type; + }; + private: std::optional<TString> Source_; std::vector<TEntryWithAlias> SelectEntries_; @@ -60,6 +75,7 @@ private: std::vector<TOrderByEntry> OrderByEntries_; std::vector<TEntryWithAlias> GroupByEntries_; std::vector<TString> HavingConjuncts_; + std::vector<TJoinEntry> JoinEntries_; std::optional<i64> Limit_; private: diff --git a/yt/yt/client/unittests/query_builder_ut.cpp b/yt/yt/client/unittests/query_builder_ut.cpp index e47027b23f..535dfdf244 100644 --- a/yt/yt/client/unittests/query_builder_ut.cpp +++ b/yt/yt/client/unittests/query_builder_ut.cpp @@ -30,6 +30,9 @@ TEST(TQueryBuilderTest, Simple) b.AddHavingConjunct("group_expr > 42"); b.AddHavingConjunct("group_expr < 420"); + b.AddJoinExpression("table1", "lookup1", "(idx) = (lookup1.idx)", ETableJoinType::Inner); + b.AddJoinExpression("table2", "lookup2", "(idx) = (lookup2.idx)", ETableJoinType::Left); + b.SetLimit(43); EXPECT_EQ(xIndex, 0); @@ -39,6 +42,8 @@ TEST(TQueryBuilderTest, Simple) EXPECT_EQ(b.Build(), "(x), (y) AS y_alias, (z) " "FROM [//t] " + "JOIN [table1] AS [lookup1] ON (idx) = (lookup1.idx) " + "LEFT JOIN [table2] AS [lookup2] ON (idx) = (lookup2.idx) " "WHERE (x > y_alias) AND (y = 177 OR y % 2 = 0) " "GROUP BY (x + y * z) AS group_expr, (x - 1) " "HAVING (group_expr > 42) AND (group_expr < 420) " |