shredded_type: <shredding> STRUCT(typed_value <shredding>) STRUCT(typed_value <shredding>, untyped_value_index UINTEGER)
Attempt to merge all overlapping fields, only keep the fields that were able to be merged
TODO: perhaps we can keep the field but demote the type to unshredded somehow? Or even use MaxLogicalType and merge the stats into that ?
TODO: perhaps we can keep the LIST part of the stats, and only demote the child to unshredded?
425432 {
425437
425442
425445
425446 auto &stats_typed_value_type = stats_typed_value.GetType();
425447 auto &other_typed_value_type = other_typed_value.GetType();
425448
425451
425452
425453 optional_ptr<BaseStatistics> new_untyped_value_stats;
425454 BaseStatistics owned_untyped_value_stats;
425455
425456 if (untyped_value_stats && other_untyped_stats) {
425457
425458 owned_untyped_value_stats = untyped_value_stats->Copy();
425459 owned_untyped_value_stats.Merge(*other_untyped_stats);
425460 new_untyped_value_stats = owned_untyped_value_stats;
425461 } else if (untyped_value_stats) {
425462
425463 owned_untyped_value_stats = untyped_value_stats->Copy();
425464 new_untyped_value_stats = owned_untyped_value_stats;
425465 } else if (other_untyped_stats) {
425466
425467 owned_untyped_value_stats = other_untyped_stats->Copy();
425468 new_untyped_value_stats = owned_untyped_value_stats;
425469 }
425470
425471 if (stats_typed_value_type.id() == LogicalTypeId::STRUCT) {
425472 if (stats_typed_value_type.id() != other_typed_value_type.id()) {
425474 return false;
425475 }
425476 auto &stats_object_children = StructType::GetChildTypes(stats_typed_value_type);
425477 auto &other_object_children = StructType::GetChildTypes(other_typed_value_type);
425478
425480 case_insensitive_map_t<idx_t> key_to_index;
425481 for (
idx_t i = 0; i < other_object_children.size(); i++) {
425482 auto &other_object_child = other_object_children[i];
425483 key_to_index.emplace(other_object_child.first, i);
425484 }
425485
425487 child_list_t<LogicalType> new_children;
425488 vector<BaseStatistics> new_child_stats;
425489
425490 for (
idx_t i = 0; i < stats_object_children.size(); i++) {
425491 auto &stats_object_child = stats_object_children[i];
425492 auto other_it = key_to_index.find(stats_object_child.first);
425493 if (other_it == key_to_index.end()) {
425494 continue;
425495 }
425496 auto &other_object_child = other_object_children[other_it->second];
425497 if (other_object_child.second.id() != stats_object_child.second.id()) {
425500 continue;
425501 }
425502
425503 auto &stats_child = StructStats::GetChildStats(stats_typed_value, i);
425504 auto &other_child = StructStats::GetChildStats(other_typed_value, other_it->second);
425505 BaseStatistics new_child;
425507 continue;
425508 }
425509 new_children.emplace_back(stats_object_child.first, new_child.GetType());
425510 new_child_stats.emplace_back(std::move(new_child));
425511 }
425512 if (new_children.empty()) {
425514 return false;
425515 }
425516
425518 auto new_object_type = LogicalType::STRUCT(std::move(new_children));
425520 for (
idx_t i = 0; i < new_child_stats.size(); i++) {
425521 StructStats::SetChildStats(new_typed_value, i, new_child_stats[i]);
425522 }
425523 new_typed_value.CombineValidity(stats_typed_value, other_typed_value);
425524 new_stats = WrapTypedValue(new_typed_value, new_untyped_value_stats);
425525 return true;
425526 } else if (stats_typed_value_type.id() == LogicalTypeId::LIST) {
425527 if (stats_typed_value_type.id() != other_typed_value_type.id()) {
425529 return false;
425530 }
425531 auto &stats_child = ListStats::GetChildStats(stats_typed_value);
425532 auto &other_child = ListStats::GetChildStats(other_typed_value);
425533
425535 BaseStatistics new_child_stats;
425536 if (!
MergeShredding(stats_child, other_child, new_child_stats)) {
425537 return false;
425538 }
425540 new_typed_value.CombineValidity(stats_typed_value, other_typed_value);
425541 ListStats::SetChildStats(new_typed_value, new_child_stats.ToUnique());
425542 new_stats = WrapTypedValue(new_typed_value, new_untyped_value_stats);
425543 return true;
425544 } else {
425545 D_ASSERT(!stats_typed_value_type.IsNested());
425546 if (stats_typed_value_type.id() != other_typed_value_type.id()) {
425548 return false;
425549 }
425550 auto new_typed_stats = stats_typed_value.Copy();
425551 new_typed_stats.Merge(other_typed_value);
425552 new_stats = WrapTypedValue(new_typed_stats, new_untyped_value_stats);
425553 return true;
425554 }
425555}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static DUCKDB_API bool MergeShredding(const BaseStatistics &stats, const BaseStatistics &other, BaseStatistics &new_stats)
Definition duckdb.cpp:425432
static DUCKDB_API optional_ptr< const BaseStatistics > GetUntypedStats(const BaseStatistics &stats)
Returns the untyped_value_index stats of a shredded stats entry - if there is any.
Definition duckdb.cpp:425064
static DUCKDB_API const BaseStatistics & GetTypedStats(const BaseStatistics &stats)
Returns the typed_value stats of a shredded stats entry.
Definition duckdb.cpp:425055