1
1
#include " yql_pq_provider_impl.h"
2
2
3
3
#include < ydb/library/yql/core/expr_nodes/yql_expr_nodes.h>
4
+ #include < ydb/library/yql/core/yql_opt_utils.h>
4
5
#include < ydb/library/yql/core/yql_type_helpers.h>
5
6
#include < ydb/library/yql/providers/common/provider/yql_data_provider_impl.h>
6
7
#include < ydb/library/yql/providers/common/provider/yql_provider_names.h>
@@ -30,22 +31,20 @@ namespace {
30
31
}
31
32
};
32
33
33
- std::unordered_set<TString> GetUsedMetadataFields (const TCoExtractMembers& extract) {
34
- std::unordered_set<TString> usedMetadataFields;
35
- for (const auto extractMember : extract.Members ()) {
36
- if (FindPqMetaFieldDescriptorBySysColumn (extractMember.StringValue ())) {
37
- usedMetadataFields.emplace (extractMember.StringValue ());
38
- }
34
+ std::unordered_set<TString> GetUsedColumnNames (const TCoExtractMembers& extractMembers) {
35
+ std::unordered_set<TString> usedColumnNames;
36
+ for (const auto & member : extractMembers.Members ()) {
37
+ usedColumnNames.emplace (member.StringValue ());
39
38
}
40
39
41
- return usedMetadataFields ;
40
+ return usedColumnNames ;
42
41
}
43
42
44
- TVector<TCoNameValueTuple> DropUnusedMetadata (const TPqTopic& pqTopic, const std::unordered_set<TString>& usedMetadataFields ) {
43
+ TVector<TCoNameValueTuple> DropUnusedMetadata (const TPqTopic& pqTopic, const std::unordered_set<TString>& usedColumnNames ) {
45
44
TVector<TCoNameValueTuple> newSourceMetadata;
46
45
for (auto metadataItem : pqTopic.Metadata ()) {
47
46
auto metadataName = metadataItem.Cast <TCoNameValueTuple>().Value ().Maybe <TCoAtom>().Cast ().StringValue ();
48
- if (usedMetadataFields .contains (metadataName)) {
47
+ if (FindPqMetaFieldDescriptorBySysColumn (metadataName) && usedColumnNames .contains (metadataName)) {
49
48
newSourceMetadata.push_back (metadataItem);
50
49
}
51
50
}
@@ -88,18 +87,18 @@ TCoNameValueTupleList DropUnusedMetadataFromDqWrapSettings(
88
87
.Done ();
89
88
}
90
89
91
- TExprNode::TPtr DropUnusedMetadataFieldsFromRowType (
90
+ TExprNode::TPtr DropUnusedRowItems (
92
91
TPositionHandle position,
93
92
const TStructExprType* oldRowType,
94
- const std::unordered_set<TString>& usedMetadataFields ,
93
+ const std::unordered_set<TString>& usedColumnNames ,
95
94
TExprContext& ctx)
96
95
{
97
96
TVector<const TItemExprType*> newFields;
98
97
newFields.reserve (oldRowType->GetSize ());
99
98
100
99
for (auto itemExprType : oldRowType->GetItems ()) {
101
100
const auto columnName = TString (itemExprType->GetName ());
102
- if (FindPqMetaFieldDescriptorBySysColumn (columnName) && !usedMetadataFields .contains (columnName)) {
101
+ if (!usedColumnNames .contains (columnName)) {
103
102
continue ;
104
103
}
105
104
@@ -109,14 +108,14 @@ TExprNode::TPtr DropUnusedMetadataFieldsFromRowType(
109
108
return ExpandType (position, *ctx.MakeType <TStructExprType>(newFields), ctx);
110
109
}
111
110
112
- TExprNode::TPtr DropUnusedMetadataFieldsFromColumns (
111
+ TExprNode::TPtr DropUnusedColumns (
113
112
TExprBase oldColumns,
114
- const std::unordered_set<TString>& usedMetadataFields ,
113
+ const std::unordered_set<TString>& usedColumnNames ,
115
114
TExprContext& ctx)
116
115
{
117
116
TExprNode::TListType res;
118
117
for (const auto & column : oldColumns.Cast <TCoAtomList>()) {
119
- if (FindPqMetaFieldDescriptorBySysColumn (column. StringValue ()) && !usedMetadataFields .contains (column.StringValue ())) {
118
+ if (!usedColumnNames .contains (column.StringValue ())) {
120
119
continue ;
121
120
}
122
121
@@ -160,57 +159,68 @@ class TPqLogicalOptProposalTransformer : public TOptimizeTransformerBase {
160
159
}*/
161
160
162
161
TMaybeNode<TExprBase> ExtractMembersOverDqWrap (TExprBase node, TExprContext& ctx) const {
163
- const auto & extract = node.Cast <TCoExtractMembers>();
164
- const auto & input = extract.Input ();
165
- const auto dqSourceWrap = input.Maybe <TDqSourceWrap>();
166
- const auto dqPqTopicSource = dqSourceWrap.Input ().Maybe <TDqPqTopicSource>();
167
- const auto pqTopic = dqPqTopicSource.Topic ().Maybe <TPqTopic>();
168
- if (!pqTopic) {
162
+ const auto & extractMembers = node.Cast <TCoExtractMembers>();
163
+ const auto & extractMembersInput = extractMembers.Input ();
164
+ const auto & maybeDqSourceWrap = extractMembersInput.Maybe <TDqSourceWrap>();
165
+ if (!maybeDqSourceWrap) {
166
+ return node;
167
+ }
168
+
169
+ const auto & dqSourceWrap = maybeDqSourceWrap.Cast ();
170
+ if (dqSourceWrap.DataSource ().Category () != PqProviderName) {
171
+ return node;
172
+ }
173
+
174
+ const auto & maybeDqPqTopicSource = dqSourceWrap.Input ().Maybe <TDqPqTopicSource>();
175
+ if (!maybeDqPqTopicSource) {
169
176
return node;
170
177
}
171
178
172
- const auto usedMetadataFields = GetUsedMetadataFields (extract);
173
- const auto newSourceMetadata = DropUnusedMetadata (pqTopic.Cast (), usedMetadataFields);
174
- if (newSourceMetadata.size () == pqTopic.Metadata ().Cast ().Size ()) {
179
+ const auto & dqPqTopicSource = maybeDqPqTopicSource.Cast ();
180
+ const auto & pqTopic = dqPqTopicSource.Topic ();
181
+
182
+ auto usedColumnNames = GetUsedColumnNames (extractMembers);
183
+ const TStructExprType* inputRowType = pqTopic.RowSpec ().Ref ().GetTypeAnn ()->Cast <TTypeExprType>()->GetType ()->Cast <TStructExprType>();
184
+ const TStructExprType* outputRowType = node.Ref ().GetTypeAnn ()->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
185
+ if (outputRowType->GetSize () == 0 && inputRowType->GetSize () > 0 ) {
186
+ auto item = GetLightColumn (*inputRowType);
187
+ YQL_ENSURE (item);
188
+ YQL_ENSURE (usedColumnNames.insert (TString (item->GetName ())).second );
189
+ }
190
+
191
+ const auto oldRowType = pqTopic.Ref ().GetTypeAnn ()->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
192
+ if (oldRowType->GetSize () == usedColumnNames.size ()) {
175
193
return node;
176
194
}
177
195
178
- const auto oldRowType = pqTopic.Ref ().GetTypeAnn ()
179
- ->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
196
+ const auto & newSourceMetadata = DropUnusedMetadata (pqTopic, usedColumnNames);
180
197
181
- auto newPqTopicSource = Build<TDqPqTopicSource>(ctx, node .Pos ())
182
- .InitFrom (dqPqTopicSource. Cast () )
198
+ const TExprNode::TPtr newPqTopicSource = Build<TDqPqTopicSource>(ctx, dqPqTopicSource .Pos ())
199
+ .InitFrom (dqPqTopicSource)
183
200
.Topic <TPqTopic>()
184
- .InitFrom (pqTopic. Cast () )
201
+ .InitFrom (pqTopic)
185
202
.Metadata ().Add (newSourceMetadata).Build ()
186
- .Build ();
187
-
188
- if (dqPqTopicSource.Columns ()) {
189
- auto newColumns = DropUnusedMetadataFieldsFromColumns (
190
- dqPqTopicSource.Columns ().Cast (),
191
- usedMetadataFields,
192
- ctx);
193
- newPqTopicSource.Columns (newColumns);
194
- }
203
+ .RowSpec (DropUnusedRowItems (pqTopic.RowSpec ().Pos (), inputRowType, usedColumnNames, ctx))
204
+ .Build ()
205
+ .Columns (DropUnusedColumns (dqPqTopicSource.Columns (), usedColumnNames, ctx))
206
+ .Done ()
207
+ .Ptr ();
195
208
196
- const auto newDqSourceWrap = Build<TDqSourceWrap>(ctx, node.Pos ())
197
- .InitFrom (dqSourceWrap.Cast ())
198
- .Input (newPqTopicSource.Done ())
199
- .Settings (DropUnusedMetadataFromDqWrapSettings (
200
- dqSourceWrap.Cast (),
201
- newSourceMetadata,
202
- ctx))
203
- .RowType (DropUnusedMetadataFieldsFromRowType (
204
- node.Pos (),
205
- oldRowType,
206
- usedMetadataFields,
207
- ctx))
209
+ const TExprNode::TPtr newDqSourceWrap = Build<TDqSourceWrap>(ctx, dqSourceWrap.Pos ())
210
+ .InitFrom (dqSourceWrap)
211
+ .Input (newPqTopicSource)
212
+ .Settings (DropUnusedMetadataFromDqWrapSettings (dqSourceWrap, newSourceMetadata, ctx))
213
+ .RowType (DropUnusedRowItems (dqSourceWrap.RowType ().Pos (), oldRowType, usedColumnNames, ctx))
208
214
.Done ()
209
215
.Ptr ();
210
216
217
+ if (outputRowType->GetSize () == usedColumnNames.size ()) {
218
+ return newDqSourceWrap;
219
+ }
220
+
211
221
return Build<TCoExtractMembers>(ctx, node.Pos ())
212
- .InitFrom (extract )
213
- .Input (ctx.ReplaceNode (input .Ptr (), dqSourceWrap.Ref (), newDqSourceWrap))
222
+ .InitFrom (extractMembers )
223
+ .Input (ctx.ReplaceNode (extractMembersInput .Ptr (), dqSourceWrap.Ref (), newDqSourceWrap))
214
224
.Done ();
215
225
}
216
226
0 commit comments