Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
cc9bcbc8
Commit
cc9bcbc8
authored
Jul 03, 2005
by
Tom Lane
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve outer-join-deduction logic to be able to propagate equalities
through multiple join clauses.
parent
76eca0ec
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
202 additions
and
157 deletions
+202
-157
src/backend/optimizer/path/pathkeys.c
src/backend/optimizer/path/pathkeys.c
+202
-157
No files found.
src/backend/optimizer/path/pathkeys.c
View file @
cc9bcbc8
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.
69 2005/07/02 23:00:40
tgl Exp $
* $PostgreSQL: pgsql/src/backend/optimizer/path/pathkeys.c,v 1.
70 2005/07/03 18:26:32
tgl Exp $
*
*
*-------------------------------------------------------------------------
*-------------------------------------------------------------------------
*/
*/
...
@@ -35,6 +35,10 @@ static PathKeyItem *makePathKeyItem(Node *key, Oid sortop, bool checkType);
...
@@ -35,6 +35,10 @@ static PathKeyItem *makePathKeyItem(Node *key, Oid sortop, bool checkType);
static
void
generate_outer_join_implications
(
PlannerInfo
*
root
,
static
void
generate_outer_join_implications
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
List
*
equi_key_set
,
Relids
*
relids
);
Relids
*
relids
);
static
void
sub_generate_join_implications
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
Relids
*
relids
,
Node
*
item1
,
Oid
sortop1
,
Relids
item1_relids
);
static
void
process_implied_const_eq
(
PlannerInfo
*
root
,
static
void
process_implied_const_eq
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
Relids
*
relids
,
List
*
equi_key_set
,
Relids
*
relids
,
Node
*
item1
,
Oid
sortop1
,
Node
*
item1
,
Oid
sortop1
,
...
@@ -250,24 +254,16 @@ generate_implied_equalities(PlannerInfo *root)
...
@@ -250,24 +254,16 @@ generate_implied_equalities(PlannerInfo *root)
i1
++
;
i1
++
;
}
}
/*
* If we have constant(s) and outer joins, try to propagate the
* constants through outer-join quals.
*/
if
(
have_consts
&&
root
->
hasOuterJoins
)
generate_outer_join_implications
(
root
,
curset
,
relids
);
/*
* A set containing only two items cannot imply any equalities
* beyond the one that created the set, so we can skip it.
*/
if
(
nitems
<
3
)
continue
;
/*
/*
* Match each item in the set with all that appear after it (it's
* Match each item in the set with all that appear after it (it's
* sufficient to generate A=B, need not process B=A too).
* sufficient to generate A=B, need not process B=A too).
*
* A set containing only two items cannot imply any equalities
* beyond the one that created the set, so we can skip this
* processing in that case.
*/
*/
if
(
nitems
>=
3
)
{
i1
=
0
;
i1
=
0
;
foreach
(
ptr1
,
curset
)
foreach
(
ptr1
,
curset
)
{
{
...
@@ -310,6 +306,14 @@ generate_implied_equalities(PlannerInfo *root)
...
@@ -310,6 +306,14 @@ generate_implied_equalities(PlannerInfo *root)
i1
++
;
i1
++
;
}
}
}
}
/*
* If we have constant(s) and outer joins, try to propagate the
* constants through outer-join quals.
*/
if
(
have_consts
&&
root
->
hasOuterJoins
)
generate_outer_join_implications
(
root
,
curset
,
relids
);
}
}
}
/*
/*
...
@@ -362,118 +366,154 @@ generate_outer_join_implications(PlannerInfo *root,
...
@@ -362,118 +366,154 @@ generate_outer_join_implications(PlannerInfo *root,
List
*
equi_key_set
,
List
*
equi_key_set
,
Relids
*
relids
)
Relids
*
relids
)
{
{
ListCell
*
l1
;
ListCell
*
l
;
int
i
=
0
;
/*
Examine each mergejoinable outer-join clause with OUTERVAR on lef
t */
/*
Process each non-constant element of equi_key_se
t */
foreach
(
l
1
,
root
->
left_join_clauses
)
foreach
(
l
,
equi_key_set
)
{
{
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l1
);
PathKeyItem
*
item1
=
(
PathKeyItem
*
)
lfirst
(
l
);
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
if
(
!
bms_is_empty
(
relids
[
i
]))
ListCell
*
l2
;
{
sub_generate_join_implications
(
root
,
equi_key_set
,
relids
,
item1
->
key
,
item1
->
sortop
,
relids
[
i
]);
}
i
++
;
}
}
/*
* sub_generate_join_implications
* Propagate a constant equality through outer join clauses.
*
* The item described by item1/sortop1/item1_relids has been determined
* to be equal to the constant(s) listed in equi_key_set. Recursively
* trace out the implications of this.
*
* equi_key_set and relids are as for generate_outer_join_implications.
*/
static
void
sub_generate_join_implications
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
Relids
*
relids
,
Node
*
item1
,
Oid
sortop1
,
Relids
item1_relids
)
/* Scan to see if it matches any element of equi_key_set */
{
foreach
(
l2
,
equi_key_set
)
ListCell
*
l
;
/*
* Examine each mergejoinable outer-join clause with OUTERVAR on left,
* looking for an OUTERVAR identical to item1
*/
foreach
(
l
,
root
->
left_join_clauses
)
{
{
PathKeyItem
*
item1
=
(
PathKeyItem
*
)
lfirst
(
l2
);
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l
);
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
if
(
equal
(
leftop
,
item1
->
key
)
&&
if
(
equal
(
leftop
,
item1
)
&&
rinfo
->
left_sortop
==
sortop1
)
rinfo
->
left_sortop
==
item1
->
sortop
)
{
{
/*
/*
* Yes
, so find constant member(s) of set and generate
* Match
, so find constant member(s) of set and generate
* implied INNERVAR = CONSTANT
* implied INNERVAR = CONSTANT
*/
*/
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
rightop
,
rightop
,
rinfo
->
right_sortop
,
rinfo
->
right_sortop
,
rinfo
->
right_relids
,
rinfo
->
right_relids
,
false
);
false
);
/*
/*
* We can remove the explicit outer
join qual, too,
* We can remove explicit tests of this outer-
join qual, too,
* since we now have tests forcing each of its sides
* since we now have tests forcing each of its sides
* to the same value.
* to the same value.
*/
*/
process_implied_equality
(
root
,
process_implied_equality
(
root
,
leftop
,
leftop
,
rightop
,
rinfo
->
left_sortop
,
rinfo
->
right_sortop
,
rinfo
->
left_relids
,
rinfo
->
right_relids
,
true
);
/*
* And recurse to see if we can deduce anything from
* INNERVAR = CONSTANT
*/
sub_generate_join_implications
(
root
,
equi_key_set
,
relids
,
rightop
,
rightop
,
rinfo
->
left_sortop
,
rinfo
->
right_sortop
,
rinfo
->
right_sortop
,
rinfo
->
left_relids
,
rinfo
->
right_relids
);
rinfo
->
right_relids
,
true
);
/* No need to match against remaining set members */
break
;
}
}
}
}
}
/*
Examine each mergejoinable outer-join clause
with OUTERVAR on right */
/*
The same, looking at clauses
with OUTERVAR on right */
foreach
(
l
1
,
root
->
right_join_clauses
)
foreach
(
l
,
root
->
right_join_clauses
)
{
{
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l1
);
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l
);
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
ListCell
*
l2
;
/* Scan to see if it matches any element of equi_key_set */
if
(
equal
(
rightop
,
item1
)
&&
rinfo
->
right_sortop
==
sortop1
)
foreach
(
l2
,
equi_key_set
)
{
PathKeyItem
*
item1
=
(
PathKeyItem
*
)
lfirst
(
l2
);
if
(
equal
(
rightop
,
item1
->
key
)
&&
rinfo
->
right_sortop
==
item1
->
sortop
)
{
{
/*
/*
* Yes
, so find constant member(s) of set and generate
* Match
, so find constant member(s) of set and generate
* implied INNERVAR = CONSTANT
* implied INNERVAR = CONSTANT
*/
*/
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
leftop
,
leftop
,
rinfo
->
left_sortop
,
rinfo
->
left_sortop
,
rinfo
->
left_relids
,
rinfo
->
left_relids
,
false
);
false
);
/*
/*
* We can remove the explicit outer
join qual, too,
* We can remove explicit tests of this outer-
join qual, too,
* since we now have tests forcing each of its sides
* since we now have tests forcing each of its sides
* to the same value.
* to the same value.
*/
*/
process_implied_equality
(
root
,
process_implied_equality
(
root
,
leftop
,
rightop
,
rinfo
->
left_sortop
,
rinfo
->
right_sortop
,
rinfo
->
left_relids
,
rinfo
->
right_relids
,
true
);
/*
* And recurse to see if we can deduce anything from
* INNERVAR = CONSTANT
*/
sub_generate_join_implications
(
root
,
equi_key_set
,
relids
,
leftop
,
leftop
,
rightop
,
rinfo
->
left_sortop
,
rinfo
->
left_sortop
,
rinfo
->
right_sortop
,
rinfo
->
left_relids
);
rinfo
->
left_relids
,
rinfo
->
right_relids
,
true
);
/* No need to match against remaining set members */
break
;
}
}
}
}
}
/* Examine each mergejoinable full-join clause */
/*
foreach
(
l1
,
root
->
full_join_clauses
)
* Only COALESCE(x,y) items can possibly match full joins
*/
if
(
IsA
(
item1
,
CoalesceExpr
))
{
{
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l1
);
CoalesceExpr
*
cexpr
=
(
CoalesceExpr
*
)
item1
;
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
Node
*
cfirst
;
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
Node
*
csecond
;
int
i1
=
0
;
ListCell
*
l2
;
/* Scan to see if it matches any element of equi_key_set */
if
(
list_length
(
cexpr
->
args
)
!=
2
)
foreach
(
l2
,
equi_key_set
)
return
;
cfirst
=
(
Node
*
)
linitial
(
cexpr
->
args
);
csecond
=
(
Node
*
)
lsecond
(
cexpr
->
args
);
/*
* Examine each mergejoinable full-join clause, looking for a
* clause of the form "x = y" matching the COALESCE(x,y) expression
*/
foreach
(
l
,
root
->
full_join_clauses
)
{
{
PathKeyItem
*
item1
=
(
PathKeyItem
*
)
lfirst
(
l2
);
RestrictInfo
*
rinfo
=
(
RestrictInfo
*
)
lfirst
(
l
);
CoalesceExpr
*
cexpr
=
(
CoalesceExpr
*
)
item1
->
key
;
Node
*
leftop
=
get_leftop
(
rinfo
->
clause
);
Node
*
rightop
=
get_rightop
(
rinfo
->
clause
);
/*
/*
* Try to match a pathkey containing a COALESCE() expression
* We can assume the COALESCE() inputs are in the same order
* to the join clause. We can assume the COALESCE() inputs
* as the join clause, since both were automatically generated
* are in the same order as the join clause, since both were
* in the cases we care about.
* automatically generated in the cases we care about.
*
*
* XXX currently this may fail to match in cross-type cases
* XXX currently this may fail to match in cross-type cases
* because the COALESCE will contain typecast operations while
* because the COALESCE will contain typecast operations while
...
@@ -482,15 +522,13 @@ generate_outer_join_implications(PlannerInfo *root,
...
@@ -482,15 +522,13 @@ generate_outer_join_implications(PlannerInfo *root,
* Is it OK to strip implicit coercions from the COALESCE
* Is it OK to strip implicit coercions from the COALESCE
* arguments? What of the sortops in such cases?
* arguments? What of the sortops in such cases?
*/
*/
if
(
IsA
(
cexpr
,
CoalesceExpr
)
&&
if
(
equal
(
leftop
,
cfirst
)
&&
list_length
(
cexpr
->
args
)
==
2
&&
equal
(
rightop
,
csecond
)
&&
equal
(
leftop
,
(
Node
*
)
linitial
(
cexpr
->
args
))
&&
rinfo
->
left_sortop
==
sortop1
&&
equal
(
rightop
,
(
Node
*
)
lsecond
(
cexpr
->
args
))
&&
rinfo
->
right_sortop
==
sortop1
)
rinfo
->
left_sortop
==
item1
->
sortop
&&
rinfo
->
right_sortop
==
item1
->
sortop
)
{
{
/*
/*
*
Yes
, so find constant member(s) of set and generate
*
Match
, so find constant member(s) of set and generate
* implied LEFTVAR = CONSTANT
* implied LEFTVAR = CONSTANT
*/
*/
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
...
@@ -506,28 +544,37 @@ generate_outer_join_implications(PlannerInfo *root,
...
@@ -506,28 +544,37 @@ generate_outer_join_implications(PlannerInfo *root,
false
);
false
);
/* ... and remove COALESCE() = CONSTANT */
/* ... and remove COALESCE() = CONSTANT */
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
process_implied_const_eq
(
root
,
equi_key_set
,
relids
,
item1
->
key
,
item1
,
item1
->
sortop
,
sortop1
,
relids
[
i1
]
,
item1_relids
,
true
);
true
);
/*
/*
* We can remove
the explicit outer
join qual, too,
* We can remove
explicit tests of this outer-
join qual, too,
* since we now have tests forcing each of its sides
* since we now have tests forcing each of its sides
* to the same value.
* to the same value.
*/
*/
process_implied_equality
(
root
,
process_implied_equality
(
root
,
leftop
,
leftop
,
rightop
,
rightop
,
rinfo
->
left_sortop
,
rinfo
->
left_sortop
,
rinfo
->
right_sortop
,
rinfo
->
right_sortop
,
rinfo
->
left_relids
,
rinfo
->
left_relids
,
rinfo
->
right_relids
,
rinfo
->
right_relids
,
true
);
true
);
/*
* And recurse to see if we can deduce anything from
* LEFTVAR = CONSTANT
*/
sub_generate_join_implications
(
root
,
equi_key_set
,
relids
,
leftop
,
rinfo
->
left_sortop
,
rinfo
->
left_relids
);
/* ... and RIGHTVAR = CONSTANT */
sub_generate_join_implications
(
root
,
equi_key_set
,
relids
,
rightop
,
rinfo
->
right_sortop
,
rinfo
->
right_relids
);
/* No need to match against remaining set members */
break
;
}
}
i1
++
;
}
}
}
}
}
}
...
@@ -537,10 +584,8 @@ generate_outer_join_implications(PlannerInfo *root,
...
@@ -537,10 +584,8 @@ generate_outer_join_implications(PlannerInfo *root,
* Apply process_implied_equality with the given item and each
* Apply process_implied_equality with the given item and each
* pseudoconstant member of equi_key_set.
* pseudoconstant member of equi_key_set.
*
*
* This is just a subroutine to save some cruft in
* equi_key_set and relids are as for generate_outer_join_implications,
* generate_outer_join_implications. equi_key_set and relids are as in
* the other parameters as for process_implied_equality.
* generate_outer_join_implications, the other parameters as for
* process_implied_equality.
*/
*/
static
void
static
void
process_implied_const_eq
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
Relids
*
relids
,
process_implied_const_eq
(
PlannerInfo
*
root
,
List
*
equi_key_set
,
Relids
*
relids
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment