aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authora-romanov <Anton.Romanov@ydb.tech>2022-12-07 12:02:51 +0300
committera-romanov <Anton.Romanov@ydb.tech>2022-12-07 12:02:51 +0300
commitc6801726ac1fd244024843fea3b8d4c76896090c (patch)
tree334da8de64b6b88b8265a6bfb154041967028729
parenteb48b31550cde5bfcc585dc6b7a8e8ccb8eb6f25 (diff)
downloadydb-c6801726ac1fd244024843fea3b8d4c76896090c.tar.gz
Fix cast of variant.
-rw-r--r--ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp31
1 files changed, 20 insertions, 11 deletions
diff --git a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp
index 4cefe0d2e7..9ba65645e0 100644
--- a/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp
+++ b/ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp
@@ -976,26 +976,35 @@ template <bool Strong>
TExprNode::TPtr ExpandCastOverVariant(const TExprNode::TPtr& input, TExprContext& ctx) {
YQL_CLOG(DEBUG, CorePeepHole) << "Expand " << input->Content() << " for Variant";
const auto targetType = input->GetTypeAnn();
+ const auto sourceType = input->Head().GetTypeAnn();
const auto targetUnderType = targetType->Cast<TVariantExprType>()->GetUnderlyingType();
+ const auto sourceUnderType = sourceType->Cast<TVariantExprType>()->GetUnderlyingType();
TExprNode::TListType variants, types;
switch (targetUnderType->GetKind()) {
case ETypeAnnotationKind::Tuple: {
- const auto& items = targetUnderType->Cast<TTupleExprType>()->GetItems();
- types.resize(items.size());
- variants.resize(items.size());
- for (ui32 i = 0U; i < variants.size(); ++i) {
+ const auto sourceTupleType = sourceUnderType->Cast<TTupleExprType>();
+ const auto targetTupleType = targetUnderType->Cast<TTupleExprType>();
+ const auto size = std::min(sourceTupleType->GetSize(), targetTupleType->GetSize());
+ types.resize(size);
+ variants.resize(size);
+ const auto& items = targetTupleType->GetItems();
+ for (ui32 i = 0U; i < size; ++i) {
types[i] = ExpandType(input->Tail().Pos(), *items[i], ctx);
- variants[i] = ctx.NewAtom(input->Pos(), ToString(i), TNodeFlags::Default);
+ variants[i] = ctx.NewAtom(input->Pos(), ctx.GetIndexAsString(i), TNodeFlags::Default);
}
break;
}
case ETypeAnnotationKind::Struct: {
- const auto& items = targetUnderType->Cast<TStructExprType>()->GetItems();
- types.resize(items.size());
- variants.resize(items.size());
- for (ui32 i = 0U; i < items.size(); ++i) {
- types[i] = ExpandType(input->Tail().Pos(), *items[i]->GetItemType(), ctx);
- variants[i] = ctx.NewAtom(input->Pos(), items[i]->GetName());
+ const auto sourceStructType = sourceUnderType->Cast<TStructExprType>();
+ const auto targetStructType = targetUnderType->Cast<TStructExprType>();
+ const auto size = std::min(sourceStructType->GetSize(), targetStructType->GetSize());
+ types.reserve(size);
+ variants.reserve(size);
+ for (const auto& item : sourceStructType->GetItems()) {
+ if (const auto type = targetStructType->FindItemType(item->GetName())) {
+ types.emplace_back(ExpandType(input->Tail().Pos(), *type, ctx));
+ variants.emplace_back(ctx.NewAtom(input->Pos(), item->GetName()));
+ }
}
break;
}