Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::FilterPushdown Class Reference
Collaboration diagram for duckdb::FilterPushdown:

Classes

struct  Filter

Public Member Functions

 FilterPushdown (Optimizer &optimizer, bool convert_mark_joins=true)
unique_ptr< LogicalOperator > Rewrite (unique_ptr< LogicalOperator > op)
 Perform filter pushdown.
ClientContext & GetContext ()
 Return a reference to the client context (from the optimizer).
void CheckMarkToSemi (LogicalOperator &op, unordered_set< idx_t > &table_bindings)

Private Member Functions

unique_ptr< LogicalOperator > PushdownAggregate (unique_ptr< LogicalOperator > op)
 Push down a LogicalAggregate op.
unique_ptr< LogicalOperator > PushdownDistinct (unique_ptr< LogicalOperator > op)
 Push down a distinct operator.
unique_ptr< LogicalOperator > PushdownFilter (unique_ptr< LogicalOperator > op)
 Push down a LogicalFilter op.
unique_ptr< LogicalOperator > PushdownCrossProduct (unique_ptr< LogicalOperator > op)
 Push down a LogicalCrossProduct op.
unique_ptr< LogicalOperator > PushdownJoin (unique_ptr< LogicalOperator > op)
 Push down a join operator.
unique_ptr< LogicalOperator > PushdownProjection (unique_ptr< LogicalOperator > op)
 Push down a LogicalProjection op.
unique_ptr< LogicalOperator > PushdownUnnest (unique_ptr< LogicalOperator > op)
 Push down a LogicalProjection op.
unique_ptr< LogicalOperator > PushdownSetOperation (unique_ptr< LogicalOperator > op)
 Push down a LogicalSetOperation op.
unique_ptr< LogicalOperator > PushdownGet (unique_ptr< LogicalOperator > op)
 Push down a LogicalGet op.
unique_ptr< LogicalOperator > PushdownLimit (unique_ptr< LogicalOperator > op)
 Push down a LogicalLimit op.
unique_ptr< LogicalOperator > PushdownWindow (unique_ptr< LogicalOperator > op)
 Push down a LogicalWindow op.
unique_ptr< LogicalOperator > PushdownInnerJoin (unique_ptr< LogicalOperator > op, unordered_set< idx_t > &left_bindings, unordered_set< idx_t > &right_bindings)
unique_ptr< LogicalOperator > PushdownLeftJoin (unique_ptr< LogicalOperator > op, unordered_set< idx_t > &left_bindings, unordered_set< idx_t > &right_bindings)
unique_ptr< LogicalOperator > PushdownOuterJoin (unique_ptr< LogicalOperator > op, unordered_set< idx_t > &left_bindings, unordered_set< idx_t > &right_bindings)
unique_ptr< LogicalOperator > PushdownSemiAntiJoin (unique_ptr< LogicalOperator > op)
unique_ptr< LogicalOperator > PushdownMarkJoin (unique_ptr< LogicalOperator > op, unordered_set< idx_t > &left_bindings, unordered_set< idx_t > &right_bindings)
unique_ptr< LogicalOperator > PushdownSingleJoin (unique_ptr< LogicalOperator > op, unordered_set< idx_t > &left_bindings, unordered_set< idx_t > &right_bindings)
unique_ptr< LogicalOperator > AddLogicalFilter (unique_ptr< LogicalOperator > op, vector< unique_ptr< Expression > > expressions)
unique_ptr< LogicalOperator > PushFinalFilters (unique_ptr< LogicalOperator > op)
 Push any remaining filters into a LogicalFilter at this level.
unique_ptr< LogicalOperator > FinishPushdown (unique_ptr< LogicalOperator > op)
unique_ptr< LogicalOperator > PushFiltersIntoDelimJoin (unique_ptr< LogicalOperator > op)
FilterResult AddFilter (unique_ptr< Expression > expr)
void ExtractFilterBindings (const Expression &expr, vector< ColumnBinding > &bindings)
void GenerateFilters ()
 Generate filters from the current set of filters stored in the FilterCombiner.
FilterResult PushFilters ()

Private Attributes

Optimizer & optimizer
FilterCombiner combiner
bool convert_mark_joins
vector< unique_ptr< Filter > > filters

Constructor & Destructor Documentation

◆ FilterPushdown()

duckdb::FilterPushdown::FilterPushdown ( Optimizer & optimizer,
bool convert_mark_joins = true )
explicit
309866 : optimizer(optimizer), combiner(optimizer.context), convert_mark_joins(convert_mark_joins) {
309867}

Member Function Documentation

◆ Rewrite()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::Rewrite ( unique_ptr< LogicalOperator > op)

Perform filter pushdown.

309869 {
309870 D_ASSERT(!combiner.HasFilters());
309871 switch (op->type) {
309872 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY:
309873 return PushdownAggregate(std::move(op));
309874 case LogicalOperatorType::LOGICAL_FILTER:
309875 return PushdownFilter(std::move(op));
309876 case LogicalOperatorType::LOGICAL_CROSS_PRODUCT:
309877 return PushdownCrossProduct(std::move(op));
309878 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN:
309879 case LogicalOperatorType::LOGICAL_ANY_JOIN:
309880 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
309881 case LogicalOperatorType::LOGICAL_DELIM_JOIN:
309882 return PushdownJoin(std::move(op));
309883 case LogicalOperatorType::LOGICAL_PROJECTION:
309884 return PushdownProjection(std::move(op));
309885 case LogicalOperatorType::LOGICAL_INTERSECT:
309886 case LogicalOperatorType::LOGICAL_EXCEPT:
309887 case LogicalOperatorType::LOGICAL_UNION:
309888 return PushdownSetOperation(std::move(op));
309889 case LogicalOperatorType::LOGICAL_DISTINCT:
309890 return PushdownDistinct(std::move(op));
309891 case LogicalOperatorType::LOGICAL_ORDER_BY:
309892 // we can just push directly through these operations without any rewriting
309893 op->children[0] = Rewrite(std::move(op->children[0]));
309894 return op;
309895 case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE: {
309896 // we can't push filters into the materialized CTE (LHS), but we do want to recurse into it
309897 FilterPushdown pushdown(optimizer, convert_mark_joins);
309898 op->children[0] = pushdown.Rewrite(std::move(op->children[0]));
309899 // we can push filters into the rest of the query plan (RHS)
309900 op->children[1] = Rewrite(std::move(op->children[1]));
309901 return op;
309902 }
309903 case LogicalOperatorType::LOGICAL_GET:
309904 return PushdownGet(std::move(op));
309905 case LogicalOperatorType::LOGICAL_LIMIT:
309906 return PushdownLimit(std::move(op));
309907 case LogicalOperatorType::LOGICAL_WINDOW:
309908 return PushdownWindow(std::move(op));
309909 case LogicalOperatorType::LOGICAL_UNNEST:
309910 return PushdownUnnest(std::move(op));
309911 default:
309912 return FinishPushdown(std::move(op));
309913 }
309914}
unique_ptr< LogicalOperator > Rewrite(unique_ptr< LogicalOperator > op)
Perform filter pushdown.
Definition duckdb.cpp:309869
unique_ptr< LogicalOperator > PushdownAggregate(unique_ptr< LogicalOperator > op)
Push down a LogicalAggregate op.
Definition duckdb.cpp:316839
unique_ptr< LogicalOperator > PushdownCrossProduct(unique_ptr< LogicalOperator > op)
Push down a LogicalCrossProduct op.
Definition duckdb.cpp:316913
unique_ptr< LogicalOperator > PushdownGet(unique_ptr< LogicalOperator > op)
Push down a LogicalGet op.
Definition duckdb.cpp:317036
unique_ptr< LogicalOperator > PushdownDistinct(unique_ptr< LogicalOperator > op)
Push down a distinct operator.
Definition duckdb.cpp:316988
unique_ptr< LogicalOperator > PushdownJoin(unique_ptr< LogicalOperator > op)
Push down a join operator.
Definition duckdb.cpp:309920
unique_ptr< LogicalOperator > PushdownUnnest(unique_ptr< LogicalOperator > op)
Push down a LogicalProjection op.
Definition duckdb.cpp:317960
unique_ptr< LogicalOperator > PushdownFilter(unique_ptr< LogicalOperator > op)
Push down a LogicalFilter op.
Definition duckdb.cpp:317009
unique_ptr< LogicalOperator > PushdownLimit(unique_ptr< LogicalOperator > op)
Push down a LogicalLimit op.
Definition duckdb.cpp:317388
unique_ptr< LogicalOperator > PushdownProjection(unique_ptr< LogicalOperator > op)
Push down a LogicalProjection op.
Definition duckdb.cpp:317723
unique_ptr< LogicalOperator > PushdownWindow(unique_ptr< LogicalOperator > op)
Push down a LogicalWindow op.
Definition duckdb.cpp:318035
unique_ptr< LogicalOperator > PushdownSetOperation(unique_ptr< LogicalOperator > op)
Push down a LogicalSetOperation op.
Definition duckdb.cpp:317841
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetContext()

ClientContext & duckdb::FilterPushdown::GetContext ( )

Return a reference to the client context (from the optimizer).

309916 {
309917 return optimizer.GetContext();
309918}
Here is the caller graph for this function:

◆ CheckMarkToSemi()

void duckdb::FilterPushdown::CheckMarkToSemi ( LogicalOperator & op,
unordered_set< idx_t > & table_bindings )
309789 {
309790 switch (op.type) {
309791 case LogicalOperatorType::LOGICAL_DELIM_JOIN:
309792 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
309793 auto &join = op.Cast<LogicalComparisonJoin>();
309794 if (join.join_type != JoinType::MARK) {
309795 break;
309796 }
309797 // if an operator above the mark join includes the mark join index,
309798 // then the mark join cannot be converted to a semi join
309799 if (table_bindings.find(join.mark_index) != table_bindings.end()) {
309800 join.convert_mark_to_semi = false;
309801 }
309802 break;
309803 }
309804 // you need to store table.column index.
309805 // if you get to a projection, you need to change the table_bindings passed so they reflect the
309806 // table index of the original expression they originated from.
309807 case LogicalOperatorType::LOGICAL_PROJECTION: {
309808 // when we encounter a projection, replace the table_bindings with
309809 // the tables in the projection
309810 auto &proj = op.Cast<LogicalProjection>();
309811 auto proj_bindings = proj.GetColumnBindings();
309812 unordered_set<idx_t> new_table_bindings;
309813 for (auto &binding : proj_bindings) {
309814 auto col_index = binding.column_index;
309815 auto &expr = proj.expressions.at(col_index);
309816 ExpressionIterator::EnumerateExpression(expr, [&](Expression &child) {
309817 if (child.GetExpressionClass() == ExpressionClass::BOUND_COLUMN_REF) {
309818 auto &col_ref = child.Cast<BoundColumnRefExpression>();
309819 new_table_bindings.insert(col_ref.binding.table_index);
309820 }
309821 });
309822 table_bindings = new_table_bindings;
309823 }
309824 break;
309825 }
309826 // It's possible a mark join index makes its way into a group by as the grouping index
309827 // when that happens we need to keep track of it to make sure we do not convert a mark join to semi.
309828 // see filter_pushdown_into_subquery.
309829 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY: {
309830 auto &aggr = op.Cast<LogicalAggregate>();
309831 auto aggr_bindings = aggr.GetColumnBindings();
309832 vector<ColumnBinding> bindings_to_keep;
309833 for (auto &expr : aggr.groups) {
309834 ExpressionIterator::EnumerateExpression(expr, [&](Expression &child) {
309835 if (child.GetExpressionClass() == ExpressionClass::BOUND_COLUMN_REF) {
309836 auto &col_ref = child.Cast<BoundColumnRefExpression>();
309837 bindings_to_keep.push_back(col_ref.binding);
309838 }
309839 });
309840 }
309841 for (auto &expr : aggr.expressions) {
309842 ExpressionIterator::EnumerateExpression(expr, [&](Expression &child) {
309843 if (child.GetExpressionClass() == ExpressionClass::BOUND_COLUMN_REF) {
309844 auto &col_ref = child.Cast<BoundColumnRefExpression>();
309845 bindings_to_keep.push_back(col_ref.binding);
309846 }
309847 });
309848 }
309849 table_bindings = unordered_set<idx_t>();
309850 for (auto &expr_binding : bindings_to_keep) {
309851 table_bindings.insert(expr_binding.table_index);
309852 }
309853 break;
309854 }
309855 default:
309856 break;
309857 }
309858
309859 // recurse into the children to find mark joins and project their indexes.
309860 for (auto &child : op.children) {
309861 CheckMarkToSemi(*child, table_bindings);
309862 }
309863}
cv::String join(const cv::String &base, const cv::String &path)

◆ PushdownAggregate()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownAggregate ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalAggregate op.

316839 {
316840 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY);
316841 auto &aggr = op->Cast<LogicalAggregate>();
316842
316843 // pushdown into AGGREGATE and GROUP BY
316844 // we cannot push expressions that refer to the aggregate
316845 FilterPushdown child_pushdown(optimizer, convert_mark_joins);
316846 for (idx_t i = 0; i < filters.size(); i++) {
316847 auto &f = *filters[i];
316848 if (f.bindings.find(aggr.aggregate_index) != f.bindings.end()) {
316849 // filter on aggregate: cannot pushdown
316850 continue;
316851 }
316852 if (f.bindings.find(aggr.groupings_index) != f.bindings.end()) {
316853 // filter on GROUPINGS function: cannot pushdown
316854 continue;
316855 }
316856 // no aggregate! we are filtering on a group
316857 // we can only push this down if the filter is in all grouping sets
316858 if (aggr.groups.empty()) {
316859 // empty group - we cannot pushdown the filter
316860 continue;
316861 }
316862
316863 vector<ColumnBinding> bindings;
316864 ExtractFilterBindings(*f.filter, bindings);
316865 if (bindings.empty()) {
316866 // we can never push down empty grouping sets
316867 continue;
316868 }
316869
316870 bool can_pushdown_filter = true;
316871 for (auto &grp : aggr.grouping_sets) {
316872 // check for each of the grouping sets if they contain all groups
316873 for (auto &binding : bindings) {
316874 if (grp.find(binding.column_index) == grp.end()) {
316875 can_pushdown_filter = false;
316876 break;
316877 }
316878 }
316879 if (!can_pushdown_filter) {
316880 break;
316881 }
316882 }
316883 if (!can_pushdown_filter) {
316884 continue;
316885 }
316886 // no aggregate! we can push this down
316887 // rewrite any group bindings within the filter
316888 f.filter = ReplaceGroupBindings(aggr, std::move(f.filter));
316889 // add the filter to the child node
316890 if (child_pushdown.AddFilter(std::move(f.filter)) == FilterResult::UNSATISFIABLE) {
316891 // filter statically evaluates to false, strip tree
316892 return make_uniq<LogicalEmptyResult>(std::move(op));
316893 }
316894 // erase the filter from here
316895 filters.erase_at(i);
316896 i--;
316897 }
316898 child_pushdown.GenerateFilters();
316899
316900 op->children[0] = child_pushdown.Rewrite(std::move(op->children[0]));
316901 return FinishPushdown(std::move(op));
316902}
void ExtractFilterBindings(const Expression &expr, vector< ColumnBinding > &bindings)
Definition duckdb.cpp:316834
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownDistinct()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownDistinct ( unique_ptr< LogicalOperator > op)
private

Push down a distinct operator.

316988 {
316989 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_DISTINCT);
316990 auto &distinct = op->Cast<LogicalDistinct>();
316991 if (!distinct.order_by) {
316992 // regular DISTINCT - can just push down
316993 op->children[0] = Rewrite(std::move(op->children[0]));
316994 return op;
316995 }
316996 // no pushdown through DISTINCT ON (yet?)
316997 return FinishPushdown(std::move(op));
316998}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownFilter()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownFilter ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalFilter op.

317009 {
317010 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_FILTER);
317011 auto &filter = op->Cast<LogicalFilter>();
317012 if (filter.HasProjectionMap()) {
317013 return FinishPushdown(std::move(op));
317014 }
317015 // filter: gather the filters and remove the filter from the set of operations
317016 for (auto &expression : filter.expressions) {
317017 if (AddFilter(std::move(expression)) == FilterResult::UNSATISFIABLE) {
317018 // filter statically evaluates to false, strip tree
317019 return make_uniq<LogicalEmptyResult>(std::move(op));
317020 }
317021 }
317022 GenerateFilters();
317023 return Rewrite(std::move(filter.children[0]));
317024}
void GenerateFilters()
Generate filters from the current set of filters stored in the FilterCombiner.
Definition duckdb.cpp:310013
void filter(InputArray image, InputArray kernel, OutputArray output)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownCrossProduct()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownCrossProduct ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalCrossProduct op.

316913 {
316914 D_ASSERT(op->children.size() > 1);
316915 FilterPushdown left_pushdown(optimizer, convert_mark_joins), right_pushdown(optimizer, convert_mark_joins);
316916 vector<unique_ptr<Expression>> join_expressions;
316917 auto join_ref_type = JoinRefType::REGULAR;
316918 switch (op->type) {
316919 case LogicalOperatorType::LOGICAL_CROSS_PRODUCT:
316920 break;
316921 default:
316922 throw InternalException("Unsupported join type for cross product push down");
316923 }
316924 unordered_set<idx_t> left_bindings, right_bindings;
316925 if (!filters.empty()) {
316926 // check to see into which side we should push the filters
316927 // first get the LHS and RHS bindings
316928 LogicalJoin::GetTableReferences(*op->children[0], left_bindings);
316929 LogicalJoin::GetTableReferences(*op->children[1], right_bindings);
316930 // now check the set of filters
316931 for (auto &f : filters) {
316932 auto side = JoinSide::GetJoinSide(f->bindings, left_bindings, right_bindings);
316933 if (side == JoinSide::LEFT) {
316934 // bindings match left side: push into left
316935 left_pushdown.filters.push_back(std::move(f));
316936 } else if (side == JoinSide::RIGHT) {
316937 right_pushdown.filters.push_back(std::move(f));
316938 } else {
316939 D_ASSERT(side == JoinSide::BOTH || side == JoinSide::NONE);
316940 // bindings match both: turn into join condition
316941 join_expressions.push_back(std::move(f->filter));
316942 }
316943 }
316944 }
316945
316946 op->children[0] = left_pushdown.Rewrite(std::move(op->children[0]));
316947 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
316948
316949 if (!join_expressions.empty()) {
316950 // join conditions found: turn into inner join
316951 // extract join conditions
316952 vector<JoinCondition> conditions;
316953 vector<unique_ptr<Expression>> arbitrary_expressions;
316954 const auto join_type = JoinType::INNER;
316955 LogicalComparisonJoin::ExtractJoinConditions(GetContext(), join_type, join_ref_type, op->children[0],
316956 op->children[1], left_bindings, right_bindings, join_expressions,
316957 conditions, arbitrary_expressions);
316958 // create the join from the join conditions
316959 auto new_op = LogicalComparisonJoin::CreateJoin(join_type, join_ref_type, std::move(op->children[0]),
316960 std::move(op->children[1]), std::move(conditions),
316961 std::move(arbitrary_expressions));
316962
316963 // possible cases are: AnyJoin, ComparisonJoin, or Filter + ComparisonJoin
316964 if (op->has_estimated_cardinality) {
316965 // set the estimated cardinality of the new operator
316966 new_op->SetEstimatedCardinality(op->estimated_cardinality);
316967 if (new_op->type == LogicalOperatorType::LOGICAL_FILTER) {
316968 // if the new operators are Filter + ComparisonJoin, also set the estimated cardinality for the join
316969 D_ASSERT(new_op->children[0]->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN);
316970 new_op->children[0]->SetEstimatedCardinality(op->estimated_cardinality);
316971 }
316972 }
316973 return new_op;
316974 } else {
316975 // no join conditions found: keep as cross product
316976 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_CROSS_PRODUCT);
316977 return op;
316978 }
316979}
ClientContext & GetContext()
Return a reference to the client context (from the optimizer).
Definition duckdb.cpp:309916
static void GetTableReferences(LogicalOperator &op, unordered_set< idx_t > &bindings)
Gets the set of table references that are reachable from this node.
Definition duckdb.cpp:382702
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownJoin ( unique_ptr< LogicalOperator > op)
private

Push down a join operator.

309920 {
309921 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
309922 op->type == LogicalOperatorType::LOGICAL_ASOF_JOIN || op->type == LogicalOperatorType::LOGICAL_ANY_JOIN ||
309923 op->type == LogicalOperatorType::LOGICAL_DELIM_JOIN);
309924 auto &join = op->Cast<LogicalJoin>();
309925
309926 const auto restore_projection_maps = join.HasProjectionMap();
309927 auto left_projection_map = join.left_projection_map;
309928 auto right_projection_map = join.right_projection_map;
309929
309930 unordered_set<idx_t> left_bindings, right_bindings;
309931 LogicalJoin::GetTableReferences(*op->children[0], left_bindings);
309932 LogicalJoin::GetTableReferences(*op->children[1], right_bindings);
309933
309934 unique_ptr<LogicalOperator> result;
309935 switch (join.join_type) {
309936 case JoinType::OUTER:
309937 result = PushdownOuterJoin(std::move(op), left_bindings, right_bindings);
309938 break;
309939 case JoinType::INNER:
309940 // AsOf joins can't push anything into the RHS, so treat it as a left join
309941 if (op->type == LogicalOperatorType::LOGICAL_ASOF_JOIN) {
309942 result = PushdownLeftJoin(std::move(op), left_bindings, right_bindings);
309943 break;
309944 }
309945 result = PushdownInnerJoin(std::move(op), left_bindings, right_bindings);
309946 break;
309947 case JoinType::LEFT:
309948 result = PushdownLeftJoin(std::move(op), left_bindings, right_bindings);
309949 break;
309950 case JoinType::MARK:
309951 result = PushdownMarkJoin(std::move(op), left_bindings, right_bindings);
309952 break;
309953 case JoinType::SINGLE:
309954 result = PushdownSingleJoin(std::move(op), left_bindings, right_bindings);
309955 break;
309956 case JoinType::SEMI:
309957 case JoinType::ANTI:
309958 result = PushdownSemiAntiJoin(std::move(op));
309959 break;
309960 default:
309961 // unsupported join type: stop pushing down
309962 return FinishPushdown(std::move(op));
309963 }
309964
309965 if (restore_projection_maps) {
309966 switch (result->type) {
309967 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN:
309968 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
309969 case LogicalOperatorType::LOGICAL_ANY_JOIN:
309970 case LogicalOperatorType::LOGICAL_DELIM_JOIN: {
309971 // Pushing down filter through join didn't change operator type (e.g., LogicalEmptyResult), restore maps
309972 auto &result_join = result->Cast<LogicalJoin>();
309973 result_join.left_projection_map = std::move(left_projection_map);
309974 result_join.right_projection_map = std::move(right_projection_map);
309975 break;
309976 }
309977 default:
309978 break;
309979 }
309980 }
309981 return result;
309982}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownProjection()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownProjection ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalProjection op.

317723 {
317724 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_PROJECTION);
317725 auto &proj = op->Cast<LogicalProjection>();
317726 // push filter through logical projection
317727 // all the BoundColumnRefExpressions in the filter should refer to the LogicalProjection
317728 // we can rewrite them by replacing those references with the expression of the LogicalProjection node
317729 FilterPushdown child_pushdown(optimizer, convert_mark_joins);
317730 // There are some expressions can not be pushed down. We should keep them
317731 // and add an extra filter operator.
317732 vector<unique_ptr<Expression>> remain_expressions;
317733 for (auto &filter : filters) {
317734 auto &f = *filter;
317735 D_ASSERT(f.bindings.size() <= 1);
317736 bool is_volatile = IsVolatile(proj, *f.filter);
317737 if (is_volatile || f.filter->CanThrow()) {
317738 // We can't push down related expressions if the column in the
317739 // expression is generated by the functions which have side effects
317740 remain_expressions.push_back(std::move(f.filter));
317741 } else {
317742 // rewrite the bindings within this subquery
317743 f.filter = ReplaceProjectionBindings(proj, std::move(f.filter));
317744 // add the filter to the child pushdown
317745 if (child_pushdown.AddFilter(std::move(f.filter)) == FilterResult::UNSATISFIABLE) {
317746 // filter statically evaluates to false, strip tree
317747 return make_uniq<LogicalEmptyResult>(std::move(op));
317748 }
317749 }
317750 }
317751 child_pushdown.GenerateFilters();
317752 // now push into children
317753 op->children[0] = child_pushdown.Rewrite(std::move(op->children[0]));
317754 if (op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
317755 // child returns an empty result: generate an empty result here too
317756 return make_uniq<LogicalEmptyResult>(std::move(op));
317757 }
317758 return AddLogicalFilter(std::move(op), std::move(remain_expressions));
317759}
Here is the caller graph for this function:

◆ PushdownUnnest()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownUnnest ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalProjection op.

317960 {
317961 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_UNNEST);
317962 auto &unnest = op->Cast<LogicalUnnest>();
317963 // push filter through logical projection
317964 // all the BoundColumnRefExpressions in the filter should refer to the LogicalProjection
317965 // we can rewrite them by replacing those references with the expression of the LogicalProjection node
317966 FilterPushdown child_pushdown(optimizer, convert_mark_joins);
317967 // There are some expressions can not be pushed down. We should keep them
317968 // and add an extra filter operator.
317969 vector<unique_ptr<Expression>> remain_expressions;
317970 for (auto &filter : filters) {
317971 auto &f = *filter;
317972 auto can_push = true;
317973 for (auto &binding : f.bindings) {
317974 if (binding == unnest.unnest_index) {
317975 can_push = false;
317976 break;
317977 }
317978 }
317979 // if the expression index table index is the unnest index, then the filter is on the
317980 // unnest, and it should not be pushed down.
317981 if (!can_push) {
317982 // We can't push down related expressions if the column in the
317983 // expression is generated by the functions which have side effects
317984 remain_expressions.push_back(std::move(f.filter));
317985 } else {
317986 // add the filter to the child pushdown
317987 if (child_pushdown.AddFilter(std::move(f.filter)) == FilterResult::UNSATISFIABLE) {
317988 // filter statically evaluates to false, strip tree
317989 return make_uniq<LogicalEmptyResult>(std::move(op));
317990 }
317991 }
317992 }
317993 child_pushdown.GenerateFilters();
317994 // now push into children
317995 op->children[0] = child_pushdown.Rewrite(std::move(op->children[0]));
317996 if (op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
317997 // child returns an empty result: generate an empty result here too
317998 return make_uniq<LogicalEmptyResult>(std::move(op));
317999 }
318000 return AddLogicalFilter(std::move(op), std::move(remain_expressions));
318001}
Here is the caller graph for this function:

◆ PushdownSetOperation()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownSetOperation ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalSetOperation op.

317841 {
317842 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_UNION || op->type == LogicalOperatorType::LOGICAL_EXCEPT ||
317843 op->type == LogicalOperatorType::LOGICAL_INTERSECT);
317844 auto &setop = op->Cast<LogicalSetOperation>();
317845
317846 for (auto &child : op->children) {
317847 auto child_bindings = child->GetColumnBindings();
317848
317849 FilterPushdown child_pushdown(optimizer, convert_mark_joins);
317850 for (auto &original_filter : filters) {
317851 // first create a copy of the filter
317852 auto filter = make_uniq<Filter>();
317853 filter->filter = original_filter->filter->Copy();
317854
317855 // rewrite references to the result of the union into references to the child index
317856 ReplaceSetOpBindings(child_bindings, *filter, filter->filter, setop);
317857
317858 // extract bindings again
317859 filter->ExtractBindings();
317860
317861 // move the filters into the child pushdown nodes
317862 child_pushdown.filters.push_back(std::move(filter));
317863 }
317864
317865 // pushdown into the child
317866 child = child_pushdown.Rewrite(std::move(child));
317867 }
317868 bool all_empty = true;
317869 for (auto &child : op->children) {
317870 if (child->type != LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
317871 all_empty = false;
317872 }
317873 }
317874 if (all_empty) {
317875 // all sides are empty: the result must be empty
317876 return make_uniq<LogicalEmptyResult>(std::move(op));
317877 }
317878 if (op->type == LogicalOperatorType::LOGICAL_UNION) {
317879 // for UNION (ALL) - delete all empty children and return
317880 for (idx_t i = 0; i < op->children.size(); i++) {
317881 if (op->children[i]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
317882 op->children.erase(op->children.begin() + static_cast<int64_t>(i));
317883 i--;
317884 }
317885 }
317886 return op;
317887 }
317888 bool left_empty = op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT;
317889 bool right_empty = op->children[1]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT;
317890 if (left_empty && setop.setop_all) {
317891 // left child is empty result
317892 switch (op->type) {
317893 case LogicalOperatorType::LOGICAL_EXCEPT:
317894 // except: if left child is empty, return empty result
317895 case LogicalOperatorType::LOGICAL_INTERSECT:
317896 // intersect: if any child is empty, return empty result itself
317897 return make_uniq<LogicalEmptyResult>(std::move(op));
317898 default:
317899 throw InternalException("Unsupported set operation");
317900 }
317901 } else if (right_empty && setop.setop_all) {
317902 // right child is empty result
317903 switch (op->type) {
317904 case LogicalOperatorType::LOGICAL_EXCEPT:
317905 if (op->children[0]->type == LogicalOperatorType::LOGICAL_PROJECTION) {
317906 // union or except with empty right child: return left child
317907 auto &projection = op->children[0]->Cast<LogicalProjection>();
317908 projection.table_index = setop.table_index;
317909 return std::move(op->children[0]);
317910 }
317911 break;
317912 case LogicalOperatorType::LOGICAL_INTERSECT:
317913 // intersect: if any child is empty, return empty result itself
317914 return make_uniq<LogicalEmptyResult>(std::move(op));
317915 default:
317916 throw InternalException("Unsupported set operation");
317917 }
317918 }
317919 return op;
317920}
::int64_t int64_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownGet()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownGet ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalGet op.

We generate the table filters that will be executed during the table scan

Now we try to pushdown the remaining filters to perform zonemap checking

317036 {
317037 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_GET);
317038 auto &get = op->Cast<LogicalGet>();
317039
317040 if (get.function.pushdown_complex_filter || get.function.filter_pushdown) {
317041 // this scan supports some form of filter push-down
317042 // check if there are any parameters
317043 // if there are, invalidate them to force a re-bind on execution
317044 for (auto &filter : filters) {
317045 if (filter->filter->HasParameter()) {
317046 // there is a parameter in the filters! invalidate it
317048 }
317049 }
317050 }
317051 if (get.function.pushdown_complex_filter) {
317052 // for the remaining filters, check if we can push any of them into the scan as well
317053 vector<unique_ptr<Expression>> expressions;
317054 expressions.reserve(filters.size());
317055 for (auto &filter : filters) {
317056 expressions.push_back(std::move(filter->filter));
317057 }
317058 filters.clear();
317059
317060 get.function.pushdown_complex_filter(optimizer.context, get, get.bind_data.get(), expressions);
317061
317062 if (expressions.empty()) {
317063 return op;
317064 }
317065 // re-generate the filters
317066 for (auto &expr : expressions) {
317067 auto f = make_uniq<Filter>();
317068 f->filter = std::move(expr);
317069 f->ExtractBindings();
317070 filters.push_back(std::move(f));
317071 }
317072 }
317073
317074 if (!get.table_filters.filters.empty() || !get.function.filter_pushdown) {
317075 // the table function does not support filter pushdown: push a LogicalFilter on top
317076 return FinishPushdown(std::move(op));
317077 }
317078 if (PushFilters() == FilterResult::UNSATISFIABLE) {
317079 return make_uniq<LogicalEmptyResult>(std::move(op));
317080 }
317081
317082 auto &column_ids = get.GetColumnIds();
317084 vector<FilterPushdownResult> pushdown_results;
317085 get.table_filters = combiner.GenerateTableScanFilters(column_ids, pushdown_results);
317086
317087 GenerateFilters();
317088
317089 for (idx_t i = pushdown_results.size(); i < filters.size(); ++i) {
317090 // any generated filters have not been pushed down yet
317091 pushdown_results.push_back(FilterPushdownResult::NO_PUSHDOWN);
317092 }
317093 // for any filters we did not manage to push into specialized table filters - try to push them as a generic
317094 // expression
317095 for (idx_t i = 0; i < filters.size(); ++i) {
317096 // get the previous pushdown result
317097 auto pushdown_result = pushdown_results[i];
317098 if (pushdown_result != FilterPushdownResult::NO_PUSHDOWN) {
317099 // this has already been (partially) pushed down - skip
317100 continue;
317101 }
317102 auto &expr = *filters[i]->filter;
317103 if (expr.IsVolatile()) {
317104 continue;
317105 }
317106 // Allow pushing down filters that can throw only if there is a single expression
317107 // For now, do not push down single expressions with IN either. Later we can change InClauseRewriter to handle
317108 // this case
317109 if (expr.CanThrow() && (expr.type == ExpressionType::COMPARE_IN || filters.size() > 1)) {
317110 continue;
317111 }
317112 pushdown_result = combiner.TryPushdownGenericExpression(get, expr);
317113 if (pushdown_result == FilterPushdownResult::PUSHED_DOWN_FULLY) {
317114 filters.erase_at(i);
317115 pushdown_results.erase_at(i);
317116 i--;
317117 }
317118 }
317119
317121 return FinishPushdown(std::move(op));
317122}
static DUCKDB_API void InvalidateRecursive(Expression &expr)
Invalidate all parameters within an expression.
Definition duckdb.cpp:376693
FilterResult PushFilters()
Definition duckdb.cpp:309983
__host__ cudaTextureObject_t get() const noexcept
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownLimit()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownLimit ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalLimit op.

317388 {
317389 auto &limit = op->Cast<LogicalLimit>();
317390
317391 if (limit.limit_val.Type() == LimitNodeType::CONSTANT_VALUE && limit.limit_val.GetConstantValue() == 0) {
317392 return make_uniq<LogicalEmptyResult>(std::move(op));
317393 }
317394
317395 return FinishPushdown(std::move(op));
317396}
Here is the caller graph for this function:

◆ PushdownWindow()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownWindow ( unique_ptr< LogicalOperator > op)
private

Push down a LogicalWindow op.

318035 {
318036 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_WINDOW);
318037 auto &window = op->Cast<LogicalWindow>();
318038 FilterPushdown pushdown(optimizer, convert_mark_joins);
318039
318040 // 1. Loop throguh the expressions, find the window expressions and investigate the partitions
318041 // if a filter applies to a partition in each window expression then you can push the filter
318042 // into the children.
318043 vector<column_binding_set_t> window_exprs_partition_bindings;
318044 for (auto &expr : window.expressions) {
318045 if (expr->GetExpressionClass() != ExpressionClass::BOUND_WINDOW) {
318046 continue;
318047 }
318048 auto &window_expr = expr->Cast<BoundWindowExpression>();
318049 auto &partitions = window_expr.partitions;
318050 if (partitions.empty()) {
318051 // If any window expression does not have partitions, we cannot push any filters.
318052 // all window expressions need to be partitioned by the same column
318053 // in order to push down the window.
318054 return FinishPushdown(std::move(op));
318055 }
318056 column_binding_set_t partition_bindings;
318057 // 2. Get the binding information of the partitions of the window expression
318058 for (auto &partition_expr : partitions) {
318059 switch (partition_expr->GetExpressionType()) {
318060 // TODO: Add expressions for function expressions like FLOOR, CEIL etc.
318061 case ExpressionType::BOUND_COLUMN_REF: {
318062 auto &partition_col = partition_expr->Cast<BoundColumnRefExpression>();
318063 partition_bindings.insert(partition_col.binding);
318064 break;
318065 }
318066 default:
318067 break;
318068 }
318069 }
318070 window_exprs_partition_bindings.push_back(partition_bindings);
318071 }
318072
318073 if (window_exprs_partition_bindings.empty()) {
318074 return FinishPushdown(std::move(op));
318075 }
318076
318077 vector<unique_ptr<Filter>> leftover_filters;
318078 // Loop through the filters. If a filter is on a partition in every window expression
318079 // it can be pushed down.
318080 for (idx_t i = 0; i < filters.size(); i++) {
318081 // the filter must be on all partition bindings
318082 vector<ColumnBinding> bindings;
318083 ExtractFilterBindings(*filters.at(i)->filter, bindings);
318084 if (CanPushdownFilter(window_exprs_partition_bindings, bindings)) {
318085 pushdown.filters.push_back(std::move(filters.at(i)));
318086 } else {
318087 leftover_filters.push_back(std::move(filters.at(i)));
318088 }
318089 }
318090 op->children[0] = pushdown.Rewrite(std::move(op->children[0]));
318091 filters = std::move(leftover_filters);
318092 return FinishPushdown(std::move(op));
318093}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PushdownInnerJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownInnerJoin ( unique_ptr< LogicalOperator > op,
unordered_set< idx_t > & left_bindings,
unordered_set< idx_t > & right_bindings )
private
317137 {
317138 auto &join = op->Cast<LogicalJoin>();
317139 D_ASSERT(join.join_type == JoinType::INNER);
317140 if (op->type == LogicalOperatorType::LOGICAL_DELIM_JOIN) {
317141 op = PushFiltersIntoDelimJoin(std::move(op));
317142 return FinishPushdown(std::move(op));
317143 }
317144 // inner join: gather all the conditions of the inner join and add to the filter list
317145 if (op->type == LogicalOperatorType::LOGICAL_ANY_JOIN) {
317146 auto &any_join = join.Cast<LogicalAnyJoin>();
317147 // any join: only one filter to add
317148 if (AddFilter(std::move(any_join.condition)) == FilterResult::UNSATISFIABLE) {
317149 // filter statically evaluates to false, strip tree
317150 return make_uniq<LogicalEmptyResult>(std::move(op));
317151 }
317152 } else {
317153 // comparison join
317154 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN);
317155 auto &comp_join = join.Cast<LogicalComparisonJoin>();
317156 // turn the conditions into filters
317157 for (auto &i : comp_join.conditions) {
317158 auto condition = JoinCondition::CreateExpression(std::move(i));
317159 if (AddFilter(std::move(condition)) == FilterResult::UNSATISFIABLE) {
317160 // filter statically evaluates to false, strip tree
317161 return make_uniq<LogicalEmptyResult>(std::move(op));
317162 }
317163 }
317164 }
317165 GenerateFilters();
317166
317167 // turn the inner join into a cross product
317168 auto cross_product = make_uniq<LogicalCrossProduct>(std::move(op->children[0]), std::move(op->children[1]));
317169
317170 // preserve the estimated cardinality of the operator
317171 if (op->has_estimated_cardinality) {
317172 cross_product->SetEstimatedCardinality(op->estimated_cardinality);
317173 }
317174
317175 // then push down cross product
317176 return PushdownCrossProduct(std::move(cross_product));
317177}
unique_ptr< LogicalOperator > PushFiltersIntoDelimJoin(unique_ptr< LogicalOperator > op)
Definition duckdb.cpp:310057
static unique_ptr< Expression > CreateExpression(JoinCondition cond)
Turns the JoinCondition into an expression; note that this destroys the JoinCondition as the expressi...
Definition duckdb.cpp:380359

◆ PushdownLeftJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownLeftJoin ( unique_ptr< LogicalOperator > op,
unordered_set< idx_t > & left_bindings,
unordered_set< idx_t > & right_bindings )
private
317257 {
317258 auto &join = op->Cast<LogicalJoin>();
317259 FilterPushdown left_pushdown(optimizer, convert_mark_joins), right_pushdown(optimizer, convert_mark_joins);
317260 // for a comparison join we create a FilterCombiner that checks if we can push conditions on LHS join conditions
317261 // into the RHS of the join
317262 FilterCombiner filter_combiner(optimizer);
317263 const auto isComparison = (op->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
317264 op->type == LogicalOperatorType::LOGICAL_ASOF_JOIN);
317265 if (isComparison) {
317266 // add all comparison conditions
317267 auto &comparison_join = op->Cast<LogicalComparisonJoin>();
317268 for (auto &cond : comparison_join.conditions) {
317269 filter_combiner.AddFilter(
317270 make_uniq<BoundComparisonExpression>(cond.comparison, cond.left->Copy(), cond.right->Copy()));
317271 }
317272 }
317273 // now check the set of filters
317274 vector<unique_ptr<Filter>> remaining_filters;
317275 for (idx_t i = 0; i < filters.size(); i++) {
317276 auto side = JoinSide::GetJoinSide(filters[i]->bindings, left_bindings, right_bindings);
317277 if (side == JoinSide::LEFT) {
317278 // bindings match left side
317279 // we can push the filter into the left side
317280 if (isComparison) {
317281 // we MIGHT be able to push it down the RHS as well, but only if it is a comparison that matches the
317282 // join predicates we use the FilterCombiner to figure this out add the expression to the FilterCombiner
317283 filter_combiner.AddFilter(filters[i]->filter->Copy());
317284 }
317285 left_pushdown.filters.push_back(std::move(filters[i]));
317286 // erase the filter from the list of filters
317287 filters.erase_at(i);
317288 i--;
317289 } else if (op->type != LogicalOperatorType::LOGICAL_ASOF_JOIN) {
317290 // bindings match right side or both sides: we cannot directly push it into the right
317291 // however, if the filter removes rows with null values from the RHS we can turn the left outer join
317292 // in an inner join, and then push down as we would push down an inner join
317293 // Edit: This is only possible if the bindings match BOTH sides, so the filter can be pushed down to both
317294 // children. If the filter can only be applied to the right side, and the filter filters
317295 // all tuples, then the inner join cannot be converted.
317296 if (FilterRemovesNull(optimizer.context, optimizer.rewriter, filters[i]->filter.get(), right_bindings)) {
317297 // the filter removes NULL values, turn it into an inner join
317298 join.join_type = JoinType::INNER;
317299 // now we can do more pushdown
317300 // move all filters we added to the left_pushdown back into the filter list
317301 for (auto &left_filter : left_pushdown.filters) {
317302 filters.push_back(std::move(left_filter));
317303 }
317304 for (auto &filter : remaining_filters) {
317305 filters.push_back(std::move(filter));
317306 }
317307 // now push down the inner join
317308 return PushdownInnerJoin(std::move(op), left_bindings, right_bindings);
317309 }
317310 // we should keep the filters which do not remove NULL values
317311 remaining_filters.push_back(std::move(filters[i]));
317312 filters.erase_at(i);
317313 i--;
317314 }
317315 }
317316 // finally we check the FilterCombiner to see if there are any predicates we can push into the RHS
317317 // we only added (1) predicates that have JoinSide::BOTH from the conditions, and
317318 // (2) predicates that have JoinSide::LEFT from the filters
317319 // we check now if this combination generated any new filters that are only on JoinSide::RIGHT
317320 // this happens if, e.g. a join condition is (i=a) and there is a filter (i=500), we can then push the filter
317321 // (a=500) into the RHS
317322 filter_combiner.GenerateFilters([&](unique_ptr<Expression> filter) {
317323 if (JoinSide::GetJoinSide(*filter, left_bindings, right_bindings) == JoinSide::RIGHT) {
317324 right_pushdown.AddFilter(std::move(filter));
317325 }
317326 });
317327 right_pushdown.GenerateFilters();
317328 op->children[0] = left_pushdown.Rewrite(std::move(op->children[0]));
317329
317330 bool rewrite_right = true;
317331 if (op->type == LogicalOperatorType::LOGICAL_ANY_JOIN) {
317332 auto &any_join = join.Cast<LogicalAnyJoin>();
317333 if (AddFilter(any_join.condition->Copy()) == FilterResult::UNSATISFIABLE) {
317334 // filter statically evaluates to false, turns it to the cross product join with 1 row NULLs
317335 if (any_join.join_type == JoinType::LEFT) {
317336 unordered_map<idx_t, vector<unique_ptr<Expression>>> projections_groups;
317337 auto column_bindings = op->children[1]->GetColumnBindings();
317338 op->children[1]->ResolveOperatorTypes();
317339 auto &types = op->children[1]->types;
317340 for (idx_t i = 0; i < column_bindings.size(); i++) {
317341 projections_groups[column_bindings[i].table_index].emplace_back(
317342 make_uniq<BoundConstantExpression>(Value(types[i])));
317343 }
317344
317345 auto create_proj_dummy_scan = [&](idx_t table_index) {
317346 auto dummy_scan = make_uniq<LogicalDummyScan>(optimizer.binder.GenerateTableIndex());
317347 auto proj = make_uniq<LogicalProjection>(table_index, std::move(projections_groups[table_index]));
317348 proj->AddChild(std::move(dummy_scan));
317349 return proj;
317350 };
317351 // make cross products on the RHS first
317352 auto begin = projections_groups.begin();
317353 D_ASSERT(begin != projections_groups.end());
317354 unique_ptr<LogicalOperator> left = create_proj_dummy_scan(begin->first);
317355 projections_groups.erase(begin);
317356 for (auto &group : projections_groups) {
317357 auto proj = create_proj_dummy_scan(group.first);
317358 auto op = LogicalCrossProduct::Create(std::move(left), std::move(proj));
317359 left = std::move(op);
317360 }
317361 // then make cross product with the LHS
317362 op = LogicalCrossProduct::Create(std::move(op->children[0]), std::move(left));
317363 rewrite_right = false;
317364 }
317365 }
317366 }
317367
317368 if (rewrite_right) {
317369 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
317370 }
317371
317372 for (auto &filter : remaining_filters) {
317373 filters.push_back(std::move(filter));
317374 }
317375
317376 return PushFinalFilters(std::move(op));
317377}
unique_ptr< LogicalOperator > PushFinalFilters(unique_ptr< LogicalOperator > op)
Push any remaining filters into a LogicalFilter at this level.
Definition duckdb.cpp:310048

◆ PushdownOuterJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownOuterJoin ( unique_ptr< LogicalOperator > op,
unordered_set< idx_t > & left_bindings,
unordered_set< idx_t > & right_bindings )
private
317659 {
317660 if (op->type != LogicalOperatorType::LOGICAL_COMPARISON_JOIN) {
317661 return FinishPushdown(std::move(op));
317662 }
317663
317664 auto &join = op->Cast<LogicalComparisonJoin>();
317665 D_ASSERT(join.join_type == JoinType::OUTER);
317666
317667 FilterPushdown left_pushdown(optimizer, convert_mark_joins), right_pushdown(optimizer, convert_mark_joins);
317668 auto has_applied_pushdown = PushDownFiltersOnCoalescedEqualJoinKeys(
317669 filters, join.conditions, [&](unique_ptr<Expression> filter) { left_pushdown.AddFilter(std::move(filter)); },
317670 [&](unique_ptr<Expression> filter) { right_pushdown.AddFilter(std::move(filter)); });
317671
317672 if (!has_applied_pushdown) {
317673 return FinishPushdown(std::move(op));
317674 }
317675
317676 left_pushdown.GenerateFilters();
317677 right_pushdown.GenerateFilters();
317678 op->children[0] = left_pushdown.Rewrite(std::move(op->children[0]));
317679 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
317680 return PushFinalFilters(std::move(op));
317681}
static bool PushDownFiltersOnCoalescedEqualJoinKeys(vector< unique_ptr< Filter > > &filters, const vector< JoinCondition > &join_conditions, const std::function< void(unique_ptr< Expression > filter)> &pushdown_left, const std::function< void(unique_ptr< Expression > filter)> &pushdown_right)
Definition duckdb.cpp:317581

◆ PushdownSemiAntiJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownSemiAntiJoin ( unique_ptr< LogicalOperator > op)
private
317773 {
317774 auto &join = op->Cast<LogicalJoin>();
317775
317776 // push all current filters down the left side
317777 op->children[0] = Rewrite(std::move(op->children[0]));
317778 FilterPushdown right_pushdown(optimizer, convert_mark_joins);
317779 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
317780
317781 bool left_empty = op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT;
317782 bool right_empty = op->children[1]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT;
317783 if (left_empty && right_empty) {
317784 // both empty: return empty result
317785 return make_uniq<LogicalEmptyResult>(std::move(op));
317786 }
317787 // TODO: if semi/anti join is created from a intersect/except statement, then we can
317788 // push filters down into both children.
317789 // filter pushdown happens before join order optimization, so right_anti and right_semi are not possible yet here
317790 if (left_empty) {
317791 // left child is empty result
317792 switch (join.join_type) {
317793 case JoinType::ANTI:
317794 case JoinType::SEMI:
317795 return make_uniq<LogicalEmptyResult>(std::move(op));
317796 default:
317797 break;
317798 }
317799 } else if (right_empty) {
317800 // right child is empty result
317801 switch (join.join_type) {
317802 case JoinType::ANTI:
317803 // just return the left child.
317804 return std::move(op->children[0]);
317805 case JoinType::SEMI:
317806 return make_uniq<LogicalEmptyResult>(std::move(op));
317807 default:
317808 break;
317809 }
317810 }
317811 return op;
317812}

◆ PushdownMarkJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownMarkJoin ( unique_ptr< LogicalOperator > op,
unordered_set< idx_t > & left_bindings,
unordered_set< idx_t > & right_bindings )
private
317409 {
317410 auto op_bindings = op->GetColumnBindings();
317411 auto &join = op->Cast<LogicalJoin>();
317412 auto &comp_join = op->Cast<LogicalComparisonJoin>();
317413 D_ASSERT(join.join_type == JoinType::MARK);
317414 D_ASSERT(op->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
317415 op->type == LogicalOperatorType::LOGICAL_DELIM_JOIN || op->type == LogicalOperatorType::LOGICAL_ASOF_JOIN);
317416
317417 right_bindings.insert(comp_join.mark_index);
317418 FilterPushdown left_pushdown(optimizer, convert_mark_joins), right_pushdown(optimizer, convert_mark_joins);
317419#ifdef DEBUG
317420 bool simplified_mark_join = false;
317421#endif
317422 // now check the set of filters
317423 for (idx_t i = 0; i < filters.size(); i++) {
317424 auto side = JoinSide::GetJoinSide(filters[i]->bindings, left_bindings, right_bindings);
317425 if (side == JoinSide::LEFT) {
317426 // bindings match left side: push into left
317427 left_pushdown.filters.push_back(std::move(filters[i]));
317428 // erase the filter from the list of filters
317429 filters.erase_at(i);
317430 i--;
317431 } else if (side == JoinSide::RIGHT) {
317432#ifdef DEBUG
317433 D_ASSERT(!simplified_mark_join);
317434#endif
317435 // this filter references the marker
317436 // we can turn this into a SEMI join if the filter is on only the marker
317437 if (filters[i]->filter->GetExpressionType() == ExpressionType::BOUND_COLUMN_REF && convert_mark_joins &&
317438 comp_join.convert_mark_to_semi) {
317439 // filter just references the marker: turn into semi join
317440#ifdef DEBUG
317441 simplified_mark_join = true;
317442#endif
317443 join.join_type = JoinType::SEMI;
317444 filters.erase_at(i);
317445 i--;
317446 continue;
317447 }
317448 // if the filter is on NOT(marker) AND the join conditions are all set to "null_values_are_equal" we can
317449 // turn this into an ANTI join if all join conditions have null_values_are_equal=true, then the result of
317450 // the MARK join is always TRUE or FALSE, and never NULL this happens in the case of a correlated EXISTS
317451 // clause
317452 if (filters[i]->filter->GetExpressionType() == ExpressionType::OPERATOR_NOT) {
317453 auto &op_expr = filters[i]->filter->Cast<BoundOperatorExpression>();
317454 if (op_expr.children[0]->GetExpressionType() == ExpressionType::BOUND_COLUMN_REF) {
317455 // the filter is NOT(marker), check the join conditions
317456 bool all_null_values_are_equal = true;
317457 for (auto &cond : comp_join.conditions) {
317458 if (cond.comparison != ExpressionType::COMPARE_DISTINCT_FROM &&
317459 cond.comparison != ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
317460 all_null_values_are_equal = false;
317461 break;
317462 }
317463 }
317464 if (all_null_values_are_equal && convert_mark_joins && comp_join.convert_mark_to_semi) {
317465#ifdef DEBUG
317466 simplified_mark_join = true;
317467#endif
317468 // all null values are equal, convert to ANTI join
317469 join.join_type = JoinType::ANTI;
317470 filters.erase_at(i);
317471 i--;
317472 continue;
317473 }
317474 }
317475 }
317476 }
317477 }
317478 op->children[0] = left_pushdown.Rewrite(std::move(op->children[0]));
317479 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
317480 return PushFinalFilters(std::move(op));
317481}

◆ PushdownSingleJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushdownSingleJoin ( unique_ptr< LogicalOperator > op,
unordered_set< idx_t > & left_bindings,
unordered_set< idx_t > & right_bindings )
private
317932 {
317933 D_ASSERT(op->Cast<LogicalJoin>().join_type == JoinType::SINGLE);
317934 FilterPushdown left_pushdown(optimizer, convert_mark_joins), right_pushdown(optimizer, convert_mark_joins);
317935 // now check the set of filters
317936 for (idx_t i = 0; i < filters.size(); i++) {
317937 auto side = JoinSide::GetJoinSide(filters[i]->bindings, left_bindings, right_bindings);
317938 if (side == JoinSide::LEFT) {
317939 // bindings match left side: push into left
317940 left_pushdown.filters.push_back(std::move(filters[i]));
317941 // erase the filter from the list of filters
317942 filters.erase_at(i);
317943 i--;
317944 }
317945 }
317946 op->children[0] = left_pushdown.Rewrite(std::move(op->children[0]));
317947 op->children[1] = right_pushdown.Rewrite(std::move(op->children[1]));
317948 return PushFinalFilters(std::move(op));
317949}

◆ AddLogicalFilter()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::AddLogicalFilter ( unique_ptr< LogicalOperator > op,
vector< unique_ptr< Expression > > expressions )
private
310027 {
310028 if (expressions.empty()) {
310029 // No left expressions, so needn't to add an extra filter operator.
310030 return op;
310031 }
310032 auto filter = make_uniq<LogicalFilter>();
310033 if (op->has_estimated_cardinality) {
310034 // set the filter's estimated cardinality as the child op's.
310035 // if the filter is created during the filter pushdown optimization, the estimated cardinality will be later
310036 // overridden during the join order optimization to a more accurate one.
310037 // if the filter is created during the statistics propagation, the estimated cardinality won't be set unless set
310038 // here. assuming the filters introduced during the statistics propagation have little effect in reducing the
310039 // cardinality, we adopt the the cardinality of the child. this could be improved by MinMax info from the
310040 // statistics propagation
310041 filter->SetEstimatedCardinality(op->estimated_cardinality);
310042 }
310043 filter->expressions = std::move(expressions);
310044 filter->children.push_back(std::move(op));
310045 return std::move(filter);
310046}

◆ PushFinalFilters()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushFinalFilters ( unique_ptr< LogicalOperator > op)
private

Push any remaining filters into a LogicalFilter at this level.

310048 {
310049 vector<unique_ptr<Expression>> expressions;
310050 for (auto &f : filters) {
310051 expressions.push_back(std::move(f->filter));
310052 }
310053
310054 return AddLogicalFilter(std::move(op), std::move(expressions));
310055}

◆ FinishPushdown()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::FinishPushdown ( unique_ptr< LogicalOperator > op)
private
310102 {
310103 // unhandled type, first perform filter pushdown in its children
310104 for (auto &child : op->children) {
310105 FilterPushdown pushdown(optimizer, convert_mark_joins);
310106 child = pushdown.Rewrite(std::move(child));
310107 }
310108 // now push any existing filters
310109 return PushFinalFilters(std::move(op));
310110}

◆ PushFiltersIntoDelimJoin()

unique_ptr< LogicalOperator > duckdb::FilterPushdown::PushFiltersIntoDelimJoin ( unique_ptr< LogicalOperator > op)
private

Adds a filter to the set of filters. Returns FilterResult::UNSATISFIABLE if the subtree should be stripped, or FilterResult::SUCCESS otherwise

310057 {
310058 for (idx_t i = 0; i < filters.size(); i++) {
310059 auto &f = *filters[i];
310060 for (auto &child : op->children) {
310061 FilterPushdown pushdown(optimizer, convert_mark_joins);
310062
310063 // check if filter bindings can be applied to the child bindings.
310064 auto child_bindings = child->GetColumnBindings();
310065 unordered_set<idx_t> child_bindings_table;
310066 for (auto &binding : child_bindings) {
310067 child_bindings_table.insert(binding.table_index);
310068 }
310069
310070 // Check if ALL bindings of the filter are present in the child
310071 bool should_push = true;
310072 for (auto &binding : f.bindings) {
310073 if (child_bindings_table.find(binding) == child_bindings_table.end()) {
310074 should_push = false;
310075 break;
310076 }
310077 }
310078
310079 if (!should_push) {
310080 continue;
310081 }
310082
310083 // copy the filter
310084 auto filter_copy = f.filter->Copy();
310085 if (pushdown.AddFilter(std::move(filter_copy)) == FilterResult::UNSATISFIABLE) {
310086 return make_uniq<LogicalEmptyResult>(std::move(op));
310087 }
310088
310089 // push the filter into the child.
310090 pushdown.GenerateFilters();
310091 child = pushdown.Rewrite(std::move(child));
310092
310093 // Don't push same filter again
310094 filters.erase_at(i);
310095 i--;
310096 break;
310097 }
310098 }
310099 return op;
310100}
Here is the call graph for this function:

◆ AddFilter()

FilterResult duckdb::FilterPushdown::AddFilter ( unique_ptr< Expression > expr)
private
309996 {
309997 if (PushFilters() == FilterResult::UNSATISFIABLE) {
309998 return FilterResult::UNSATISFIABLE;
309999 }
310000 // split up the filters by AND predicate
310001 vector<unique_ptr<Expression>> expressions;
310002 expressions.push_back(std::move(expr));
310003 LogicalFilter::SplitPredicates(expressions);
310004 // push the filters into the combiner
310005 for (auto &child_expr : expressions) {
310006 if (combiner.AddFilter(std::move(child_expr)) == FilterResult::UNSATISFIABLE) {
310007 return FilterResult::UNSATISFIABLE;
310008 }
310009 }
310010 return FilterResult::SUCCESS;
310011}

◆ ExtractFilterBindings()

void duckdb::FilterPushdown::ExtractFilterBindings ( const Expression & expr,
vector< ColumnBinding > & bindings )
private

Extract filter bindings to compare them with expressions in an operator and determine if the filter can be pushed down

316834 {
316835 ExpressionIterator::VisitExpression<BoundColumnRefExpression>(
316836 expr, [&](const BoundColumnRefExpression &colref) { bindings.push_back(colref.binding); });
316837}
Here is the caller graph for this function:

◆ GenerateFilters()

void duckdb::FilterPushdown::GenerateFilters ( )
private

Generate filters from the current set of filters stored in the FilterCombiner.

310013 {
310014 if (!filters.empty()) {
310015 D_ASSERT(!combiner.HasFilters());
310016 return;
310017 }
310018 combiner.GenerateFilters([&](unique_ptr<Expression> filter) {
310019 auto f = make_uniq<Filter>();
310020 f->filter = std::move(filter);
310021 f->ExtractBindings();
310022 filters.push_back(std::move(f));
310023 });
310024}
Here is the caller graph for this function:

◆ PushFilters()

FilterResult duckdb::FilterPushdown::PushFilters ( )
private

if there are filters in this FilterPushdown node, push them into the combiner. Returns FilterResult::UNSATISFIABLE if the subtree should be stripped, or FilterResult::SUCCESS otherwise

309983 {
309984 for (auto &f : filters) {
309985 auto result = combiner.AddFilter(std::move(f->filter));
309986 D_ASSERT(result != FilterResult::UNSUPPORTED);
309987 if (result == FilterResult::UNSATISFIABLE) {
309988 // one of the filters is unsatisfiable - abort filter pushdown
309989 return FilterResult::UNSATISFIABLE;
309990 }
309991 }
309992 filters.clear();
309993 return FilterResult::SUCCESS;
309994}
Here is the caller graph for this function:

The documentation for this class was generated from the following file: