OSDN Git Service

Change copyright year to 2020.
[pghintplan/pg_hint_plan.git] / make_join_rel.c
index 3877fb2..863fbd7 100644 (file)
@@ -1,29 +1,21 @@
 /*-------------------------------------------------------------------------
  *
  * make_join_rel.c
- *       Routines copied from PostgreSQL core distribution.
+ *       Routines copied from PostgreSQL core distribution with some
+ *       modifications.
  *
  * src/backend/optimizer/path/joinrels.c
  *     make_join_rel()
  *
- * Portions Copyright (c) 2013, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
- * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
+ * Portions Copyright (c) 2013-2020, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *-------------------------------------------------------------------------
  */
 
 /*
- * make_join_rel
- *        Find or create a join RelOptInfo that represents the join of
- *        the two given rels, and add to it path information for paths
- *        created with the two rels as outer and inner rel.
- *        (The join rel may already contain paths generated from other
- *        pairs of rels that add up to the same set of base rels.)
- *
- * NB: will return NULL if attempted join is not valid.  This can happen
- * when working with outer joins, or with IN or EXISTS clauses that have been
- * turned into joins.
+ * adjust_rows: tweak estimated row numbers according to the hint.
  */
 static double
 adjust_rows(double rows, RowsHint *hint)
@@ -44,7 +36,7 @@ adjust_rows(double rows, RowsHint *hint)
        hint->base.state = HINT_STATE_USED;
        if (result < 1.0)
                ereport(WARNING,
-                               (errmsg("make rows estimation 1 since below 1 : %s",
+                               (errmsg("Force estimate to be at least one row, to avoid possible divide-by-zero when interpolating costs : %s",
                                        hint->base.hint_str)));
        result = clamp_row_est(result);
        elog(DEBUG1, "adjusted rows %d to %d", (int) rows, (int) result);
@@ -52,6 +44,18 @@ adjust_rows(double rows, RowsHint *hint)
        return result;
 }
 
+/*
+ * make_join_rel
+ *        Find or create a join RelOptInfo that represents the join of
+ *        the two given rels, and add to it path information for paths
+ *        created with the two rels as outer and inner rel.
+ *        (The join rel may already contain paths generated from other
+ *        pairs of rels that add up to the same set of base rels.)
+ *
+ * NB: will return NULL if attempted join is not valid.  This can happen
+ * when working with outer joins, or with IN or EXISTS clauses that have been
+ * turned into joins.
+ */
 RelOptInfo *
 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 {
@@ -62,9 +66,6 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
        RelOptInfo *joinrel;
        List       *restrictlist;
 
-       RowsHint   *rows_hint = NULL;
-       int                     i;
-
        /* We should never try to join two overlapping sets of rels. */
        Assert(!bms_overlap(rel1->relids, rel2->relids));
 
@@ -91,7 +92,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
        /*
         * If it's a plain inner join, then we won't have found anything in
-        * join_info_list.      Make up a SpecialJoinInfo so that selectivity
+        * join_info_list.  Make up a SpecialJoinInfo so that selectivity
         * estimation functions will know what's being joined.
         */
        if (sjinfo == NULL)
@@ -106,7 +107,10 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                /* we don't bother trying to make the remaining fields valid */
                sjinfo->lhs_strict = false;
                sjinfo->delay_upper_joins = false;
-               sjinfo->join_quals = NIL;
+               sjinfo->semi_can_btree = false;
+               sjinfo->semi_can_hash = false;
+               sjinfo->semi_operators = NIL;
+               sjinfo->semi_rhs_exprs = NIL;
        }
 
        /*
@@ -116,60 +120,83 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
        joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
                                                         &restrictlist);
 
-       /* Apply appropriate Rows hint to the join node, if any. */
-       for (i = 0; i < current_hint->num_hints[HINT_TYPE_ROWS]; i++)
+       /* !!! START: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
        {
-               rows_hint = current_hint->rows_hints[i];
+               RowsHint   *rows_hint = NULL;
+               int                     i;
+               RowsHint   *justforme = NULL;
+               RowsHint   *domultiply = NULL;
 
-               /*
-                * This Rows hint specifies aliasname is error, or does not exist in
-                * query.
-                */
-               if (!rows_hint->joinrelids ||
-                       rows_hint->base.state == HINT_STATE_ERROR)
-                       continue;
-               if (bms_equal(joinrelids, rows_hint->joinrelids))
+               /* Search for applicable rows hint for this join node */
+               for (i = 0; i < current_hint_state->num_hints[HINT_TYPE_ROWS]; i++)
                {
+                       rows_hint = current_hint_state->rows_hints[i];
+
                        /*
-                        * This join RelOptInfo is exactly a Rows hint specifies, so adjust
-                        * rows estimateion with the hint's content.  Here we never have
-                        * another hint which has same relation combination, so we can skip
-                        * rest of hints.
+                        * Skip this rows_hint if it is invalid from the first or it
+                        * doesn't target any join rels.
                         */
-                       if (rows_hint->base.state == HINT_STATE_NOTUSED)
-                               joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
+                       if (!rows_hint->joinrelids ||
+                               rows_hint->base.state == HINT_STATE_ERROR)
+                               continue;
+
+                       if (bms_equal(joinrelids, rows_hint->joinrelids))
+                       {
+                               /*
+                                * This joinrel is just the target of this rows_hint, so tweak
+                                * rows estimation according to the hint.
+                                */
+                               justforme = rows_hint;
+                       }
+                       else if (!(bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
+                                          bms_is_subset(rows_hint->joinrelids, rel2->relids)) &&
+                                        bms_is_subset(rows_hint->joinrelids, joinrelids) &&
+                                        rows_hint->value_type == RVT_MULTI)
+                       {
+                               /*
+                                * If the rows_hint's target relids is not a subset of both of
+                                * component rels and is a subset of this joinrel, ths hint's
+                                * targets spread over both component rels. This menas that
+                                * this hint has been never applied so far and this joinrel is
+                                * the first (and only) chance to fire in current join tree.
+                                * Only the multiplication hint has the cumulative nature so we
+                                * apply only RVT_MULTI in this way.
+                                */
+                               domultiply = rows_hint;
+                       }
                }
-               else if (bms_is_subset(rows_hint->joinrelids, rel1->relids) ||
-                                bms_is_subset(rows_hint->joinrelids, rel2->relids))
+
+               if (justforme)
                {
                        /*
-                        * Otherwise if the relation combination specified in thee Rows
-                        * hint is subset of the set of join elements, re-estimate rows and
-                        * costs again to reflect the adjustment done in down.  This is
-                        * necessary for the first permutation of the combination the
-                        * relations, but it's difficult to determine that this is the
-                        * first, so do this everytime.
+                        * If a hint just for me is found, no other adjust method is
+                        * useles, but this cannot be more than twice becuase this joinrel
+                        * is already adjusted by this hint.
                         */
-                       set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
-                                                                          restrictlist);
+                       if (justforme->base.state == HINT_STATE_NOTUSED)
+                               joinrel->rows = adjust_rows(joinrel->rows, justforme);
                }
-               else if (bms_is_subset(rows_hint->joinrelids, joinrelids))
+               else
                {
-                       /*
-                        * If the combination specifed in the Rows hints is subset of the
-                        * join relation and spreads over both children, 
-                        *
-                        * We do adjust rows estimation only when the value type was
-                        * multiplication, because other value types are meanless.
-                        */
-                       if (rows_hint->value_type == RVT_MULTI)
+                       if (domultiply)
                        {
+                               /*
+                                * If we have multiple routes up to this joinrel which are not
+                                * applicable this hint, this multiply hint will applied more
+                                * than twice. But there's no means to know of that,
+                                * re-estimate the row number of this joinrel always just
+                                * before applying the hint. This is a bit different from
+                                * normal planner behavior but it doesn't harm so much.
+                                */
                                set_joinrel_size_estimates(root, joinrel, rel1, rel2, sjinfo,
                                                                                   restrictlist);
-                               joinrel->rows = adjust_rows(joinrel->rows, rows_hint);
+                               
+                               joinrel->rows = adjust_rows(joinrel->rows, domultiply);
                        }
+                       
                }
        }
+       /* !!! END: HERE IS THE PART WHICH ADDED FOR PG_HINT_PLAN !!! */
 
        /*
         * If we've already proven this join is empty, we needn't consider any
@@ -181,6 +208,27 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                return joinrel;
        }
 
+       /* Add paths to the join relation. */
+       populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
+                                                               restrictlist);
+
+       bms_free(joinrelids);
+
+       return joinrel;
+}
+
+/*
+ * populate_joinrel_with_paths
+ *       Add paths to the given joinrel for given pair of joining relations. The
+ *       SpecialJoinInfo provides details about the join and the restrictlist
+ *       contains the join clauses and the other clauses applicable for given pair
+ *       of the joining relations.
+ */
+static void
+populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
+                                                       RelOptInfo *rel2, RelOptInfo *joinrel,
+                                                       SpecialJoinInfo *sjinfo, List *restrictlist)
+{
        /*
         * Consider paths using each rel as both outer and inner.  Depending on
         * the join type, a provably empty outer or inner rel might mean the join
@@ -203,7 +251,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
        {
                case JOIN_INNER:
                        if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
-                               restriction_is_constant_false(restrictlist, false))
+                               restriction_is_constant_false(restrictlist, joinrel, false))
                        {
                                mark_dummy_rel(joinrel);
                                break;
@@ -217,12 +265,12 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                        break;
                case JOIN_LEFT:
                        if (is_dummy_rel(rel1) ||
-                               restriction_is_constant_false(restrictlist, true))
+                               restriction_is_constant_false(restrictlist, joinrel, true))
                        {
                                mark_dummy_rel(joinrel);
                                break;
                        }
-                       if (restriction_is_constant_false(restrictlist, false) &&
+                       if (restriction_is_constant_false(restrictlist, joinrel, false) &&
                                bms_is_subset(rel2->relids, sjinfo->syn_righthand))
                                mark_dummy_rel(rel2);
                        add_paths_to_joinrel(root, joinrel, rel1, rel2,
@@ -234,7 +282,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                        break;
                case JOIN_FULL:
                        if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
-                               restriction_is_constant_false(restrictlist, true))
+                               restriction_is_constant_false(restrictlist, joinrel, true))
                        {
                                mark_dummy_rel(joinrel);
                                break;
@@ -270,7 +318,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                                bms_is_subset(sjinfo->min_righthand, rel2->relids))
                        {
                                if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
-                                       restriction_is_constant_false(restrictlist, false))
+                                       restriction_is_constant_false(restrictlist, joinrel, false))
                                {
                                        mark_dummy_rel(joinrel);
                                        break;
@@ -293,7 +341,7 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                                                                   sjinfo) != NULL)
                        {
                                if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
-                                       restriction_is_constant_false(restrictlist, false))
+                                       restriction_is_constant_false(restrictlist, joinrel, false))
                                {
                                        mark_dummy_rel(joinrel);
                                        break;
@@ -308,12 +356,12 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                        break;
                case JOIN_ANTI:
                        if (is_dummy_rel(rel1) ||
-                               restriction_is_constant_false(restrictlist, true))
+                               restriction_is_constant_false(restrictlist, joinrel, true))
                        {
                                mark_dummy_rel(joinrel);
                                break;
                        }
-                       if (restriction_is_constant_false(restrictlist, false) &&
+                       if (restriction_is_constant_false(restrictlist, joinrel, false) &&
                                bms_is_subset(rel2->relids, sjinfo->syn_righthand))
                                mark_dummy_rel(rel2);
                        add_paths_to_joinrel(root, joinrel, rel1, rel2,
@@ -326,7 +374,6 @@ make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
                        break;
        }
 
-       bms_free(joinrelids);
-
-       return joinrel;
+       /* Apply partitionwise join technique, if possible. */
+       try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
 }