Move clause_sides_match_join() into restrictinfo.h
authorDavid Rowley <[email protected]>
Tue, 15 Oct 2024 08:14:21 +0000 (21:14 +1300)
committerDavid Rowley <[email protected]>
Tue, 15 Oct 2024 08:14:21 +0000 (21:14 +1300)
Two near-identical copies of clause_sides_match_join() existed in
joinpath.c and analyzejoins.c.  Deduplicate this by moving the function
into restrictinfo.h.

It isn't quite clear that keeping the inline property of this function
is worthwhile, but this commit is just an exercise in code
deduplication.  More effort would be required to determine if the inline
property is worth keeping.

Author: James Hunter <[email protected]>
Discussion: https://postgr.es/m/CAJVSvF7Nm_9kgMLOch4c-5fbh3MYg%3D9BdnDx3Dv7Fcb64zr64Q%40mail.gmail.com

src/backend/optimizer/path/joinpath.c
src/backend/optimizer/plan/analyzejoins.c
src/include/optimizer/restrictinfo.h

index a244300463c3d23e8870fabbc08b54bcf947625d..39711384801e5dac4351e4d822f5e7500b899762 100644 (file)
@@ -25,6 +25,7 @@
 #include "optimizer/paths.h"
 #include "optimizer/placeholder.h"
 #include "optimizer/planmain.h"
+#include "optimizer/restrictinfo.h"
 #include "utils/lsyscache.h"
 #include "utils/typcache.h"
 
@@ -58,9 +59,6 @@ static void try_partial_mergejoin_path(PlannerInfo *root,
 static void sort_inner_and_outer(PlannerInfo *root, RelOptInfo *joinrel,
                                 RelOptInfo *outerrel, RelOptInfo *innerrel,
                                 JoinType jointype, JoinPathExtraData *extra);
-static inline bool clause_sides_match_join(RestrictInfo *rinfo,
-                                          RelOptInfo *outerrel,
-                                          RelOptInfo *innerrel);
 static void match_unsorted_outer(PlannerInfo *root, RelOptInfo *joinrel,
                                 RelOptInfo *outerrel, RelOptInfo *innerrel,
                                 JoinType jointype, JoinPathExtraData *extra);
@@ -470,7 +468,8 @@ paraminfo_get_equal_hashops(PlannerInfo *root, ParamPathInfo *param_info,
             * with 2 args.
             */
            if (!IsA(opexpr, OpExpr) || list_length(opexpr->args) != 2 ||
-               !clause_sides_match_join(rinfo, outerrel, innerrel))
+               !clause_sides_match_join(rinfo, outerrel->relids,
+                                        innerrel->relids))
            {
                list_free(*operators);
                list_free(*param_exprs);
@@ -1320,37 +1319,6 @@ try_partial_hashjoin_path(PlannerInfo *root,
                                          hashclauses));
 }
 
-/*
- * clause_sides_match_join
- *   Determine whether a join clause is of the right form to use in this join.
- *
- * We already know that the clause is a binary opclause referencing only the
- * rels in the current join.  The point here is to check whether it has the
- * form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
- * rather than mixing outer and inner vars on either side.  If it matches,
- * we set the transient flag outer_is_left to identify which side is which.
- */
-static inline bool
-clause_sides_match_join(RestrictInfo *rinfo, RelOptInfo *outerrel,
-                       RelOptInfo *innerrel)
-{
-   if (bms_is_subset(rinfo->left_relids, outerrel->relids) &&
-       bms_is_subset(rinfo->right_relids, innerrel->relids))
-   {
-       /* lefthand side is outer */
-       rinfo->outer_is_left = true;
-       return true;
-   }
-   else if (bms_is_subset(rinfo->left_relids, innerrel->relids) &&
-            bms_is_subset(rinfo->right_relids, outerrel->relids))
-   {
-       /* righthand side is outer */
-       rinfo->outer_is_left = false;
-       return true;
-   }
-   return false;               /* no good for these input relations */
-}
-
 /*
  * sort_inner_and_outer
  *   Create mergejoin join paths by explicitly sorting both the outer and
@@ -2264,7 +2232,8 @@ hash_inner_and_outer(PlannerInfo *root,
        /*
         * Check if clause has the form "outer op inner" or "inner op outer".
         */
-       if (!clause_sides_match_join(restrictinfo, outerrel, innerrel))
+       if (!clause_sides_match_join(restrictinfo, outerrel->relids,
+                                    innerrel->relids))
            continue;           /* no good for these input relations */
 
        /*
@@ -2549,7 +2518,8 @@ select_mergejoin_clauses(PlannerInfo *root,
        /*
         * Check if clause has the form "outer op inner" or "inner op outer".
         */
-       if (!clause_sides_match_join(restrictinfo, outerrel, innerrel))
+       if (!clause_sides_match_join(restrictinfo, outerrel->relids,
+                                    innerrel->relids))
        {
            have_nonmergeable_joinclause = true;
            continue;           /* no good for these input relations */
index 928d926645e863942d166416d62a52e1f7e93707..5bc16c4bfc76d75d149c1c50434f232b18b24665 100644 (file)
@@ -115,37 +115,6 @@ restart:
    return joinlist;
 }
 
-/*
- * clause_sides_match_join
- *   Determine whether a join clause is of the right form to use in this join.
- *
- * We already know that the clause is a binary opclause referencing only the
- * rels in the current join.  The point here is to check whether it has the
- * form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
- * rather than mixing outer and inner vars on either side.  If it matches,
- * we set the transient flag outer_is_left to identify which side is which.
- */
-static inline bool
-clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids,
-                       Relids innerrelids)
-{
-   if (bms_is_subset(rinfo->left_relids, outerrelids) &&
-       bms_is_subset(rinfo->right_relids, innerrelids))
-   {
-       /* lefthand side is outer */
-       rinfo->outer_is_left = true;
-       return true;
-   }
-   else if (bms_is_subset(rinfo->left_relids, innerrelids) &&
-            bms_is_subset(rinfo->right_relids, outerrelids))
-   {
-       /* righthand side is outer */
-       rinfo->outer_is_left = false;
-       return true;
-   }
-   return false;               /* no good for these input relations */
-}
-
 /*
  * join_is_removable
  *   Check whether we need not perform this special join at all, because
index 1b42c832c5923737f915ad2d4ec0ec4ef6e4209e..fe03a8ecd342c40c6899bc140c43dbb6725d4a63 100644 (file)
@@ -48,4 +48,35 @@ extern bool join_clause_is_movable_into(RestrictInfo *rinfo,
                                        Relids currentrelids,
                                        Relids current_and_outer);
 
+/*
+ * clause_sides_match_join
+ *   Determine whether a join clause is of the right form to use in this join.
+ *
+ * We already know that the clause is a binary opclause referencing only the
+ * rels in the current join.  The point here is to check whether it has the
+ * form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
+ * rather than mixing outer and inner vars on either side.  If it matches,
+ * we set the transient flag outer_is_left to identify which side is which.
+ */
+static inline bool
+clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids,
+                       Relids innerrelids)
+{
+   if (bms_is_subset(rinfo->left_relids, outerrelids) &&
+       bms_is_subset(rinfo->right_relids, innerrelids))
+   {
+       /* lefthand side is outer */
+       rinfo->outer_is_left = true;
+       return true;
+   }
+   else if (bms_is_subset(rinfo->left_relids, innerrelids) &&
+            bms_is_subset(rinfo->right_relids, outerrelids))
+   {
+       /* righthand side is outer */
+       rinfo->outer_is_left = false;
+       return true;
+   }
+   return false;               /* no good for these input relations */
+}
+
 #endif                         /* RESTRICTINFO_H */