@@ -2229,11 +2229,11 @@ TStatus AnnotateOpEmptySource(const TExprNode::TPtr& input, TExprContext& ctx) {
2229
2229
return TStatus::Ok;
2230
2230
}
2231
2231
2232
- TStatus AnnotateOpMapElement (const TExprNode::TPtr& input, TExprContext& ctx) {
2233
- const TTypeAnnotationNode* inputType = input->ChildPtr (TKqpOpMapElement ::idx_Input)->GetTypeAnn ();
2232
+ TStatus AnnotateOpMapElementLambda (const TExprNode::TPtr& input, TExprContext& ctx) {
2233
+ const TTypeAnnotationNode* inputType = input->ChildPtr (TKqpOpMapElementLambda ::idx_Input)->GetTypeAnn ();
2234
2234
const TTypeAnnotationNode* itemType = inputType->Cast <TListExprType>()->GetItemType ();
2235
2235
2236
- auto & lambda = input->ChildRef (TKqpOpMapElement ::idx_Lambda);
2236
+ auto & lambda = input->ChildRef (TKqpOpMapElementLambda ::idx_Lambda);
2237
2237
if (!UpdateLambdaAllArgumentsTypes (lambda, {itemType}, ctx)) {
2238
2238
return IGraphTransformer::TStatus::Error;
2239
2239
}
@@ -2243,44 +2243,92 @@ TStatus AnnotateOpMapElement(const TExprNode::TPtr& input, TExprContext& ctx) {
2243
2243
return IGraphTransformer::TStatus::Repeat;
2244
2244
}
2245
2245
2246
- input->SetTypeAnn (lambdaType);
2246
+ auto variable = input->ChildRef (TKqpOpMapElementLambda::idx_Variable);
2247
+ auto res = ctx.MakeType <TItemExprType>(variable->Content (), lambdaType);
2248
+
2249
+ input->SetTypeAnn (res);
2247
2250
return TStatus::Ok;
2248
2251
}
2249
2252
2250
- TStatus AnnotateOpMap (const TExprNode::TPtr& input, TExprContext& ctx, TTypeAnnotationContext& typesCtx) {
2253
+ TStatus AnnotateOpMapElementRename (const TExprNode::TPtr& input, TExprContext& ctx) {
2254
+ Y_UNUSED (ctx);
2251
2255
2252
- const TTypeAnnotationNode* inputType = input->ChildPtr (TKqpOpMap::idx_Input)->GetTypeAnn ();
2253
- // YQL_CLOG(TRACE, CoreDq) << "Annotating OpMap, input type:" << *inputType;
2256
+ const TTypeAnnotationNode* inputType = input->ChildPtr (TKqpOpMapElementLambda::idx_Input)->GetTypeAnn ();
2257
+ auto structType = inputType->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
2258
+ auto typeItems = structType->GetItems ();
2254
2259
2255
- const TTypeAnnotationNode* itemType = inputType->Cast <TListExprType>()->GetItemType ();
2256
- // YQL_CLOG(TRACE, CoreDq) << "item type:" << *itemType;
2260
+ auto from = input->ChildRef (TKqpOpMapElementRename::idx_From);
2261
+ auto typeIt = std::find_if (typeItems.begin (), typeItems.end (), [&from](const TItemExprType* t){
2262
+ return from->Content () == t->GetName ();
2263
+ });
2264
+
2265
+ if (typeIt==typeItems.end ()) {
2266
+ YQL_CLOG (TRACE, CoreDq) << " Trying to find " << from->Content () << " in " << *(TTypeAnnotationNode*)structType;
2267
+ }
2268
+
2269
+ Y_ENSURE (typeIt!=typeItems.end ());
2270
+
2271
+ auto variable = input->ChildRef (TKqpOpMapElementRename::idx_Variable);
2272
+ auto res = ctx.MakeType <TItemExprType>(variable->Content (), (*typeIt)->GetItemType ());
2273
+
2274
+ input->SetTypeAnn (res);
2275
+ return TStatus::Ok;
2276
+ }
2277
+
2278
+ TStatus AnnotateOpMap (const TExprNode::TPtr& input, TExprContext& ctx, TTypeAnnotationContext& typesCtx) {
2279
+ Y_UNUSED (typesCtx);
2257
2280
2258
2281
TVector<const TItemExprType*> structItemTypes;
2259
2282
2283
+ if (input->ChildrenSize () <= TKqpOpMap::idx_Project) {
2284
+ const TTypeAnnotationNode* inputType = input->ChildPtr (TKqpOpMap::idx_Input)->GetTypeAnn ();
2285
+ auto structType = inputType->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
2286
+
2287
+ for (auto t : structType->GetItems ()) {
2288
+ structItemTypes.push_back (t);
2289
+ }
2290
+ }
2291
+
2260
2292
for (size_t idx = 0 ; idx < input->ChildPtr (TKqpOpMap::idx_MapElements)->ChildrenSize (); idx++) {
2261
2293
auto & element = input->ChildPtr (TKqpOpMap::idx_MapElements)->ChildRef (idx);
2262
- auto variable = element->ChildPtr (TKqpOpMapElement::idx_Variable);
2263
- auto & lambda = element->ChildRef (TKqpOpMapElement::idx_Lambda);
2294
+ auto type = element->GetTypeAnn ();
2295
+ structItemTypes.push_back ((TItemExprType*)type);
2296
+ }
2264
2297
2265
- if (!UpdateLambdaAllArgumentsTypes (lambda, {itemType}, ctx)) {
2266
- return IGraphTransformer::TStatus::Error;
2267
- }
2298
+ auto resultItemType = ctx.MakeType <TStructExprType>(structItemTypes);
2299
+ const TTypeAnnotationNode* resultAnn = ctx.MakeType <TListExprType>(resultItemType);
2268
2300
2269
- auto lambdaType = lambda->GetTypeAnn ();
2270
- if (!lambdaType) {
2271
- return IGraphTransformer::TStatus::Repeat;
2301
+ input->SetTypeAnn (resultAnn);
2302
+ // typesCtx.ExpectedTypes[input->UniqueId()] = resultAnn;
2303
+
2304
+ YQL_CLOG (TRACE, CoreDq) << " Type annotation for OpMap done: " << *resultAnn;
2305
+
2306
+ return TStatus::Ok;
2307
+ }
2308
+
2309
+ TStatus AnnotateOpProject (const TExprNode::TPtr& input, TExprContext& ctx) {
2310
+ auto structType = input->ChildPtr (TKqpOpProject::idx_Input)->GetTypeAnn ()->Cast <TListExprType>()->GetItemType ()->Cast <TStructExprType>();
2311
+ TVector<const TItemExprType*> structItemTypes;
2312
+ auto typeItems = structType->GetItems ();
2313
+
2314
+ for (size_t i=0 ; i<input->ChildPtr (TKqpOpProject::idx_ProjectList)->ChildrenSize (); i++) {
2315
+ auto proj = input->ChildPtr (TKqpOpProject::idx_ProjectList)->ChildRef (i);
2316
+ auto typeItemIt = std::find_if (typeItems.begin (), typeItems.end (), [&proj](const TItemExprType* t){
2317
+ return proj->Content () == t->GetName ();
2318
+ });
2319
+ if (typeItemIt == typeItems.end ()) {
2320
+ continue ;
2272
2321
}
2273
2322
2274
- structItemTypes.push_back (ctx. MakeType <TItemExprType>(variable-> Content (),lambdaType) );
2323
+ structItemTypes.push_back (*typeItemIt );
2275
2324
}
2276
2325
2277
2326
auto resultItemType = ctx.MakeType <TStructExprType>(structItemTypes);
2278
2327
const TTypeAnnotationNode* resultAnn = ctx.MakeType <TListExprType>(resultItemType);
2279
2328
2280
2329
input->SetTypeAnn (resultAnn);
2281
- typesCtx.ExpectedTypes [input->UniqueId ()] = resultAnn;
2282
2330
2283
- YQL_CLOG (TRACE, CoreDq) << " Type annotation for OpMap done: " << *resultAnn;
2331
+ YQL_CLOG (TRACE, CoreDq) << " Type annotation for OpProject done: " << *resultAnn;
2284
2332
2285
2333
return TStatus::Ok;
2286
2334
}
@@ -2536,14 +2584,22 @@ TAutoPtr<IGraphTransformer> CreateKqpTypeAnnotationTransformer(const TString& cl
2536
2584
return AnnotateOpEmptySource (input, ctx);
2537
2585
}
2538
2586
2539
- if (TKqpOpMapElement::Match (input.Get ())) {
2540
- return AnnotateOpMapElement (input, ctx);
2587
+ if (TKqpOpMapElementLambda::Match (input.Get ())) {
2588
+ return AnnotateOpMapElementLambda (input, ctx);
2589
+ }
2590
+
2591
+ if (TKqpOpMapElementRename::Match (input.Get ())) {
2592
+ return AnnotateOpMapElementRename (input, ctx);
2541
2593
}
2542
2594
2543
2595
if (TKqpOpMap::Match (input.Get ())) {
2544
2596
return AnnotateOpMap (input, ctx, typesCtx);
2545
2597
}
2546
2598
2599
+ if (TKqpOpProject::Match (input.Get ())) {
2600
+ return AnnotateOpProject (input, ctx);
2601
+ }
2602
+
2547
2603
if (TKqpOpFilter::Match (input.Get ())) {
2548
2604
return AnnotateOpFilter (input, ctx);
2549
2605
}
0 commit comments