321268 {
321269
321270 if ((op.type != LogicalOperatorType::LOGICAL_FILTER && op.type != LogicalOperatorType::LOGICAL_ANY_JOIN) ||
321271 !is_root) {
321272 return nullptr;
321273 }
321274
321275
321276 auto &conjunction = bindings[0].get().Cast<BoundConjunctionExpression>();
321277 if (conjunction.GetExpressionType() != ExpressionType::CONJUNCTION_OR) {
321278 return nullptr;
321279 }
321280
321281
321282 auto &children = conjunction.children;
321283 bool eligible = false;
321284 for (const auto &child : children) {
321285 if (child->GetExpressionClass() == ExpressionClass::BOUND_CONJUNCTION &&
321286 child->GetExpressionType() == ExpressionType::CONJUNCTION_AND &&
321287 ExpressionReferencesMultipleTables(*child)) {
321288 eligible = true;
321289 break;
321290 }
321291 }
321292 if (!eligible) {
321293 return nullptr;
321294 }
321295
321296
321297 auto conjuncted_expressions = make_uniq_array<unordered_map<idx_t, unique_ptr<Expression>>>(children.size());
321298 for (
idx_t child_idx = 0; child_idx < children.size(); child_idx++) {
321299 conjuncted_expressions[child_idx] = unordered_map<idx_t, unique_ptr<Expression>>();
321300 ExtractConjunctedExpressions(*children[child_idx], conjuncted_expressions[child_idx]);
321301 }
321302
321303 auto derived_filter = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND);
321304 for (auto &entry : conjuncted_expressions[0]) {
321305 auto derived_entry_filter = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR);
321306 derived_entry_filter->children.push_back(entry.second->Copy());
321307
321308 bool found = true;
321309 for (
idx_t conj_idx = 1; conj_idx < children.size(); conj_idx++) {
321310 auto &other_entry = conjuncted_expressions[conj_idx];
321311 auto other_it = other_entry.find(entry.first);
321312 if (other_it == other_entry.end()) {
321313 found = false;
321314 break;
321315 }
321316 derived_entry_filter->children.push_back(other_it->second->Copy());
321317 }
321318
321319 if (!found) {
321320 continue;
321321 }
321322
321323 derived_filter->children.push_back(std::move(derived_entry_filter));
321324 }
321325
321326 if (derived_filter->children.empty()) {
321327 return nullptr;
321328 }
321329
321330
321331 auto result = make_uniq_base<Expression, BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND);
321332 auto &result_conjunction = result->Cast<BoundConjunctionExpression>();
321333 result_conjunction.children.push_back(conjunction.Copy());
321334
321335 if (derived_filter->children.size() == 1) {
321336 result_conjunction.children.push_back(std::move(derived_filter->children[0]));
321337 } else {
321338 result_conjunction.children.push_back(std::move(derived_filter));
321339 }
321340 return result;
321341}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237