215821 {
215822 auto &cast_data = parameters.cast_data->Cast<StructBoundCastData>();
215823 auto &lstate = parameters.local_state->Cast<StructCastLocalState>();
215824
215825 D_ASSERT(source.GetType().id() == LogicalTypeId::STRUCT);
215826 D_ASSERT(result.GetType().id() == LogicalTypeId::UNION);
215827 D_ASSERT(cast_data.target.id() == LogicalTypeId::UNION);
215828
215829 auto &source_children = StructVector::GetEntries(source);
215830 auto &target_children = StructVector::GetEntries(result);
215831
215832 for (
idx_t i = 0; i < source_children.size(); i++) {
215833 auto &result_child_vector = *target_children[i];
215834 auto &source_child_vector = *source_children[i];
215835 CastParameters child_parameters(parameters, cast_data.child_cast_info[i].cast_data, lstate.local_states[i]);
215836 auto converted =
215837 cast_data.child_cast_info[i].function(source_child_vector, result_child_vector, count, child_parameters);
215838 (void)converted;
215839 D_ASSERT(converted);
215840
215841 result_child_vector.Flatten(count);
215842 }
215843
215844 if (source.GetVectorType() == VectorType::CONSTANT_VECTOR) {
215845 result.SetVectorType(VectorType::CONSTANT_VECTOR);
215846 ConstantVector::SetNull(result, ConstantVector::IsNull(source));
215847
215848
215849 auto &tag_vec = *target_children[0];
215850 ConstantVector::SetNull(result, ConstantVector::IsNull(tag_vec));
215851 } else {
215852
215853 auto &tag_vec = *target_children[0];
215854 UnifiedVectorFormat source_data, tag_data;
215855 source.ToUnifiedFormat(count, source_data);
215856 tag_vec.ToUnifiedFormat(count, tag_data);
215857
215858 for (
idx_t i = 0; i < count; i++) {
215859 if (!source_data.validity.RowIsValid(source_data.sel->get_index(i)) ||
215860 !tag_data.validity.RowIsValid(tag_data.sel->get_index(i))) {
215861 FlatVector::SetNull(result, i, true);
215862 }
215863 }
215864 }
215865
215867 switch (check_tags) {
215868 case UnionInvalidReason::TAG_OUT_OF_RANGE:
215869 throw ConversionException("One or more of the tags do not point to a valid union member");
215870 case UnionInvalidReason::VALIDITY_OVERLAP:
215871 throw ConversionException("One or more rows in the produced UNION have validity set for more than 1 member");
215872 case UnionInvalidReason::TAG_MISMATCH:
215873 throw ConversionException(
215874 "One or more rows in the produced UNION have tags that don't point to the valid member");
215875 case UnionInvalidReason::NULL_TAG:
215876 throw ConversionException("One or more rows in the produced UNION have a NULL tag");
215877 case UnionInvalidReason::VALID:
215878 break;
215879 default:
215880 throw InternalException("Struct to union cast failed for unknown reason");
215881 }
215882
215883 result.Verify(count);
215884 return true;
215885}
static DUCKDB_API UnionInvalidReason CheckUnionValidity(Vector &vector, idx_t count, const SelectionVector &sel= *FlatVector::IncrementalSelectionVector())
Raw selection vector passed in (not merged with any other selection vectors).
Definition duckdb.cpp:138482