1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00
Files
mariadb/sql/opt_join_runs.cc
Dave Gosselin 4561ccad91 MDEV-40620: Allow a nested join as a FULL JOIN operand
Phase 2 rejected two shapes.  A FULL JOIN whose right operand was a
nested join raised ER_FULL_JOIN_BASE_TABLES_ONLY, and a FULL JOIN on the
inner side of an enclosing LEFT or RIGHT JOIN raised
ER_FULL_JOIN_NOT_ALLOWED_IN_OUTER_JOIN.  Both shapes are allowed now and
neither error is raised any more.

A FULL JOIN emits the rows of one side that never matched with the other
side null complemented.  An enclosing join's condition has to see those
rows complete, which it cannot do while the operand is only a set of
tables spread through the join order.  An operand that is a nested join
is therefore computed into a temporary table before the enclosing join
runs, so one JOIN_TAB stands for the whole operand and carries the FULL
JOIN's own marks.

The chosen plan has to keep such an operand on an unbroken span of the
join order, and that span becomes a run of JOIN_TABs of its own, the way
a materialized semi join already is.  Nothing outside the operand is
positioned while the operand is computed, so a table inside it can carry
no condition that names an outside table, an equality class that spans
the boundary is split at it, and the outer join scope of a join outside
it stops there.  A nest goes on naming a table that table elimination
removed, and an eliminated table is a const table, so the test for
entering the run leaves the eliminated tables out.  The test for
entering a materialized semi join run reads the same way.

The operand keeps a copy of its own ON expression, since simplifying the
join tree merges an inner join's ON into the enclosing outer join's ON,
which is not equivalent for a FULL JOIN because it changes which rows of
the right side never match.  An operand can itself be built from more
than one join, so an ON expression can sit a level or two below the top
of it, and every such expression is recorded on the operand as a whole,
which is the nest that reads them back.  A predicate over the operand is
checked again at the entry standing for it, once the null complementing
has happened.  A FULL JOIN's left side WHERE predicates are deferred to
the table that completes its right side even when that side is a nest.
The enclosing ON contributes no access path for the operand.

Reading a row back restores what a nest table cannot hold on its own,
whether the row was null complemented for a column declared NOT NULL,
and the status that says whether the table holds a usable row at all.

The JOIN_TAB standing for a run covers every table of the nest, the
eliminated ones included.  The plan output therefore skips only a
JOIN_TAB that is a table of the query, since a JOIN_TAB standing for a
run is never eliminated.

The FULL JOIN code moves out of sql_select.cc, sql_base.cc and table.cc
into opt_full_join.cc and opt_full_join.h.  JOIN::full_joins holds a
Full_join_info, declared in opt_full_join_info.h, for each FULL JOIN
that simplify_joins did not rewrite.  It holds the two sides, the WHERE
conjuncts held back for the FULL JOIN, the tab that reads its right
side, the duplicate filter of the right rows that matched, and the next
FULL JOIN whose null complement pass runs from the same tab.  It
replaces fj_dups, fj_first_target and fj_next_target on JOIN_TAB and
fj_left_cond on TABLE_LIST.  The list is built for each JOIN, so a
prepared statement starts every execution with nothing held.

opt_join_runs.cc and opt_join_runs.h hold the runs.
get_best_combination records every run of the chosen join order in a
Join_run_layout before it writes any JOIN_TAB, and the layout gives the
exact number of top level JOIN_TABs.  make_join_select walks the plan
with Join_tab_walk, which keeps the tables a run sets aside on entry
until the walk reaches the run's placeholder.  A FULL JOIN run can be
inside a materialized semi join, so the walk keeps the runs on a stack.

make_join_select decides again which conditions wait for a FULL JOIN's
right partner, on each condition about to be attached to a table.
Distributing the WHERE builds conditions for one table out of
disjunctions that span both sides, and a HAVING predicate moved into
the WHERE can name the left side alone, so the choice made while the
WHERE is rewritten misses both.  That earlier choice stays, since it
keeps the left side out of ref and range analysis.  Whether a condition
reads a table after the partner is decided from the join order
positions of the tables it reads.

An enclosing join's ON supplies no key value for a table on a FULL
JOIN's left side, because the null complement pass is complete only
once every left row has been read.  A key value that reads only tables
of the same left side is still allowed.  An ORDER BY over a FULL JOIN
that simplify_joins kept sorts through a temporary table, because the
null complement pass emits right rows after the scan of the first
table.  EXPLAIN gathers the tables of each run at its own depth, and an
operand nest inside a materialized semi join reports under that semi
join's id.

Sections 22 to 28 cover the new shapes.  The section covering more than
one matching right side row becomes Section 28, which adds a nest operand
to the same case, and the two sections that came with the fixes before
this one are renumbered to follow it.  Sections 35 to 42 cover an ON
expression below the top of an operand, an enclosing ON that keeps the
left side NULLs, operand nests that table elimination shrinks, a
condition offered to a table inside a run, a prepared WHERE that folds
away on a later execution, a FULL JOIN run inside a materialized semi
join, and a left side condition over a table before the partner.
Sections 8, 18 and 27 gain left side predicates that appear only once
the WHERE is rewritten.  The helper that gives every column an index of
its own comes with them, ahead of the axis that turns it on.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-21 08:02:24 -04:00

291 lines
7.5 KiB
C++

/*
Copyright (c) 2026, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
/**
@file
@brief
Where the runs of a picked join order begin and end, and the opening
and finishing of their JOIN_TAB arrays.
*/
#include "mariadb.h"
#include "sql_priv.h"
#include "sql_select.h"
#include "opt_full_join.h"
#include "opt_join_runs.h"
/* Used to build a pointer to a permanently null Item * for on_expr_ref. */
static Item * const null_ptr= NULL;
/*
The materialized semi join whose run begins at the given position of
the picked join order, otherwise NULL.
*/
static SJ_MATERIALIZATION_INFO *sjm_run_at(JOIN *join, uint pos)
{
POSITION *cur_pos= &join->best_positions[pos];
if (cur_pos->sj_strategy != SJ_OPT_MATERIALIZE &&
cur_pos->sj_strategy != SJ_OPT_MATERIALIZE_SCAN)
return NULL;
return cur_pos->table->emb_sj_nest->sj_mat_info;
}
/*
Whether run a is opened before run b, in the order described at
Join_run_layout.
*/
static bool opens_before(const Join_run *a, const Join_run *b)
{
if (a->first != b->first)
return a->first < b->first;
if (a->count != b->count)
return a->count > b->count;
return a->kind == JOIN_TAB_RANGE_SJM && b->kind != JOIN_TAB_RANGE_SJM;
}
/*
Record the runs of the picked join order. The semi join strategies
have to be final.
RETURN
false OK
true Out of memory
*/
bool Join_run_layout::init(JOIN *join)
{
const Join_run *top= NULL;
runs= NULL;
run_count= 0;
max_runs= 0;
/*
The FULL JOIN operand nests are strictly nested or disjoint and each
run of one holds at least two tables, so there are fewer of them than
there are tables. At most one semi join run begins at a position.
*/
if (join->full_join_nest_tables)
max_runs+= join->table_count;
for (uint i= 0; i < join->table_count; i++)
{
if (sjm_run_at(join, i))
max_runs++;
}
if (max_runs && !(runs= join->thd->alloc<Join_run>(max_runs)))
return true;
if (join->full_join_nest_tables)
record_full_join_runs(join, this);
for (uint i= 0; i < join->table_count; i++)
{
SJ_MATERIALIZATION_INFO *sjm= sjm_run_at(join, i);
if (sjm)
add(JOIN_TAB_RANGE_SJM, i, sjm->tables, NULL, sjm);
}
/*
A run that begins at a position the previous top level run covers is
held by it. Every other run takes one entry of the top level array
for all the positions it covers.
*/
top_level_count= join->table_count;
for (const Join_run *run= begin(); run != end(); run++)
{
if (top && run->first <= top->last())
continue;
top= run;
top_level_count-= run->count - 1;
}
return false;
}
/*
Add a run, keeping the runs in the order they are opened. Runs that
opens_before() does not order keep the order they were added in.
*/
void Join_run_layout::add(Join_tab_range_type kind, uint first, uint count,
TABLE_LIST *nest, SJ_MATERIALIZATION_INFO *sjm)
{
Join_run run= { kind, first, count, nest, sjm };
uint i;
DBUG_ASSERT(run_count < max_runs);
for (i= run_count; i && opens_before(&run, &runs[i - 1]); i--)
runs[i]= runs[i - 1];
runs[i]= run;
run_count++;
}
/*
Open a run at entry j of the array being filled in. j becomes the
run's placeholder and is then pointed at the first entry of the run's
own array, so the tables to follow and any run opened among them go
into it.
The array has an entry for each position the run covers, which is an
upper bound because a run inside it takes one entry for two positions
or more. The array is narrowed to the entries used when the run
finishes.
A semi join placeholder reads the temporary table holding the semi
join's result, and takes its row count from the materialization. A
FULL JOIN placeholder is recorded on its nest.
RETURN
false OK
true Out of memory
*/
bool Join_run_stack::open(THD *thd, JOIN *join, const Join_run *run,
JOIN_TAB *&j)
{
JOIN_TAB *children;
DBUG_ASSERT(!depth || run->last() <= last[depth - 1]);
if (!(children= thd->alloc<JOIN_TAB>(run->count)))
return true;
bzero((void*) j, sizeof(JOIN_TAB));
j->join= join;
j->table= NULL;
j->ref.key= -1;
j->on_expr_ref= (Item**) &null_ptr;
j->bush_root_tab= innermost();
j->bush_children.start= children;
j->bush_children.end= children + run->count;
j->bush_children.kind= run->kind;
j->bush_children.nest= run->nest;
if (run->kind == JOIN_TAB_RANGE_SJM)
{
SJ_MATERIALIZATION_INFO *sjm= run->sjm;
/* The unique index is always in 'possible keys' in EXPLAIN */
j->keys= key_map(1);
j->records_read= (sjm->is_sj_scan ? sjm->rows : 1.0);
j->records_init= j->records_out= j->records_read;
j->records= (ha_rows) j->records_read;
j->cond_selectivity= 1.0;
j->join_read_time= 0.0; /* Not saved currently */
j->join_loops= 0.0;
join->join_tab_ranges.push_back(&j->bush_children, thd->mem_root);
}
else
{
run->nest->nested_join->materialized_full_join= TRUE;
run->nest->nested_join->materialized_full_join_tab= j;
if (join->join_tab_ranges.push_back(&j->bush_children, thd->mem_root))
return true;
}
DBUG_ASSERT(depth < MAX_TABLES);
placeholder[depth]= j;
last[depth]= run->last();
depth++;
j= children;
return false;
}
/*
Finish the runs whose last position is pos, innermost first, once j,
the entry for pos, is filled in. j is the last entry of the innermost
of them, and the placeholder of each finished run is the last entry of
the run that holds it.
RETURN
The entry the next position follows, which is the placeholder of the
outermost run finished here, or j when no run ends at pos.
*/
JOIN_TAB *Join_run_stack::finish_ended(uint pos, JOIN_TAB *j)
{
while (depth && last[depth - 1] == pos)
{
JOIN_TAB *root= placeholder[--depth];
j->last_leaf_in_bush= TRUE;
root->bush_children.end= j + 1;
j= root;
}
return j;
}
Join_tab_walk::Join_tab_walk(JOIN *join_arg)
: join(join_arg), cur(first_depth_first_tab(join_arg)), depth(0)
{}
void Join_tab_walk::next()
{
if (ends_set_aside())
depth--;
cur= next_depth_first_tab(join, cur);
}
/*
The placeholder of the outermost run of the given kind that holds the
current JOIN_TAB, or NULL when no such run holds it.
*/
JOIN_TAB *Join_tab_walk::outermost_run(Join_tab_range_type kind) const
{
JOIN_TAB *found= NULL;
for (JOIN_TAB *root= cur->bush_root_tab; root; root= root->bush_root_tab)
{
if (root->bush_children.kind == kind)
found= root;
}
return found;
}
/*
Set map aside until the walk reaches run, the placeholder of a run
that holds the current JOIN_TAB.
*/
void Join_tab_walk::set_aside(JOIN_TAB *run, table_map map)
{
DBUG_ASSERT(run);
#ifndef DBUG_OFF
if (depth)
{
JOIN_TAB *root= run->bush_root_tab;
while (root && root != placeholder[depth - 1])
root= root->bush_root_tab;
DBUG_ASSERT(root);
}
#endif
DBUG_ASSERT(depth < MAX_TABLES);
placeholder[depth]= run;
set_aside_map[depth]= map;
depth++;
}