215162 {
215163 vector<BoundCastInfo> child_cast_info;
215164 auto &source_children = StructType::GetChildTypes(source);
215165 auto &target_children = StructType::GetChildTypes(target);
215166
215167 auto target_is_unnamed = target_children.empty() || StructType::IsUnnamed(target);
215168 auto source_is_unnamed = source_children.empty() || StructType::IsUnnamed(source);
215169
215170 auto is_unnamed = target_is_unnamed || source_is_unnamed;
215171 if (is_unnamed && source_children.size() != target_children.size()) {
215172 throw TypeMismatchException(input.query_location, source, target, "Cannot cast STRUCTs of different size");
215173 }
215174
215175 InsertionOrderPreservingMap<idx_t> target_children_map;
215176 if (!is_unnamed) {
215177 for (
idx_t i = 0; i < target_children.size(); i++) {
215178 auto &name = target_children[i].first;
215179 if (target_children_map.find(name) != target_children_map.end()) {
215180 throw NotImplementedException("Error while casting - duplicate name \"%s\" in struct", name);
215181 }
215182 target_children_map[name] = i;
215183 }
215184 }
215185
215186 vector<idx_t> source_indexes;
215187 vector<idx_t> target_indexes;
215188 vector<idx_t> target_null_indexes;
215189 bool has_any_match = is_unnamed;
215190
215191 for (
idx_t i = 0; i < source_children.size(); i++) {
215192 auto &source_child = source_children[i];
215193 auto target_idx = i;
215194
215195
215196 if (!is_unnamed) {
215197 auto target_child = target_children_map.find(source_child.first);
215198 if (target_child == target_children_map.end()) {
215199
215200 continue;
215201 }
215202 target_idx = target_child->second;
215203 target_children_map.erase(target_child);
215204 has_any_match = true;
215205 }
215206
215207 source_indexes.push_back(i);
215208 target_indexes.push_back(target_idx);
215209 auto child_cast = input.GetCastFunction(source_child.second, target_children[target_idx].second);
215210 child_cast_info.push_back(std::move(child_cast));
215211 }
215212
215213 if (!has_any_match) {
215214 throw BinderException("STRUCT to STRUCT cast must have at least one matching member");
215215 }
215216
215217
215218
215219 for (const auto &target_child : target_children_map) {
215220 target_null_indexes.push_back(target_child.second);
215221 }
215222
215223 return make_uniq<StructBoundCastData>(std::move(child_cast_info), target, std::move(source_indexes),
215224 std::move(target_indexes), std::move(target_null_indexes));
215225}