aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvvvv <vvvv@ydb.tech>2022-08-23 15:34:37 +0300
committervvvv <vvvv@ydb.tech>2022-08-23 15:34:37 +0300
commita3069b94849161bcc18b5d63a6be2d0aaec4fa23 (patch)
tree8eb98f94116d0336e357c993202da112bd2cf398
parent0f22555742cda02eee3aa1517b5033e98712c83d (diff)
downloadydb-a3069b94849161bcc18b5d63a6be2d0aaec4fa23.tar.gz
better handling of extra sort columns in case of aggregations
-rw-r--r--ydb/library/yql/core/common_opt/yql_co_pgselect.cpp39
1 files changed, 30 insertions, 9 deletions
diff --git a/ydb/library/yql/core/common_opt/yql_co_pgselect.cpp b/ydb/library/yql/core/common_opt/yql_co_pgselect.cpp
index 8c49169aaa..907b816fb3 100644
--- a/ydb/library/yql/core/common_opt/yql_co_pgselect.cpp
+++ b/ydb/library/yql/core/common_opt/yql_co_pgselect.cpp
@@ -2368,21 +2368,42 @@ TExprNode::TPtr AddExtColumns(const TExprNode::TPtr& lambda, const TExprNode::TP
.Build();
}
-void BuildExtraSortColumns(const TExprNode::TPtr& extraSortColumns, size_t aggIndexBegin, size_t aggIndexEnd, TVector<TString>& list) {
+void BuildExtraSortColumns(bool hasAggregation, const TExprNode::TPtr& groupBy, const TExprNode::TPtr& extraSortColumns, size_t aggIndexBegin, size_t aggIndexEnd, TVector<TString>& list) {
if (extraSortColumns) {
+ TVector<TString> extra;
for (const auto& x : extraSortColumns->Tail().Children()) {
for (const auto& y : x->Children()) {
- list.push_back(TString(y->Content()));
+ extra.push_back(TString(y->Content()));
}
}
- }
+
+ if (!hasAggregation) {
+ // all extra columns
+ list.insert(list.end(), extra.begin(), extra.end());
+ } else if (groupBy) {
+ // keep only keys
+ THashSet<TString> keys;
+ for (const auto& group : groupBy->Tail().Children()) {
+ const auto& lambda = group->Tail();
+ YQL_ENSURE(lambda.IsLambda());
+ YQL_ENSURE(lambda.Tail().IsCallable("Member"));
+ keys.insert(TString(lambda.Tail().Tail().Content()));
+ }
+
+ for (const auto& x : extra) {
+ if (keys.contains(x)) {
+ list.push_back(x);
+ }
+ }
+ }
+ }
for (auto aggIndex = aggIndexBegin; aggIndex < aggIndexEnd; ++aggIndex) {
list.push_back("_yql_agg_" + ToString(aggIndex));
}
}
-TExprNode::TPtr AddExtraSortColumns(const TExprNode::TPtr& lambda, const TExprNode::TPtr& extraSortColumns,
+TExprNode::TPtr AddExtraSortColumns(const TExprNode::TPtr& lambda, bool hasAggregation, const TExprNode::TPtr& groupBy, const TExprNode::TPtr& extraSortColumns,
size_t aggIndexBegin, size_t aggIndexEnd, TExprContext& ctx) {
return ctx.Builder(lambda->Pos())
.Lambda()
@@ -2399,7 +2420,7 @@ TExprNode::TPtr AddExtraSortColumns(const TExprNode::TPtr& lambda, const TExprNo
.Callable(1, "AsStruct")
.Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder & {
TVector<TString> list;
- BuildExtraSortColumns(extraSortColumns, aggIndexBegin, aggIndexEnd, list);
+ BuildExtraSortColumns(hasAggregation, groupBy, extraSortColumns, aggIndexBegin, aggIndexEnd, list);
for (ui32 i = 0; i < list.size(); ++i) {
TStringBuf from = list[i];
from.SkipPrefix("_yql_extra_");
@@ -2421,7 +2442,7 @@ TExprNode::TPtr AddExtraSortColumns(const TExprNode::TPtr& lambda, const TExprNo
.Build();
}
-TExprNode::TPtr RemoveExtraSortColumns(const TExprNode::TPtr& list, const TExprNode::TPtr& extraSortColumns,
+TExprNode::TPtr RemoveExtraSortColumns(const TExprNode::TPtr& list, bool hasAggregation, const TExprNode::TPtr& groupBy, const TExprNode::TPtr& extraSortColumns,
size_t aggIndexBegin, size_t aggIndexEnd, const TVector<TString>& sublinkColumns, TExprContext& ctx) {
return ctx.Builder(list->Pos())
.Callable("OrderedMap")
@@ -2433,7 +2454,7 @@ TExprNode::TPtr RemoveExtraSortColumns(const TExprNode::TPtr& list, const TExprN
.List(1)
.Do([&](TExprNodeBuilder& parent) -> TExprNodeBuilder & {
TVector<TString> list;
- BuildExtraSortColumns(extraSortColumns, aggIndexBegin, aggIndexEnd, list);
+ BuildExtraSortColumns(hasAggregation, groupBy, extraSortColumns, aggIndexBegin, aggIndexEnd, list);
list.insert(list.end(), sublinkColumns.begin(), sublinkColumns.end());
for (ui32 i = 0; i < list.size(); ++i) {
parent.Atom(i, list[i]);
@@ -2835,7 +2856,7 @@ TExprNode::TPtr ExpandPgSelectImpl(const TExprNode::TPtr& node, TExprContext& ct
bool hasExtraSortColumns = (extraSortColumns || (aggsSizeBeforeSort < aggs.size()));
if (hasExtraSortColumns) {
YQL_ENSURE(!distinctAll && !distinctOn);
- projectionLambda = AddExtraSortColumns(projectionLambda, extraSortColumns, aggsSizeBeforeSort, aggs.size(), ctx);
+ projectionLambda = AddExtraSortColumns(projectionLambda, (!aggs.empty() || groupBy), groupBy, extraSortColumns, aggsSizeBeforeSort, aggs.size(), ctx);
}
list = ctx.Builder(node->Pos())
@@ -2867,7 +2888,7 @@ TExprNode::TPtr ExpandPgSelectImpl(const TExprNode::TPtr& node, TExprContext& ct
}
if (hasExtraSortColumns) {
- list = RemoveExtraSortColumns(list, extraSortColumns, aggsSizeBeforeSort, aggs.size(), sublinkColumns, ctx);
+ list = RemoveExtraSortColumns(list, (!aggs.empty() || groupBy), groupBy, extraSortColumns, aggsSizeBeforeSort, aggs.size(), sublinkColumns, ctx);
}
}