Raw selection vector passed in (not merged with any other selection vectors).
138482 {
138483 D_ASSERT(vector_p.GetType().id() == LogicalTypeId::UNION);
138484
138485
138486 const SelectionVector *sel = &sel_p;
138487 SelectionVector owned_sel;
138488 Vector *vector = &vector_p;
138489 if (vector->GetVectorType() == VectorType::DICTIONARY_VECTOR) {
138490
138491 auto &child = DictionaryVector::Child(*vector);
138492 D_ASSERT(child.GetVectorType() != VectorType::DICTIONARY_VECTOR);
138493 auto &dict_sel = DictionaryVector::SelVector(*vector);
138494
138495 auto new_buffer = dict_sel.Slice(*sel, count);
138496 owned_sel.Initialize(new_buffer);
138497 sel = &owned_sel;
138498 vector = &child;
138499 } else if (vector->GetVectorType() == VectorType::CONSTANT_VECTOR) {
138500 sel = ConstantVector::ZeroSelectionVector(count, owned_sel);
138501 }
138502
138503 auto member_count = UnionType::GetMemberCount(vector_p.GetType());
138504 if (member_count == 0) {
138505 return UnionInvalidReason::NO_MEMBERS;
138506 }
138507
138508 UnifiedVectorFormat vector_vdata;
138509 vector_p.ToUnifiedFormat(count, vector_vdata);
138510
138511 auto &entries = StructVector::GetEntries(vector_p);
138512 duckdb::vector<UnifiedVectorFormat> child_vdata(entries.size());
138513 for (
idx_t entry_idx = 0; entry_idx < entries.size(); entry_idx++) {
138514 auto &child = *entries[entry_idx];
138515 child.ToUnifiedFormat(count, child_vdata[entry_idx]);
138516 }
138517
138518 auto &tag_vdata = child_vdata[0];
138519
138520 for (
idx_t row_idx = 0; row_idx < count; row_idx++) {
138521 auto mapped_idx = sel->get_index(row_idx);
138522
138523 if (!vector_vdata.validity.RowIsValid(mapped_idx)) {
138524 continue;
138525 }
138526
138527 auto tag_idx = tag_vdata.sel->get_index(sel_p.get_index(row_idx));
138528 if (!tag_vdata.validity.RowIsValid(tag_idx)) {
138529
138530 return UnionInvalidReason::NULL_TAG;
138531 }
138532 auto tag = UnifiedVectorFormat::GetData<union_tag_t>(tag_vdata)[tag_idx];
138533 if (tag >= member_count) {
138534 return UnionInvalidReason::TAG_OUT_OF_RANGE;
138535 }
138536
138537 bool found_valid = false;
138538 for (
idx_t i = 0; i < member_count; i++) {
138539 auto &member_vdata = child_vdata[1 + i];
138540 idx_t member_idx = member_vdata.sel->get_index(sel_p.get_index(row_idx));
138541 if (!member_vdata.validity.RowIsValid(member_idx)) {
138542 continue;
138543 }
138544 if (found_valid) {
138545 return UnionInvalidReason::VALIDITY_OVERLAP;
138546 }
138547 found_valid = true;
138548 if (tag != static_cast<union_tag_t>(i)) {
138549 return UnionInvalidReason::TAG_MISMATCH;
138550 }
138551 }
138552 }
138553
138554 return UnionInvalidReason::VALID;
138555}