joininfo.c 2.6 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * joininfo.c--
4
 *	  JoinInfo node manipulation routines
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/joininfo.c,v 1.9 1998/08/04 16:44:17 momjian Exp $
11 12 13 14 15 16 17 18
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "nodes/relation.h"

#include "optimizer/internal.h"
19
#include "optimizer/joininfo.h"
20 21 22 23
#include "optimizer/var.h"
#include "optimizer/clauses.h"


24
/*
25
 * joininfo-member--
26 27 28 29
 *	  Determines whether a node has already been created for a join
 *	  between a set of join relations and the relation described by
 *	  'joininfo-list'.
 *
30
 * 'join-relids' is a list of relids corresponding to the join relation
31 32 33
 * 'joininfo-list' is the list of joininfo nodes against which this is
 *				checked
 *
34 35
 * Returns the corresponding node in 'joininfo-list' if such a node
 * exists.
36
 *
37
 */
38
JInfo *
39
joininfo_member(List *join_relids, List *joininfo_list)
40
{
41 42
	List	   *i = NIL;
	List	   *other_rels = NIL;
43

44 45 46 47 48 49 50
	foreach(i, joininfo_list)
	{
		other_rels = lfirst(i);
		if (same(join_relids, ((JInfo *) other_rels)->otherrels))
			return ((JInfo *) other_rels);
	}
	return ((JInfo *) NULL);
51 52 53
}


54
/*
55
 * find-joininfo-node--
56 57 58 59 60
 *	  Find the joininfo node within a relation entry corresponding
 *	  to a join between 'this_rel' and the relations in 'join-relids'.	A
 *	  new node is created and added to the relation entry's joininfo
 *	  field if the desired one can't be found.
 *
61
 * Returns a joininfo node.
62
 *
63
 */
64
JInfo *
Bruce Momjian's avatar
Bruce Momjian committed
65
find_joininfo_node(RelOptInfo *this_rel, List *join_relids)
66
{
67 68
	JInfo	   *joininfo = joininfo_member(join_relids,
										   this_rel->joininfo);
69 70 71 72 73 74

	if (joininfo == NULL)
	{
		joininfo = makeNode(JInfo);
		joininfo->otherrels = join_relids;
		joininfo->jinfoclauseinfo = NIL;
75
		joininfo->mergejoinable = false;
76 77 78 79 80
		joininfo->hashjoinable = false;
		joininfo->inactive = false;
		this_rel->joininfo = lcons(joininfo, this_rel->joininfo);
	}
	return (joininfo);
81 82
}

83
/*
84
 * other-join-clause-var--
85 86 87
 *	  Determines whether a var node is contained within a joinclause
 *	  of the form(op var var).
 *
88
 * Returns the other var node in the joinclause if it is, nil if not.
89
 *
90
 */
91
Var *
92
other_join_clause_var(Var *var, Expr *clause)
93
{
94 95 96
	Var		   *retval;
	Var		   *l,
			   *r;
97

98
	retval = (Var *) NULL;
99

100 101 102 103
	if (var != NULL && join_clause_p((Node *) clause))
	{
		l = (Var *) get_leftop(clause);
		r = (Var *) get_rightop(clause);
104

105 106 107 108 109
		if (var_equal(var, l))
			retval = r;
		else if (var_equal(var, r))
			retval = l;
	}
110

111
	return (retval);
112
}