326709 {
326710 auto &window_expr = window.expressions[0]->Cast<BoundWindowExpression>();
326711 vector<ColumnBinding> projections(window_expr.partitions.size() + args.size());
326712
326713
326714 for (
idx_t i = 0; i < window_expr.partitions.size(); i++) {
326715 auto &
partition = window_expr.partitions[i];
326716 if (!ExtractSingleBinding(&partition, projections[i])) {
326717 return false;
326718 }
326719 }
326720 for (
idx_t i = 0; i < args.size(); i++) {
326721 auto &
arg = args[i];
326722 if (!ExtractSingleBinding(&
arg, projections[window_expr.partitions.size() + i])) {
326723 return false;
326724 }
326725 }
326726
326727 reference<LogicalOperator> op = *window.children[0];
326728
326729
326730 while (!op.get().children.empty()) {
326731 stack.push_back(op);
326732 switch (op.get().type) {
326733 case LogicalOperatorType::LOGICAL_PROJECTION: {
326734 auto &projection = op.get().Cast<LogicalProjection>();
326735 for (
idx_t i = 0; i < projections.size(); i++) {
326736 if (projection.table_index != projections[i].table_index) {
326737 return false;
326738 }
326739 const idx_t projection_idx = projections[i].column_index;
326740 if (projection_idx >= projection.expressions.size()) {
326741 return false;
326742 }
326743 if (!ExtractSingleBinding(&projection.expressions[projection_idx], projections[i])) {
326744 return false;
326745 }
326746 }
326747 op = *op.get().children[0];
326748 break;
326749 }
326750 case LogicalOperatorType::LOGICAL_FILTER: {
326751 op = *op.get().children[0];
326752 break;
326753 }
326754 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
326755 auto &
join = op.get().Cast<LogicalComparisonJoin>();
326756 if (
join.join_type != JoinType::INNER &&
join.join_type != JoinType::SEMI &&
326757 join.join_type != JoinType::ANTI) {
326758 return false;
326759 }
326760
326761
326762
326763 column_binding_map_t<ColumnBinding> replaceable_bindings;
326764 for (
auto &condition :
join.conditions) {
326765 if (condition.comparison != ExpressionType::COMPARE_EQUAL) {
326766 return false;
326767 }
326768 ColumnBinding left_binding;
326769 if (!ExtractSingleBinding(&condition.left, left_binding, true)) {
326770 return false;
326771 }
326772 ColumnBinding right_binding;
326773 if (!ExtractSingleBinding(&condition.right, right_binding, true)) {
326774 return false;
326775 }
326776
326777 replaceable_bindings[left_binding] = right_binding;
326778 replaceable_bindings[right_binding] = left_binding;
326779 }
326780
326781 auto left_column_bindings =
join.children[0]->GetColumnBindings();
326782 auto right_column_bindings =
join.children[1]->GetColumnBindings();
326783 auto lidxs =
join.children[0]->GetTableIndex();
326784 auto ridxs =
join.children[1]->GetTableIndex();
326785 if (lidxs.size() != 1 || ridxs.size() != 1) {
326786 return false;
326787 }
326788 auto left_idx = lidxs[0];
326789 auto right_idx = ridxs[0];
326790
326791 bool all_left_replaceable = true;
326792 bool all_right_replaceable = true;
326793 for (
idx_t i = 0; i < projections.size(); i++) {
326794 const auto &projection = projections[i];
326795 if (projection.table_index != left_idx && projection.table_index != right_idx) {
326796 return false;
326797 }
326798 if (projection.table_index == left_idx && projection.column_index >= left_column_bindings.size()) {
326799 return false;
326800 }
326801 if (projection.table_index == right_idx && projection.column_index >= right_column_bindings.size()) {
326802 return false;
326803 }
326804 auto &column_binding = projection.table_index == left_idx
326805 ? left_column_bindings[projection.column_index]
326806 : right_column_bindings[projection.column_index];
326807 if (replaceable_bindings.find(column_binding) == replaceable_bindings.end()) {
326808 if (column_binding.table_index == left_idx) {
326809 all_left_replaceable = false;
326810 } else {
326811 all_right_replaceable = false;
326812 }
326813 }
326814 }
326815
326816 if (!all_left_replaceable && !all_right_replaceable) {
326817
326818 return false;
326819 }
326820
326821 idx_t replace_table_idx = all_right_replaceable ? right_idx : left_idx;
326822 for (
idx_t i = 0; i < projections.size(); i++) {
326823 const auto projection_idx = projections[i];
326824 if (projection_idx.table_index != left_idx && projection_idx.table_index != right_idx) {
326825 return false;
326826 }
326827 if (projection_idx.table_index == left_idx &&
326828 projection_idx.column_index >= left_column_bindings.size()) {
326829 return false;
326830 }
326831 if (projection_idx.table_index == right_idx &&
326832 projection_idx.column_index >= right_column_bindings.size()) {
326833 return false;
326834 }
326835 auto &column_binding = projection_idx.table_index == left_idx
326836 ? left_column_bindings[projection_idx.column_index]
326837 : right_column_bindings[projection_idx.column_index];
326838 if (column_binding.table_index == replace_table_idx) {
326839 projections[i] = replaceable_bindings[column_binding];
326840 }
326841 }
326842
326843 if (all_right_replaceable) {
326844 op = *op.get().children[0];
326845 } else {
326846 op = *op.get().children[1];
326847 }
326848 break;
326849 }
326850 default: {
326851 return false;
326852 }
326853 }
326854 }
326855 stack.push_back(op);
326856
326857 if (op.get().type != LogicalOperatorType::LOGICAL_GET) {
326858
326859
326860 return false;
326861 }
326862 auto &logical_get = op.get().Cast<LogicalGet>();
326863 if (!logical_get.function.late_materialization || !logical_get.function.get_row_id_columns) {
326864 return false;
326865 }
326866
326867 const auto rowid_column_idxs = logical_get.function.get_row_id_columns(context, logical_get.bind_data.get());
326868 for (const auto &col_idx : rowid_column_idxs) {
326869 auto entry = logical_get.virtual_columns.find(col_idx);
326870 if (entry == logical_get.virtual_columns.end()) {
326871 return false;
326872 }
326873 }
326874
326875 for (
idx_t i = 0; i < projections.size(); i++) {
326876 if (projections[i].column_index != i) {
326877 for (auto &proj : projections) {
326878 lhs_projections.push_back(proj.column_index);
326879 }
326880 break;
326881 }
326882 }
326883 return true;
326884}
internal::named_arg< T, Char > arg(const S &name, const T &arg)
Definition duckdb.cpp:16242